blob: 991378f6d49c9073db69e85b77278878a2f20f0a [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brianb7978af2007-01-09 19:17:17 -07003 * Version: 6.5.3
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Brianb7978af2007-01-09 19:17:17 -07005 * Copyright (C) 1999-2007 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"
Brianc0551f02006-12-14 15:02:37 -070038#include "prog_parameter.h"
39#include "prog_statevars.h"
Brian Paul8c41a142005-11-19 15:36:28 +000040#include "context.h"
Brian Paul6211a142006-08-24 23:37:13 +000041#include "macros.h"
Brian Paul8c41a142005-11-19 15:36:28 +000042#include "mtypes.h"
Brianc0551f02006-12-14 15:02:37 -070043#include "prog_instruction.h"
Brian Paul8c41a142005-11-19 15:36:28 +000044
45
Brian Paul6211a142006-08-24 23:37:13 +000046/* For ARB programs, use the NV instruction limits */
47#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
48 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000049
50
51/**
52 * This is basically a union of the vertex_program and fragment_program
53 * structs that we can use to parse the program into
54 *
55 * XXX we can probably get rid of this entirely someday.
56 */
57struct arb_program
58{
Brian Paul122629f2006-07-20 16:49:57 +000059 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000060
61 GLuint Position; /* Just used for error reporting while parsing */
62 GLuint MajorVersion;
63 GLuint MinorVersion;
64
65 /* ARB_vertex_progmra options */
66 GLboolean HintPositionInvariant;
67
68 /* ARB_fragment_progmra options */
69 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
70 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
71
72 /* ARB_fragment_program specifics */
73 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
74 GLuint NumAluInstructions;
75 GLuint NumTexInstructions;
76 GLuint NumTexIndirections;
77
78 GLboolean UsesKill;
79};
80
Ian Romanick9bdfee32005-07-18 12:31:24 +000081
Brian Paula6c423d2004-08-25 15:59:48 +000082
Jouk Jansen40322e12004-04-05 08:50:36 +000083/* TODO:
84 * Fragment Program Stuff:
85 * -----------------------------------------------------
86 *
87 * - things from Michal's email
88 * + overflow on atoi
89 * + not-overflowing floats (don't use parse_integer..)
90 * + can remove range checking in arbparse.c
91 *
92 * - check all limits of number of various variables
93 * + parameters
94 *
95 * - test! test! test!
96 *
97 * Vertex Program Stuff:
98 * -----------------------------------------------------
99 * - Optimize param array usage and count limits correctly, see spec,
100 * section 2.14.3.7
101 * + Record if an array is reference absolutly or relatively (or both)
102 * + For absolute arrays, store a bitmap of accesses
103 * + For single parameters, store an access flag
104 * + After parsing, make a parameter cleanup and merging pass, where
105 * relative arrays are layed out first, followed by abs arrays, and
106 * finally single state.
107 * + Remap offsets for param src and dst registers
108 * + Now we can properly count parameter usage
109 *
110 * - Multiple state binding errors in param arrays (see spec, just before
111 * section 2.14.3.3)
112 * - grep for XXX
113 *
114 * Mesa Stuff
115 * -----------------------------------------------------
116 * - User clipping planes vs. PositionInvariant
117 * - Is it sufficient to just multiply by the mvp to transform in the
118 * PositionInvariant case? Or do we need something more involved?
119 *
120 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
121 * - fetch state listed in program_parameters list
122 * + WTF should this go???
123 * + currently in nvvertexec.c and s_nvfragprog.c
124 *
125 * - allow for multiple address registers (and fetch address regs properly)
126 *
127 * Cosmetic Stuff
128 * -----------------------------------------------------
129 * - remove any leftover unused grammer.c stuff (dict_ ?)
130 * - fix grammer.c error handling so its not static
131 * - #ifdef around stuff pertaining to extentions
132 *
133 * Outstanding Questions:
134 * -----------------------------------------------------
135 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
136 * what gets hacked off because of this:
137 * + VERTEX_ATTRIB_MATRIXINDEX
138 * + VERTEX_ATTRIB_WEIGHT
139 * + MATRIX_MODELVIEW
140 * + MATRIX_PALETTE
141 *
142 * - When can we fetch env/local params from their own register files, and
143 * when to we have to fetch them into the main state register file?
144 * (think arrays)
145 *
146 * Grammar Changes:
147 * -----------------------------------------------------
148 */
149
150/* Changes since moving the file to shader directory
151
1522004-III-4 ------------------------------------------------------------
153- added #include "grammar_mesa.h"
154- removed grammar specific code part (it resides now in grammar.c)
155- added GL_ARB_fragment_program_shadow tokens
156- modified #include "arbparse_syn.h"
157- major changes inside _mesa_parse_arb_program()
158- check the program string for '\0' characters
159- copy the program string to a one-byte-longer location to have
160 it null-terminated
161- position invariance test (not writing to result.position) moved
162 to syntax part
163*/
164
165typedef GLubyte *production;
166
Brian Paul11a54c32006-11-15 19:54:25 +0000167
Jouk Jansen40322e12004-04-05 08:50:36 +0000168/**
169 * This is the text describing the rules to parse the grammar
170 */
Brian Paul11a54c32006-11-15 19:54:25 +0000171LONGSTRING static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000172#include "arbprogram_syn.h"
173;
174
175/**
176 * These should match up with the values defined in arbprogram.syn
177 */
178
179/*
180 Changes:
181 - changed and merged V_* and F_* opcode values to OP_*.
182 - added GL_ARB_fragment_program_shadow specific tokens (michal)
183*/
Michal Krolb80bc052004-10-21 14:09:54 +0000184#define REVISION 0x09
Jouk Jansen40322e12004-04-05 08:50:36 +0000185
186/* program type */
187#define FRAGMENT_PROGRAM 0x01
188#define VERTEX_PROGRAM 0x02
189
190/* program section */
191#define OPTION 0x01
192#define INSTRUCTION 0x02
193#define DECLARATION 0x03
194#define END 0x04
195
Michal Krolb80bc052004-10-21 14:09:54 +0000196/* GL_ARB_fragment_program option */
197#define ARB_PRECISION_HINT_FASTEST 0x00
198#define ARB_PRECISION_HINT_NICEST 0x01
199#define ARB_FOG_EXP 0x02
200#define ARB_FOG_EXP2 0x03
201#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000202
Michal Krolb80bc052004-10-21 14:09:54 +0000203/* GL_ARB_vertex_program option */
204#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000205
Michal Krolb80bc052004-10-21 14:09:54 +0000206/* GL_ARB_fragment_program_shadow option */
207#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000208
Michal Krolb80bc052004-10-21 14:09:54 +0000209/* GL_ARB_draw_buffers option */
210#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000211
Jouk Jansen40322e12004-04-05 08:50:36 +0000212/* GL_ARB_fragment_program instruction class */
213#define OP_ALU_INST 0x00
214#define OP_TEX_INST 0x01
215
216/* GL_ARB_vertex_program instruction class */
217/* OP_ALU_INST */
218
219/* GL_ARB_fragment_program instruction type */
220#define OP_ALU_VECTOR 0x00
221#define OP_ALU_SCALAR 0x01
222#define OP_ALU_BINSC 0x02
223#define OP_ALU_BIN 0x03
224#define OP_ALU_TRI 0x04
225#define OP_ALU_SWZ 0x05
226#define OP_TEX_SAMPLE 0x06
227#define OP_TEX_KIL 0x07
228
229/* GL_ARB_vertex_program instruction type */
230#define OP_ALU_ARL 0x08
231/* OP_ALU_VECTOR */
232/* OP_ALU_SCALAR */
233/* OP_ALU_BINSC */
234/* OP_ALU_BIN */
235/* OP_ALU_TRI */
236/* OP_ALU_SWZ */
237
238/* GL_ARB_fragment_program instruction code */
239#define OP_ABS 0x00
240#define OP_ABS_SAT 0x1B
241#define OP_FLR 0x09
242#define OP_FLR_SAT 0x26
243#define OP_FRC 0x0A
244#define OP_FRC_SAT 0x27
245#define OP_LIT 0x0C
246#define OP_LIT_SAT 0x2A
247#define OP_MOV 0x11
248#define OP_MOV_SAT 0x30
249#define OP_COS 0x1F
250#define OP_COS_SAT 0x20
251#define OP_EX2 0x07
252#define OP_EX2_SAT 0x25
253#define OP_LG2 0x0B
254#define OP_LG2_SAT 0x29
255#define OP_RCP 0x14
256#define OP_RCP_SAT 0x33
257#define OP_RSQ 0x15
258#define OP_RSQ_SAT 0x34
259#define OP_SIN 0x38
260#define OP_SIN_SAT 0x39
261#define OP_SCS 0x35
262#define OP_SCS_SAT 0x36
263#define OP_POW 0x13
264#define OP_POW_SAT 0x32
265#define OP_ADD 0x01
266#define OP_ADD_SAT 0x1C
267#define OP_DP3 0x03
268#define OP_DP3_SAT 0x21
269#define OP_DP4 0x04
270#define OP_DP4_SAT 0x22
271#define OP_DPH 0x05
272#define OP_DPH_SAT 0x23
273#define OP_DST 0x06
274#define OP_DST_SAT 0x24
275#define OP_MAX 0x0F
276#define OP_MAX_SAT 0x2E
277#define OP_MIN 0x10
278#define OP_MIN_SAT 0x2F
279#define OP_MUL 0x12
280#define OP_MUL_SAT 0x31
281#define OP_SGE 0x16
282#define OP_SGE_SAT 0x37
283#define OP_SLT 0x17
284#define OP_SLT_SAT 0x3A
285#define OP_SUB 0x18
286#define OP_SUB_SAT 0x3B
287#define OP_XPD 0x1A
288#define OP_XPD_SAT 0x43
289#define OP_CMP 0x1D
290#define OP_CMP_SAT 0x1E
291#define OP_LRP 0x2B
292#define OP_LRP_SAT 0x2C
293#define OP_MAD 0x0E
294#define OP_MAD_SAT 0x2D
295#define OP_SWZ 0x19
296#define OP_SWZ_SAT 0x3C
297#define OP_TEX 0x3D
298#define OP_TEX_SAT 0x3E
299#define OP_TXB 0x3F
300#define OP_TXB_SAT 0x40
301#define OP_TXP 0x41
302#define OP_TXP_SAT 0x42
303#define OP_KIL 0x28
304
305/* GL_ARB_vertex_program instruction code */
306#define OP_ARL 0x02
307/* OP_ABS */
308/* OP_FLR */
309/* OP_FRC */
310/* OP_LIT */
311/* OP_MOV */
312/* OP_EX2 */
313#define OP_EXP 0x08
314/* OP_LG2 */
315#define OP_LOG 0x0D
316/* OP_RCP */
317/* OP_RSQ */
318/* OP_POW */
319/* OP_ADD */
320/* OP_DP3 */
321/* OP_DP4 */
322/* OP_DPH */
323/* OP_DST */
324/* OP_MAX */
325/* OP_MIN */
326/* OP_MUL */
327/* OP_SGE */
328/* OP_SLT */
329/* OP_SUB */
330/* OP_XPD */
331/* OP_MAD */
332/* OP_SWZ */
333
334/* fragment attribute binding */
335#define FRAGMENT_ATTRIB_COLOR 0x01
336#define FRAGMENT_ATTRIB_TEXCOORD 0x02
337#define FRAGMENT_ATTRIB_FOGCOORD 0x03
338#define FRAGMENT_ATTRIB_POSITION 0x04
339
340/* vertex attribute binding */
341#define VERTEX_ATTRIB_POSITION 0x01
342#define VERTEX_ATTRIB_WEIGHT 0x02
343#define VERTEX_ATTRIB_NORMAL 0x03
344#define VERTEX_ATTRIB_COLOR 0x04
345#define VERTEX_ATTRIB_FOGCOORD 0x05
346#define VERTEX_ATTRIB_TEXCOORD 0x06
347#define VERTEX_ATTRIB_MATRIXINDEX 0x07
348#define VERTEX_ATTRIB_GENERIC 0x08
349
350/* fragment result binding */
351#define FRAGMENT_RESULT_COLOR 0x01
352#define FRAGMENT_RESULT_DEPTH 0x02
353
354/* vertex result binding */
355#define VERTEX_RESULT_POSITION 0x01
356#define VERTEX_RESULT_COLOR 0x02
357#define VERTEX_RESULT_FOGCOORD 0x03
358#define VERTEX_RESULT_POINTSIZE 0x04
359#define VERTEX_RESULT_TEXCOORD 0x05
360
361/* texture target */
362#define TEXTARGET_1D 0x01
363#define TEXTARGET_2D 0x02
364#define TEXTARGET_3D 0x03
365#define TEXTARGET_RECT 0x04
366#define TEXTARGET_CUBE 0x05
367/* GL_ARB_fragment_program_shadow */
368#define TEXTARGET_SHADOW1D 0x06
369#define TEXTARGET_SHADOW2D 0x07
370#define TEXTARGET_SHADOWRECT 0x08
371
372/* face type */
373#define FACE_FRONT 0x00
374#define FACE_BACK 0x01
375
376/* color type */
377#define COLOR_PRIMARY 0x00
378#define COLOR_SECONDARY 0x01
379
380/* component */
381#define COMPONENT_X 0x00
382#define COMPONENT_Y 0x01
383#define COMPONENT_Z 0x02
384#define COMPONENT_W 0x03
385#define COMPONENT_0 0x04
386#define COMPONENT_1 0x05
387
388/* array index type */
389#define ARRAY_INDEX_ABSOLUTE 0x00
390#define ARRAY_INDEX_RELATIVE 0x01
391
392/* matrix name */
393#define MATRIX_MODELVIEW 0x01
394#define MATRIX_PROJECTION 0x02
395#define MATRIX_MVP 0x03
396#define MATRIX_TEXTURE 0x04
397#define MATRIX_PALETTE 0x05
398#define MATRIX_PROGRAM 0x06
399
400/* matrix modifier */
401#define MATRIX_MODIFIER_IDENTITY 0x00
402#define MATRIX_MODIFIER_INVERSE 0x01
403#define MATRIX_MODIFIER_TRANSPOSE 0x02
404#define MATRIX_MODIFIER_INVTRANS 0x03
405
406/* constant type */
407#define CONSTANT_SCALAR 0x01
408#define CONSTANT_VECTOR 0x02
409
410/* program param type */
411#define PROGRAM_PARAM_ENV 0x01
412#define PROGRAM_PARAM_LOCAL 0x02
413
414/* register type */
415#define REGISTER_ATTRIB 0x01
416#define REGISTER_PARAM 0x02
417#define REGISTER_RESULT 0x03
418#define REGISTER_ESTABLISHED_NAME 0x04
419
420/* param binding */
421#define PARAM_NULL 0x00
422#define PARAM_ARRAY_ELEMENT 0x01
423#define PARAM_STATE_ELEMENT 0x02
424#define PARAM_PROGRAM_ELEMENT 0x03
425#define PARAM_PROGRAM_ELEMENTS 0x04
426#define PARAM_CONSTANT 0x05
427
428/* param state property */
429#define STATE_MATERIAL_PARSER 0x01
430#define STATE_LIGHT_PARSER 0x02
431#define STATE_LIGHT_MODEL 0x03
432#define STATE_LIGHT_PROD 0x04
433#define STATE_FOG 0x05
434#define STATE_MATRIX_ROWS 0x06
435/* GL_ARB_fragment_program */
436#define STATE_TEX_ENV 0x07
437#define STATE_DEPTH 0x08
438/* GL_ARB_vertex_program */
439#define STATE_TEX_GEN 0x09
440#define STATE_CLIP_PLANE 0x0A
441#define STATE_POINT 0x0B
442
443/* state material property */
444#define MATERIAL_AMBIENT 0x01
445#define MATERIAL_DIFFUSE 0x02
446#define MATERIAL_SPECULAR 0x03
447#define MATERIAL_EMISSION 0x04
448#define MATERIAL_SHININESS 0x05
449
450/* state light property */
451#define LIGHT_AMBIENT 0x01
452#define LIGHT_DIFFUSE 0x02
453#define LIGHT_SPECULAR 0x03
454#define LIGHT_POSITION 0x04
455#define LIGHT_ATTENUATION 0x05
456#define LIGHT_HALF 0x06
457#define LIGHT_SPOT_DIRECTION 0x07
458
459/* state light model property */
460#define LIGHT_MODEL_AMBIENT 0x01
461#define LIGHT_MODEL_SCENECOLOR 0x02
462
463/* state light product property */
464#define LIGHT_PROD_AMBIENT 0x01
465#define LIGHT_PROD_DIFFUSE 0x02
466#define LIGHT_PROD_SPECULAR 0x03
467
468/* state texture environment property */
469#define TEX_ENV_COLOR 0x01
470
471/* state texture generation coord property */
472#define TEX_GEN_EYE 0x01
473#define TEX_GEN_OBJECT 0x02
474
475/* state fog property */
476#define FOG_COLOR 0x01
477#define FOG_PARAMS 0x02
478
479/* state depth property */
480#define DEPTH_RANGE 0x01
481
482/* state point parameters property */
483#define POINT_SIZE 0x01
484#define POINT_ATTENUATION 0x02
485
486/* declaration */
487#define ATTRIB 0x01
488#define PARAM 0x02
489#define TEMP 0x03
490#define OUTPUT 0x04
491#define ALIAS 0x05
492/* GL_ARB_vertex_program */
493#define ADDRESS 0x06
494
495/*-----------------------------------------------------------------------
496 * From here on down is the semantic checking portion
497 *
498 */
499
500/**
501 * Variable Table Handling functions
502 */
503typedef enum
504{
505 vt_none,
506 vt_address,
507 vt_attrib,
508 vt_param,
509 vt_temp,
510 vt_output,
511 vt_alias
512} var_type;
513
514
Brian Paul7aebaf32005-10-30 21:23:23 +0000515/**
516 * Setting an explicit field for each of the binding properties is a bit
517 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000518 */
519struct var_cache
520{
Brian Paul6a769d92006-04-28 15:42:15 +0000521 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000522 var_type type;
523 GLuint address_binding; /* The index of the address register we should
524 * be using */
525 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000526 GLuint attrib_is_generic; /* If the attrib was specified through a generic
527 * vertex attrib */
528 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000529 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000530 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
531 * that this is aliased to */
532 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
533 * PROGRAM_ENV_PARAM} */
534 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
535 * the tokens representing our bound state (or constants)
536 * start */
537 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
538 * we take up with our state tokens or constants. Note that
539 * this is _not_ the same as the number of param registers
540 * we eventually use */
541 struct var_cache *next;
542};
543
544static GLvoid
545var_cache_create (struct var_cache **va)
546{
547 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
548 if (*va) {
549 (**va).name = NULL;
550 (**va).type = vt_none;
551 (**va).attrib_binding = ~0;
552 (**va).attrib_is_generic = 0;
553 (**va).temp_binding = ~0;
554 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000555 (**va).param_binding_type = ~0;
556 (**va).param_binding_begin = ~0;
557 (**va).param_binding_length = ~0;
558 (**va).alias_binding = NULL;
559 (**va).next = NULL;
560 }
561}
562
563static GLvoid
564var_cache_destroy (struct var_cache **va)
565{
566 if (*va) {
567 var_cache_destroy (&(**va).next);
568 _mesa_free (*va);
569 *va = NULL;
570 }
571}
572
573static GLvoid
574var_cache_append (struct var_cache **va, struct var_cache *nv)
575{
576 if (*va)
577 var_cache_append (&(**va).next, nv);
578 else
579 *va = nv;
580}
581
582static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000583var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000584{
Brian Paul0a360cf2005-01-17 01:21:03 +0000585 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000586
587 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000588 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000589 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000590 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000591 return va;
592 }
593
594 va = va->next;
595 }
596
597 return NULL;
598}
599
Brian Paula088f162006-09-05 23:08:51 +0000600
601
602/**
603 * Called when an error is detected while parsing/compiling a program.
604 * Sets the ctx->Program.ErrorString field to descript and records a
605 * GL_INVALID_OPERATION error.
606 * \param position position of error in program string
607 * \param descrip verbose error description
608 */
609static void
610program_error(GLcontext *ctx, GLint position, const char *descrip)
611{
612 if (descrip) {
613 const char *prefix = "glProgramString(", *suffix = ")";
614 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
615 _mesa_strlen(prefix) +
616 _mesa_strlen(suffix) + 1);
617 if (str) {
618 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
619 _mesa_error(ctx, GL_INVALID_OPERATION, str);
620 _mesa_free(str);
621 }
622 }
623 _mesa_set_program_error(ctx, position, descrip);
624}
625
626
627
Jouk Jansen40322e12004-04-05 08:50:36 +0000628/**
629 * constructs an integer from 4 GLubytes in LE format
630 */
631static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000632parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000633{
634 GLuint value;
635
636 value = (GLuint) (*(*inst)++);
637 value += (GLuint) (*(*inst)++) * 0x100;
638 value += (GLuint) (*(*inst)++) * 0x10000;
639 value += (GLuint) (*(*inst)++) * 0x1000000;
640
641 return value;
642}
643
644/**
645 * This will, given a string, lookup the string as a variable name in the
646 * var cache. If the name is found, the var cache node corresponding to the
647 * var name is returned. If it is not found, a new entry is allocated
648 *
649 * \param I Points into the binary array where the string identifier begins
650 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
651 * \return The location on the var_cache corresponding the the string starting at I
652 */
653static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000654parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000655 struct arb_program *Program, GLuint * found)
656{
Brian Paula088f162006-09-05 23:08:51 +0000657 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000658 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000659 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000660
661 *inst += _mesa_strlen ((char *) i) + 1;
662
663 va = var_cache_find (*vc_head, i);
664
665 if (va) {
666 *found = 1;
667 return va;
668 }
669
670 *found = 0;
671 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000672 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000673
674 var_cache_append (vc_head, va);
675
676 return va;
677}
678
679static char *
Brian Paula088f162006-09-05 23:08:51 +0000680parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000681{
Brian Paula088f162006-09-05 23:08:51 +0000682 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000683 (void) Program;
684
Jouk Jansen40322e12004-04-05 08:50:36 +0000685 *inst += _mesa_strlen ((char *) i) + 1;
686
687 return (char *) i;
688}
689
690/**
Brian Paul05908952004-06-08 15:20:23 +0000691 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000692 */
Brian Paul05908952004-06-08 15:20:23 +0000693static GLint
Brian Paula088f162006-09-05 23:08:51 +0000694parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000695{
696 /*return *(*inst)++ != '+'; */
697
698 if (**inst == '-') {
699 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000700 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000701 }
702 else if (**inst == '+') {
703 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000704 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000705 }
706
Brian Paul05908952004-06-08 15:20:23 +0000707 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000708}
709
710/**
711 * parses and returns signed integer
712 */
713static GLint
Brian Paula088f162006-09-05 23:08:51 +0000714parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000715{
716 GLint sign;
717 GLint value;
718
719 /* check if *inst points to '+' or '-'
720 * if yes, grab the sign and increment *inst
721 */
722 sign = parse_sign (inst);
723
724 /* now check if *inst points to 0
725 * if yes, increment the *inst and return the default value
726 */
727 if (**inst == 0) {
728 (*inst)++;
729 return 0;
730 }
731
732 /* parse the integer as you normally would do it */
733 value = _mesa_atoi (parse_string_without_adding (inst, Program));
734
735 /* now, after terminating 0 there is a position
736 * to parse it - parse_position()
737 */
738 Program->Position = parse_position (inst);
739
Brian Paul05908952004-06-08 15:20:23 +0000740 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000741}
742
743/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000744 Accumulate this string of digits, and return them as
745 a large integer represented in floating point (for range).
746 If scale is not NULL, also accumulates a power-of-ten
747 integer scale factor that represents the number of digits
748 in the string.
749*/
750static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000751parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000752{
753 GLdouble value = 0.0;
754 GLdouble oscale = 1.0;
755
756 if (**inst == 0) { /* this string of digits is empty-- do nothing */
757 (*inst)++;
758 }
759 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000760 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000761 GLubyte digit = *((*inst)++);
762 value = value * 10.0 + (GLint) (digit - '0');
763 oscale *= 10.0;
764 }
765 assert(**inst == 0); /* integer string should end with 0 */
766 (*inst)++; /* skip over terminating 0 */
767 Program->Position = parse_position(inst); /* skip position (from integer) */
768 }
769 if (scale)
770 *scale = oscale;
771 return value;
772}
773
774/**
775 Parse an unsigned floating-point number from this stream of tokenized
776 characters. Example floating-point formats supported:
777 12.34
778 12
779 0.34
780 .34
781 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000782 */
783static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000784parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000785{
Brian Paul1ff8f502005-02-16 15:08:29 +0000786 GLint exponent;
787 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000788
Brian Paul1ff8f502005-02-16 15:08:29 +0000789 whole = parse_float_string(inst, Program, 0);
790 fraction = parse_float_string(inst, Program, &fracScale);
791
792 /* Parse signed exponent */
793 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000794
Brian Paul1ff8f502005-02-16 15:08:29 +0000795 /* Assemble parts of floating-point number: */
796 return (GLfloat) ((whole + fraction / fracScale) *
797 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000798}
799
800
801/**
802 */
803static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000804parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000805{
Brian Paul05908952004-06-08 15:20:23 +0000806 GLint sign = parse_sign (inst);
807 GLfloat value = parse_float (inst, Program);
808 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000809}
810
811/**
812 * This picks out a constant value from the parsed array. The constant vector is r
813 * returned in the *values array, which should be of length 4.
814 *
815 * \param values - The 4 component vector with the constant value in it
816 */
817static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000818parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000819 GLboolean use)
820{
821 GLuint components, i;
822
823
824 switch (*(*inst)++) {
825 case CONSTANT_SCALAR:
826 if (use == GL_TRUE) {
827 values[0] =
828 values[1] =
829 values[2] = values[3] = parse_float (inst, Program);
830 }
831 else {
832 values[0] =
833 values[1] =
834 values[2] = values[3] = parse_signed_float (inst, Program);
835 }
836
837 break;
838 case CONSTANT_VECTOR:
839 values[0] = values[1] = values[2] = 0;
840 values[3] = 1;
841 components = *(*inst)++;
842 for (i = 0; i < components; i++) {
843 values[i] = parse_signed_float (inst, Program);
844 }
845 break;
846 }
847}
848
849/**
850 * \param offset The offset from the address register that we should
851 * address
852 *
853 * \return 0 on sucess, 1 on error
854 */
855static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000856parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
857 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000858{
Brian Paul6a769d92006-04-28 15:42:15 +0000859 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000860 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000861 return 0;
862}
863
864/**
865 * \param color 0 if color type is primary, 1 if color type is secondary
866 * \return 0 on sucess, 1 on error
867 */
868static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000869parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000870 GLint * color)
871{
Brian Paula6c423d2004-08-25 15:59:48 +0000872 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000873 *color = *(*inst)++ != COLOR_PRIMARY;
874 return 0;
875}
876
877/**
878 * Get an integer corresponding to a generic vertex attribute.
879 *
880 * \return 0 on sucess, 1 on error
881 */
882static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000883parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000884 struct arb_program *Program, GLuint *attrib)
885{
Brian Paulbd997cd2004-07-20 21:12:56 +0000886 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000887
Michal Krolbb38cad2006-04-11 11:41:11 +0000888 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000889 {
Brian Paula088f162006-09-05 23:08:51 +0000890 program_error(ctx, Program->Position,
891 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000892 return 1;
893 }
894
Brian Paulbd997cd2004-07-20 21:12:56 +0000895 *attrib = (GLuint) i;
896
Jouk Jansen40322e12004-04-05 08:50:36 +0000897 return 0;
898}
899
900
901/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000902 * \param color The index of the color buffer to write into
903 * \return 0 on sucess, 1 on error
904 */
905static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000906parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000907 struct arb_program *Program, GLuint * color)
908{
909 GLint i = parse_integer (inst, Program);
910
911 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000912 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000913 return 1;
914 }
915
916 *color = (GLuint) i;
917 return 0;
918}
919
920
921/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000922 * \param coord The texture unit index
923 * \return 0 on sucess, 1 on error
924 */
925static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000926parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000927 struct arb_program *Program, GLuint * coord)
928{
Brian Paulbd997cd2004-07-20 21:12:56 +0000929 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000930
Brian Paula6c423d2004-08-25 15:59:48 +0000931 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000932 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000933 return 1;
934 }
935
Brian Paulbd997cd2004-07-20 21:12:56 +0000936 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000937 return 0;
938}
939
940/**
941 * \param coord The weight index
942 * \return 0 on sucess, 1 on error
943 */
944static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000945parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000946 GLint * coord)
947{
948 *coord = parse_integer (inst, Program);
949
950 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000951 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000952 return 1;
953 }
954
955 return 0;
956}
957
958/**
959 * \param coord The clip plane index
960 * \return 0 on sucess, 1 on error
961 */
962static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000963parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000964 struct arb_program *Program, GLint * coord)
965{
966 *coord = parse_integer (inst, Program);
967
968 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +0000969 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000970 return 1;
971 }
972
973 return 0;
974}
975
976
977/**
978 * \return 0 on front face, 1 on back face
979 */
980static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000981parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000982{
983 switch (*(*inst)++) {
984 case FACE_FRONT:
985 return 0;
986
987 case FACE_BACK:
988 return 1;
989 }
990 return 0;
991}
992
993
994/**
995 * Given a matrix and a modifier token on the binary array, return tokens
996 * that _mesa_fetch_state() [program.c] can understand.
997 *
998 * \param matrix - the matrix we are talking about
999 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1000 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1001 * \return 0 on sucess, 1 on failure
1002 */
1003static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001004parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001005 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1006{
1007 GLubyte mat = *(*inst)++;
1008
1009 *matrix_idx = 0;
1010
1011 switch (mat) {
1012 case MATRIX_MODELVIEW:
1013 *matrix = STATE_MODELVIEW;
1014 *matrix_idx = parse_integer (inst, Program);
1015 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001016 program_error(ctx, Program->Position,
1017 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001018 return 1;
1019 }
1020 break;
1021
1022 case MATRIX_PROJECTION:
1023 *matrix = STATE_PROJECTION;
1024 break;
1025
1026 case MATRIX_MVP:
1027 *matrix = STATE_MVP;
1028 break;
1029
1030 case MATRIX_TEXTURE:
1031 *matrix = STATE_TEXTURE;
1032 *matrix_idx = parse_integer (inst, Program);
1033 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001034 program_error(ctx, Program->Position, "Invalid Texture Unit");
1035 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001036 return 1;
1037 }
1038 break;
1039
1040 /* This is not currently supported (ARB_matrix_palette) */
1041 case MATRIX_PALETTE:
1042 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001043 program_error(ctx, Program->Position,
1044 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001045 return 1;
1046 break;
1047
1048 case MATRIX_PROGRAM:
1049 *matrix = STATE_PROGRAM;
1050 *matrix_idx = parse_integer (inst, Program);
1051 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001052 program_error(ctx, Program->Position, "Invalid Program Matrix");
1053 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001054 return 1;
1055 }
1056 break;
1057 }
1058
1059 switch (*(*inst)++) {
1060 case MATRIX_MODIFIER_IDENTITY:
1061 *matrix_modifier = 0;
1062 break;
1063 case MATRIX_MODIFIER_INVERSE:
1064 *matrix_modifier = STATE_MATRIX_INVERSE;
1065 break;
1066 case MATRIX_MODIFIER_TRANSPOSE:
1067 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1068 break;
1069 case MATRIX_MODIFIER_INVTRANS:
1070 *matrix_modifier = STATE_MATRIX_INVTRANS;
1071 break;
1072 }
1073
1074 return 0;
1075}
1076
1077
1078/**
1079 * This parses a state string (rather, the binary version of it) into
1080 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1081 *
1082 * \param inst - the start in the binary arry to start working from
1083 * \param state_tokens - the storage for the 6-token state description
1084 * \return - 0 on sucess, 1 on error
1085 */
1086static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001087parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001088 struct arb_program *Program, GLint * state_tokens)
1089{
1090 switch (*(*inst)++) {
1091 case STATE_MATERIAL_PARSER:
1092 state_tokens[0] = STATE_MATERIAL;
1093 state_tokens[1] = parse_face_type (inst);
1094 switch (*(*inst)++) {
1095 case MATERIAL_AMBIENT:
1096 state_tokens[2] = STATE_AMBIENT;
1097 break;
1098 case MATERIAL_DIFFUSE:
1099 state_tokens[2] = STATE_DIFFUSE;
1100 break;
1101 case MATERIAL_SPECULAR:
1102 state_tokens[2] = STATE_SPECULAR;
1103 break;
1104 case MATERIAL_EMISSION:
1105 state_tokens[2] = STATE_EMISSION;
1106 break;
1107 case MATERIAL_SHININESS:
1108 state_tokens[2] = STATE_SHININESS;
1109 break;
1110 }
1111 break;
1112
1113 case STATE_LIGHT_PARSER:
1114 state_tokens[0] = STATE_LIGHT;
1115 state_tokens[1] = parse_integer (inst, Program);
1116
1117 /* Check the value of state_tokens[1] against the # of lights */
1118 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001119 program_error(ctx, Program->Position, "Invalid Light Number");
1120 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001121 return 1;
1122 }
1123
1124 switch (*(*inst)++) {
1125 case LIGHT_AMBIENT:
1126 state_tokens[2] = STATE_AMBIENT;
1127 break;
1128 case LIGHT_DIFFUSE:
1129 state_tokens[2] = STATE_DIFFUSE;
1130 break;
1131 case LIGHT_SPECULAR:
1132 state_tokens[2] = STATE_SPECULAR;
1133 break;
1134 case LIGHT_POSITION:
1135 state_tokens[2] = STATE_POSITION;
1136 break;
1137 case LIGHT_ATTENUATION:
1138 state_tokens[2] = STATE_ATTENUATION;
1139 break;
1140 case LIGHT_HALF:
1141 state_tokens[2] = STATE_HALF;
1142 break;
1143 case LIGHT_SPOT_DIRECTION:
1144 state_tokens[2] = STATE_SPOT_DIRECTION;
1145 break;
1146 }
1147 break;
1148
1149 case STATE_LIGHT_MODEL:
1150 switch (*(*inst)++) {
1151 case LIGHT_MODEL_AMBIENT:
1152 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1153 break;
1154 case LIGHT_MODEL_SCENECOLOR:
1155 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1156 state_tokens[1] = parse_face_type (inst);
1157 break;
1158 }
1159 break;
1160
1161 case STATE_LIGHT_PROD:
1162 state_tokens[0] = STATE_LIGHTPROD;
1163 state_tokens[1] = parse_integer (inst, Program);
1164
1165 /* Check the value of state_tokens[1] against the # of lights */
1166 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001167 program_error(ctx, Program->Position, "Invalid Light Number");
1168 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001169 return 1;
1170 }
1171
1172 state_tokens[2] = parse_face_type (inst);
1173 switch (*(*inst)++) {
1174 case LIGHT_PROD_AMBIENT:
1175 state_tokens[3] = STATE_AMBIENT;
1176 break;
1177 case LIGHT_PROD_DIFFUSE:
1178 state_tokens[3] = STATE_DIFFUSE;
1179 break;
1180 case LIGHT_PROD_SPECULAR:
1181 state_tokens[3] = STATE_SPECULAR;
1182 break;
1183 }
1184 break;
1185
1186
1187 case STATE_FOG:
1188 switch (*(*inst)++) {
1189 case FOG_COLOR:
1190 state_tokens[0] = STATE_FOG_COLOR;
1191 break;
1192 case FOG_PARAMS:
1193 state_tokens[0] = STATE_FOG_PARAMS;
1194 break;
1195 }
1196 break;
1197
1198 case STATE_TEX_ENV:
1199 state_tokens[1] = parse_integer (inst, Program);
1200 switch (*(*inst)++) {
1201 case TEX_ENV_COLOR:
1202 state_tokens[0] = STATE_TEXENV_COLOR;
1203 break;
1204 }
1205 break;
1206
1207 case STATE_TEX_GEN:
1208 {
1209 GLuint type, coord;
1210
1211 state_tokens[0] = STATE_TEXGEN;
1212 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1213
1214 if (parse_texcoord_num (ctx, inst, Program, &coord))
1215 return 1;
1216 state_tokens[1] = coord;
1217
1218 /* EYE or OBJECT */
1219 type = *(*inst++);
1220
1221 /* 0 - s, 1 - t, 2 - r, 3 - q */
1222 coord = *(*inst++);
1223
1224 if (type == TEX_GEN_EYE) {
1225 switch (coord) {
1226 case COMPONENT_X:
1227 state_tokens[2] = STATE_TEXGEN_EYE_S;
1228 break;
1229 case COMPONENT_Y:
1230 state_tokens[2] = STATE_TEXGEN_EYE_T;
1231 break;
1232 case COMPONENT_Z:
1233 state_tokens[2] = STATE_TEXGEN_EYE_R;
1234 break;
1235 case COMPONENT_W:
1236 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1237 break;
1238 }
1239 }
1240 else {
1241 switch (coord) {
1242 case COMPONENT_X:
1243 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1244 break;
1245 case COMPONENT_Y:
1246 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1247 break;
1248 case COMPONENT_Z:
1249 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1250 break;
1251 case COMPONENT_W:
1252 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1253 break;
1254 }
1255 }
1256 }
1257 break;
1258
1259 case STATE_DEPTH:
1260 switch (*(*inst)++) {
1261 case DEPTH_RANGE:
1262 state_tokens[0] = STATE_DEPTH_RANGE;
1263 break;
1264 }
1265 break;
1266
1267 case STATE_CLIP_PLANE:
1268 state_tokens[0] = STATE_CLIPPLANE;
1269 state_tokens[1] = parse_integer (inst, Program);
1270 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1271 return 1;
1272 break;
1273
1274 case STATE_POINT:
1275 switch (*(*inst++)) {
1276 case POINT_SIZE:
1277 state_tokens[0] = STATE_POINT_SIZE;
1278 break;
1279
1280 case POINT_ATTENUATION:
1281 state_tokens[0] = STATE_POINT_ATTENUATION;
1282 break;
1283 }
1284 break;
1285
1286 /* XXX: I think this is the correct format for a matrix row */
1287 case STATE_MATRIX_ROWS:
1288 state_tokens[0] = STATE_MATRIX;
1289 if (parse_matrix
1290 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1291 &state_tokens[5]))
1292 return 1;
1293
1294 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1295
1296 if ((**inst) != 0) { /* Either the last row, 0 */
1297 state_tokens[4] = parse_integer (inst, Program);
1298 if (state_tokens[4] < state_tokens[3]) {
Brian Paula088f162006-09-05 23:08:51 +00001299 program_error(ctx, Program->Position,
1300 "Second matrix index less than the first");
1301 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001302 return 1;
1303 }
1304 }
1305 else {
1306 state_tokens[4] = state_tokens[3];
1307 (*inst)++;
1308 }
1309 break;
1310 }
1311
1312 return 0;
1313}
1314
1315/**
1316 * This parses a state string (rather, the binary version of it) into
1317 * a 6-token similar for the state fetching code in program.c
1318 *
1319 * One might ask, why fetch these parameters into just like you fetch
1320 * state when they are already stored in other places?
1321 *
1322 * Because of array offsets -> We can stick env/local parameters in the
1323 * middle of a parameter array and then index someplace into the array
1324 * when we execute.
1325 *
1326 * One optimization might be to only do this for the cases where the
1327 * env/local parameters end up inside of an array, and leave the
1328 * single parameters (or arrays of pure env/local pareameters) in their
1329 * respective register files.
1330 *
1331 * For ENV parameters, the format is:
1332 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1333 * state_tokens[1] = STATE_ENV
1334 * state_tokens[2] = the parameter index
1335 *
1336 * for LOCAL parameters, the format is:
1337 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1338 * state_tokens[1] = STATE_LOCAL
1339 * state_tokens[2] = the parameter index
1340 *
1341 * \param inst - the start in the binary arry to start working from
1342 * \param state_tokens - the storage for the 6-token state description
1343 * \return - 0 on sucess, 1 on failure
1344 */
1345static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001346parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001347 struct arb_program *Program, GLint * state_tokens)
1348{
1349 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1350 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1351 else
1352 state_tokens[0] = STATE_VERTEX_PROGRAM;
1353
1354
1355 switch (*(*inst)++) {
1356 case PROGRAM_PARAM_ENV:
1357 state_tokens[1] = STATE_ENV;
1358 state_tokens[2] = parse_integer (inst, Program);
1359
1360 /* Check state_tokens[2] against the number of ENV parameters available */
1361 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001362 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001363 ||
1364 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001365 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001366 program_error(ctx, Program->Position,
1367 "Invalid Program Env Parameter");
1368 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001369 return 1;
1370 }
1371
1372 break;
1373
1374 case PROGRAM_PARAM_LOCAL:
1375 state_tokens[1] = STATE_LOCAL;
1376 state_tokens[2] = parse_integer (inst, Program);
1377
1378 /* Check state_tokens[2] against the number of LOCAL parameters available */
1379 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001380 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001381 ||
1382 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001383 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001384 program_error(ctx, Program->Position,
1385 "Invalid Program Local Parameter");
1386 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001387 return 1;
1388 }
1389 break;
1390 }
1391
1392 return 0;
1393}
1394
1395/**
1396 * For ARB_vertex_program, programs are not allowed to use both an explicit
1397 * vertex attribute and a generic vertex attribute corresponding to the same
1398 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1399 *
1400 * This will walk our var_cache and make sure that nobody does anything fishy.
1401 *
1402 * \return 0 on sucess, 1 on error
1403 */
1404static GLuint
1405generic_attrib_check(struct var_cache *vc_head)
1406{
1407 int a;
1408 struct var_cache *curr;
1409 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1410 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1411
1412 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1413 explicitAttrib[a] = GL_FALSE;
1414 genericAttrib[a] = GL_FALSE;
1415 }
1416
1417 curr = vc_head;
1418 while (curr) {
1419 if (curr->type == vt_attrib) {
1420 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001421 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001422 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001423 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001424 }
1425
1426 curr = curr->next;
1427 }
1428
1429 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1430 if ((explicitAttrib[a]) && (genericAttrib[a]))
1431 return 1;
1432 }
1433
1434 return 0;
1435}
1436
1437/**
1438 * This will handle the binding side of an ATTRIB var declaration
1439 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001440 * \param inputReg returns the input register index, one of the
1441 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001442 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001443 */
1444static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001445parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001446 struct arb_program *Program,
1447 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001448{
Jouk Jansen40322e12004-04-05 08:50:36 +00001449 GLint err = 0;
1450
1451 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001452
Jouk Jansen40322e12004-04-05 08:50:36 +00001453 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1454 switch (*(*inst)++) {
1455 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001456 {
1457 GLint coord;
1458 err = parse_color_type (ctx, inst, Program, &coord);
1459 *inputReg = FRAG_ATTRIB_COL0 + coord;
1460 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001461 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001462 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001463 {
1464 GLuint texcoord;
1465 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1466 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1467 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001468 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001469 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001470 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001471 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001473 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001475 default:
1476 err = 1;
1477 break;
1478 }
1479 }
1480 else {
1481 switch (*(*inst)++) {
1482 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001483 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001484 break;
1485
1486 case VERTEX_ATTRIB_WEIGHT:
1487 {
1488 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001489 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001490 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001491#if 1
1492 /* hack for Warcraft (see bug 8060) */
1493 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001494 break;
Brian Paul3a557502006-09-05 23:15:29 +00001495#else
Brian Paula088f162006-09-05 23:08:51 +00001496 program_error(ctx, Program->Position,
1497 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001498 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001499#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001500 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001501
1502 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001503 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001504 break;
1505
1506 case VERTEX_ATTRIB_COLOR:
1507 {
1508 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001509 err = parse_color_type (ctx, inst, Program, &color);
1510 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001511 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001512 }
1513 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001514 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001515 }
1516 }
1517 break;
1518
1519 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001520 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001521 break;
1522
1523 case VERTEX_ATTRIB_TEXCOORD:
1524 {
1525 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001527 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001528 }
1529 break;
1530
Jouk Jansen40322e12004-04-05 08:50:36 +00001531 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001532 /* Not supported at this time */
1533 {
1534 const char *msg = "ARB_palette_matrix not supported";
1535 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001536 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001537 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001538 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001539
1540 case VERTEX_ATTRIB_GENERIC:
1541 {
1542 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001543 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001544 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001545 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001546 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1547 * attributes do not alias the conventional vertex
1548 * attributes.
1549 */
1550 if (attrib > 0)
1551 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001552 else
1553 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001554 }
1555 }
1556 break;
1557
1558 default:
1559 err = 1;
1560 break;
1561 }
1562 }
1563
Jouk Jansen40322e12004-04-05 08:50:36 +00001564 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001565 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001566 }
1567
Brian Paulde997602005-11-12 17:53:14 +00001568 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001569
1570 return err;
1571}
1572
Brian Paul7aebaf32005-10-30 21:23:23 +00001573
Jouk Jansen40322e12004-04-05 08:50:36 +00001574/**
1575 * This translates between a binary token for an output variable type
1576 * and the mesa token for the same thing.
1577 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001578 * \param inst The parsed tokens
1579 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001580 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001581 */
1582static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001583parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001584 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001585{
Brian Paul7aebaf32005-10-30 21:23:23 +00001586 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001587
Brian Paul7aebaf32005-10-30 21:23:23 +00001588 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001589 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001590 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001591 GLuint out_color;
1592
Brian Paulbe76b7f2004-10-04 14:40:05 +00001593 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001594 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001595 */
1596 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001597 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001598 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001599 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001600 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001601 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1602 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001603 }
1604 break;
1605
1606 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001608 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001609 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001610 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001611 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001612 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001613 GLint color_type;
1614 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001615 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1616 if (err)
1617 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001618
Jouk Jansen40322e12004-04-05 08:50:36 +00001619 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001620 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001622 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001623 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001625 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 }
1627 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001629 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001630 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001631 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001632 }
1633 /* primary color */
1634 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001635 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001636 }
1637 }
1638 }
1639 break;
1640
1641 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001642 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001643 break;
1644
1645 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001646 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001647 break;
1648
1649 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001650 {
1651 GLuint unit;
1652 if (parse_texcoord_num (ctx, inst, Program, &unit))
1653 return 1;
1654 *outputReg = VERT_RESULT_TEX0 + unit;
1655 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001656 break;
1657 }
1658
Brian Paulde997602005-11-12 17:53:14 +00001659 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001660
1661 return 0;
1662}
1663
Brian Paul7aebaf32005-10-30 21:23:23 +00001664
Jouk Jansen40322e12004-04-05 08:50:36 +00001665/**
1666 * This handles the declaration of ATTRIB variables
1667 *
1668 * XXX: Still needs
1669 * parse_vert_attrib_binding(), or something like that
1670 *
1671 * \return 0 on sucess, 1 on error
1672 */
1673static GLint
Brian Paula088f162006-09-05 23:08:51 +00001674parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001675 struct arb_program *Program)
1676{
1677 GLuint found;
1678 char *error_msg;
1679 struct var_cache *attrib_var;
1680
1681 attrib_var = parse_string (inst, vc_head, Program, &found);
1682 Program->Position = parse_position (inst);
1683 if (found) {
1684 error_msg = (char *)
1685 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1686 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1687 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001688 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001689 _mesa_free (error_msg);
1690 return 1;
1691 }
1692
1693 attrib_var->type = vt_attrib;
1694
Brian Paul7aebaf32005-10-30 21:23:23 +00001695 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001696 &attrib_var->attrib_is_generic))
1697 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001698
Brian Paul7aebaf32005-10-30 21:23:23 +00001699 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001700 program_error(ctx, Program->Position,
1701 "Cannot use both a generic vertex attribute "
1702 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001703 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001704 }
1705
1706 Program->Base.NumAttributes++;
1707 return 0;
1708}
1709
1710/**
1711 * \param use -- TRUE if we're called when declaring implicit parameters,
1712 * FALSE if we're declaraing variables. This has to do with
1713 * if we get a signed or unsigned float for scalar constants
1714 */
1715static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001716parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001717 struct var_cache *param_var,
1718 struct arb_program *Program, GLboolean use)
1719{
1720 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001721 GLuint err = 0;
Brianb7978af2007-01-09 19:17:17 -07001722 GLint state_tokens[STATE_LENGTH];
Jouk Jansen40322e12004-04-05 08:50:36 +00001723 GLfloat const_values[4];
1724
Jouk Jansen40322e12004-04-05 08:50:36 +00001725 switch (*(*inst)++) {
1726 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001727 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1728 return 1;
1729
1730 /* If we adding STATE_MATRIX that has multiple rows, we need to
1731 * unroll it and call _mesa_add_state_reference() for each row
1732 */
1733 if ((state_tokens[0] == STATE_MATRIX)
1734 && (state_tokens[3] != state_tokens[4])) {
1735 GLint row;
1736 GLint first_row = state_tokens[3];
1737 GLint last_row = state_tokens[4];
1738
1739 for (row = first_row; row <= last_row; row++) {
1740 state_tokens[3] = state_tokens[4] = row;
1741
Brian Paulde997602005-11-12 17:53:14 +00001742 idx = _mesa_add_state_reference(Program->Base.Parameters,
1743 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001744 if (param_var->param_binding_begin == ~0U)
1745 param_var->param_binding_begin = idx;
1746 param_var->param_binding_length++;
1747 Program->Base.NumParameters++;
1748 }
1749 }
1750 else {
Brian Paulde997602005-11-12 17:53:14 +00001751 idx = _mesa_add_state_reference(Program->Base.Parameters,
1752 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001753 if (param_var->param_binding_begin == ~0U)
1754 param_var->param_binding_begin = idx;
1755 param_var->param_binding_length++;
1756 Program->Base.NumParameters++;
1757 }
1758 break;
1759
1760 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001761 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1762 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001763 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001764 if (param_var->param_binding_begin == ~0U)
1765 param_var->param_binding_begin = idx;
1766 param_var->param_binding_length++;
1767 Program->Base.NumParameters++;
1768
1769 /* Check if there is more: 0 -> we're done, else its an integer */
1770 if (**inst) {
1771 GLuint out_of_range, new_idx;
1772 GLuint start_idx = state_tokens[2] + 1;
1773 GLuint end_idx = parse_integer (inst, Program);
1774
1775 out_of_range = 0;
1776 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1777 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001778 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001779 || ((state_tokens[1] == STATE_LOCAL)
1780 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001781 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001782 out_of_range = 1;
1783 }
1784 else {
1785 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001786 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001787 || ((state_tokens[1] == STATE_LOCAL)
1788 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001789 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001790 out_of_range = 1;
1791 }
1792 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001793 program_error(ctx, Program->Position,
1794 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001795 return 1;
1796 }
1797
1798 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1799 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001800 idx = _mesa_add_state_reference(Program->Base.Parameters,
1801 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001802 param_var->param_binding_length++;
1803 Program->Base.NumParameters++;
1804 }
1805 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001806 else {
1807 (*inst)++;
1808 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001809 break;
1810
1811 case PARAM_CONSTANT:
1812 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001813 idx = _mesa_add_named_constant(Program->Base.Parameters,
1814 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001815 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001816 if (param_var->param_binding_begin == ~0U)
1817 param_var->param_binding_begin = idx;
1818 param_var->param_binding_length++;
1819 Program->Base.NumParameters++;
1820 break;
1821
1822 default:
Brian Paula088f162006-09-05 23:08:51 +00001823 program_error(ctx, Program->Position,
1824 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001825 return 1;
1826 }
1827
1828 /* Make sure we haven't blown past our parameter limits */
1829 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1830 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001831 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001832 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1833 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001834 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001835 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001836 return 1;
1837 }
1838
1839 return err;
1840}
1841
Brian Paul7aebaf32005-10-30 21:23:23 +00001842
Jouk Jansen40322e12004-04-05 08:50:36 +00001843/**
1844 * This picks out PARAM program parameter bindings.
1845 *
1846 * XXX: This needs to be stressed & tested
1847 *
1848 * \return 0 on sucess, 1 on error
1849 */
1850static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001851parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001852 struct arb_program *Program)
1853{
Brian Paulbd997cd2004-07-20 21:12:56 +00001854 GLuint found, err;
1855 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001856 struct var_cache *param_var;
1857
1858 err = 0;
1859 param_var = parse_string (inst, vc_head, Program, &found);
1860 Program->Position = parse_position (inst);
1861
1862 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001863 char *error_msg = (char *)
1864 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001865 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1866 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001867 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001868 _mesa_free (error_msg);
1869 return 1;
1870 }
1871
1872 specified_length = parse_integer (inst, Program);
1873
1874 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001875 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001876 return 1;
1877 }
1878
1879 param_var->type = vt_param;
1880 param_var->param_binding_length = 0;
1881
1882 /* Right now, everything is shoved into the main state register file.
1883 *
1884 * In the future, it would be nice to leave things ENV/LOCAL params
1885 * in their respective register files, if possible
1886 */
1887 param_var->param_binding_type = PROGRAM_STATE_VAR;
1888
1889 /* Remember to:
1890 * * - add each guy to the parameter list
1891 * * - increment the param_var->param_binding_len
1892 * * - store the param_var->param_binding_begin for the first one
1893 * * - compare the actual len to the specified len at the end
1894 */
1895 while (**inst != PARAM_NULL) {
1896 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1897 return 1;
1898 }
1899
1900 /* Test array length here! */
1901 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001902 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001903 program_error(ctx, Program->Position,
1904 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001905 }
1906 }
1907
1908 (*inst)++;
1909
1910 return 0;
1911}
1912
1913/**
1914 *
1915 */
1916static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001917parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001918 struct arb_program *Program, struct var_cache **new_var)
1919{
1920 struct var_cache *param_var;
1921
1922 /* First, insert a dummy entry into the var_cache */
1923 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001924 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001925 param_var->type = vt_param;
1926
1927 param_var->param_binding_length = 0;
1928 /* Don't fill in binding_begin; We use the default value of -1
1929 * to tell if its already initialized, elsewhere.
1930 *
1931 * param_var->param_binding_begin = 0;
1932 */
1933 param_var->param_binding_type = PROGRAM_STATE_VAR;
1934
1935 var_cache_append (vc_head, param_var);
1936
1937 /* Then fill it with juicy parameter goodness */
1938 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1939 return 1;
1940
1941 *new_var = param_var;
1942
1943 return 0;
1944}
1945
1946
1947/**
1948 * This handles the declaration of TEMP variables
1949 *
1950 * \return 0 on sucess, 1 on error
1951 */
1952static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001953parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001954 struct arb_program *Program)
1955{
1956 GLuint found;
1957 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001958
1959 while (**inst != 0) {
1960 temp_var = parse_string (inst, vc_head, Program, &found);
1961 Program->Position = parse_position (inst);
1962 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001963 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001964 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1965 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1966 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001967 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001968 _mesa_free (error_msg);
1969 return 1;
1970 }
1971
1972 temp_var->type = vt_temp;
1973
1974 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1975 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001976 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001977 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1978 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001979 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00001980 program_error(ctx, Program->Position,
1981 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00001982 return 1;
1983 }
1984
1985 temp_var->temp_binding = Program->Base.NumTemporaries;
1986 Program->Base.NumTemporaries++;
1987 }
1988 (*inst)++;
1989
1990 return 0;
1991}
1992
1993/**
1994 * This handles variables of the OUTPUT variety
1995 *
1996 * \return 0 on sucess, 1 on error
1997 */
1998static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001999parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002000 struct arb_program *Program)
2001{
2002 GLuint found;
2003 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002004 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002005
2006 output_var = parse_string (inst, vc_head, Program, &found);
2007 Program->Position = parse_position (inst);
2008 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002009 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002010 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2011 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2012 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002013 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002014 _mesa_free (error_msg);
2015 return 1;
2016 }
2017
2018 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002019
2020 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2021 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002022}
2023
2024/**
2025 * This handles variables of the ALIAS kind
2026 *
2027 * \return 0 on sucess, 1 on error
2028 */
2029static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002030parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002031 struct arb_program *Program)
2032{
2033 GLuint found;
2034 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002035
2036 temp_var = parse_string (inst, vc_head, Program, &found);
2037 Program->Position = parse_position (inst);
2038
2039 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002040 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002041 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2042 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2043 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002044 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002045 _mesa_free (error_msg);
2046 return 1;
2047 }
2048
2049 temp_var->type = vt_alias;
2050 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2051 Program->Position = parse_position (inst);
2052
2053 if (!found)
2054 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002055 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002056 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2057 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2058 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002059 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002060 _mesa_free (error_msg);
2061 return 1;
2062 }
2063
2064 return 0;
2065}
2066
2067/**
2068 * This handles variables of the ADDRESS kind
2069 *
2070 * \return 0 on sucess, 1 on error
2071 */
2072static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002073parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002074 struct arb_program *Program)
2075{
2076 GLuint found;
2077 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002078
2079 while (**inst != 0) {
2080 temp_var = parse_string (inst, vc_head, Program, &found);
2081 Program->Position = parse_position (inst);
2082 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002083 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002084 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2085 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2086 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002087 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002088 _mesa_free (error_msg);
2089 return 1;
2090 }
2091
2092 temp_var->type = vt_address;
2093
2094 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002095 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002096 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002097 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002098 return 1;
2099 }
2100
2101 temp_var->address_binding = Program->Base.NumAddressRegs;
2102 Program->Base.NumAddressRegs++;
2103 }
2104 (*inst)++;
2105
2106 return 0;
2107}
2108
2109/**
2110 * Parse a program declaration
2111 *
2112 * \return 0 on sucess, 1 on error
2113 */
2114static GLint
Brian Paula088f162006-09-05 23:08:51 +00002115parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002116 struct arb_program *Program)
2117{
2118 GLint err = 0;
2119
2120 switch (*(*inst)++) {
2121 case ADDRESS:
2122 err = parse_address (ctx, inst, vc_head, Program);
2123 break;
2124
2125 case ALIAS:
2126 err = parse_alias (ctx, inst, vc_head, Program);
2127 break;
2128
2129 case ATTRIB:
2130 err = parse_attrib (ctx, inst, vc_head, Program);
2131 break;
2132
2133 case OUTPUT:
2134 err = parse_output (ctx, inst, vc_head, Program);
2135 break;
2136
2137 case PARAM:
2138 err = parse_param (ctx, inst, vc_head, Program);
2139 break;
2140
2141 case TEMP:
2142 err = parse_temp (ctx, inst, vc_head, Program);
2143 break;
2144 }
2145
2146 return err;
2147}
2148
2149/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002150 * Handle the parsing out of a masked destination register, either for a
2151 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002152 *
2153 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002154 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002155 * position invariant
2156 *
2157 * \param File - The register file we write to
2158 * \param Index - The register index we write to
2159 * \param WriteMask - The mask controlling which components we write (1->write)
2160 *
2161 * \return 0 on sucess, 1 on error
2162 */
2163static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002164parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002165 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002166 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002167{
Brian Paul7aebaf32005-10-30 21:23:23 +00002168 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002169 struct var_cache *dst;
2170
2171 /* We either have a result register specified, or a
2172 * variable that may or may not be writable
2173 */
2174 switch (*(*inst)++) {
2175 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002176 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002177 return 1;
2178 *File = PROGRAM_OUTPUT;
2179 break;
2180
2181 case REGISTER_ESTABLISHED_NAME:
2182 dst = parse_string (inst, vc_head, Program, &result);
2183 Program->Position = parse_position (inst);
2184
2185 /* If the name has never been added to our symbol table, we're hosed */
2186 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002187 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002188 return 1;
2189 }
2190
2191 switch (dst->type) {
2192 case vt_output:
2193 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002194 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002195 break;
2196
2197 case vt_temp:
2198 *File = PROGRAM_TEMPORARY;
2199 *Index = dst->temp_binding;
2200 break;
2201
2202 /* If the var type is not vt_output or vt_temp, no go */
2203 default:
Brian Paula088f162006-09-05 23:08:51 +00002204 program_error(ctx, Program->Position,
2205 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002206 return 1;
2207 }
2208 break;
2209
2210 default:
Brian Paula088f162006-09-05 23:08:51 +00002211 program_error(ctx, Program->Position,
2212 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002213 return 1;
2214 }
2215
2216
2217 /* Position invariance test */
2218 /* This test is done now in syntax portion - when position invariance OPTION
2219 is specified, "result.position" rule is disabled so there is no way
2220 to write the position
2221 */
2222 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2223 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002224 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002225 "Vertex program specified position invariance and wrote vertex position");
2226 }*/
2227
2228 /* And then the mask.
2229 * w,a -> bit 0
2230 * z,b -> bit 1
2231 * y,g -> bit 2
2232 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002233 *
2234 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002235 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002236 tmp = (GLint) *(*inst)++;
2237 *WriteMask = (((tmp>>3) & 0x1) |
2238 ((tmp>>1) & 0x2) |
2239 ((tmp<<1) & 0x4) |
2240 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002241
2242 return 0;
2243}
2244
2245
2246/**
2247 * Handle the parsing of a address register
2248 *
2249 * \param Index - The register index we write to
2250 *
2251 * \return 0 on sucess, 1 on error
2252 */
2253static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002254parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002255 struct var_cache **vc_head,
2256 struct arb_program *Program, GLint * Index)
2257{
2258 struct var_cache *dst;
2259 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002260
2261 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002262
2263 dst = parse_string (inst, vc_head, Program, &result);
2264 Program->Position = parse_position (inst);
2265
2266 /* If the name has never been added to our symbol table, we're hosed */
2267 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002268 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002269 return 1;
2270 }
2271
2272 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002273 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002274 return 1;
2275 }
2276
2277 return 0;
2278}
2279
Brian Paul8e8fa632005-07-01 02:03:33 +00002280#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002281/**
2282 * Handle the parsing out of a masked address register
2283 *
2284 * \param Index - The register index we write to
2285 * \param WriteMask - The mask controlling which components we write (1->write)
2286 *
2287 * \return 0 on sucess, 1 on error
2288 */
2289static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002290parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002291 struct var_cache **vc_head,
2292 struct arb_program *Program, GLint * Index,
2293 GLboolean * WriteMask)
2294{
2295 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2296 return 1;
2297
2298 /* This should be 0x8 */
2299 (*inst)++;
2300
2301 /* Writemask of .x is implied */
2302 WriteMask[0] = 1;
2303 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2304
2305 return 0;
2306}
Brian Paul8e8fa632005-07-01 02:03:33 +00002307#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002308
2309/**
2310 * Parse out a swizzle mask.
2311 *
Brian Paul32df89e2005-10-29 18:26:43 +00002312 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002313 *
2314 * The len parameter allows us to grab 4 components for a vector
2315 * swizzle, or just 1 component for a scalar src register selection
2316 */
Brian Paul32df89e2005-10-29 18:26:43 +00002317static void
Brian Paula088f162006-09-05 23:08:51 +00002318parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002319{
Brian Paul32df89e2005-10-29 18:26:43 +00002320 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002321
Brian Paul32df89e2005-10-29 18:26:43 +00002322 for (i = 0; i < 4; i++)
2323 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002324
Brian Paul32df89e2005-10-29 18:26:43 +00002325 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002326 switch (*(*inst)++) {
2327 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002328 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002329 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002330 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002331 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002332 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002333 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002334 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002335 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002336 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002337 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002338 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002339 default:
2340 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2341 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002342 }
2343 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002344}
2345
Jouk Jansen40322e12004-04-05 08:50:36 +00002346
Brian Paul32df89e2005-10-29 18:26:43 +00002347/**
2348 * Parse an extended swizzle mask which is a sequence of
2349 * four x/y/z/w/0/1 tokens.
2350 * \return swizzle four swizzle values
2351 * \return negateMask four element bitfield
2352 */
2353static void
Brian Paula088f162006-09-05 23:08:51 +00002354parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002355 GLubyte *negateMask)
2356{
2357 GLint i;
2358
2359 *negateMask = 0x0;
2360 for (i = 0; i < 4; i++) {
2361 GLubyte swz;
2362 if (parse_sign(inst) == -1)
2363 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002364
2365 swz = *(*inst)++;
2366
2367 switch (swz) {
2368 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002369 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002370 break;
2371 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002372 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002373 break;
2374 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002375 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002376 break;
2377 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002378 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002379 break;
2380 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002381 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002382 break;
2383 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002384 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002385 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002386 default:
2387 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2388 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002389 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002390 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002391}
2392
2393
2394static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002395parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2396 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002397 struct arb_program *Program,
2398 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002399 GLboolean *IsRelOffset )
2400{
2401 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002402 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002403 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002404
Keith Whitwell7c26b612005-04-21 14:46:57 +00002405 *IsRelOffset = 0;
2406
Jouk Jansen40322e12004-04-05 08:50:36 +00002407 /* And the binding for the src */
2408 switch (*(*inst)++) {
2409 case REGISTER_ATTRIB:
2410 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002411 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002412 return 1;
2413 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002414 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002415
2416 /* We need to insert a dummy variable into the var_cache so we can
2417 * catch generic vertex attrib aliasing errors
2418 */
2419 var_cache_create(&src);
2420 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002421 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002422 src->attrib_binding = binding;
2423 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002424 var_cache_append(vc_head, src);
2425 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002426 program_error(ctx, Program->Position,
2427 "Cannot use both a generic vertex attribute "
2428 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002429 return 1;
2430 }
2431 break;
2432
2433 case REGISTER_PARAM:
2434 switch (**inst) {
2435 case PARAM_ARRAY_ELEMENT:
2436 (*inst)++;
2437 src = parse_string (inst, vc_head, Program, &found);
2438 Program->Position = parse_position (inst);
2439
2440 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002441 program_error(ctx, Program->Position,
2442 "2: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002443 return 1;
2444 }
2445
Brian Paul95801792005-12-06 15:41:43 +00002446 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002447
2448 switch (*(*inst)++) {
2449 case ARRAY_INDEX_ABSOLUTE:
2450 offset = parse_integer (inst, Program);
2451
2452 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002453 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002454 program_error(ctx, Program->Position,
2455 "Index out of range");
2456 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002457 return 1;
2458 }
2459
2460 *Index = src->param_binding_begin + offset;
2461 break;
2462
2463 case ARRAY_INDEX_RELATIVE:
2464 {
2465 GLint addr_reg_idx, rel_off;
2466
2467 /* First, grab the address regiseter */
2468 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2469 return 1;
2470
2471 /* And the .x */
2472 ((*inst)++);
2473 ((*inst)++);
2474 ((*inst)++);
2475 ((*inst)++);
2476
2477 /* Then the relative offset */
2478 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2479
2480 /* And store it properly */
2481 *Index = src->param_binding_begin + rel_off;
2482 *IsRelOffset = 1;
2483 }
2484 break;
2485 }
2486 break;
2487
2488 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002489 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2490 return 1;
2491
Brian Paul95801792005-12-06 15:41:43 +00002492 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002493 *Index = src->param_binding_begin;
2494 break;
2495 }
2496 break;
2497
2498 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002499 src = parse_string (inst, vc_head, Program, &found);
2500 Program->Position = parse_position (inst);
2501
2502 /* If the name has never been added to our symbol table, we're hosed */
2503 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002504 program_error(ctx, Program->Position,
2505 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002506 return 1;
2507 }
2508
2509 switch (src->type) {
2510 case vt_attrib:
2511 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002512 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002513 break;
2514
2515 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2516 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002517 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002518 *Index = src->param_binding_begin;
2519 break;
2520
2521 case vt_temp:
2522 *File = PROGRAM_TEMPORARY;
2523 *Index = src->temp_binding;
2524 break;
2525
2526 /* If the var type is vt_output no go */
2527 default:
Brian Paula088f162006-09-05 23:08:51 +00002528 program_error(ctx, Program->Position,
2529 "destination register is read only");
2530 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002531 return 1;
2532 }
2533 break;
2534
2535 default:
Brian Paula088f162006-09-05 23:08:51 +00002536 program_error(ctx, Program->Position,
2537 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002538 return 1;
2539 }
2540
2541 return 0;
2542}
2543
2544/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002545 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002546 */
2547static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002548parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002549 struct var_cache **vc_head,
2550 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002551 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002552{
Brian Paul7aebaf32005-10-30 21:23:23 +00002553 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002554 GLint index;
2555 GLboolean negate;
2556 GLubyte swizzle[4];
2557 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002558
Jouk Jansen40322e12004-04-05 08:50:36 +00002559 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002560 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002561
2562 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002563 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002564 return 1;
2565
2566 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002567 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002568
Brian Paul32df89e2005-10-29 18:26:43 +00002569 reg->File = file;
2570 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002571 reg->NegateBase = negate;
2572 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002573 return 0;
2574}
2575
Jouk Jansen40322e12004-04-05 08:50:36 +00002576
Brian Paulb7fc1c32006-08-30 23:38:03 +00002577/**
2578 * Parse fragment program destination register.
2579 * \return 1 if error, 0 if no error.
2580 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002581static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002582parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002583 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002584 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002585{
Brian Paul7aebaf32005-10-30 21:23:23 +00002586 GLint mask;
2587 GLuint idx;
2588 enum register_file file;
2589
Keith Whitwell7c26b612005-04-21 14:46:57 +00002590 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002591 return 1;
2592
Keith Whitwell7c26b612005-04-21 14:46:57 +00002593 reg->File = file;
2594 reg->Index = idx;
2595 reg->WriteMask = mask;
2596 return 0;
2597}
2598
2599
Brian Paul7aebaf32005-10-30 21:23:23 +00002600/**
2601 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002602 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002603 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002604static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002605parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002606 struct var_cache **vc_head,
2607 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002608 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002609{
Brian Paul7aebaf32005-10-30 21:23:23 +00002610 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002611 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002612 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002613 GLubyte Swizzle[4];
2614 GLboolean IsRelOffset;
2615
2616 /* Grab the sign */
2617 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2618
2619 /* And the src reg */
2620 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2621 return 1;
2622
2623 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002624 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002625
Keith Whitwell7c26b612005-04-21 14:46:57 +00002626 reg->File = File;
2627 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002628 reg->NegateBase = Negate;
2629 reg->Swizzle = (Swizzle[0] << 0);
2630
Jouk Jansen40322e12004-04-05 08:50:36 +00002631 return 0;
2632}
2633
Keith Whitwell7c26b612005-04-21 14:46:57 +00002634
Jouk Jansen40322e12004-04-05 08:50:36 +00002635/**
2636 * This is a big mother that handles getting opcodes into the instruction
2637 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002638 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002639 */
2640static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002641parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002642 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002643 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002644{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002645 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002646 GLuint texcoord;
2647 GLubyte instClass, type, code;
2648 GLboolean rel;
2649
Brian Pauld6272e02006-10-29 18:03:16 +00002650 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002651
2652 /* Record the position in the program string for debugging */
2653 fp->StringPos = Program->Position;
2654
2655 /* OP_ALU_INST or OP_TEX_INST */
2656 instClass = *(*inst)++;
2657
2658 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2659 * OP_TEX_{SAMPLE, KIL}
2660 */
2661 type = *(*inst)++;
2662
2663 /* The actual opcode name */
2664 code = *(*inst)++;
2665
2666 /* Increment the correct count */
2667 switch (instClass) {
2668 case OP_ALU_INST:
2669 Program->NumAluInstructions++;
2670 break;
2671 case OP_TEX_INST:
2672 Program->NumTexInstructions++;
2673 break;
2674 }
2675
Jouk Jansen40322e12004-04-05 08:50:36 +00002676 switch (type) {
2677 case OP_ALU_VECTOR:
2678 switch (code) {
2679 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002680 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002681 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002682 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002683 break;
2684
2685 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002686 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002687 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002688 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002689 break;
2690
2691 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002692 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002693 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002694 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002695 break;
2696
2697 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002698 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002699 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002700 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002701 break;
2702
2703 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002704 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002705 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002706 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002707 break;
2708 }
2709
Keith Whitwell7c26b612005-04-21 14:46:57 +00002710 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002711 return 1;
2712
Keith Whitwell7c26b612005-04-21 14:46:57 +00002713 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002714 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002715 break;
2716
2717 case OP_ALU_SCALAR:
2718 switch (code) {
2719 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002720 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002721 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002722 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002723 break;
2724
2725 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002726 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002727 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002728 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002729 break;
2730
2731 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002732 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002733 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002734 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002735 break;
2736
2737 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002738 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002739 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002740 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002741 break;
2742
2743 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002744 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002745 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002746 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 break;
2748
2749 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002750 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002752 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 break;
2754
2755 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002756 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002757 case OP_SCS:
2758
Brian Paul7e807512005-11-05 17:10:45 +00002759 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 break;
2761 }
2762
Keith Whitwell7c26b612005-04-21 14:46:57 +00002763 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002764 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002765
2766 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002768 break;
2769
2770 case OP_ALU_BINSC:
2771 switch (code) {
2772 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002773 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002774 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002775 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002776 break;
2777 }
2778
Keith Whitwell7c26b612005-04-21 14:46:57 +00002779 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002780 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002781
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002783 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002784 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 }
2786 break;
2787
2788
2789 case OP_ALU_BIN:
2790 switch (code) {
2791 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002792 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002794 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002795 break;
2796
2797 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002798 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002799 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002800 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002801 break;
2802
2803 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002804 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002805 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002806 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 break;
2808
2809 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002810 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002811 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002812 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 break;
2814
2815 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002816 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002817 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002818 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 break;
2820
2821 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002822 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002823 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002824 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 break;
2826
2827 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002828 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002829 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002830 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 break;
2832
2833 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002834 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002836 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002837 break;
2838
2839 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002840 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002842 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002843 break;
2844
2845 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002846 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002848 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 break;
2850
2851 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002852 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002854 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 break;
2856
2857 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002858 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002859 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002860 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002861 break;
2862 }
2863
Keith Whitwell7c26b612005-04-21 14:46:57 +00002864 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002865 return 1;
2866 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002867 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2868 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002869 }
2870 break;
2871
2872 case OP_ALU_TRI:
2873 switch (code) {
2874 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002875 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002876 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002877 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002878 break;
2879
2880 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002881 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002882 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002883 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002884 break;
2885
2886 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002887 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002888 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002889 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002890 break;
2891 }
2892
Keith Whitwell7c26b612005-04-21 14:46:57 +00002893 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002894 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002895
Jouk Jansen40322e12004-04-05 08:50:36 +00002896 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002897 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2898 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 }
2900 break;
2901
2902 case OP_ALU_SWZ:
2903 switch (code) {
2904 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002905 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002906 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002907 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 break;
2909 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002910 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002911 return 1;
2912
Keith Whitwell7c26b612005-04-21 14:46:57 +00002913 {
Brian Paul32df89e2005-10-29 18:26:43 +00002914 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002915 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002916 enum register_file file;
2917 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002918
Brian Paul32df89e2005-10-29 18:26:43 +00002919 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002920 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002921 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2922 fp->SrcReg[0].File = file;
2923 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002924 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002925 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2926 swizzle[1],
2927 swizzle[2],
2928 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002929 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002930 break;
2931
2932 case OP_TEX_SAMPLE:
2933 switch (code) {
2934 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002935 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002937 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002938 break;
2939
2940 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002941 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002942 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002943 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002944 break;
2945
2946 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002947 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002948 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002949 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002950 break;
2951 }
2952
Keith Whitwell7c26b612005-04-21 14:46:57 +00002953 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002954 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002955
2956 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002957 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002958
2959 /* texImageUnit */
2960 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2961 return 1;
2962 fp->TexSrcUnit = texcoord;
2963
2964 /* texTarget */
2965 switch (*(*inst)++) {
2966 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00002967 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002968 break;
2969 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00002970 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002971 break;
2972 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00002973 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002974 break;
2975 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00002976 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002977 break;
2978 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00002979 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002980 break;
2981 case TEXTARGET_SHADOW1D:
2982 case TEXTARGET_SHADOW2D:
2983 case TEXTARGET_SHADOWRECT:
2984 /* TODO ARB_fragment_program_shadow code */
2985 break;
2986 }
Brian Paulb7fc1c32006-08-30 23:38:03 +00002987 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
2988 /* Check that both "2D" and "CUBE" (for example) aren't both used */
2989 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00002990 program_error(ctx, Program->Position,
2991 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00002992 return 1;
2993 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002994 break;
2995
2996 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00002997 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002998 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002999 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003000 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003001 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003002 default:
3003 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3004 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003005 }
3006
3007 return 0;
3008}
3009
Keith Whitwell7c26b612005-04-21 14:46:57 +00003010static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003011parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003012 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003013 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003014{
Brian Paul7aebaf32005-10-30 21:23:23 +00003015 GLint mask;
3016 GLuint idx;
3017 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003018
3019 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3020 return 1;
3021
3022 reg->File = file;
3023 reg->Index = idx;
3024 reg->WriteMask = mask;
3025 return 0;
3026}
3027
3028/**
3029 * Handle the parsing out of a masked address register
3030 *
3031 * \param Index - The register index we write to
3032 * \param WriteMask - The mask controlling which components we write (1->write)
3033 *
3034 * \return 0 on sucess, 1 on error
3035 */
3036static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003037parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003038 struct var_cache **vc_head,
3039 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003040 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003041{
3042 GLint idx;
3043
3044 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3045 return 1;
3046
3047 /* This should be 0x8 */
3048 (*inst)++;
3049
3050 reg->File = PROGRAM_ADDRESS;
3051 reg->Index = idx;
3052
3053 /* Writemask of .x is implied */
3054 reg->WriteMask = 0x1;
3055 return 0;
3056}
3057
3058/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003059 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003060 */
3061static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003062parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003063 struct var_cache **vc_head,
3064 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003065 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003066{
Brian Paul7aebaf32005-10-30 21:23:23 +00003067 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003068 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003069 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003070 GLubyte swizzle[4];
3071 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003072
3073 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003074 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003075
3076 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003077 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003078 return 1;
3079
3080 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003081 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003082
Brian Paul32df89e2005-10-29 18:26:43 +00003083 reg->File = file;
3084 reg->Index = index;
3085 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3086 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003087 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003088 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003089 return 0;
3090}
3091
3092
3093static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003094parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003095 struct var_cache **vc_head,
3096 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003097 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003098{
Brian Paul7aebaf32005-10-30 21:23:23 +00003099 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003100 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003101 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003102 GLubyte Swizzle[4];
3103 GLboolean IsRelOffset;
3104
3105 /* Grab the sign */
3106 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3107
3108 /* And the src reg */
3109 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3110 return 1;
3111
3112 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003113 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003114
3115 reg->File = File;
3116 reg->Index = Index;
3117 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003118 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003119 reg->RelAddr = IsRelOffset;
3120 return 0;
3121}
3122
3123
Jouk Jansen40322e12004-04-05 08:50:36 +00003124/**
3125 * This is a big mother that handles getting opcodes into the instruction
3126 * and handling the src & dst registers for vertex program instructions
3127 */
3128static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003129parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003130 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003131 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003132{
3133 GLint a;
3134 GLubyte type, code;
3135
3136 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3137 type = *(*inst)++;
3138
3139 /* The actual opcode name */
3140 code = *(*inst)++;
3141
Brian Pauld6272e02006-10-29 18:03:16 +00003142 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003143 /* Record the position in the program string for debugging */
3144 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003145
3146 switch (type) {
3147 /* XXX: */
3148 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003149 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003150
3151 /* Remember to set SrcReg.RelAddr; */
3152
3153 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003154 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003155 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003156
Jouk Jansen40322e12004-04-05 08:50:36 +00003157 vp->DstReg.File = PROGRAM_ADDRESS;
3158
3159 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003160 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003161 return 1;
3162
3163 break;
3164
3165 case OP_ALU_VECTOR:
3166 switch (code) {
3167 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003168 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003169 break;
3170 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003171 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003172 break;
3173 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003174 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003175 break;
3176 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003177 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003178 break;
3179 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003180 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003181 break;
3182 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003183
3184 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003185 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003186
3187 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003188 return 1;
3189 break;
3190
3191 case OP_ALU_SCALAR:
3192 switch (code) {
3193 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003194 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003195 break;
3196 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003197 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003198 break;
3199 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003200 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003201 break;
3202 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003203 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003204 break;
3205 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003206 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003207 break;
3208 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003209 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003210 break;
3211 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003212 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003213 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003214
3215 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003216 return 1;
3217 break;
3218
3219 case OP_ALU_BINSC:
3220 switch (code) {
3221 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003222 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003223 break;
3224 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003225 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003226 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003227
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003229 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003230 return 1;
3231 }
3232 break;
3233
3234 case OP_ALU_BIN:
3235 switch (code) {
3236 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003237 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003238 break;
3239 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003240 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003241 break;
3242 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003243 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003244 break;
3245 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003246 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003247 break;
3248 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003249 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003250 break;
3251 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003252 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003253 break;
3254 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003255 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003256 break;
3257 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003258 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003259 break;
3260 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003261 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003262 break;
3263 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003264 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003265 break;
3266 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003267 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003268 break;
3269 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003270 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003271 break;
3272 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003273 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003274 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003275
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003277 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003278 return 1;
3279 }
3280 break;
3281
3282 case OP_ALU_TRI:
3283 switch (code) {
3284 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003285 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003286 break;
3287 }
3288
Keith Whitwell7c26b612005-04-21 14:46:57 +00003289 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003290 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003291
Jouk Jansen40322e12004-04-05 08:50:36 +00003292 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003293 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 return 1;
3295 }
3296 break;
3297
3298 case OP_ALU_SWZ:
3299 switch (code) {
3300 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003301 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003302 break;
3303 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003304 {
Brian Paul32df89e2005-10-29 18:26:43 +00003305 GLubyte swizzle[4];
3306 GLubyte negateMask;
3307 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003308 enum register_file file;
3309 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003310
Keith Whitwell7c26b612005-04-21 14:46:57 +00003311 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3312 return 1;
3313
Brian Paul32df89e2005-10-29 18:26:43 +00003314 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003315 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003316 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3317 vp->SrcReg[0].File = file;
3318 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003319 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003320 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3321 swizzle[1],
3322 swizzle[2],
3323 swizzle[3]);
3324 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003325 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003326 break;
3327 }
3328 return 0;
3329}
3330
3331#if DEBUG_PARSING
3332
3333static GLvoid
3334print_state_token (GLint token)
3335{
3336 switch (token) {
3337 case STATE_MATERIAL:
3338 fprintf (stderr, "STATE_MATERIAL ");
3339 break;
3340 case STATE_LIGHT:
3341 fprintf (stderr, "STATE_LIGHT ");
3342 break;
3343
3344 case STATE_LIGHTMODEL_AMBIENT:
3345 fprintf (stderr, "STATE_AMBIENT ");
3346 break;
3347
3348 case STATE_LIGHTMODEL_SCENECOLOR:
3349 fprintf (stderr, "STATE_SCENECOLOR ");
3350 break;
3351
3352 case STATE_LIGHTPROD:
3353 fprintf (stderr, "STATE_LIGHTPROD ");
3354 break;
3355
3356 case STATE_TEXGEN:
3357 fprintf (stderr, "STATE_TEXGEN ");
3358 break;
3359
3360 case STATE_FOG_COLOR:
3361 fprintf (stderr, "STATE_FOG_COLOR ");
3362 break;
3363
3364 case STATE_FOG_PARAMS:
3365 fprintf (stderr, "STATE_FOG_PARAMS ");
3366 break;
3367
3368 case STATE_CLIPPLANE:
3369 fprintf (stderr, "STATE_CLIPPLANE ");
3370 break;
3371
3372 case STATE_POINT_SIZE:
3373 fprintf (stderr, "STATE_POINT_SIZE ");
3374 break;
3375
3376 case STATE_POINT_ATTENUATION:
3377 fprintf (stderr, "STATE_ATTENUATION ");
3378 break;
3379
3380 case STATE_MATRIX:
3381 fprintf (stderr, "STATE_MATRIX ");
3382 break;
3383
3384 case STATE_MODELVIEW:
3385 fprintf (stderr, "STATE_MODELVIEW ");
3386 break;
3387
3388 case STATE_PROJECTION:
3389 fprintf (stderr, "STATE_PROJECTION ");
3390 break;
3391
3392 case STATE_MVP:
3393 fprintf (stderr, "STATE_MVP ");
3394 break;
3395
3396 case STATE_TEXTURE:
3397 fprintf (stderr, "STATE_TEXTURE ");
3398 break;
3399
3400 case STATE_PROGRAM:
3401 fprintf (stderr, "STATE_PROGRAM ");
3402 break;
3403
3404 case STATE_MATRIX_INVERSE:
3405 fprintf (stderr, "STATE_INVERSE ");
3406 break;
3407
3408 case STATE_MATRIX_TRANSPOSE:
3409 fprintf (stderr, "STATE_TRANSPOSE ");
3410 break;
3411
3412 case STATE_MATRIX_INVTRANS:
3413 fprintf (stderr, "STATE_INVTRANS ");
3414 break;
3415
3416 case STATE_AMBIENT:
3417 fprintf (stderr, "STATE_AMBIENT ");
3418 break;
3419
3420 case STATE_DIFFUSE:
3421 fprintf (stderr, "STATE_DIFFUSE ");
3422 break;
3423
3424 case STATE_SPECULAR:
3425 fprintf (stderr, "STATE_SPECULAR ");
3426 break;
3427
3428 case STATE_EMISSION:
3429 fprintf (stderr, "STATE_EMISSION ");
3430 break;
3431
3432 case STATE_SHININESS:
3433 fprintf (stderr, "STATE_SHININESS ");
3434 break;
3435
3436 case STATE_HALF:
3437 fprintf (stderr, "STATE_HALF ");
3438 break;
3439
3440 case STATE_POSITION:
3441 fprintf (stderr, "STATE_POSITION ");
3442 break;
3443
3444 case STATE_ATTENUATION:
3445 fprintf (stderr, "STATE_ATTENUATION ");
3446 break;
3447
3448 case STATE_SPOT_DIRECTION:
3449 fprintf (stderr, "STATE_DIRECTION ");
3450 break;
3451
3452 case STATE_TEXGEN_EYE_S:
3453 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3454 break;
3455
3456 case STATE_TEXGEN_EYE_T:
3457 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3458 break;
3459
3460 case STATE_TEXGEN_EYE_R:
3461 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3462 break;
3463
3464 case STATE_TEXGEN_EYE_Q:
3465 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3466 break;
3467
3468 case STATE_TEXGEN_OBJECT_S:
3469 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3470 break;
3471
3472 case STATE_TEXGEN_OBJECT_T:
3473 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3474 break;
3475
3476 case STATE_TEXGEN_OBJECT_R:
3477 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3478 break;
3479
3480 case STATE_TEXGEN_OBJECT_Q:
3481 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3482 break;
3483
3484 case STATE_TEXENV_COLOR:
3485 fprintf (stderr, "STATE_TEXENV_COLOR ");
3486 break;
3487
3488 case STATE_DEPTH_RANGE:
3489 fprintf (stderr, "STATE_DEPTH_RANGE ");
3490 break;
3491
3492 case STATE_VERTEX_PROGRAM:
3493 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3494 break;
3495
3496 case STATE_FRAGMENT_PROGRAM:
3497 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3498 break;
3499
3500 case STATE_ENV:
3501 fprintf (stderr, "STATE_ENV ");
3502 break;
3503
3504 case STATE_LOCAL:
3505 fprintf (stderr, "STATE_LOCAL ");
3506 break;
3507
3508 }
3509 fprintf (stderr, "[%d] ", token);
3510}
3511
3512
3513static GLvoid
3514debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3515 struct arb_program *Program)
3516{
3517 struct var_cache *vc;
3518 GLint a, b;
3519
3520 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3521
3522 /* First of all, print out the contents of the var_cache */
3523 vc = vc_head;
3524 while (vc) {
3525 fprintf (stderr, "[%x]\n", vc);
3526 switch (vc->type) {
3527 case vt_none:
3528 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3529 break;
3530 case vt_attrib:
3531 fprintf (stderr, "ATTRIB %s\n", vc->name);
3532 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3533 break;
3534 case vt_param:
3535 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3536 vc->param_binding_begin, vc->param_binding_length);
3537 b = vc->param_binding_begin;
3538 for (a = 0; a < vc->param_binding_length; a++) {
3539 fprintf (stderr, "%s\n",
3540 Program->Parameters->Parameters[a + b].Name);
3541 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3542 print_state_token (Program->Parameters->Parameters[a + b].
3543 StateIndexes[0]);
3544 print_state_token (Program->Parameters->Parameters[a + b].
3545 StateIndexes[1]);
3546 print_state_token (Program->Parameters->Parameters[a + b].
3547 StateIndexes[2]);
3548 print_state_token (Program->Parameters->Parameters[a + b].
3549 StateIndexes[3]);
3550 print_state_token (Program->Parameters->Parameters[a + b].
3551 StateIndexes[4]);
3552 print_state_token (Program->Parameters->Parameters[a + b].
3553 StateIndexes[5]);
3554 }
3555 else
3556 fprintf (stderr, "%f %f %f %f\n",
3557 Program->Parameters->Parameters[a + b].Values[0],
3558 Program->Parameters->Parameters[a + b].Values[1],
3559 Program->Parameters->Parameters[a + b].Values[2],
3560 Program->Parameters->Parameters[a + b].Values[3]);
3561 }
3562 break;
3563 case vt_temp:
3564 fprintf (stderr, "TEMP %s\n", vc->name);
3565 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3566 break;
3567 case vt_output:
3568 fprintf (stderr, "OUTPUT %s\n", vc->name);
3569 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3570 break;
3571 case vt_alias:
3572 fprintf (stderr, "ALIAS %s\n", vc->name);
3573 fprintf (stderr, " binding: 0x%x (%s)\n",
3574 vc->alias_binding, vc->alias_binding->name);
3575 break;
3576 }
3577 vc = vc->next;
3578 }
3579}
3580
Brian Paul72030e02005-11-03 03:30:34 +00003581#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003582
3583
3584/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003585 * The main loop for parsing a fragment or vertex program
3586 *
Brian Paul72030e02005-11-03 03:30:34 +00003587 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003588 */
Brian Paul72030e02005-11-03 03:30:34 +00003589static GLint
Brian Paula088f162006-09-05 23:08:51 +00003590parse_instructions(GLcontext * ctx, const GLubyte * inst,
3591 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003592{
Brian Paul72030e02005-11-03 03:30:34 +00003593 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3594 ? ctx->Const.FragmentProgram.MaxInstructions
3595 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003596 GLint err = 0;
3597
Brian Paul72030e02005-11-03 03:30:34 +00003598 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3599
Jouk Jansen40322e12004-04-05 08:50:36 +00003600 Program->MajorVersion = (GLuint) * inst++;
3601 Program->MinorVersion = (GLuint) * inst++;
3602
3603 while (*inst != END) {
3604 switch (*inst++) {
3605
3606 case OPTION:
3607 switch (*inst++) {
3608 case ARB_PRECISION_HINT_FASTEST:
3609 Program->PrecisionOption = GL_FASTEST;
3610 break;
3611
3612 case ARB_PRECISION_HINT_NICEST:
3613 Program->PrecisionOption = GL_NICEST;
3614 break;
3615
3616 case ARB_FOG_EXP:
3617 Program->FogOption = GL_EXP;
3618 break;
3619
3620 case ARB_FOG_EXP2:
3621 Program->FogOption = GL_EXP2;
3622 break;
3623
3624 case ARB_FOG_LINEAR:
3625 Program->FogOption = GL_LINEAR;
3626 break;
3627
3628 case ARB_POSITION_INVARIANT:
3629 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003630 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003631 break;
3632
3633 case ARB_FRAGMENT_PROGRAM_SHADOW:
3634 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3635 /* TODO ARB_fragment_program_shadow code */
3636 }
3637 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003638
3639 case ARB_DRAW_BUFFERS:
3640 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3641 /* do nothing for now */
3642 }
3643 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003644 }
3645 break;
3646
3647 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003648 /* check length */
3649 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003650 program_error(ctx, Program->Position,
3651 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003652 return 1;
3653 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003654 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003655 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003656 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003657 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003658 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003659 }
3660 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003661 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003662 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003663 }
3664
Brian Paul72030e02005-11-03 03:30:34 +00003665 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003666 Program->Base.NumInstructions++;
3667 break;
3668
3669 case DECLARATION:
3670 err = parse_declaration (ctx, &inst, vc_head, Program);
3671 break;
3672
3673 default:
3674 break;
3675 }
3676
3677 if (err)
3678 break;
3679 }
3680
3681 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003682 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003683 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003684 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003685 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003686 /* YYY Wrong Position in program, whatever, at least not random -> crash
3687 Program->Position = parse_position (&inst);
3688 */
Brian Paul8c41a142005-11-19 15:36:28 +00003689 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003690 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003691 Program->Base.NumInstructions++;
3692
Brian Paul05051032005-11-01 04:36:33 +00003693 /*
3694 * Initialize native counts to logical counts. The device driver may
3695 * change them if program is translated into a hardware program.
3696 */
3697 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3698 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3699 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3700 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3701 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003702
Jouk Jansen40322e12004-04-05 08:50:36 +00003703 return err;
3704}
3705
Brian Paul7aebaf32005-10-30 21:23:23 +00003706
Jouk Jansen40322e12004-04-05 08:50:36 +00003707/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003708__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003709#include "grammar_syn.h"
3710;
3711
Brian Paul7aebaf32005-10-30 21:23:23 +00003712
Brian Paul8c41a142005-11-19 15:36:28 +00003713/**
3714 * Set a grammar parameter.
3715 * \param name the grammar parameter
3716 * \param value the new parameter value
3717 * \return 0 if OK, 1 if error
3718 */
3719static int
3720set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003721{
3722 char error_msg[300];
3723 GLint error_pos;
3724
Brian Paul8c41a142005-11-19 15:36:28 +00003725 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003726 return 0;
3727
3728 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3729 _mesa_set_program_error (ctx, error_pos, error_msg);
3730 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3731 return 1;
3732}
3733
Brian Paul8c41a142005-11-19 15:36:28 +00003734
3735/**
3736 * Enable support for the given language option in the parser.
3737 * \return 1 if OK, 0 if error
3738 */
3739static int
3740enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003741{
Brian Paul8c41a142005-11-19 15:36:28 +00003742 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003743}
3744
Brian Paul8c41a142005-11-19 15:36:28 +00003745
3746/**
3747 * Enable parser extensions based on which OpenGL extensions are supported
3748 * by this rendering context.
3749 *
3750 * \return GL_TRUE if OK, GL_FALSE if error.
3751 */
3752static GLboolean
3753enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003754{
Brian Paul8c41a142005-11-19 15:36:28 +00003755#if 0
3756 /* These are not supported at this time */
3757 if ((ctx->Extensions.ARB_vertex_blend ||
3758 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003759 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003760 return GL_FALSE;
3761 if (ctx->Extensions.ARB_matrix_palette
3762 && !enable_ext(ctx, id, "matrix_palette"))
3763 return GL_FALSE;
3764 if (ctx->Extensions.ARB_fragment_program_shadow
3765 && !enable_ext(ctx, id, "fragment_program_shadow"))
3766 return GL_FALSE;
3767#endif
3768 if (ctx->Extensions.EXT_point_parameters
3769 && !enable_ext(ctx, id, "point_parameters"))
3770 return GL_FALSE;
3771 if (ctx->Extensions.EXT_secondary_color
3772 && !enable_ext(ctx, id, "secondary_color"))
3773 return GL_FALSE;
3774 if (ctx->Extensions.EXT_fog_coord
3775 && !enable_ext(ctx, id, "fog_coord"))
3776 return GL_FALSE;
3777 if (ctx->Extensions.NV_texture_rectangle
3778 && !enable_ext(ctx, id, "texture_rectangle"))
3779 return GL_FALSE;
3780 if (ctx->Extensions.ARB_draw_buffers
3781 && !enable_ext(ctx, id, "draw_buffers"))
3782 return GL_FALSE;
3783
Brian Paul3a557502006-09-05 23:15:29 +00003784#if 1
3785 /* hack for Warcraft (see bug 8060) */
3786 enable_ext(ctx, id, "vertex_blend");
3787#endif
3788
Brian Paul8c41a142005-11-19 15:36:28 +00003789 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003790}
3791
Brian Paul8c41a142005-11-19 15:36:28 +00003792
Jouk Jansen40322e12004-04-05 08:50:36 +00003793/**
3794 * This kicks everything off.
3795 *
3796 * \param ctx - The GL Context
3797 * \param str - The program string
3798 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003799 * \param program - The arb_program struct to return all the parsed info in
3800 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003801 */
Brian Paul8c41a142005-11-19 15:36:28 +00003802static GLboolean
3803_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3804 const GLubyte *str, GLsizei len,
3805 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003806{
3807 GLint a, err, error_pos;
3808 char error_msg[300];
3809 GLuint parsed_len;
3810 struct var_cache *vc_head;
3811 grammar arbprogram_syn_id;
3812 GLubyte *parsed, *inst;
3813 GLubyte *strz = NULL;
3814 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3815
Brian Paul8c41a142005-11-19 15:36:28 +00003816 /* set the program target before parsing */
3817 program->Base.Target = target;
3818
Brian Paul7f76b8f2004-09-10 01:05:39 +00003819 /* Reset error state */
3820 _mesa_set_program_error(ctx, -1, NULL);
3821
Brian Paul8c41a142005-11-19 15:36:28 +00003822 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003823 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003824 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003825 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003826 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003827
3828 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3829 if (grammar_syn_id == 0) {
3830 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003831 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003832 _mesa_set_program_error (ctx, error_pos, error_msg);
3833 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003834 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003835 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003836 }
3837
Brian Paul8c41a142005-11-19 15:36:28 +00003838 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3839 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003840
Brian Paul49a80ca2006-04-28 15:40:11 +00003841 /* 'parsed' is unused here */
3842 _mesa_free (parsed);
3843 parsed = NULL;
3844
Brian Paul45703642005-10-29 15:52:31 +00003845 /* NOTE: we can't destroy grammar_syn_id right here because
3846 * grammar_destroy() can reset the last error
3847 */
Brian Paul8c41a142005-11-19 15:36:28 +00003848 if (err) {
3849 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003850 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3851 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003852 _mesa_error (ctx, GL_INVALID_OPERATION,
3853 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003854 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003855 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003856 }
3857
3858 grammar_destroy (grammar_syn_id);
3859
3860 arbprogram_syn_is_ok = 1;
3861 }
3862
3863 /* create the grammar object */
3864 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3865 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003866 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003867 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3868 _mesa_set_program_error (ctx, error_pos, error_msg);
3869 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003870 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003871 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003872 }
3873
3874 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003875 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003876 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3877 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003878 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003879 }
3880
Brian Paul8c41a142005-11-19 15:36:28 +00003881 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3882 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003883 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003884 }
3885
3886 /* check for NULL character occurences */
3887 {
Brian Paul8c41a142005-11-19 15:36:28 +00003888 GLint i;
3889 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003890 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003891 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003892 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003893 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003894 }
Brian Paul8c41a142005-11-19 15:36:28 +00003895 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003896 }
3897
3898 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003899 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003900 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003901 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3902 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003903 return GL_FALSE;
3904 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003905 _mesa_memcpy (strz, str, len);
3906 strz[len] = '\0';
3907
Michal Krolb80bc052004-10-21 14:09:54 +00003908 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003909 err = !grammar_fast_check(arbprogram_syn_id, strz,
3910 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003911
3912 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003913 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003914 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3915 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003916
Brian Paulb3aefd12005-09-19 20:12:32 +00003917#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003918 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003919 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003920 int line, col;
3921 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003922 fprintf(stderr, "program: %s\n", (char *) strz);
3923 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003924 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3925 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003926 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3927 } while (0)
3928#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003929
Brian Paula088f162006-09-05 23:08:51 +00003930 _mesa_free(strz);
3931 _mesa_free(parsed);
3932
Jouk Jansen40322e12004-04-05 08:50:36 +00003933 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003934 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003935 }
3936
Jouk Jansen40322e12004-04-05 08:50:36 +00003937 grammar_destroy (arbprogram_syn_id);
3938
Brian Paul8c41a142005-11-19 15:36:28 +00003939 /*
3940 * Program string is syntactically correct at this point
3941 * Parse the tokenized version of the program now, generating
3942 * vertex/fragment program instructions.
3943 */
3944
Jouk Jansen40322e12004-04-05 08:50:36 +00003945 /* Initialize the arb_program struct */
3946 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003947 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003948 program->Base.NumInstructions =
3949 program->Base.NumTemporaries =
3950 program->Base.NumParameters =
3951 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003952 program->Base.Parameters = _mesa_new_parameter_list ();
3953 program->Base.InputsRead = 0x0;
3954 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003955 program->Position = 0;
3956 program->MajorVersion = program->MinorVersion = 0;
3957 program->PrecisionOption = GL_DONT_CARE;
3958 program->FogOption = GL_NONE;
3959 program->HintPositionInvariant = GL_FALSE;
3960 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003961 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003962 program->NumAluInstructions =
3963 program->NumTexInstructions =
3964 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003965 program->UsesKill = 0;
3966
Jouk Jansen40322e12004-04-05 08:50:36 +00003967 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003968 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003969
3970 /* Start examining the tokens in the array */
3971 inst = parsed;
3972
3973 /* Check the grammer rev */
3974 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003975 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003976 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003977 }
3978 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003979 /* ignore program target */
3980 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003981 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003982 }
3983
3984 /*debug_variables(ctx, vc_head, program); */
3985
3986 /* We're done with the parsed binary array */
3987 var_cache_destroy (&vc_head);
3988
3989 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003990
Brian Paul8c41a142005-11-19 15:36:28 +00003991 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3992 * to size [ap.Base.NumInstructions].
3993 */
Brian Paula7543902006-08-24 21:58:32 +00003994 program->Base.Instructions
3995 = _mesa_realloc_instructions(program->Base.Instructions,
3996 MAX_INSTRUCTIONS,
3997 program->Base.NumInstructions);
3998
Brian Paul45703642005-10-29 15:52:31 +00003999 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004000}
Brian Paul8c41a142005-11-19 15:36:28 +00004001
4002
4003
4004void
4005_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4006 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004007 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004008{
4009 struct arb_program ap;
4010 GLuint i;
4011
4012 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004013 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004014 /* Error in the program. Just return. */
4015 return;
4016 }
4017
4018 /* Copy the relevant contents of the arb_program struct into the
4019 * fragment_program struct.
4020 */
4021 program->Base.String = ap.Base.String;
4022 program->Base.NumInstructions = ap.Base.NumInstructions;
4023 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4024 program->Base.NumParameters = ap.Base.NumParameters;
4025 program->Base.NumAttributes = ap.Base.NumAttributes;
4026 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004027 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4028 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4029 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4030 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4031 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07004032 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
4033 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
4034 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
4035 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
4036 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
4037 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004038 program->Base.InputsRead = ap.Base.InputsRead;
4039 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4040 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
Brianc9db2232007-01-04 17:22:19 -07004041 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Brian Paul8c41a142005-11-19 15:36:28 +00004042 program->FogOption = ap.FogOption;
4043
4044 if (program->Base.Instructions)
4045 _mesa_free(program->Base.Instructions);
4046 program->Base.Instructions = ap.Base.Instructions;
4047
4048 if (program->Base.Parameters)
4049 _mesa_free_parameter_list(program->Base.Parameters);
4050 program->Base.Parameters = ap.Base.Parameters;
4051
4052#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00004053 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
4054 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00004055#endif
4056}
4057
4058
4059
4060/**
4061 * Parse the vertex program string. If success, update the given
4062 * vertex_program object with the new program. Else, leave the vertex_program
4063 * object unchanged.
4064 */
4065void
4066_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4067 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004068 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004069{
4070 struct arb_program ap;
4071
4072 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4073
Brian Paul95801792005-12-06 15:41:43 +00004074 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004075 /* Error in the program. Just return. */
4076 return;
4077 }
4078
4079 /* Copy the relevant contents of the arb_program struct into the
4080 * vertex_program struct.
4081 */
4082 program->Base.String = ap.Base.String;
4083 program->Base.NumInstructions = ap.Base.NumInstructions;
4084 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4085 program->Base.NumParameters = ap.Base.NumParameters;
4086 program->Base.NumAttributes = ap.Base.NumAttributes;
4087 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004088 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4089 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4090 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4091 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4092 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004093 program->Base.InputsRead = ap.Base.InputsRead;
4094 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4095 program->IsPositionInvariant = ap.HintPositionInvariant;
4096
4097 if (program->Base.Instructions)
4098 _mesa_free(program->Base.Instructions);
4099 program->Base.Instructions = ap.Base.Instructions;
4100
4101 if (program->Base.Parameters)
4102 _mesa_free_parameter_list(program->Base.Parameters);
4103 program->Base.Parameters = ap.Base.Parameters;
4104
4105#if DEBUG_VP
Brian Paul11a54c32006-11-15 19:54:25 +00004106 _mesa_printf("____________Vertex program %u __________\n", program->Base.ID);
Brian Paul8c41a142005-11-19 15:36:28 +00004107 _mesa_print_program(&program->Base);
4108#endif
4109}