blob: cdf1b3dc1d2c06f7dc69223a3299a6871bb10425 [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian Paul54cfe692005-10-21 15:22:36 +00003 * Version: 6.5
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Michal Krolbb38cad2006-04-11 11:41:11 +00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
Jouk Jansen40322e12004-04-05 08:50:36 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#define DEBUG_PARSING 0
26
27/**
28 * \file arbprogparse.c
29 * ARB_*_program parser core
30 * \author Karl Rasche
31 */
32
Jouk Jansen40322e12004-04-05 08:50:36 +000033#include "glheader.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000034#include "imports.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000035#include "arbprogparse.h"
36#include "grammar_mesa.h"
Brian Paul32df89e2005-10-29 18:26:43 +000037#include "program.h"
Brian Paul8c41a142005-11-19 15:36:28 +000038#include "context.h"
39#include "mtypes.h"
40#include "program_instruction.h"
41
42
43#define MAX_INSTRUCTIONS 256
44
45
46/**
47 * This is basically a union of the vertex_program and fragment_program
48 * structs that we can use to parse the program into
49 *
50 * XXX we can probably get rid of this entirely someday.
51 */
52struct arb_program
53{
54 struct program Base;
55
56 GLuint Position; /* Just used for error reporting while parsing */
57 GLuint MajorVersion;
58 GLuint MinorVersion;
59
60 /* ARB_vertex_progmra options */
61 GLboolean HintPositionInvariant;
62
63 /* ARB_fragment_progmra options */
64 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
65 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
66
67 /* ARB_fragment_program specifics */
68 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
69 GLuint NumAluInstructions;
70 GLuint NumTexInstructions;
71 GLuint NumTexIndirections;
72
73 GLboolean UsesKill;
74};
75
Ian Romanick9bdfee32005-07-18 12:31:24 +000076
Alan Hourihane38b317d2004-12-14 09:11:52 +000077#ifndef __extension__
78#if !defined(__GNUC__) || (__GNUC__ < 2) || \
79 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
Brian Paula6c423d2004-08-25 15:59:48 +000080# define __extension__
81#endif
Alan Hourihane38b317d2004-12-14 09:11:52 +000082#endif
Brian Paula6c423d2004-08-25 15:59:48 +000083
Jouk Jansen40322e12004-04-05 08:50:36 +000084/* TODO:
85 * Fragment Program Stuff:
86 * -----------------------------------------------------
87 *
88 * - things from Michal's email
89 * + overflow on atoi
90 * + not-overflowing floats (don't use parse_integer..)
91 * + can remove range checking in arbparse.c
92 *
93 * - check all limits of number of various variables
94 * + parameters
95 *
96 * - test! test! test!
97 *
98 * Vertex Program Stuff:
99 * -----------------------------------------------------
100 * - Optimize param array usage and count limits correctly, see spec,
101 * section 2.14.3.7
102 * + Record if an array is reference absolutly or relatively (or both)
103 * + For absolute arrays, store a bitmap of accesses
104 * + For single parameters, store an access flag
105 * + After parsing, make a parameter cleanup and merging pass, where
106 * relative arrays are layed out first, followed by abs arrays, and
107 * finally single state.
108 * + Remap offsets for param src and dst registers
109 * + Now we can properly count parameter usage
110 *
111 * - Multiple state binding errors in param arrays (see spec, just before
112 * section 2.14.3.3)
113 * - grep for XXX
114 *
115 * Mesa Stuff
116 * -----------------------------------------------------
117 * - User clipping planes vs. PositionInvariant
118 * - Is it sufficient to just multiply by the mvp to transform in the
119 * PositionInvariant case? Or do we need something more involved?
120 *
121 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
122 * - fetch state listed in program_parameters list
123 * + WTF should this go???
124 * + currently in nvvertexec.c and s_nvfragprog.c
125 *
126 * - allow for multiple address registers (and fetch address regs properly)
127 *
128 * Cosmetic Stuff
129 * -----------------------------------------------------
130 * - remove any leftover unused grammer.c stuff (dict_ ?)
131 * - fix grammer.c error handling so its not static
132 * - #ifdef around stuff pertaining to extentions
133 *
134 * Outstanding Questions:
135 * -----------------------------------------------------
136 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
137 * what gets hacked off because of this:
138 * + VERTEX_ATTRIB_MATRIXINDEX
139 * + VERTEX_ATTRIB_WEIGHT
140 * + MATRIX_MODELVIEW
141 * + MATRIX_PALETTE
142 *
143 * - When can we fetch env/local params from their own register files, and
144 * when to we have to fetch them into the main state register file?
145 * (think arrays)
146 *
147 * Grammar Changes:
148 * -----------------------------------------------------
149 */
150
151/* Changes since moving the file to shader directory
152
1532004-III-4 ------------------------------------------------------------
154- added #include "grammar_mesa.h"
155- removed grammar specific code part (it resides now in grammar.c)
156- added GL_ARB_fragment_program_shadow tokens
157- modified #include "arbparse_syn.h"
158- major changes inside _mesa_parse_arb_program()
159- check the program string for '\0' characters
160- copy the program string to a one-byte-longer location to have
161 it null-terminated
162- position invariance test (not writing to result.position) moved
163 to syntax part
164*/
165
166typedef GLubyte *production;
167
168/**
169 * This is the text describing the rules to parse the grammar
170 */
Brian Paula6c423d2004-08-25 15:59:48 +0000171__extension__ 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 *
583var_cache_find (struct var_cache *va, GLubyte * name)
584{
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
600/**
601 * constructs an integer from 4 GLubytes in LE format
602 */
603static GLuint
604parse_position (GLubyte ** inst)
605{
606 GLuint value;
607
608 value = (GLuint) (*(*inst)++);
609 value += (GLuint) (*(*inst)++) * 0x100;
610 value += (GLuint) (*(*inst)++) * 0x10000;
611 value += (GLuint) (*(*inst)++) * 0x1000000;
612
613 return value;
614}
615
616/**
617 * This will, given a string, lookup the string as a variable name in the
618 * var cache. If the name is found, the var cache node corresponding to the
619 * var name is returned. If it is not found, a new entry is allocated
620 *
621 * \param I Points into the binary array where the string identifier begins
622 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
623 * \return The location on the var_cache corresponding the the string starting at I
624 */
625static struct var_cache *
626parse_string (GLubyte ** inst, struct var_cache **vc_head,
627 struct arb_program *Program, GLuint * found)
628{
629 GLubyte *i = *inst;
630 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000631 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000632
633 *inst += _mesa_strlen ((char *) i) + 1;
634
635 va = var_cache_find (*vc_head, i);
636
637 if (va) {
638 *found = 1;
639 return va;
640 }
641
642 *found = 0;
643 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000644 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000645
646 var_cache_append (vc_head, va);
647
648 return va;
649}
650
651static char *
652parse_string_without_adding (GLubyte ** inst, struct arb_program *Program)
653{
654 GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000655 (void) Program;
656
Jouk Jansen40322e12004-04-05 08:50:36 +0000657 *inst += _mesa_strlen ((char *) i) + 1;
658
659 return (char *) i;
660}
661
662/**
Brian Paul05908952004-06-08 15:20:23 +0000663 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000664 */
Brian Paul05908952004-06-08 15:20:23 +0000665static GLint
Jouk Jansen40322e12004-04-05 08:50:36 +0000666parse_sign (GLubyte ** inst)
667{
668 /*return *(*inst)++ != '+'; */
669
670 if (**inst == '-') {
671 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000672 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000673 }
674 else if (**inst == '+') {
675 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000676 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000677 }
678
Brian Paul05908952004-06-08 15:20:23 +0000679 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000680}
681
682/**
683 * parses and returns signed integer
684 */
685static GLint
686parse_integer (GLubyte ** inst, struct arb_program *Program)
687{
688 GLint sign;
689 GLint value;
690
691 /* check if *inst points to '+' or '-'
692 * if yes, grab the sign and increment *inst
693 */
694 sign = parse_sign (inst);
695
696 /* now check if *inst points to 0
697 * if yes, increment the *inst and return the default value
698 */
699 if (**inst == 0) {
700 (*inst)++;
701 return 0;
702 }
703
704 /* parse the integer as you normally would do it */
705 value = _mesa_atoi (parse_string_without_adding (inst, Program));
706
707 /* now, after terminating 0 there is a position
708 * to parse it - parse_position()
709 */
710 Program->Position = parse_position (inst);
711
Brian Paul05908952004-06-08 15:20:23 +0000712 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000713}
714
715/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000716 Accumulate this string of digits, and return them as
717 a large integer represented in floating point (for range).
718 If scale is not NULL, also accumulates a power-of-ten
719 integer scale factor that represents the number of digits
720 in the string.
721*/
722static GLdouble
723parse_float_string(GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
724{
725 GLdouble value = 0.0;
726 GLdouble oscale = 1.0;
727
728 if (**inst == 0) { /* this string of digits is empty-- do nothing */
729 (*inst)++;
730 }
731 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000732 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000733 GLubyte digit = *((*inst)++);
734 value = value * 10.0 + (GLint) (digit - '0');
735 oscale *= 10.0;
736 }
737 assert(**inst == 0); /* integer string should end with 0 */
738 (*inst)++; /* skip over terminating 0 */
739 Program->Position = parse_position(inst); /* skip position (from integer) */
740 }
741 if (scale)
742 *scale = oscale;
743 return value;
744}
745
746/**
747 Parse an unsigned floating-point number from this stream of tokenized
748 characters. Example floating-point formats supported:
749 12.34
750 12
751 0.34
752 .34
753 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000754 */
755static GLfloat
756parse_float (GLubyte ** inst, struct arb_program *Program)
757{
Brian Paul1ff8f502005-02-16 15:08:29 +0000758 GLint exponent;
759 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000760
Brian Paul1ff8f502005-02-16 15:08:29 +0000761 whole = parse_float_string(inst, Program, 0);
762 fraction = parse_float_string(inst, Program, &fracScale);
763
764 /* Parse signed exponent */
765 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000766
Brian Paul1ff8f502005-02-16 15:08:29 +0000767 /* Assemble parts of floating-point number: */
768 return (GLfloat) ((whole + fraction / fracScale) *
769 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000770}
771
772
773/**
774 */
775static GLfloat
776parse_signed_float (GLubyte ** inst, struct arb_program *Program)
777{
Brian Paul05908952004-06-08 15:20:23 +0000778 GLint sign = parse_sign (inst);
779 GLfloat value = parse_float (inst, Program);
780 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000781}
782
783/**
784 * This picks out a constant value from the parsed array. The constant vector is r
785 * returned in the *values array, which should be of length 4.
786 *
787 * \param values - The 4 component vector with the constant value in it
788 */
789static GLvoid
790parse_constant (GLubyte ** inst, GLfloat *values, struct arb_program *Program,
791 GLboolean use)
792{
793 GLuint components, i;
794
795
796 switch (*(*inst)++) {
797 case CONSTANT_SCALAR:
798 if (use == GL_TRUE) {
799 values[0] =
800 values[1] =
801 values[2] = values[3] = parse_float (inst, Program);
802 }
803 else {
804 values[0] =
805 values[1] =
806 values[2] = values[3] = parse_signed_float (inst, Program);
807 }
808
809 break;
810 case CONSTANT_VECTOR:
811 values[0] = values[1] = values[2] = 0;
812 values[3] = 1;
813 components = *(*inst)++;
814 for (i = 0; i < components; i++) {
815 values[i] = parse_signed_float (inst, Program);
816 }
817 break;
818 }
819}
820
821/**
822 * \param offset The offset from the address register that we should
823 * address
824 *
825 * \return 0 on sucess, 1 on error
826 */
827static GLuint
828parse_relative_offset (GLcontext *ctx, GLubyte **inst, struct arb_program *Program,
829 GLint *offset)
830{
Brian Paul6a769d92006-04-28 15:42:15 +0000831 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000832 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000833 return 0;
834}
835
836/**
837 * \param color 0 if color type is primary, 1 if color type is secondary
838 * \return 0 on sucess, 1 on error
839 */
840static GLuint
841parse_color_type (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
842 GLint * color)
843{
Brian Paula6c423d2004-08-25 15:59:48 +0000844 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000845 *color = *(*inst)++ != COLOR_PRIMARY;
846 return 0;
847}
848
849/**
850 * Get an integer corresponding to a generic vertex attribute.
851 *
852 * \return 0 on sucess, 1 on error
853 */
854static GLuint
855parse_generic_attrib_num(GLcontext *ctx, GLubyte ** inst,
856 struct arb_program *Program, GLuint *attrib)
857{
Brian Paulbd997cd2004-07-20 21:12:56 +0000858 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000859
Michal Krolbb38cad2006-04-11 11:41:11 +0000860 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000861 {
862 _mesa_set_program_error (ctx, Program->Position,
863 "Invalid generic vertex attribute index");
864 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid generic vertex attribute index");
865
866 return 1;
867 }
868
Brian Paulbd997cd2004-07-20 21:12:56 +0000869 *attrib = (GLuint) i;
870
Jouk Jansen40322e12004-04-05 08:50:36 +0000871 return 0;
872}
873
874
875/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000876 * \param color The index of the color buffer to write into
877 * \return 0 on sucess, 1 on error
878 */
879static GLuint
880parse_output_color_num (GLcontext * ctx, GLubyte ** inst,
881 struct arb_program *Program, GLuint * color)
882{
883 GLint i = parse_integer (inst, Program);
884
885 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
886 _mesa_set_program_error (ctx, Program->Position,
887 "Invalid draw buffer index");
888 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid draw buffer index");
889 return 1;
890 }
891
892 *color = (GLuint) i;
893 return 0;
894}
895
896
897/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000898 * \param coord The texture unit index
899 * \return 0 on sucess, 1 on error
900 */
901static GLuint
902parse_texcoord_num (GLcontext * ctx, GLubyte ** inst,
903 struct arb_program *Program, GLuint * coord)
904{
Brian Paulbd997cd2004-07-20 21:12:56 +0000905 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000906
Brian Paula6c423d2004-08-25 15:59:48 +0000907 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000908 _mesa_set_program_error (ctx, Program->Position,
909 "Invalid texture unit index");
910 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid texture unit index");
911 return 1;
912 }
913
Brian Paulbd997cd2004-07-20 21:12:56 +0000914 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000915 return 0;
916}
917
918/**
919 * \param coord The weight index
920 * \return 0 on sucess, 1 on error
921 */
922static GLuint
923parse_weight_num (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
924 GLint * coord)
925{
926 *coord = parse_integer (inst, Program);
927
928 if ((*coord < 0) || (*coord >= 1)) {
929 _mesa_set_program_error (ctx, Program->Position,
930 "Invalid weight index");
931 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid weight index");
932 return 1;
933 }
934
935 return 0;
936}
937
938/**
939 * \param coord The clip plane index
940 * \return 0 on sucess, 1 on error
941 */
942static GLuint
943parse_clipplane_num (GLcontext * ctx, GLubyte ** inst,
944 struct arb_program *Program, GLint * coord)
945{
946 *coord = parse_integer (inst, Program);
947
948 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
949 _mesa_set_program_error (ctx, Program->Position,
950 "Invalid clip plane index");
951 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid clip plane index");
952 return 1;
953 }
954
955 return 0;
956}
957
958
959/**
960 * \return 0 on front face, 1 on back face
961 */
962static GLuint
963parse_face_type (GLubyte ** inst)
964{
965 switch (*(*inst)++) {
966 case FACE_FRONT:
967 return 0;
968
969 case FACE_BACK:
970 return 1;
971 }
972 return 0;
973}
974
975
976/**
977 * Given a matrix and a modifier token on the binary array, return tokens
978 * that _mesa_fetch_state() [program.c] can understand.
979 *
980 * \param matrix - the matrix we are talking about
981 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
982 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
983 * \return 0 on sucess, 1 on failure
984 */
985static GLuint
986parse_matrix (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
987 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
988{
989 GLubyte mat = *(*inst)++;
990
991 *matrix_idx = 0;
992
993 switch (mat) {
994 case MATRIX_MODELVIEW:
995 *matrix = STATE_MODELVIEW;
996 *matrix_idx = parse_integer (inst, Program);
997 if (*matrix_idx > 0) {
998 _mesa_set_program_error (ctx, Program->Position,
999 "ARB_vertex_blend not supported\n");
1000 _mesa_error (ctx, GL_INVALID_OPERATION,
1001 "ARB_vertex_blend not supported\n");
1002 return 1;
1003 }
1004 break;
1005
1006 case MATRIX_PROJECTION:
1007 *matrix = STATE_PROJECTION;
1008 break;
1009
1010 case MATRIX_MVP:
1011 *matrix = STATE_MVP;
1012 break;
1013
1014 case MATRIX_TEXTURE:
1015 *matrix = STATE_TEXTURE;
1016 *matrix_idx = parse_integer (inst, Program);
1017 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
1018 _mesa_set_program_error (ctx, Program->Position,
1019 "Invalid Texture Unit");
1020 _mesa_error (ctx, GL_INVALID_OPERATION,
1021 "Invalid Texture Unit: %d", *matrix_idx);
1022 return 1;
1023 }
1024 break;
1025
1026 /* This is not currently supported (ARB_matrix_palette) */
1027 case MATRIX_PALETTE:
1028 *matrix_idx = parse_integer (inst, Program);
1029 _mesa_set_program_error (ctx, Program->Position,
1030 "ARB_matrix_palette not supported\n");
1031 _mesa_error (ctx, GL_INVALID_OPERATION,
1032 "ARB_matrix_palette not supported\n");
1033 return 1;
1034 break;
1035
1036 case MATRIX_PROGRAM:
1037 *matrix = STATE_PROGRAM;
1038 *matrix_idx = parse_integer (inst, Program);
1039 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
1040 _mesa_set_program_error (ctx, Program->Position,
1041 "Invalid Program Matrix");
1042 _mesa_error (ctx, GL_INVALID_OPERATION,
1043 "Invalid Program Matrix: %d", *matrix_idx);
1044 return 1;
1045 }
1046 break;
1047 }
1048
1049 switch (*(*inst)++) {
1050 case MATRIX_MODIFIER_IDENTITY:
1051 *matrix_modifier = 0;
1052 break;
1053 case MATRIX_MODIFIER_INVERSE:
1054 *matrix_modifier = STATE_MATRIX_INVERSE;
1055 break;
1056 case MATRIX_MODIFIER_TRANSPOSE:
1057 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1058 break;
1059 case MATRIX_MODIFIER_INVTRANS:
1060 *matrix_modifier = STATE_MATRIX_INVTRANS;
1061 break;
1062 }
1063
1064 return 0;
1065}
1066
1067
1068/**
1069 * This parses a state string (rather, the binary version of it) into
1070 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1071 *
1072 * \param inst - the start in the binary arry to start working from
1073 * \param state_tokens - the storage for the 6-token state description
1074 * \return - 0 on sucess, 1 on error
1075 */
1076static GLuint
1077parse_state_single_item (GLcontext * ctx, GLubyte ** inst,
1078 struct arb_program *Program, GLint * state_tokens)
1079{
1080 switch (*(*inst)++) {
1081 case STATE_MATERIAL_PARSER:
1082 state_tokens[0] = STATE_MATERIAL;
1083 state_tokens[1] = parse_face_type (inst);
1084 switch (*(*inst)++) {
1085 case MATERIAL_AMBIENT:
1086 state_tokens[2] = STATE_AMBIENT;
1087 break;
1088 case MATERIAL_DIFFUSE:
1089 state_tokens[2] = STATE_DIFFUSE;
1090 break;
1091 case MATERIAL_SPECULAR:
1092 state_tokens[2] = STATE_SPECULAR;
1093 break;
1094 case MATERIAL_EMISSION:
1095 state_tokens[2] = STATE_EMISSION;
1096 break;
1097 case MATERIAL_SHININESS:
1098 state_tokens[2] = STATE_SHININESS;
1099 break;
1100 }
1101 break;
1102
1103 case STATE_LIGHT_PARSER:
1104 state_tokens[0] = STATE_LIGHT;
1105 state_tokens[1] = parse_integer (inst, Program);
1106
1107 /* Check the value of state_tokens[1] against the # of lights */
1108 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1109 _mesa_set_program_error (ctx, Program->Position,
1110 "Invalid Light Number");
1111 _mesa_error (ctx, GL_INVALID_OPERATION,
1112 "Invalid Light Number: %d", state_tokens[1]);
1113 return 1;
1114 }
1115
1116 switch (*(*inst)++) {
1117 case LIGHT_AMBIENT:
1118 state_tokens[2] = STATE_AMBIENT;
1119 break;
1120 case LIGHT_DIFFUSE:
1121 state_tokens[2] = STATE_DIFFUSE;
1122 break;
1123 case LIGHT_SPECULAR:
1124 state_tokens[2] = STATE_SPECULAR;
1125 break;
1126 case LIGHT_POSITION:
1127 state_tokens[2] = STATE_POSITION;
1128 break;
1129 case LIGHT_ATTENUATION:
1130 state_tokens[2] = STATE_ATTENUATION;
1131 break;
1132 case LIGHT_HALF:
1133 state_tokens[2] = STATE_HALF;
1134 break;
1135 case LIGHT_SPOT_DIRECTION:
1136 state_tokens[2] = STATE_SPOT_DIRECTION;
1137 break;
1138 }
1139 break;
1140
1141 case STATE_LIGHT_MODEL:
1142 switch (*(*inst)++) {
1143 case LIGHT_MODEL_AMBIENT:
1144 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1145 break;
1146 case LIGHT_MODEL_SCENECOLOR:
1147 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1148 state_tokens[1] = parse_face_type (inst);
1149 break;
1150 }
1151 break;
1152
1153 case STATE_LIGHT_PROD:
1154 state_tokens[0] = STATE_LIGHTPROD;
1155 state_tokens[1] = parse_integer (inst, Program);
1156
1157 /* Check the value of state_tokens[1] against the # of lights */
1158 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1159 _mesa_set_program_error (ctx, Program->Position,
1160 "Invalid Light Number");
1161 _mesa_error (ctx, GL_INVALID_OPERATION,
1162 "Invalid Light Number: %d", state_tokens[1]);
1163 return 1;
1164 }
1165
1166 state_tokens[2] = parse_face_type (inst);
1167 switch (*(*inst)++) {
1168 case LIGHT_PROD_AMBIENT:
1169 state_tokens[3] = STATE_AMBIENT;
1170 break;
1171 case LIGHT_PROD_DIFFUSE:
1172 state_tokens[3] = STATE_DIFFUSE;
1173 break;
1174 case LIGHT_PROD_SPECULAR:
1175 state_tokens[3] = STATE_SPECULAR;
1176 break;
1177 }
1178 break;
1179
1180
1181 case STATE_FOG:
1182 switch (*(*inst)++) {
1183 case FOG_COLOR:
1184 state_tokens[0] = STATE_FOG_COLOR;
1185 break;
1186 case FOG_PARAMS:
1187 state_tokens[0] = STATE_FOG_PARAMS;
1188 break;
1189 }
1190 break;
1191
1192 case STATE_TEX_ENV:
1193 state_tokens[1] = parse_integer (inst, Program);
1194 switch (*(*inst)++) {
1195 case TEX_ENV_COLOR:
1196 state_tokens[0] = STATE_TEXENV_COLOR;
1197 break;
1198 }
1199 break;
1200
1201 case STATE_TEX_GEN:
1202 {
1203 GLuint type, coord;
1204
1205 state_tokens[0] = STATE_TEXGEN;
1206 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1207
1208 if (parse_texcoord_num (ctx, inst, Program, &coord))
1209 return 1;
1210 state_tokens[1] = coord;
1211
1212 /* EYE or OBJECT */
1213 type = *(*inst++);
1214
1215 /* 0 - s, 1 - t, 2 - r, 3 - q */
1216 coord = *(*inst++);
1217
1218 if (type == TEX_GEN_EYE) {
1219 switch (coord) {
1220 case COMPONENT_X:
1221 state_tokens[2] = STATE_TEXGEN_EYE_S;
1222 break;
1223 case COMPONENT_Y:
1224 state_tokens[2] = STATE_TEXGEN_EYE_T;
1225 break;
1226 case COMPONENT_Z:
1227 state_tokens[2] = STATE_TEXGEN_EYE_R;
1228 break;
1229 case COMPONENT_W:
1230 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1231 break;
1232 }
1233 }
1234 else {
1235 switch (coord) {
1236 case COMPONENT_X:
1237 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1238 break;
1239 case COMPONENT_Y:
1240 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1241 break;
1242 case COMPONENT_Z:
1243 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1244 break;
1245 case COMPONENT_W:
1246 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1247 break;
1248 }
1249 }
1250 }
1251 break;
1252
1253 case STATE_DEPTH:
1254 switch (*(*inst)++) {
1255 case DEPTH_RANGE:
1256 state_tokens[0] = STATE_DEPTH_RANGE;
1257 break;
1258 }
1259 break;
1260
1261 case STATE_CLIP_PLANE:
1262 state_tokens[0] = STATE_CLIPPLANE;
1263 state_tokens[1] = parse_integer (inst, Program);
1264 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1265 return 1;
1266 break;
1267
1268 case STATE_POINT:
1269 switch (*(*inst++)) {
1270 case POINT_SIZE:
1271 state_tokens[0] = STATE_POINT_SIZE;
1272 break;
1273
1274 case POINT_ATTENUATION:
1275 state_tokens[0] = STATE_POINT_ATTENUATION;
1276 break;
1277 }
1278 break;
1279
1280 /* XXX: I think this is the correct format for a matrix row */
1281 case STATE_MATRIX_ROWS:
1282 state_tokens[0] = STATE_MATRIX;
1283 if (parse_matrix
1284 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1285 &state_tokens[5]))
1286 return 1;
1287
1288 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1289
1290 if ((**inst) != 0) { /* Either the last row, 0 */
1291 state_tokens[4] = parse_integer (inst, Program);
1292 if (state_tokens[4] < state_tokens[3]) {
1293 _mesa_set_program_error (ctx, Program->Position,
1294 "Second matrix index less than the first");
1295 _mesa_error (ctx, GL_INVALID_OPERATION,
1296 "Second matrix index (%d) less than the first (%d)",
1297 state_tokens[4], state_tokens[3]);
1298 return 1;
1299 }
1300 }
1301 else {
1302 state_tokens[4] = state_tokens[3];
1303 (*inst)++;
1304 }
1305 break;
1306 }
1307
1308 return 0;
1309}
1310
1311/**
1312 * This parses a state string (rather, the binary version of it) into
1313 * a 6-token similar for the state fetching code in program.c
1314 *
1315 * One might ask, why fetch these parameters into just like you fetch
1316 * state when they are already stored in other places?
1317 *
1318 * Because of array offsets -> We can stick env/local parameters in the
1319 * middle of a parameter array and then index someplace into the array
1320 * when we execute.
1321 *
1322 * One optimization might be to only do this for the cases where the
1323 * env/local parameters end up inside of an array, and leave the
1324 * single parameters (or arrays of pure env/local pareameters) in their
1325 * respective register files.
1326 *
1327 * For ENV parameters, the format is:
1328 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1329 * state_tokens[1] = STATE_ENV
1330 * state_tokens[2] = the parameter index
1331 *
1332 * for LOCAL parameters, the format is:
1333 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1334 * state_tokens[1] = STATE_LOCAL
1335 * state_tokens[2] = the parameter index
1336 *
1337 * \param inst - the start in the binary arry to start working from
1338 * \param state_tokens - the storage for the 6-token state description
1339 * \return - 0 on sucess, 1 on failure
1340 */
1341static GLuint
1342parse_program_single_item (GLcontext * ctx, GLubyte ** inst,
1343 struct arb_program *Program, GLint * state_tokens)
1344{
1345 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1346 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1347 else
1348 state_tokens[0] = STATE_VERTEX_PROGRAM;
1349
1350
1351 switch (*(*inst)++) {
1352 case PROGRAM_PARAM_ENV:
1353 state_tokens[1] = STATE_ENV;
1354 state_tokens[2] = parse_integer (inst, Program);
1355
1356 /* Check state_tokens[2] against the number of ENV parameters available */
1357 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001358 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001359 ||
1360 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001361 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001362 _mesa_set_program_error (ctx, Program->Position,
1363 "Invalid Program Env Parameter");
1364 _mesa_error (ctx, GL_INVALID_OPERATION,
1365 "Invalid Program Env Parameter: %d",
1366 state_tokens[2]);
1367 return 1;
1368 }
1369
1370 break;
1371
1372 case PROGRAM_PARAM_LOCAL:
1373 state_tokens[1] = STATE_LOCAL;
1374 state_tokens[2] = parse_integer (inst, Program);
1375
1376 /* Check state_tokens[2] against the number of LOCAL parameters available */
1377 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001378 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001379 ||
1380 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001381 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001382 _mesa_set_program_error (ctx, Program->Position,
1383 "Invalid Program Local Parameter");
1384 _mesa_error (ctx, GL_INVALID_OPERATION,
1385 "Invalid Program Local Parameter: %d",
1386 state_tokens[2]);
1387 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.
Jouk Jansen40322e12004-04-05 08:50:36 +00001442 * \return returns 0 on sucess, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001443 */
1444static GLuint
Brian Paul18e7c5c2005-10-30 21:46:00 +00001445parse_attrib_binding(GLcontext * ctx, GLubyte ** inst,
1446 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 {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001488 const char *msg = "ARB_vertex_blend not supported";
Jouk Jansen40322e12004-04-05 08:50:36 +00001489 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001490 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001491 *inputReg = VERT_ATTRIB_WEIGHT;
1492 _mesa_set_program_error(ctx, Program->Position, msg);
1493 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001494 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001495 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001496
1497 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001498 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001499 break;
1500
1501 case VERTEX_ATTRIB_COLOR:
1502 {
1503 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001504 err = parse_color_type (ctx, inst, Program, &color);
1505 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001506 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001507 }
1508 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001509 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001510 }
1511 }
1512 break;
1513
1514 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001515 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001516 break;
1517
1518 case VERTEX_ATTRIB_TEXCOORD:
1519 {
1520 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001521 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001522 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001523 }
1524 break;
1525
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001527 /* Not supported at this time */
1528 {
1529 const char *msg = "ARB_palette_matrix not supported";
1530 parse_integer (inst, Program);
1531 _mesa_set_program_error (ctx, Program->Position, msg);
1532 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
1533 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001534 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001535
1536 case VERTEX_ATTRIB_GENERIC:
1537 {
1538 GLuint attrib;
Jouk Jansen40322e12004-04-05 08:50:36 +00001539 if (!parse_generic_attrib_num(ctx, inst, Program, &attrib)) {
1540 *is_generic = 1;
Brian Paul919f6a02006-05-29 14:37:56 +00001541 if (attrib >= MAX_VERTEX_PROGRAM_ATTRIBS) {
1542 const char *msg = "Invalid generic vertex attribute reference";
1543 _mesa_set_program_error (ctx, Program->Position, msg);
1544 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
1545 return 1;
1546 }
Brian Paul095c6692006-04-25 00:21:32 +00001547 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1548 * attributes do not alias the conventional vertex
1549 * attributes.
1550 */
1551 if (attrib > 0)
1552 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001553 else
1554 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001555 }
1556 }
1557 break;
1558
1559 default:
1560 err = 1;
1561 break;
1562 }
1563 }
1564
1565 /* Can this even happen? */
1566 if (err) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001567 const char *msg = "Bad attribute binding";
1568 _mesa_set_program_error(ctx, Program->Position, msg);
1569 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001570 }
1571
Brian Paulde997602005-11-12 17:53:14 +00001572 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001573
1574 return err;
1575}
1576
Brian Paul7aebaf32005-10-30 21:23:23 +00001577
Jouk Jansen40322e12004-04-05 08:50:36 +00001578/**
1579 * This translates between a binary token for an output variable type
1580 * and the mesa token for the same thing.
1581 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001582 * \param inst The parsed tokens
1583 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001584 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001585 */
1586static GLuint
Brian Paul7aebaf32005-10-30 21:23:23 +00001587parse_result_binding(GLcontext *ctx, GLubyte **inst,
1588 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001589{
Brian Paul7aebaf32005-10-30 21:23:23 +00001590 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001591
Brian Paul7aebaf32005-10-30 21:23:23 +00001592 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001593 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001594 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001595 GLuint out_color;
1596
Brian Paulbe76b7f2004-10-04 14:40:05 +00001597 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001598 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001599 */
1600 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001601 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001602 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001603 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001604 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001605 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1606 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 }
1608 break;
1609
1610 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001611 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001612 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001613 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001614 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001615 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001616 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 GLint color_type;
1618 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001619 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1620 if (err)
1621 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001622
Jouk Jansen40322e12004-04-05 08:50:36 +00001623 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001624 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001625 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001626 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001627 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001629 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001630 }
1631 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001632 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001633 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001634 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001635 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001636 }
1637 /* primary color */
1638 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001639 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001640 }
1641 }
1642 }
1643 break;
1644
1645 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001646 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001647 break;
1648
1649 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001650 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001651 break;
1652
1653 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001654 {
1655 GLuint unit;
1656 if (parse_texcoord_num (ctx, inst, Program, &unit))
1657 return 1;
1658 *outputReg = VERT_RESULT_TEX0 + unit;
1659 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001660 break;
1661 }
1662
Brian Paulde997602005-11-12 17:53:14 +00001663 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001664
1665 return 0;
1666}
1667
Brian Paul7aebaf32005-10-30 21:23:23 +00001668
Jouk Jansen40322e12004-04-05 08:50:36 +00001669/**
1670 * This handles the declaration of ATTRIB variables
1671 *
1672 * XXX: Still needs
1673 * parse_vert_attrib_binding(), or something like that
1674 *
1675 * \return 0 on sucess, 1 on error
1676 */
1677static GLint
1678parse_attrib (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1679 struct arb_program *Program)
1680{
1681 GLuint found;
1682 char *error_msg;
1683 struct var_cache *attrib_var;
1684
1685 attrib_var = parse_string (inst, vc_head, Program, &found);
1686 Program->Position = parse_position (inst);
1687 if (found) {
1688 error_msg = (char *)
1689 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1690 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1691 attrib_var->name);
1692
1693 _mesa_set_program_error (ctx, Program->Position, error_msg);
1694 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1695
1696 _mesa_free (error_msg);
1697 return 1;
1698 }
1699
1700 attrib_var->type = vt_attrib;
1701
Brian Paul7aebaf32005-10-30 21:23:23 +00001702 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001703 &attrib_var->attrib_is_generic))
1704 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001705
Brian Paul7aebaf32005-10-30 21:23:23 +00001706 if (generic_attrib_check(*vc_head)) {
1707 _mesa_set_program_error(ctx, Program->Position,
1708 "Cannot use both a generic vertex attribute "
1709 "and a specific attribute of the same type");
1710 _mesa_error(ctx, GL_INVALID_OPERATION,
1711 "Cannot use both a generic vertex attribute and a specific "
1712 "attribute of the same type");
1713 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001714 }
1715
1716 Program->Base.NumAttributes++;
1717 return 0;
1718}
1719
1720/**
1721 * \param use -- TRUE if we're called when declaring implicit parameters,
1722 * FALSE if we're declaraing variables. This has to do with
1723 * if we get a signed or unsigned float for scalar constants
1724 */
1725static GLuint
1726parse_param_elements (GLcontext * ctx, GLubyte ** inst,
1727 struct var_cache *param_var,
1728 struct arb_program *Program, GLboolean use)
1729{
1730 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001731 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001732 GLint state_tokens[6];
1733 GLfloat const_values[4];
1734
Jouk Jansen40322e12004-04-05 08:50:36 +00001735 switch (*(*inst)++) {
1736 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001737 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1738 return 1;
1739
1740 /* If we adding STATE_MATRIX that has multiple rows, we need to
1741 * unroll it and call _mesa_add_state_reference() for each row
1742 */
1743 if ((state_tokens[0] == STATE_MATRIX)
1744 && (state_tokens[3] != state_tokens[4])) {
1745 GLint row;
1746 GLint first_row = state_tokens[3];
1747 GLint last_row = state_tokens[4];
1748
1749 for (row = first_row; row <= last_row; row++) {
1750 state_tokens[3] = state_tokens[4] = row;
1751
Brian Paulde997602005-11-12 17:53:14 +00001752 idx = _mesa_add_state_reference(Program->Base.Parameters,
1753 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001754 if (param_var->param_binding_begin == ~0U)
1755 param_var->param_binding_begin = idx;
1756 param_var->param_binding_length++;
1757 Program->Base.NumParameters++;
1758 }
1759 }
1760 else {
Brian Paulde997602005-11-12 17:53:14 +00001761 idx = _mesa_add_state_reference(Program->Base.Parameters,
1762 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001763 if (param_var->param_binding_begin == ~0U)
1764 param_var->param_binding_begin = idx;
1765 param_var->param_binding_length++;
1766 Program->Base.NumParameters++;
1767 }
1768 break;
1769
1770 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001771 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1772 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001773 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001774 if (param_var->param_binding_begin == ~0U)
1775 param_var->param_binding_begin = idx;
1776 param_var->param_binding_length++;
1777 Program->Base.NumParameters++;
1778
1779 /* Check if there is more: 0 -> we're done, else its an integer */
1780 if (**inst) {
1781 GLuint out_of_range, new_idx;
1782 GLuint start_idx = state_tokens[2] + 1;
1783 GLuint end_idx = parse_integer (inst, Program);
1784
1785 out_of_range = 0;
1786 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1787 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001788 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001789 || ((state_tokens[1] == STATE_LOCAL)
1790 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001791 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001792 out_of_range = 1;
1793 }
1794 else {
1795 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001796 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001797 || ((state_tokens[1] == STATE_LOCAL)
1798 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001799 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001800 out_of_range = 1;
1801 }
1802 if (out_of_range) {
1803 _mesa_set_program_error (ctx, Program->Position,
1804 "Invalid Program Parameter");
1805 _mesa_error (ctx, GL_INVALID_OPERATION,
1806 "Invalid Program Parameter: %d", end_idx);
1807 return 1;
1808 }
1809
1810 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1811 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001812 idx = _mesa_add_state_reference(Program->Base.Parameters,
1813 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001814 param_var->param_binding_length++;
1815 Program->Base.NumParameters++;
1816 }
1817 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001818 else {
1819 (*inst)++;
1820 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001821 break;
1822
1823 case PARAM_CONSTANT:
1824 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001825 idx = _mesa_add_named_constant(Program->Base.Parameters,
1826 (char *) param_var->name,
1827 const_values);
Jouk Jansen40322e12004-04-05 08:50:36 +00001828 if (param_var->param_binding_begin == ~0U)
1829 param_var->param_binding_begin = idx;
1830 param_var->param_binding_length++;
1831 Program->Base.NumParameters++;
1832 break;
1833
1834 default:
Brian Paul7aebaf32005-10-30 21:23:23 +00001835 _mesa_set_program_error(ctx, Program->Position,
1836 "Unexpected token in parse_param_elements()");
1837 _mesa_error(ctx, GL_INVALID_OPERATION,
1838 "Unexpected token in parse_param_elements()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001839 return 1;
1840 }
1841
1842 /* Make sure we haven't blown past our parameter limits */
1843 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1844 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001845 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001846 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1847 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001848 ctx->Const.FragmentProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001849 _mesa_set_program_error (ctx, Program->Position,
1850 "Too many parameter variables");
1851 _mesa_error (ctx, GL_INVALID_OPERATION, "Too many parameter variables");
1852 return 1;
1853 }
1854
1855 return err;
1856}
1857
Brian Paul7aebaf32005-10-30 21:23:23 +00001858
Jouk Jansen40322e12004-04-05 08:50:36 +00001859/**
1860 * This picks out PARAM program parameter bindings.
1861 *
1862 * XXX: This needs to be stressed & tested
1863 *
1864 * \return 0 on sucess, 1 on error
1865 */
1866static GLuint
1867parse_param (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1868 struct arb_program *Program)
1869{
Brian Paulbd997cd2004-07-20 21:12:56 +00001870 GLuint found, err;
1871 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001872 struct var_cache *param_var;
1873
1874 err = 0;
1875 param_var = parse_string (inst, vc_head, Program, &found);
1876 Program->Position = parse_position (inst);
1877
1878 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001879 char *error_msg = (char *)
1880 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001881 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1882 param_var->name);
1883
1884 _mesa_set_program_error (ctx, Program->Position, error_msg);
1885 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1886
1887 _mesa_free (error_msg);
1888 return 1;
1889 }
1890
1891 specified_length = parse_integer (inst, Program);
1892
1893 if (specified_length < 0) {
1894 _mesa_set_program_error (ctx, Program->Position,
1895 "Negative parameter array length");
1896 _mesa_error (ctx, GL_INVALID_OPERATION,
1897 "Negative parameter array length: %d", specified_length);
1898 return 1;
1899 }
1900
1901 param_var->type = vt_param;
1902 param_var->param_binding_length = 0;
1903
1904 /* Right now, everything is shoved into the main state register file.
1905 *
1906 * In the future, it would be nice to leave things ENV/LOCAL params
1907 * in their respective register files, if possible
1908 */
1909 param_var->param_binding_type = PROGRAM_STATE_VAR;
1910
1911 /* Remember to:
1912 * * - add each guy to the parameter list
1913 * * - increment the param_var->param_binding_len
1914 * * - store the param_var->param_binding_begin for the first one
1915 * * - compare the actual len to the specified len at the end
1916 */
1917 while (**inst != PARAM_NULL) {
1918 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1919 return 1;
1920 }
1921
1922 /* Test array length here! */
1923 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001924 if (specified_length != (int)param_var->param_binding_length) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001925 const char *msg
1926 = "Declared parameter array length does not match parameter list";
1927 _mesa_set_program_error(ctx, Program->Position, msg);
1928 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001929 }
1930 }
1931
1932 (*inst)++;
1933
1934 return 0;
1935}
1936
1937/**
1938 *
1939 */
1940static GLuint
1941parse_param_use (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1942 struct arb_program *Program, struct var_cache **new_var)
1943{
1944 struct var_cache *param_var;
1945
1946 /* First, insert a dummy entry into the var_cache */
1947 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001948 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001949 param_var->type = vt_param;
1950
1951 param_var->param_binding_length = 0;
1952 /* Don't fill in binding_begin; We use the default value of -1
1953 * to tell if its already initialized, elsewhere.
1954 *
1955 * param_var->param_binding_begin = 0;
1956 */
1957 param_var->param_binding_type = PROGRAM_STATE_VAR;
1958
1959 var_cache_append (vc_head, param_var);
1960
1961 /* Then fill it with juicy parameter goodness */
1962 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1963 return 1;
1964
1965 *new_var = param_var;
1966
1967 return 0;
1968}
1969
1970
1971/**
1972 * This handles the declaration of TEMP variables
1973 *
1974 * \return 0 on sucess, 1 on error
1975 */
1976static GLuint
1977parse_temp (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1978 struct arb_program *Program)
1979{
1980 GLuint found;
1981 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001982
1983 while (**inst != 0) {
1984 temp_var = parse_string (inst, vc_head, Program, &found);
1985 Program->Position = parse_position (inst);
1986 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001987 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001988 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1989 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1990 temp_var->name);
1991
1992 _mesa_set_program_error (ctx, Program->Position, error_msg);
1993 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1994
1995 _mesa_free (error_msg);
1996 return 1;
1997 }
1998
1999 temp_var->type = vt_temp;
2000
2001 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2002 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002003 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002004 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2005 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002006 ctx->Const.VertexProgram.MaxTemps))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002007 _mesa_set_program_error (ctx, Program->Position,
2008 "Too many TEMP variables declared");
2009 _mesa_error (ctx, GL_INVALID_OPERATION,
2010 "Too many TEMP variables declared");
2011 return 1;
2012 }
2013
2014 temp_var->temp_binding = Program->Base.NumTemporaries;
2015 Program->Base.NumTemporaries++;
2016 }
2017 (*inst)++;
2018
2019 return 0;
2020}
2021
2022/**
2023 * This handles variables of the OUTPUT variety
2024 *
2025 * \return 0 on sucess, 1 on error
2026 */
2027static GLuint
2028parse_output (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2029 struct arb_program *Program)
2030{
2031 GLuint found;
2032 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002033 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002034
2035 output_var = parse_string (inst, vc_head, Program, &found);
2036 Program->Position = parse_position (inst);
2037 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002038 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002039 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2040 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2041 output_var->name);
2042
2043 _mesa_set_program_error (ctx, Program->Position, error_msg);
2044 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2045
2046 _mesa_free (error_msg);
2047 return 1;
2048 }
2049
2050 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002051
2052 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2053 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002054}
2055
2056/**
2057 * This handles variables of the ALIAS kind
2058 *
2059 * \return 0 on sucess, 1 on error
2060 */
2061static GLuint
2062parse_alias (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2063 struct arb_program *Program)
2064{
2065 GLuint found;
2066 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002067
2068 temp_var = parse_string (inst, vc_head, Program, &found);
2069 Program->Position = parse_position (inst);
2070
2071 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002072 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002073 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2074 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2075 temp_var->name);
2076
2077 _mesa_set_program_error (ctx, Program->Position, error_msg);
2078 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2079
2080 _mesa_free (error_msg);
2081 return 1;
2082 }
2083
2084 temp_var->type = vt_alias;
2085 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2086 Program->Position = parse_position (inst);
2087
2088 if (!found)
2089 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002090 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002091 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2092 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2093 temp_var->alias_binding->name);
2094
2095 _mesa_set_program_error (ctx, Program->Position, error_msg);
2096 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2097
2098 _mesa_free (error_msg);
2099 return 1;
2100 }
2101
2102 return 0;
2103}
2104
2105/**
2106 * This handles variables of the ADDRESS kind
2107 *
2108 * \return 0 on sucess, 1 on error
2109 */
2110static GLuint
2111parse_address (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2112 struct arb_program *Program)
2113{
2114 GLuint found;
2115 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002116
2117 while (**inst != 0) {
2118 temp_var = parse_string (inst, vc_head, Program, &found);
2119 Program->Position = parse_position (inst);
2120 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002121 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002122 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2123 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2124 temp_var->name);
2125
2126 _mesa_set_program_error (ctx, Program->Position, error_msg);
2127 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2128
2129 _mesa_free (error_msg);
2130 return 1;
2131 }
2132
2133 temp_var->type = vt_address;
2134
2135 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002136 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002137 const char *msg = "Too many ADDRESS variables declared";
2138 _mesa_set_program_error(ctx, Program->Position, msg);
2139
2140 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002141 return 1;
2142 }
2143
2144 temp_var->address_binding = Program->Base.NumAddressRegs;
2145 Program->Base.NumAddressRegs++;
2146 }
2147 (*inst)++;
2148
2149 return 0;
2150}
2151
2152/**
2153 * Parse a program declaration
2154 *
2155 * \return 0 on sucess, 1 on error
2156 */
2157static GLint
2158parse_declaration (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2159 struct arb_program *Program)
2160{
2161 GLint err = 0;
2162
2163 switch (*(*inst)++) {
2164 case ADDRESS:
2165 err = parse_address (ctx, inst, vc_head, Program);
2166 break;
2167
2168 case ALIAS:
2169 err = parse_alias (ctx, inst, vc_head, Program);
2170 break;
2171
2172 case ATTRIB:
2173 err = parse_attrib (ctx, inst, vc_head, Program);
2174 break;
2175
2176 case OUTPUT:
2177 err = parse_output (ctx, inst, vc_head, Program);
2178 break;
2179
2180 case PARAM:
2181 err = parse_param (ctx, inst, vc_head, Program);
2182 break;
2183
2184 case TEMP:
2185 err = parse_temp (ctx, inst, vc_head, Program);
2186 break;
2187 }
2188
2189 return err;
2190}
2191
2192/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002193 * Handle the parsing out of a masked destination register, either for a
2194 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002195 *
2196 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002197 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002198 * position invariant
2199 *
2200 * \param File - The register file we write to
2201 * \param Index - The register index we write to
2202 * \param WriteMask - The mask controlling which components we write (1->write)
2203 *
2204 * \return 0 on sucess, 1 on error
2205 */
2206static GLuint
2207parse_masked_dst_reg (GLcontext * ctx, GLubyte ** inst,
2208 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002209 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002210{
Brian Paul7aebaf32005-10-30 21:23:23 +00002211 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002212 struct var_cache *dst;
2213
2214 /* We either have a result register specified, or a
2215 * variable that may or may not be writable
2216 */
2217 switch (*(*inst)++) {
2218 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002219 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002220 return 1;
2221 *File = PROGRAM_OUTPUT;
2222 break;
2223
2224 case REGISTER_ESTABLISHED_NAME:
2225 dst = parse_string (inst, vc_head, Program, &result);
2226 Program->Position = parse_position (inst);
2227
2228 /* If the name has never been added to our symbol table, we're hosed */
2229 if (!result) {
2230 _mesa_set_program_error (ctx, Program->Position,
2231 "0: Undefined variable");
2232 _mesa_error (ctx, GL_INVALID_OPERATION, "0: Undefined variable: %s",
2233 dst->name);
2234 return 1;
2235 }
2236
2237 switch (dst->type) {
2238 case vt_output:
2239 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002240 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002241 break;
2242
2243 case vt_temp:
2244 *File = PROGRAM_TEMPORARY;
2245 *Index = dst->temp_binding;
2246 break;
2247
2248 /* If the var type is not vt_output or vt_temp, no go */
2249 default:
2250 _mesa_set_program_error (ctx, Program->Position,
2251 "Destination register is read only");
2252 _mesa_error (ctx, GL_INVALID_OPERATION,
2253 "Destination register is read only: %s",
2254 dst->name);
2255 return 1;
2256 }
2257 break;
2258
2259 default:
2260 _mesa_set_program_error (ctx, Program->Position,
2261 "Unexpected opcode in parse_masked_dst_reg()");
2262 _mesa_error (ctx, GL_INVALID_OPERATION,
2263 "Unexpected opcode in parse_masked_dst_reg()");
2264 return 1;
2265 }
2266
2267
2268 /* Position invariance test */
2269 /* This test is done now in syntax portion - when position invariance OPTION
2270 is specified, "result.position" rule is disabled so there is no way
2271 to write the position
2272 */
2273 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2274 (*Index == 0)) {
2275 _mesa_set_program_error (ctx, Program->Position,
2276 "Vertex program specified position invariance and wrote vertex position");
2277 _mesa_error (ctx, GL_INVALID_OPERATION,
2278 "Vertex program specified position invariance and wrote vertex position");
2279 }*/
2280
2281 /* And then the mask.
2282 * w,a -> bit 0
2283 * z,b -> bit 1
2284 * y,g -> bit 2
2285 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002286 *
2287 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002288 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002289 tmp = (GLint) *(*inst)++;
2290 *WriteMask = (((tmp>>3) & 0x1) |
2291 ((tmp>>1) & 0x2) |
2292 ((tmp<<1) & 0x4) |
2293 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002294
2295 return 0;
2296}
2297
2298
2299/**
2300 * Handle the parsing of a address register
2301 *
2302 * \param Index - The register index we write to
2303 *
2304 * \return 0 on sucess, 1 on error
2305 */
2306static GLuint
2307parse_address_reg (GLcontext * ctx, GLubyte ** inst,
2308 struct var_cache **vc_head,
2309 struct arb_program *Program, GLint * Index)
2310{
2311 struct var_cache *dst;
2312 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002313
2314 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002315
2316 dst = parse_string (inst, vc_head, Program, &result);
2317 Program->Position = parse_position (inst);
2318
2319 /* If the name has never been added to our symbol table, we're hosed */
2320 if (!result) {
2321 _mesa_set_program_error (ctx, Program->Position, "Undefined variable");
2322 _mesa_error (ctx, GL_INVALID_OPERATION, "Undefined variable: %s",
2323 dst->name);
2324 return 1;
2325 }
2326
2327 if (dst->type != vt_address) {
2328 _mesa_set_program_error (ctx, Program->Position,
2329 "Variable is not of type ADDRESS");
2330 _mesa_error (ctx, GL_INVALID_OPERATION,
2331 "Variable: %s is not of type ADDRESS", dst->name);
2332 return 1;
2333 }
2334
2335 return 0;
2336}
2337
Brian Paul8e8fa632005-07-01 02:03:33 +00002338#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002339/**
2340 * Handle the parsing out of a masked address register
2341 *
2342 * \param Index - The register index we write to
2343 * \param WriteMask - The mask controlling which components we write (1->write)
2344 *
2345 * \return 0 on sucess, 1 on error
2346 */
2347static GLuint
2348parse_masked_address_reg (GLcontext * ctx, GLubyte ** inst,
2349 struct var_cache **vc_head,
2350 struct arb_program *Program, GLint * Index,
2351 GLboolean * WriteMask)
2352{
2353 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2354 return 1;
2355
2356 /* This should be 0x8 */
2357 (*inst)++;
2358
2359 /* Writemask of .x is implied */
2360 WriteMask[0] = 1;
2361 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2362
2363 return 0;
2364}
Brian Paul8e8fa632005-07-01 02:03:33 +00002365#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002366
2367/**
2368 * Parse out a swizzle mask.
2369 *
Brian Paul32df89e2005-10-29 18:26:43 +00002370 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002371 *
2372 * The len parameter allows us to grab 4 components for a vector
2373 * swizzle, or just 1 component for a scalar src register selection
2374 */
Brian Paul32df89e2005-10-29 18:26:43 +00002375static void
2376parse_swizzle_mask(GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002377{
Brian Paul32df89e2005-10-29 18:26:43 +00002378 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002379
Brian Paul32df89e2005-10-29 18:26:43 +00002380 for (i = 0; i < 4; i++)
2381 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002382
Brian Paul32df89e2005-10-29 18:26:43 +00002383 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002384 switch (*(*inst)++) {
2385 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002386 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002388 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002389 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002390 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002391 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002392 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002393 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002394 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002395 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002396 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002397 default:
2398 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2399 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002400 }
2401 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002402}
2403
Jouk Jansen40322e12004-04-05 08:50:36 +00002404
Brian Paul32df89e2005-10-29 18:26:43 +00002405/**
2406 * Parse an extended swizzle mask which is a sequence of
2407 * four x/y/z/w/0/1 tokens.
2408 * \return swizzle four swizzle values
2409 * \return negateMask four element bitfield
2410 */
2411static void
2412parse_extended_swizzle_mask(GLubyte **inst, GLubyte swizzle[4],
2413 GLubyte *negateMask)
2414{
2415 GLint i;
2416
2417 *negateMask = 0x0;
2418 for (i = 0; i < 4; i++) {
2419 GLubyte swz;
2420 if (parse_sign(inst) == -1)
2421 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002422
2423 swz = *(*inst)++;
2424
2425 switch (swz) {
2426 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002427 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002428 break;
2429 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002430 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002431 break;
2432 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002433 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002434 break;
2435 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002436 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002437 break;
2438 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002439 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002440 break;
2441 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002442 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002443 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002444 default:
2445 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2446 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002447 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002448 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002449}
2450
2451
2452static GLuint
2453parse_src_reg (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002454 struct arb_program *Program,
2455 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002456 GLboolean *IsRelOffset )
2457{
2458 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002459 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002460 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002461
Keith Whitwell7c26b612005-04-21 14:46:57 +00002462 *IsRelOffset = 0;
2463
Jouk Jansen40322e12004-04-05 08:50:36 +00002464 /* And the binding for the src */
2465 switch (*(*inst)++) {
2466 case REGISTER_ATTRIB:
2467 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002468 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002469 return 1;
2470 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002471 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002472
2473 /* We need to insert a dummy variable into the var_cache so we can
2474 * catch generic vertex attrib aliasing errors
2475 */
2476 var_cache_create(&src);
2477 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002478 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002479 src->attrib_binding = binding;
2480 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002481 var_cache_append(vc_head, src);
2482 if (generic_attrib_check(*vc_head)) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002483 const char *msg = "Cannot use both a generic vertex attribute "
2484 "and a specific attribute of the same type";
2485 _mesa_set_program_error (ctx, Program->Position, msg);
2486 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002487 return 1;
2488 }
2489 break;
2490
2491 case REGISTER_PARAM:
2492 switch (**inst) {
2493 case PARAM_ARRAY_ELEMENT:
2494 (*inst)++;
2495 src = parse_string (inst, vc_head, Program, &found);
2496 Program->Position = parse_position (inst);
2497
2498 if (!found) {
2499 _mesa_set_program_error (ctx, Program->Position,
2500 "2: Undefined variable");
2501 _mesa_error (ctx, GL_INVALID_OPERATION,
2502 "2: Undefined variable: %s", src->name);
2503 return 1;
2504 }
2505
Brian Paul95801792005-12-06 15:41:43 +00002506 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002507
2508 switch (*(*inst)++) {
2509 case ARRAY_INDEX_ABSOLUTE:
2510 offset = parse_integer (inst, Program);
2511
2512 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002513 || (offset >= (int)src->param_binding_length)) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002514 _mesa_set_program_error (ctx, Program->Position,
2515 "Index out of range");
2516 _mesa_error (ctx, GL_INVALID_OPERATION,
2517 "Index %d out of range for %s", offset,
2518 src->name);
2519 return 1;
2520 }
2521
2522 *Index = src->param_binding_begin + offset;
2523 break;
2524
2525 case ARRAY_INDEX_RELATIVE:
2526 {
2527 GLint addr_reg_idx, rel_off;
2528
2529 /* First, grab the address regiseter */
2530 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2531 return 1;
2532
2533 /* And the .x */
2534 ((*inst)++);
2535 ((*inst)++);
2536 ((*inst)++);
2537 ((*inst)++);
2538
2539 /* Then the relative offset */
2540 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2541
2542 /* And store it properly */
2543 *Index = src->param_binding_begin + rel_off;
2544 *IsRelOffset = 1;
2545 }
2546 break;
2547 }
2548 break;
2549
2550 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002551 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2552 return 1;
2553
Brian Paul95801792005-12-06 15:41:43 +00002554 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002555 *Index = src->param_binding_begin;
2556 break;
2557 }
2558 break;
2559
2560 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002561 src = parse_string (inst, vc_head, Program, &found);
2562 Program->Position = parse_position (inst);
2563
2564 /* If the name has never been added to our symbol table, we're hosed */
2565 if (!found) {
2566 _mesa_set_program_error (ctx, Program->Position,
2567 "3: Undefined variable");
2568 _mesa_error (ctx, GL_INVALID_OPERATION, "3: Undefined variable: %s",
2569 src->name);
2570 return 1;
2571 }
2572
2573 switch (src->type) {
2574 case vt_attrib:
2575 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002576 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002577 break;
2578
2579 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2580 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002581 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002582 *Index = src->param_binding_begin;
2583 break;
2584
2585 case vt_temp:
2586 *File = PROGRAM_TEMPORARY;
2587 *Index = src->temp_binding;
2588 break;
2589
2590 /* If the var type is vt_output no go */
2591 default:
2592 _mesa_set_program_error (ctx, Program->Position,
2593 "destination register is read only");
2594 _mesa_error (ctx, GL_INVALID_OPERATION,
2595 "destination register is read only: %s",
2596 src->name);
2597 return 1;
2598 }
2599 break;
2600
2601 default:
2602 _mesa_set_program_error (ctx, Program->Position,
2603 "Unknown token in parse_src_reg");
2604 _mesa_error (ctx, GL_INVALID_OPERATION,
2605 "Unknown token in parse_src_reg");
2606 return 1;
2607 }
2608
2609 return 0;
2610}
2611
2612/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002613 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002614 */
2615static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00002616parse_fp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
2617 struct var_cache **vc_head,
2618 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002619 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002620{
Brian Paul7aebaf32005-10-30 21:23:23 +00002621 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002622 GLint index;
2623 GLboolean negate;
2624 GLubyte swizzle[4];
2625 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002626
Jouk Jansen40322e12004-04-05 08:50:36 +00002627 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002628 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002629
2630 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002631 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002632 return 1;
2633
2634 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002635 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002636
Brian Paul32df89e2005-10-29 18:26:43 +00002637 reg->File = file;
2638 reg->Index = index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002639 reg->Abs = 0; /* NV only */
2640 reg->NegateAbs = 0; /* NV only */
Brian Paul32df89e2005-10-29 18:26:43 +00002641 reg->NegateBase = negate;
2642 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002643 return 0;
2644}
2645
Jouk Jansen40322e12004-04-05 08:50:36 +00002646
Keith Whitwell7c26b612005-04-21 14:46:57 +00002647static GLuint
2648parse_fp_dst_reg(GLcontext * ctx, GLubyte ** inst,
2649 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002650 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002651{
Brian Paul7aebaf32005-10-30 21:23:23 +00002652 GLint mask;
2653 GLuint idx;
2654 enum register_file file;
2655
Keith Whitwell7c26b612005-04-21 14:46:57 +00002656 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002657 return 1;
2658
Keith Whitwell7c26b612005-04-21 14:46:57 +00002659 reg->CondMask = 0; /* NV only */
2660 reg->CondSwizzle = 0; /* NV only */
2661 reg->File = file;
2662 reg->Index = idx;
2663 reg->WriteMask = mask;
2664 return 0;
2665}
2666
2667
Brian Paul7aebaf32005-10-30 21:23:23 +00002668/**
2669 * Parse fragment program scalar src register.
2670 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002671static GLuint
2672parse_fp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002673 struct var_cache **vc_head,
2674 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002675 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002676{
Brian Paul7aebaf32005-10-30 21:23:23 +00002677 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002678 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002679 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002680 GLubyte Swizzle[4];
2681 GLboolean IsRelOffset;
2682
2683 /* Grab the sign */
2684 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2685
2686 /* And the src reg */
2687 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2688 return 1;
2689
2690 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002691 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002692
Keith Whitwell7c26b612005-04-21 14:46:57 +00002693 reg->File = File;
2694 reg->Index = Index;
2695 reg->Abs = 0; /* NV only */
2696 reg->NegateAbs = 0; /* NV only */
2697 reg->NegateBase = Negate;
2698 reg->Swizzle = (Swizzle[0] << 0);
2699
Jouk Jansen40322e12004-04-05 08:50:36 +00002700 return 0;
2701}
2702
Keith Whitwell7c26b612005-04-21 14:46:57 +00002703
Jouk Jansen40322e12004-04-05 08:50:36 +00002704/**
2705 * This is a big mother that handles getting opcodes into the instruction
2706 * and handling the src & dst registers for fragment program instructions
2707 */
2708static GLuint
2709parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2710 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002711 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002712{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002713 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002714 GLuint texcoord;
2715 GLubyte instClass, type, code;
2716 GLboolean rel;
2717
Brian Paul7e807512005-11-05 17:10:45 +00002718 _mesa_init_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002719
2720 /* Record the position in the program string for debugging */
2721 fp->StringPos = Program->Position;
2722
2723 /* OP_ALU_INST or OP_TEX_INST */
2724 instClass = *(*inst)++;
2725
2726 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2727 * OP_TEX_{SAMPLE, KIL}
2728 */
2729 type = *(*inst)++;
2730
2731 /* The actual opcode name */
2732 code = *(*inst)++;
2733
2734 /* Increment the correct count */
2735 switch (instClass) {
2736 case OP_ALU_INST:
2737 Program->NumAluInstructions++;
2738 break;
2739 case OP_TEX_INST:
2740 Program->NumTexInstructions++;
2741 break;
2742 }
2743
Jouk Jansen40322e12004-04-05 08:50:36 +00002744 switch (type) {
2745 case OP_ALU_VECTOR:
2746 switch (code) {
2747 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002748 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002749 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002750 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 break;
2752
2753 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002754 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002755 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002756 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002757 break;
2758
2759 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002760 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002761 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002762 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002763 break;
2764
2765 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002766 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002768 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002769 break;
2770
2771 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002772 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002773 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002774 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 break;
2776 }
2777
Keith Whitwell7c26b612005-04-21 14:46:57 +00002778 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002779 return 1;
2780
Keith Whitwell7c26b612005-04-21 14:46:57 +00002781 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 break;
2784
2785 case OP_ALU_SCALAR:
2786 switch (code) {
2787 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002788 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002789 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002790 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 break;
2792
2793 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002794 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002795 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002796 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 break;
2798
2799 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002800 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002801 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002802 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002803 break;
2804
2805 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002806 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002808 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002809 break;
2810
2811 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002812 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002814 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002815 break;
2816
2817 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002818 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002820 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002821 break;
2822
2823 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002824 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 case OP_SCS:
2826
Brian Paul7e807512005-11-05 17:10:45 +00002827 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002828 break;
2829 }
2830
Keith Whitwell7c26b612005-04-21 14:46:57 +00002831 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002832 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002833
2834 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002836 break;
2837
2838 case OP_ALU_BINSC:
2839 switch (code) {
2840 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002841 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002842 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002843 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 break;
2845 }
2846
Keith Whitwell7c26b612005-04-21 14:46:57 +00002847 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002848 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002849
Jouk Jansen40322e12004-04-05 08:50:36 +00002850 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002851 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002852 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 }
2854 break;
2855
2856
2857 case OP_ALU_BIN:
2858 switch (code) {
2859 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002860 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002861 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002862 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 break;
2864
2865 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002866 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002867 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002868 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002869 break;
2870
2871 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002872 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002873 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002874 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002875 break;
2876
2877 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002878 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002879 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002880 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002881 break;
2882
2883 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002884 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002886 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002887 break;
2888
2889 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002890 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002892 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 break;
2894
2895 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002896 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002898 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 break;
2900
2901 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002902 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002903 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002904 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002905 break;
2906
2907 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002908 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002909 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002910 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002911 break;
2912
2913 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002914 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002915 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002916 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002917 break;
2918
2919 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002920 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002921 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002922 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002923 break;
2924
2925 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002926 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002927 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002928 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002929 break;
2930 }
2931
Keith Whitwell7c26b612005-04-21 14:46:57 +00002932 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002933 return 1;
2934 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002935 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2936 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002937 }
2938 break;
2939
2940 case OP_ALU_TRI:
2941 switch (code) {
2942 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002943 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002944 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002945 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002946 break;
2947
2948 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002949 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002950 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002951 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002952 break;
2953
2954 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002955 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002956 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002957 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002958 break;
2959 }
2960
Keith Whitwell7c26b612005-04-21 14:46:57 +00002961 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002962 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002963
Jouk Jansen40322e12004-04-05 08:50:36 +00002964 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002965 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2966 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002967 }
2968 break;
2969
2970 case OP_ALU_SWZ:
2971 switch (code) {
2972 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002973 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002974 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002975 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002976 break;
2977 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002978 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002979 return 1;
2980
Keith Whitwell7c26b612005-04-21 14:46:57 +00002981 {
Brian Paul32df89e2005-10-29 18:26:43 +00002982 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002983 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002984 enum register_file file;
2985 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002986
Brian Paul32df89e2005-10-29 18:26:43 +00002987 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002988 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002989 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2990 fp->SrcReg[0].File = file;
2991 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002992 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002993 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2994 swizzle[1],
2995 swizzle[2],
2996 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002997 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002998 break;
2999
3000 case OP_TEX_SAMPLE:
3001 switch (code) {
3002 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003003 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003004 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00003005 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003006 break;
3007
3008 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003009 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003010 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003011 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003012 break;
3013
3014 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003015 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003016 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003017 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003018 break;
3019 }
3020
Keith Whitwell7c26b612005-04-21 14:46:57 +00003021 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003022 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003023
3024 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003025 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003026
3027 /* texImageUnit */
3028 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3029 return 1;
3030 fp->TexSrcUnit = texcoord;
3031
3032 /* texTarget */
3033 switch (*(*inst)++) {
3034 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003035 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003036 break;
3037 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003038 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003039 break;
3040 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003041 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003042 break;
3043 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003044 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003045 break;
3046 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003047 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003048 break;
3049 case TEXTARGET_SHADOW1D:
3050 case TEXTARGET_SHADOW2D:
3051 case TEXTARGET_SHADOWRECT:
3052 /* TODO ARB_fragment_program_shadow code */
3053 break;
3054 }
Brian Paul7e807512005-11-05 17:10:45 +00003055 Program->TexturesUsed[texcoord] |= (1<<fp->TexSrcTarget);
Jouk Jansen40322e12004-04-05 08:50:36 +00003056 break;
3057
3058 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003059 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003060 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003061 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003062 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003063 break;
3064 }
3065
3066 return 0;
3067}
3068
Keith Whitwell7c26b612005-04-21 14:46:57 +00003069static GLuint
3070parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3071 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003072 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003073{
Brian Paul7aebaf32005-10-30 21:23:23 +00003074 GLint mask;
3075 GLuint idx;
3076 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003077
3078 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3079 return 1;
3080
3081 reg->File = file;
3082 reg->Index = idx;
3083 reg->WriteMask = mask;
3084 return 0;
3085}
3086
3087/**
3088 * Handle the parsing out of a masked address register
3089 *
3090 * \param Index - The register index we write to
3091 * \param WriteMask - The mask controlling which components we write (1->write)
3092 *
3093 * \return 0 on sucess, 1 on error
3094 */
3095static GLuint
3096parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3097 struct var_cache **vc_head,
3098 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003099 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003100{
3101 GLint idx;
3102
3103 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3104 return 1;
3105
3106 /* This should be 0x8 */
3107 (*inst)++;
3108
3109 reg->File = PROGRAM_ADDRESS;
3110 reg->Index = idx;
3111
3112 /* Writemask of .x is implied */
3113 reg->WriteMask = 0x1;
3114 return 0;
3115}
3116
3117/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003118 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003119 */
3120static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003121parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3122 struct var_cache **vc_head,
3123 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003124 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003125{
Brian Paul7aebaf32005-10-30 21:23:23 +00003126 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003127 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003128 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003129 GLubyte swizzle[4];
3130 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003131
3132 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003133 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003134
3135 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003136 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003137 return 1;
3138
3139 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003140 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003141
Brian Paul32df89e2005-10-29 18:26:43 +00003142 reg->File = file;
3143 reg->Index = index;
3144 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3145 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003146 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003147 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003148 return 0;
3149}
3150
3151
3152static GLuint
3153parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003154 struct var_cache **vc_head,
3155 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003156 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003157{
Brian Paul7aebaf32005-10-30 21:23:23 +00003158 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003159 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003160 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003161 GLubyte Swizzle[4];
3162 GLboolean IsRelOffset;
3163
3164 /* Grab the sign */
3165 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3166
3167 /* And the src reg */
3168 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3169 return 1;
3170
3171 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003172 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003173
3174 reg->File = File;
3175 reg->Index = Index;
3176 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003177 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003178 reg->RelAddr = IsRelOffset;
3179 return 0;
3180}
3181
3182
Jouk Jansen40322e12004-04-05 08:50:36 +00003183/**
3184 * This is a big mother that handles getting opcodes into the instruction
3185 * and handling the src & dst registers for vertex program instructions
3186 */
3187static GLuint
3188parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3189 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003190 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003191{
3192 GLint a;
3193 GLubyte type, code;
3194
3195 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3196 type = *(*inst)++;
3197
3198 /* The actual opcode name */
3199 code = *(*inst)++;
3200
Brian Paul7e807512005-11-05 17:10:45 +00003201 _mesa_init_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003202 /* Record the position in the program string for debugging */
3203 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003204
3205 switch (type) {
3206 /* XXX: */
3207 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003208 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003209
3210 /* Remember to set SrcReg.RelAddr; */
3211
3212 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003213 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003214 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003215
Jouk Jansen40322e12004-04-05 08:50:36 +00003216 vp->DstReg.File = PROGRAM_ADDRESS;
3217
3218 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003219 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003220 return 1;
3221
3222 break;
3223
3224 case OP_ALU_VECTOR:
3225 switch (code) {
3226 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003227 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 break;
3229 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003230 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003231 break;
3232 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003233 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003234 break;
3235 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003236 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003237 break;
3238 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003239 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003240 break;
3241 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003242
3243 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003244 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003245
3246 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003247 return 1;
3248 break;
3249
3250 case OP_ALU_SCALAR:
3251 switch (code) {
3252 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003253 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 break;
3255 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003256 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003257 break;
3258 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003259 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003260 break;
3261 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003262 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003263 break;
3264 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003265 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003266 break;
3267 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003268 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003269 break;
3270 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003271 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003272 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003273
3274 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003275 return 1;
3276 break;
3277
3278 case OP_ALU_BINSC:
3279 switch (code) {
3280 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003281 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003282 break;
3283 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003284 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003285 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003286
Jouk Jansen40322e12004-04-05 08:50:36 +00003287 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003288 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003289 return 1;
3290 }
3291 break;
3292
3293 case OP_ALU_BIN:
3294 switch (code) {
3295 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003296 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 break;
3298 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003299 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 break;
3301 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003302 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003303 break;
3304 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003305 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003306 break;
3307 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003308 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 break;
3310 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003311 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003312 break;
3313 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003314 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003315 break;
3316 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003317 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003318 break;
3319 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003320 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003321 break;
3322 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003323 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003324 break;
3325 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003326 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003327 break;
3328 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003329 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003330 break;
3331 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003332 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003333 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003334
Jouk Jansen40322e12004-04-05 08:50:36 +00003335 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003336 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003337 return 1;
3338 }
3339 break;
3340
3341 case OP_ALU_TRI:
3342 switch (code) {
3343 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003344 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003345 break;
3346 }
3347
Keith Whitwell7c26b612005-04-21 14:46:57 +00003348 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003349 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003350
Jouk Jansen40322e12004-04-05 08:50:36 +00003351 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003352 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003353 return 1;
3354 }
3355 break;
3356
3357 case OP_ALU_SWZ:
3358 switch (code) {
3359 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003360 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003361 break;
3362 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003363 {
Brian Paul32df89e2005-10-29 18:26:43 +00003364 GLubyte swizzle[4];
3365 GLubyte negateMask;
3366 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003367 enum register_file file;
3368 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003369
Keith Whitwell7c26b612005-04-21 14:46:57 +00003370 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3371 return 1;
3372
Brian Paul32df89e2005-10-29 18:26:43 +00003373 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003374 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003375 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3376 vp->SrcReg[0].File = file;
3377 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003378 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003379 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3380 swizzle[1],
3381 swizzle[2],
3382 swizzle[3]);
3383 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003384 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003385 break;
3386 }
3387 return 0;
3388}
3389
3390#if DEBUG_PARSING
3391
3392static GLvoid
3393print_state_token (GLint token)
3394{
3395 switch (token) {
3396 case STATE_MATERIAL:
3397 fprintf (stderr, "STATE_MATERIAL ");
3398 break;
3399 case STATE_LIGHT:
3400 fprintf (stderr, "STATE_LIGHT ");
3401 break;
3402
3403 case STATE_LIGHTMODEL_AMBIENT:
3404 fprintf (stderr, "STATE_AMBIENT ");
3405 break;
3406
3407 case STATE_LIGHTMODEL_SCENECOLOR:
3408 fprintf (stderr, "STATE_SCENECOLOR ");
3409 break;
3410
3411 case STATE_LIGHTPROD:
3412 fprintf (stderr, "STATE_LIGHTPROD ");
3413 break;
3414
3415 case STATE_TEXGEN:
3416 fprintf (stderr, "STATE_TEXGEN ");
3417 break;
3418
3419 case STATE_FOG_COLOR:
3420 fprintf (stderr, "STATE_FOG_COLOR ");
3421 break;
3422
3423 case STATE_FOG_PARAMS:
3424 fprintf (stderr, "STATE_FOG_PARAMS ");
3425 break;
3426
3427 case STATE_CLIPPLANE:
3428 fprintf (stderr, "STATE_CLIPPLANE ");
3429 break;
3430
3431 case STATE_POINT_SIZE:
3432 fprintf (stderr, "STATE_POINT_SIZE ");
3433 break;
3434
3435 case STATE_POINT_ATTENUATION:
3436 fprintf (stderr, "STATE_ATTENUATION ");
3437 break;
3438
3439 case STATE_MATRIX:
3440 fprintf (stderr, "STATE_MATRIX ");
3441 break;
3442
3443 case STATE_MODELVIEW:
3444 fprintf (stderr, "STATE_MODELVIEW ");
3445 break;
3446
3447 case STATE_PROJECTION:
3448 fprintf (stderr, "STATE_PROJECTION ");
3449 break;
3450
3451 case STATE_MVP:
3452 fprintf (stderr, "STATE_MVP ");
3453 break;
3454
3455 case STATE_TEXTURE:
3456 fprintf (stderr, "STATE_TEXTURE ");
3457 break;
3458
3459 case STATE_PROGRAM:
3460 fprintf (stderr, "STATE_PROGRAM ");
3461 break;
3462
3463 case STATE_MATRIX_INVERSE:
3464 fprintf (stderr, "STATE_INVERSE ");
3465 break;
3466
3467 case STATE_MATRIX_TRANSPOSE:
3468 fprintf (stderr, "STATE_TRANSPOSE ");
3469 break;
3470
3471 case STATE_MATRIX_INVTRANS:
3472 fprintf (stderr, "STATE_INVTRANS ");
3473 break;
3474
3475 case STATE_AMBIENT:
3476 fprintf (stderr, "STATE_AMBIENT ");
3477 break;
3478
3479 case STATE_DIFFUSE:
3480 fprintf (stderr, "STATE_DIFFUSE ");
3481 break;
3482
3483 case STATE_SPECULAR:
3484 fprintf (stderr, "STATE_SPECULAR ");
3485 break;
3486
3487 case STATE_EMISSION:
3488 fprintf (stderr, "STATE_EMISSION ");
3489 break;
3490
3491 case STATE_SHININESS:
3492 fprintf (stderr, "STATE_SHININESS ");
3493 break;
3494
3495 case STATE_HALF:
3496 fprintf (stderr, "STATE_HALF ");
3497 break;
3498
3499 case STATE_POSITION:
3500 fprintf (stderr, "STATE_POSITION ");
3501 break;
3502
3503 case STATE_ATTENUATION:
3504 fprintf (stderr, "STATE_ATTENUATION ");
3505 break;
3506
3507 case STATE_SPOT_DIRECTION:
3508 fprintf (stderr, "STATE_DIRECTION ");
3509 break;
3510
3511 case STATE_TEXGEN_EYE_S:
3512 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3513 break;
3514
3515 case STATE_TEXGEN_EYE_T:
3516 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3517 break;
3518
3519 case STATE_TEXGEN_EYE_R:
3520 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3521 break;
3522
3523 case STATE_TEXGEN_EYE_Q:
3524 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3525 break;
3526
3527 case STATE_TEXGEN_OBJECT_S:
3528 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3529 break;
3530
3531 case STATE_TEXGEN_OBJECT_T:
3532 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3533 break;
3534
3535 case STATE_TEXGEN_OBJECT_R:
3536 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3537 break;
3538
3539 case STATE_TEXGEN_OBJECT_Q:
3540 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3541 break;
3542
3543 case STATE_TEXENV_COLOR:
3544 fprintf (stderr, "STATE_TEXENV_COLOR ");
3545 break;
3546
3547 case STATE_DEPTH_RANGE:
3548 fprintf (stderr, "STATE_DEPTH_RANGE ");
3549 break;
3550
3551 case STATE_VERTEX_PROGRAM:
3552 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3553 break;
3554
3555 case STATE_FRAGMENT_PROGRAM:
3556 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3557 break;
3558
3559 case STATE_ENV:
3560 fprintf (stderr, "STATE_ENV ");
3561 break;
3562
3563 case STATE_LOCAL:
3564 fprintf (stderr, "STATE_LOCAL ");
3565 break;
3566
3567 }
3568 fprintf (stderr, "[%d] ", token);
3569}
3570
3571
3572static GLvoid
3573debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3574 struct arb_program *Program)
3575{
3576 struct var_cache *vc;
3577 GLint a, b;
3578
3579 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3580
3581 /* First of all, print out the contents of the var_cache */
3582 vc = vc_head;
3583 while (vc) {
3584 fprintf (stderr, "[%x]\n", vc);
3585 switch (vc->type) {
3586 case vt_none:
3587 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3588 break;
3589 case vt_attrib:
3590 fprintf (stderr, "ATTRIB %s\n", vc->name);
3591 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3592 break;
3593 case vt_param:
3594 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3595 vc->param_binding_begin, vc->param_binding_length);
3596 b = vc->param_binding_begin;
3597 for (a = 0; a < vc->param_binding_length; a++) {
3598 fprintf (stderr, "%s\n",
3599 Program->Parameters->Parameters[a + b].Name);
3600 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3601 print_state_token (Program->Parameters->Parameters[a + b].
3602 StateIndexes[0]);
3603 print_state_token (Program->Parameters->Parameters[a + b].
3604 StateIndexes[1]);
3605 print_state_token (Program->Parameters->Parameters[a + b].
3606 StateIndexes[2]);
3607 print_state_token (Program->Parameters->Parameters[a + b].
3608 StateIndexes[3]);
3609 print_state_token (Program->Parameters->Parameters[a + b].
3610 StateIndexes[4]);
3611 print_state_token (Program->Parameters->Parameters[a + b].
3612 StateIndexes[5]);
3613 }
3614 else
3615 fprintf (stderr, "%f %f %f %f\n",
3616 Program->Parameters->Parameters[a + b].Values[0],
3617 Program->Parameters->Parameters[a + b].Values[1],
3618 Program->Parameters->Parameters[a + b].Values[2],
3619 Program->Parameters->Parameters[a + b].Values[3]);
3620 }
3621 break;
3622 case vt_temp:
3623 fprintf (stderr, "TEMP %s\n", vc->name);
3624 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3625 break;
3626 case vt_output:
3627 fprintf (stderr, "OUTPUT %s\n", vc->name);
3628 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3629 break;
3630 case vt_alias:
3631 fprintf (stderr, "ALIAS %s\n", vc->name);
3632 fprintf (stderr, " binding: 0x%x (%s)\n",
3633 vc->alias_binding, vc->alias_binding->name);
3634 break;
3635 }
3636 vc = vc->next;
3637 }
3638}
3639
Brian Paul72030e02005-11-03 03:30:34 +00003640#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003641
3642
3643/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003644 * The main loop for parsing a fragment or vertex program
3645 *
Brian Paul72030e02005-11-03 03:30:34 +00003646 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003647 */
Brian Paul72030e02005-11-03 03:30:34 +00003648static GLint
Brian Paul8c41a142005-11-19 15:36:28 +00003649parse_instructions(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
Brian Paul45703642005-10-29 15:52:31 +00003650 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003651{
Brian Paul72030e02005-11-03 03:30:34 +00003652 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3653 ? ctx->Const.FragmentProgram.MaxInstructions
3654 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003655 GLint err = 0;
3656
Brian Paul72030e02005-11-03 03:30:34 +00003657 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3658
Jouk Jansen40322e12004-04-05 08:50:36 +00003659 Program->MajorVersion = (GLuint) * inst++;
3660 Program->MinorVersion = (GLuint) * inst++;
3661
3662 while (*inst != END) {
3663 switch (*inst++) {
3664
3665 case OPTION:
3666 switch (*inst++) {
3667 case ARB_PRECISION_HINT_FASTEST:
3668 Program->PrecisionOption = GL_FASTEST;
3669 break;
3670
3671 case ARB_PRECISION_HINT_NICEST:
3672 Program->PrecisionOption = GL_NICEST;
3673 break;
3674
3675 case ARB_FOG_EXP:
3676 Program->FogOption = GL_EXP;
3677 break;
3678
3679 case ARB_FOG_EXP2:
3680 Program->FogOption = GL_EXP2;
3681 break;
3682
3683 case ARB_FOG_LINEAR:
3684 Program->FogOption = GL_LINEAR;
3685 break;
3686
3687 case ARB_POSITION_INVARIANT:
3688 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003689 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003690 break;
3691
3692 case ARB_FRAGMENT_PROGRAM_SHADOW:
3693 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3694 /* TODO ARB_fragment_program_shadow code */
3695 }
3696 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003697
3698 case ARB_DRAW_BUFFERS:
3699 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3700 /* do nothing for now */
3701 }
3702 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003703 }
3704 break;
3705
3706 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003707 /* check length */
3708 if (Program->Base.NumInstructions + 1 >= maxInst) {
3709 const char *msg = "Max instruction count exceeded";
3710 _mesa_set_program_error(ctx, Program->Position, msg);
3711 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3712 return 1;
3713 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003714 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003715 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003716 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003718 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003719 }
3720 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003721 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003722 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003723 }
3724
Brian Paul72030e02005-11-03 03:30:34 +00003725 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003726 Program->Base.NumInstructions++;
3727 break;
3728
3729 case DECLARATION:
3730 err = parse_declaration (ctx, &inst, vc_head, Program);
3731 break;
3732
3733 default:
3734 break;
3735 }
3736
3737 if (err)
3738 break;
3739 }
3740
3741 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003742 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003743 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul8c41a142005-11-19 15:36:28 +00003744 _mesa_init_instruction(Program->Base.Instructions + numInst);
3745 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003746 /* YYY Wrong Position in program, whatever, at least not random -> crash
3747 Program->Position = parse_position (&inst);
3748 */
Brian Paul8c41a142005-11-19 15:36:28 +00003749 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003750 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003751 Program->Base.NumInstructions++;
3752
Brian Paul05051032005-11-01 04:36:33 +00003753 /*
3754 * Initialize native counts to logical counts. The device driver may
3755 * change them if program is translated into a hardware program.
3756 */
3757 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3758 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3759 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3760 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3761 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003762
Jouk Jansen40322e12004-04-05 08:50:36 +00003763 return err;
3764}
3765
Brian Paul7aebaf32005-10-30 21:23:23 +00003766
Jouk Jansen40322e12004-04-05 08:50:36 +00003767/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003768__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003769#include "grammar_syn.h"
3770;
3771
Brian Paul7aebaf32005-10-30 21:23:23 +00003772
Brian Paul8c41a142005-11-19 15:36:28 +00003773/**
3774 * Set a grammar parameter.
3775 * \param name the grammar parameter
3776 * \param value the new parameter value
3777 * \return 0 if OK, 1 if error
3778 */
3779static int
3780set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003781{
3782 char error_msg[300];
3783 GLint error_pos;
3784
Brian Paul8c41a142005-11-19 15:36:28 +00003785 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003786 return 0;
3787
3788 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3789 _mesa_set_program_error (ctx, error_pos, error_msg);
3790 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3791 return 1;
3792}
3793
Brian Paul8c41a142005-11-19 15:36:28 +00003794
3795/**
3796 * Enable support for the given language option in the parser.
3797 * \return 1 if OK, 0 if error
3798 */
3799static int
3800enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003801{
Brian Paul8c41a142005-11-19 15:36:28 +00003802 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003803}
3804
Brian Paul8c41a142005-11-19 15:36:28 +00003805
3806/**
3807 * Enable parser extensions based on which OpenGL extensions are supported
3808 * by this rendering context.
3809 *
3810 * \return GL_TRUE if OK, GL_FALSE if error.
3811 */
3812static GLboolean
3813enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003814{
Brian Paul8c41a142005-11-19 15:36:28 +00003815#if 0
3816 /* These are not supported at this time */
3817 if ((ctx->Extensions.ARB_vertex_blend ||
3818 ctx->Extensions.EXT_vertex_weighting)
3819 && !enable_ext(ctx, id, "point_parameters"))
3820 return GL_FALSE;
3821 if (ctx->Extensions.ARB_matrix_palette
3822 && !enable_ext(ctx, id, "matrix_palette"))
3823 return GL_FALSE;
3824 if (ctx->Extensions.ARB_fragment_program_shadow
3825 && !enable_ext(ctx, id, "fragment_program_shadow"))
3826 return GL_FALSE;
3827#endif
3828 if (ctx->Extensions.EXT_point_parameters
3829 && !enable_ext(ctx, id, "point_parameters"))
3830 return GL_FALSE;
3831 if (ctx->Extensions.EXT_secondary_color
3832 && !enable_ext(ctx, id, "secondary_color"))
3833 return GL_FALSE;
3834 if (ctx->Extensions.EXT_fog_coord
3835 && !enable_ext(ctx, id, "fog_coord"))
3836 return GL_FALSE;
3837 if (ctx->Extensions.NV_texture_rectangle
3838 && !enable_ext(ctx, id, "texture_rectangle"))
3839 return GL_FALSE;
3840 if (ctx->Extensions.ARB_draw_buffers
3841 && !enable_ext(ctx, id, "draw_buffers"))
3842 return GL_FALSE;
3843
3844 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003845}
3846
Brian Paul8c41a142005-11-19 15:36:28 +00003847
Jouk Jansen40322e12004-04-05 08:50:36 +00003848/**
3849 * This kicks everything off.
3850 *
3851 * \param ctx - The GL Context
3852 * \param str - The program string
3853 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003854 * \param program - The arb_program struct to return all the parsed info in
3855 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003856 */
Brian Paul8c41a142005-11-19 15:36:28 +00003857static GLboolean
3858_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3859 const GLubyte *str, GLsizei len,
3860 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003861{
3862 GLint a, err, error_pos;
3863 char error_msg[300];
3864 GLuint parsed_len;
3865 struct var_cache *vc_head;
3866 grammar arbprogram_syn_id;
3867 GLubyte *parsed, *inst;
3868 GLubyte *strz = NULL;
3869 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3870
Brian Paul8c41a142005-11-19 15:36:28 +00003871 /* set the program target before parsing */
3872 program->Base.Target = target;
3873
Brian Paul7f76b8f2004-09-10 01:05:39 +00003874 /* Reset error state */
3875 _mesa_set_program_error(ctx, -1, NULL);
3876
Brian Paul8c41a142005-11-19 15:36:28 +00003877 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003878 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003879 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003880 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003881 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003882
3883 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3884 if (grammar_syn_id == 0) {
3885 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003886 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003887 _mesa_set_program_error (ctx, error_pos, error_msg);
3888 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003889 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003890 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003891 }
3892
Brian Paul8c41a142005-11-19 15:36:28 +00003893 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3894 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003895
Brian Paul49a80ca2006-04-28 15:40:11 +00003896 /* 'parsed' is unused here */
3897 _mesa_free (parsed);
3898 parsed = NULL;
3899
Brian Paul45703642005-10-29 15:52:31 +00003900 /* NOTE: we can't destroy grammar_syn_id right here because
3901 * grammar_destroy() can reset the last error
3902 */
Brian Paul8c41a142005-11-19 15:36:28 +00003903 if (err) {
3904 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003905 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3906 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003907 _mesa_error (ctx, GL_INVALID_OPERATION,
3908 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003909 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003910 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003911 }
3912
3913 grammar_destroy (grammar_syn_id);
3914
3915 arbprogram_syn_is_ok = 1;
3916 }
3917
3918 /* create the grammar object */
3919 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3920 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003921 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003922 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3923 _mesa_set_program_error (ctx, error_pos, error_msg);
3924 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003925 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003926 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003927 }
3928
3929 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003930 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003931 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3932 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003933 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003934 }
3935
Brian Paul8c41a142005-11-19 15:36:28 +00003936 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3937 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003938 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003939 }
3940
3941 /* check for NULL character occurences */
3942 {
Brian Paul8c41a142005-11-19 15:36:28 +00003943 GLint i;
3944 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003945 if (str[i] == '\0') {
3946 _mesa_set_program_error (ctx, i, "invalid character");
Brian Paul8c41a142005-11-19 15:36:28 +00003947 _mesa_error (ctx, GL_INVALID_OPERATION,
3948 "glProgramStringARB(illegal character)");
Jouk Jansen40322e12004-04-05 08:50:36 +00003949 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003950 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003951 }
Brian Paul8c41a142005-11-19 15:36:28 +00003952 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003953 }
3954
3955 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003956 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003957 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003958 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3959 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003960 return GL_FALSE;
3961 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003962 _mesa_memcpy (strz, str, len);
3963 strz[len] = '\0';
3964
Michal Krolb80bc052004-10-21 14:09:54 +00003965 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003966 err = !grammar_fast_check(arbprogram_syn_id, strz,
3967 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003968
3969 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003970 if (err) {
3971 _mesa_free(strz);
Brian Paul49a80ca2006-04-28 15:40:11 +00003972 _mesa_free(parsed);
Jouk Jansen40322e12004-04-05 08:50:36 +00003973 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3974 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003975 _mesa_error (ctx, GL_INVALID_OPERATION,
3976 "glProgramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003977
3978 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003979#if DEBUG_PARSING
3980 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003981 int line, col;
3982 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003983 fprintf(stderr, "program: %s\n", (char *) strz);
3984 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
3985 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003986 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3987 } while (0)
3988#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003989
Jouk Jansen40322e12004-04-05 08:50:36 +00003990 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003991 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003992 }
3993
Jouk Jansen40322e12004-04-05 08:50:36 +00003994 grammar_destroy (arbprogram_syn_id);
3995
Brian Paul8c41a142005-11-19 15:36:28 +00003996 /*
3997 * Program string is syntactically correct at this point
3998 * Parse the tokenized version of the program now, generating
3999 * vertex/fragment program instructions.
4000 */
4001
Jouk Jansen40322e12004-04-05 08:50:36 +00004002 /* Initialize the arb_program struct */
4003 program->Base.String = strz;
Brian Paul8c41a142005-11-19 15:36:28 +00004004 program->Base.Instructions = (struct prog_instruction *)
4005 _mesa_malloc(MAX_INSTRUCTIONS * sizeof(struct prog_instruction));
Jouk Jansen40322e12004-04-05 08:50:36 +00004006 program->Base.NumInstructions =
4007 program->Base.NumTemporaries =
4008 program->Base.NumParameters =
4009 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00004010 program->Base.Parameters = _mesa_new_parameter_list ();
4011 program->Base.InputsRead = 0x0;
4012 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004013 program->Position = 0;
4014 program->MajorVersion = program->MinorVersion = 0;
4015 program->PrecisionOption = GL_DONT_CARE;
4016 program->FogOption = GL_NONE;
4017 program->HintPositionInvariant = GL_FALSE;
4018 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00004019 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004020 program->NumAluInstructions =
4021 program->NumTexInstructions =
4022 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00004023 program->UsesKill = 0;
4024
Jouk Jansen40322e12004-04-05 08:50:36 +00004025 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00004026 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004027
4028 /* Start examining the tokens in the array */
4029 inst = parsed;
4030
4031 /* Check the grammer rev */
4032 if (*inst++ != REVISION) {
4033 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00004034 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul45cd2f92005-11-03 02:25:10 +00004035 "glProgramStringARB(Grammar version mismatch)");
Brian Paul45703642005-10-29 15:52:31 +00004036 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004037 }
4038 else {
Michal Krolb80bc052004-10-21 14:09:54 +00004039 /* ignore program target */
4040 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00004041 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00004042 }
4043
4044 /*debug_variables(ctx, vc_head, program); */
4045
4046 /* We're done with the parsed binary array */
4047 var_cache_destroy (&vc_head);
4048
4049 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00004050
Brian Paul8c41a142005-11-19 15:36:28 +00004051 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4052 * to size [ap.Base.NumInstructions].
4053 */
4054 program->Base.Instructions = (struct prog_instruction *)
4055 _mesa_realloc(program->Base.Instructions,
4056 MAX_INSTRUCTIONS * sizeof(struct prog_instruction),/*orig*/
4057 program->Base.NumInstructions * sizeof(struct prog_instruction));
4058
Brian Paul45703642005-10-29 15:52:31 +00004059 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004060}
Brian Paul8c41a142005-11-19 15:36:28 +00004061
4062
4063
4064void
4065_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4066 const GLvoid *str, GLsizei len,
4067 struct fragment_program *program)
4068{
4069 struct arb_program ap;
4070 GLuint i;
4071
4072 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004073 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004074 /* Error in the program. Just return. */
4075 return;
4076 }
4077
4078 /* Copy the relevant contents of the arb_program struct into the
4079 * fragment_program struct.
4080 */
4081 program->Base.String = ap.Base.String;
4082 program->Base.NumInstructions = ap.Base.NumInstructions;
4083 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4084 program->Base.NumParameters = ap.Base.NumParameters;
4085 program->Base.NumAttributes = ap.Base.NumAttributes;
4086 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4087 program->NumAluInstructions = ap.NumAluInstructions;
4088 program->NumTexInstructions = ap.NumTexInstructions;
4089 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004090 program->NumNativeAluInstructions = ap.NumAluInstructions;
4091 program->NumNativeTexInstructions = ap.NumTexInstructions;
4092 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004093 program->Base.InputsRead = ap.Base.InputsRead;
4094 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4095 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4096 program->TexturesUsed[i] = ap.TexturesUsed[i];
4097 program->FogOption = ap.FogOption;
4098
4099 if (program->Base.Instructions)
4100 _mesa_free(program->Base.Instructions);
4101 program->Base.Instructions = ap.Base.Instructions;
4102
4103 if (program->Base.Parameters)
4104 _mesa_free_parameter_list(program->Base.Parameters);
4105 program->Base.Parameters = ap.Base.Parameters;
4106
4107#if DEBUG_FP
4108 _mesa_print_program(&program.Base);
4109#endif
4110}
4111
4112
4113
4114/**
4115 * Parse the vertex program string. If success, update the given
4116 * vertex_program object with the new program. Else, leave the vertex_program
4117 * object unchanged.
4118 */
4119void
4120_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4121 const GLvoid *str, GLsizei len,
4122 struct vertex_program *program)
4123{
4124 struct arb_program ap;
4125
4126 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4127
Brian Paul95801792005-12-06 15:41:43 +00004128 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004129 /* Error in the program. Just return. */
4130 return;
4131 }
4132
4133 /* Copy the relevant contents of the arb_program struct into the
4134 * vertex_program struct.
4135 */
4136 program->Base.String = ap.Base.String;
4137 program->Base.NumInstructions = ap.Base.NumInstructions;
4138 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4139 program->Base.NumParameters = ap.Base.NumParameters;
4140 program->Base.NumAttributes = ap.Base.NumAttributes;
4141 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4142 program->Base.InputsRead = ap.Base.InputsRead;
4143 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4144 program->IsPositionInvariant = ap.HintPositionInvariant;
4145
4146 if (program->Base.Instructions)
4147 _mesa_free(program->Base.Instructions);
4148 program->Base.Instructions = ap.Base.Instructions;
4149
4150 if (program->Base.Parameters)
4151 _mesa_free_parameter_list(program->Base.Parameters);
4152 program->Base.Parameters = ap.Base.Parameters;
4153
4154#if DEBUG_VP
4155 _mesa_print_program(&program->Base);
4156#endif
4157}