blob: b596b79bc7922d48af7600b12b588b048a16366d [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 Paul095c6692006-04-25 00:21:32 +00001541 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1542 * attributes do not alias the conventional vertex
1543 * attributes.
1544 */
1545 if (attrib > 0)
1546 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001547 }
1548 }
1549 break;
1550
1551 default:
1552 err = 1;
1553 break;
1554 }
1555 }
1556
1557 /* Can this even happen? */
1558 if (err) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001559 const char *msg = "Bad attribute binding";
1560 _mesa_set_program_error(ctx, Program->Position, msg);
1561 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001562 }
1563
Brian Paulde997602005-11-12 17:53:14 +00001564 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001565
1566 return err;
1567}
1568
Brian Paul7aebaf32005-10-30 21:23:23 +00001569
Jouk Jansen40322e12004-04-05 08:50:36 +00001570/**
1571 * This translates between a binary token for an output variable type
1572 * and the mesa token for the same thing.
1573 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001574 * \param inst The parsed tokens
1575 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001576 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001577 */
1578static GLuint
Brian Paul7aebaf32005-10-30 21:23:23 +00001579parse_result_binding(GLcontext *ctx, GLubyte **inst,
1580 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001581{
Brian Paul7aebaf32005-10-30 21:23:23 +00001582 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001583
Brian Paul7aebaf32005-10-30 21:23:23 +00001584 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001585 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001586 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001587 GLuint out_color;
1588
Brian Paulbe76b7f2004-10-04 14:40:05 +00001589 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001590 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001591 */
1592 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001593 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001594 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001595 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001596 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001597 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1598 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001599 }
1600 break;
1601
1602 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001603 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001604 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001605 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001606 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001608 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001609 GLint color_type;
1610 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001611 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1612 if (err)
1613 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001614
Jouk Jansen40322e12004-04-05 08:50:36 +00001615 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001616 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001618 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001619 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001620 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001621 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001622 }
1623 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001625 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001627 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 }
1629 /* primary color */
1630 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001631 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001632 }
1633 }
1634 }
1635 break;
1636
1637 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001638 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001639 break;
1640
1641 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001642 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001643 break;
1644
1645 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001646 {
1647 GLuint unit;
1648 if (parse_texcoord_num (ctx, inst, Program, &unit))
1649 return 1;
1650 *outputReg = VERT_RESULT_TEX0 + unit;
1651 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001652 break;
1653 }
1654
Brian Paulde997602005-11-12 17:53:14 +00001655 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001656
1657 return 0;
1658}
1659
Brian Paul7aebaf32005-10-30 21:23:23 +00001660
Jouk Jansen40322e12004-04-05 08:50:36 +00001661/**
1662 * This handles the declaration of ATTRIB variables
1663 *
1664 * XXX: Still needs
1665 * parse_vert_attrib_binding(), or something like that
1666 *
1667 * \return 0 on sucess, 1 on error
1668 */
1669static GLint
1670parse_attrib (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1671 struct arb_program *Program)
1672{
1673 GLuint found;
1674 char *error_msg;
1675 struct var_cache *attrib_var;
1676
1677 attrib_var = parse_string (inst, vc_head, Program, &found);
1678 Program->Position = parse_position (inst);
1679 if (found) {
1680 error_msg = (char *)
1681 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1682 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1683 attrib_var->name);
1684
1685 _mesa_set_program_error (ctx, Program->Position, error_msg);
1686 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1687
1688 _mesa_free (error_msg);
1689 return 1;
1690 }
1691
1692 attrib_var->type = vt_attrib;
1693
Brian Paul7aebaf32005-10-30 21:23:23 +00001694 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001695 &attrib_var->attrib_is_generic))
1696 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001697
Brian Paul7aebaf32005-10-30 21:23:23 +00001698 if (generic_attrib_check(*vc_head)) {
1699 _mesa_set_program_error(ctx, Program->Position,
1700 "Cannot use both a generic vertex attribute "
1701 "and a specific attribute of the same type");
1702 _mesa_error(ctx, GL_INVALID_OPERATION,
1703 "Cannot use both a generic vertex attribute and a specific "
1704 "attribute of the same type");
1705 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001706 }
1707
1708 Program->Base.NumAttributes++;
1709 return 0;
1710}
1711
1712/**
1713 * \param use -- TRUE if we're called when declaring implicit parameters,
1714 * FALSE if we're declaraing variables. This has to do with
1715 * if we get a signed or unsigned float for scalar constants
1716 */
1717static GLuint
1718parse_param_elements (GLcontext * ctx, GLubyte ** inst,
1719 struct var_cache *param_var,
1720 struct arb_program *Program, GLboolean use)
1721{
1722 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001723 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001724 GLint state_tokens[6];
1725 GLfloat const_values[4];
1726
Jouk Jansen40322e12004-04-05 08:50:36 +00001727 switch (*(*inst)++) {
1728 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001729 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1730 return 1;
1731
1732 /* If we adding STATE_MATRIX that has multiple rows, we need to
1733 * unroll it and call _mesa_add_state_reference() for each row
1734 */
1735 if ((state_tokens[0] == STATE_MATRIX)
1736 && (state_tokens[3] != state_tokens[4])) {
1737 GLint row;
1738 GLint first_row = state_tokens[3];
1739 GLint last_row = state_tokens[4];
1740
1741 for (row = first_row; row <= last_row; row++) {
1742 state_tokens[3] = state_tokens[4] = row;
1743
Brian Paulde997602005-11-12 17:53:14 +00001744 idx = _mesa_add_state_reference(Program->Base.Parameters,
1745 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001746 if (param_var->param_binding_begin == ~0U)
1747 param_var->param_binding_begin = idx;
1748 param_var->param_binding_length++;
1749 Program->Base.NumParameters++;
1750 }
1751 }
1752 else {
Brian Paulde997602005-11-12 17:53:14 +00001753 idx = _mesa_add_state_reference(Program->Base.Parameters,
1754 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001755 if (param_var->param_binding_begin == ~0U)
1756 param_var->param_binding_begin = idx;
1757 param_var->param_binding_length++;
1758 Program->Base.NumParameters++;
1759 }
1760 break;
1761
1762 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001763 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1764 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001765 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001766 if (param_var->param_binding_begin == ~0U)
1767 param_var->param_binding_begin = idx;
1768 param_var->param_binding_length++;
1769 Program->Base.NumParameters++;
1770
1771 /* Check if there is more: 0 -> we're done, else its an integer */
1772 if (**inst) {
1773 GLuint out_of_range, new_idx;
1774 GLuint start_idx = state_tokens[2] + 1;
1775 GLuint end_idx = parse_integer (inst, Program);
1776
1777 out_of_range = 0;
1778 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1779 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001780 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001781 || ((state_tokens[1] == STATE_LOCAL)
1782 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001783 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001784 out_of_range = 1;
1785 }
1786 else {
1787 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001788 && (end_idx >= ctx->Const.VertexProgram.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.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001792 out_of_range = 1;
1793 }
1794 if (out_of_range) {
1795 _mesa_set_program_error (ctx, Program->Position,
1796 "Invalid Program Parameter");
1797 _mesa_error (ctx, GL_INVALID_OPERATION,
1798 "Invalid Program Parameter: %d", end_idx);
1799 return 1;
1800 }
1801
1802 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1803 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001804 idx = _mesa_add_state_reference(Program->Base.Parameters,
1805 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001806 param_var->param_binding_length++;
1807 Program->Base.NumParameters++;
1808 }
1809 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001810 else {
1811 (*inst)++;
1812 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001813 break;
1814
1815 case PARAM_CONSTANT:
1816 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001817 idx = _mesa_add_named_constant(Program->Base.Parameters,
1818 (char *) param_var->name,
1819 const_values);
Jouk Jansen40322e12004-04-05 08:50:36 +00001820 if (param_var->param_binding_begin == ~0U)
1821 param_var->param_binding_begin = idx;
1822 param_var->param_binding_length++;
1823 Program->Base.NumParameters++;
1824 break;
1825
1826 default:
Brian Paul7aebaf32005-10-30 21:23:23 +00001827 _mesa_set_program_error(ctx, Program->Position,
1828 "Unexpected token in parse_param_elements()");
1829 _mesa_error(ctx, GL_INVALID_OPERATION,
1830 "Unexpected token in parse_param_elements()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001831 return 1;
1832 }
1833
1834 /* Make sure we haven't blown past our parameter limits */
1835 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1836 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001837 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001838 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1839 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001840 ctx->Const.FragmentProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001841 _mesa_set_program_error (ctx, Program->Position,
1842 "Too many parameter variables");
1843 _mesa_error (ctx, GL_INVALID_OPERATION, "Too many parameter variables");
1844 return 1;
1845 }
1846
1847 return err;
1848}
1849
Brian Paul7aebaf32005-10-30 21:23:23 +00001850
Jouk Jansen40322e12004-04-05 08:50:36 +00001851/**
1852 * This picks out PARAM program parameter bindings.
1853 *
1854 * XXX: This needs to be stressed & tested
1855 *
1856 * \return 0 on sucess, 1 on error
1857 */
1858static GLuint
1859parse_param (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1860 struct arb_program *Program)
1861{
Brian Paulbd997cd2004-07-20 21:12:56 +00001862 GLuint found, err;
1863 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001864 struct var_cache *param_var;
1865
1866 err = 0;
1867 param_var = parse_string (inst, vc_head, Program, &found);
1868 Program->Position = parse_position (inst);
1869
1870 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001871 char *error_msg = (char *)
1872 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001873 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1874 param_var->name);
1875
1876 _mesa_set_program_error (ctx, Program->Position, error_msg);
1877 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1878
1879 _mesa_free (error_msg);
1880 return 1;
1881 }
1882
1883 specified_length = parse_integer (inst, Program);
1884
1885 if (specified_length < 0) {
1886 _mesa_set_program_error (ctx, Program->Position,
1887 "Negative parameter array length");
1888 _mesa_error (ctx, GL_INVALID_OPERATION,
1889 "Negative parameter array length: %d", specified_length);
1890 return 1;
1891 }
1892
1893 param_var->type = vt_param;
1894 param_var->param_binding_length = 0;
1895
1896 /* Right now, everything is shoved into the main state register file.
1897 *
1898 * In the future, it would be nice to leave things ENV/LOCAL params
1899 * in their respective register files, if possible
1900 */
1901 param_var->param_binding_type = PROGRAM_STATE_VAR;
1902
1903 /* Remember to:
1904 * * - add each guy to the parameter list
1905 * * - increment the param_var->param_binding_len
1906 * * - store the param_var->param_binding_begin for the first one
1907 * * - compare the actual len to the specified len at the end
1908 */
1909 while (**inst != PARAM_NULL) {
1910 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1911 return 1;
1912 }
1913
1914 /* Test array length here! */
1915 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001916 if (specified_length != (int)param_var->param_binding_length) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001917 const char *msg
1918 = "Declared parameter array length does not match parameter list";
1919 _mesa_set_program_error(ctx, Program->Position, msg);
1920 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001921 }
1922 }
1923
1924 (*inst)++;
1925
1926 return 0;
1927}
1928
1929/**
1930 *
1931 */
1932static GLuint
1933parse_param_use (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1934 struct arb_program *Program, struct var_cache **new_var)
1935{
1936 struct var_cache *param_var;
1937
1938 /* First, insert a dummy entry into the var_cache */
1939 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001940 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001941 param_var->type = vt_param;
1942
1943 param_var->param_binding_length = 0;
1944 /* Don't fill in binding_begin; We use the default value of -1
1945 * to tell if its already initialized, elsewhere.
1946 *
1947 * param_var->param_binding_begin = 0;
1948 */
1949 param_var->param_binding_type = PROGRAM_STATE_VAR;
1950
1951 var_cache_append (vc_head, param_var);
1952
1953 /* Then fill it with juicy parameter goodness */
1954 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1955 return 1;
1956
1957 *new_var = param_var;
1958
1959 return 0;
1960}
1961
1962
1963/**
1964 * This handles the declaration of TEMP variables
1965 *
1966 * \return 0 on sucess, 1 on error
1967 */
1968static GLuint
1969parse_temp (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1970 struct arb_program *Program)
1971{
1972 GLuint found;
1973 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001974
1975 while (**inst != 0) {
1976 temp_var = parse_string (inst, vc_head, Program, &found);
1977 Program->Position = parse_position (inst);
1978 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001979 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001980 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1981 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1982 temp_var->name);
1983
1984 _mesa_set_program_error (ctx, Program->Position, error_msg);
1985 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1986
1987 _mesa_free (error_msg);
1988 return 1;
1989 }
1990
1991 temp_var->type = vt_temp;
1992
1993 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1994 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001995 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001996 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1997 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001998 ctx->Const.VertexProgram.MaxTemps))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001999 _mesa_set_program_error (ctx, Program->Position,
2000 "Too many TEMP variables declared");
2001 _mesa_error (ctx, GL_INVALID_OPERATION,
2002 "Too many TEMP variables declared");
2003 return 1;
2004 }
2005
2006 temp_var->temp_binding = Program->Base.NumTemporaries;
2007 Program->Base.NumTemporaries++;
2008 }
2009 (*inst)++;
2010
2011 return 0;
2012}
2013
2014/**
2015 * This handles variables of the OUTPUT variety
2016 *
2017 * \return 0 on sucess, 1 on error
2018 */
2019static GLuint
2020parse_output (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2021 struct arb_program *Program)
2022{
2023 GLuint found;
2024 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002025 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002026
2027 output_var = parse_string (inst, vc_head, Program, &found);
2028 Program->Position = parse_position (inst);
2029 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002030 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002031 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2032 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2033 output_var->name);
2034
2035 _mesa_set_program_error (ctx, Program->Position, error_msg);
2036 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2037
2038 _mesa_free (error_msg);
2039 return 1;
2040 }
2041
2042 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002043
2044 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2045 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002046}
2047
2048/**
2049 * This handles variables of the ALIAS kind
2050 *
2051 * \return 0 on sucess, 1 on error
2052 */
2053static GLuint
2054parse_alias (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2055 struct arb_program *Program)
2056{
2057 GLuint found;
2058 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002059
2060 temp_var = parse_string (inst, vc_head, Program, &found);
2061 Program->Position = parse_position (inst);
2062
2063 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002064 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002065 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2066 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2067 temp_var->name);
2068
2069 _mesa_set_program_error (ctx, Program->Position, error_msg);
2070 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2071
2072 _mesa_free (error_msg);
2073 return 1;
2074 }
2075
2076 temp_var->type = vt_alias;
2077 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2078 Program->Position = parse_position (inst);
2079
2080 if (!found)
2081 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002082 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002083 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2084 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2085 temp_var->alias_binding->name);
2086
2087 _mesa_set_program_error (ctx, Program->Position, error_msg);
2088 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2089
2090 _mesa_free (error_msg);
2091 return 1;
2092 }
2093
2094 return 0;
2095}
2096
2097/**
2098 * This handles variables of the ADDRESS kind
2099 *
2100 * \return 0 on sucess, 1 on error
2101 */
2102static GLuint
2103parse_address (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2104 struct arb_program *Program)
2105{
2106 GLuint found;
2107 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002108
2109 while (**inst != 0) {
2110 temp_var = parse_string (inst, vc_head, Program, &found);
2111 Program->Position = parse_position (inst);
2112 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002113 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002114 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2115 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2116 temp_var->name);
2117
2118 _mesa_set_program_error (ctx, Program->Position, error_msg);
2119 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2120
2121 _mesa_free (error_msg);
2122 return 1;
2123 }
2124
2125 temp_var->type = vt_address;
2126
2127 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002128 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002129 const char *msg = "Too many ADDRESS variables declared";
2130 _mesa_set_program_error(ctx, Program->Position, msg);
2131
2132 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002133 return 1;
2134 }
2135
2136 temp_var->address_binding = Program->Base.NumAddressRegs;
2137 Program->Base.NumAddressRegs++;
2138 }
2139 (*inst)++;
2140
2141 return 0;
2142}
2143
2144/**
2145 * Parse a program declaration
2146 *
2147 * \return 0 on sucess, 1 on error
2148 */
2149static GLint
2150parse_declaration (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2151 struct arb_program *Program)
2152{
2153 GLint err = 0;
2154
2155 switch (*(*inst)++) {
2156 case ADDRESS:
2157 err = parse_address (ctx, inst, vc_head, Program);
2158 break;
2159
2160 case ALIAS:
2161 err = parse_alias (ctx, inst, vc_head, Program);
2162 break;
2163
2164 case ATTRIB:
2165 err = parse_attrib (ctx, inst, vc_head, Program);
2166 break;
2167
2168 case OUTPUT:
2169 err = parse_output (ctx, inst, vc_head, Program);
2170 break;
2171
2172 case PARAM:
2173 err = parse_param (ctx, inst, vc_head, Program);
2174 break;
2175
2176 case TEMP:
2177 err = parse_temp (ctx, inst, vc_head, Program);
2178 break;
2179 }
2180
2181 return err;
2182}
2183
2184/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002185 * Handle the parsing out of a masked destination register, either for a
2186 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002187 *
2188 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002189 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002190 * position invariant
2191 *
2192 * \param File - The register file we write to
2193 * \param Index - The register index we write to
2194 * \param WriteMask - The mask controlling which components we write (1->write)
2195 *
2196 * \return 0 on sucess, 1 on error
2197 */
2198static GLuint
2199parse_masked_dst_reg (GLcontext * ctx, GLubyte ** inst,
2200 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002201 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002202{
Brian Paul7aebaf32005-10-30 21:23:23 +00002203 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002204 struct var_cache *dst;
2205
2206 /* We either have a result register specified, or a
2207 * variable that may or may not be writable
2208 */
2209 switch (*(*inst)++) {
2210 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002211 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002212 return 1;
2213 *File = PROGRAM_OUTPUT;
2214 break;
2215
2216 case REGISTER_ESTABLISHED_NAME:
2217 dst = parse_string (inst, vc_head, Program, &result);
2218 Program->Position = parse_position (inst);
2219
2220 /* If the name has never been added to our symbol table, we're hosed */
2221 if (!result) {
2222 _mesa_set_program_error (ctx, Program->Position,
2223 "0: Undefined variable");
2224 _mesa_error (ctx, GL_INVALID_OPERATION, "0: Undefined variable: %s",
2225 dst->name);
2226 return 1;
2227 }
2228
2229 switch (dst->type) {
2230 case vt_output:
2231 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002232 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002233 break;
2234
2235 case vt_temp:
2236 *File = PROGRAM_TEMPORARY;
2237 *Index = dst->temp_binding;
2238 break;
2239
2240 /* If the var type is not vt_output or vt_temp, no go */
2241 default:
2242 _mesa_set_program_error (ctx, Program->Position,
2243 "Destination register is read only");
2244 _mesa_error (ctx, GL_INVALID_OPERATION,
2245 "Destination register is read only: %s",
2246 dst->name);
2247 return 1;
2248 }
2249 break;
2250
2251 default:
2252 _mesa_set_program_error (ctx, Program->Position,
2253 "Unexpected opcode in parse_masked_dst_reg()");
2254 _mesa_error (ctx, GL_INVALID_OPERATION,
2255 "Unexpected opcode in parse_masked_dst_reg()");
2256 return 1;
2257 }
2258
2259
2260 /* Position invariance test */
2261 /* This test is done now in syntax portion - when position invariance OPTION
2262 is specified, "result.position" rule is disabled so there is no way
2263 to write the position
2264 */
2265 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2266 (*Index == 0)) {
2267 _mesa_set_program_error (ctx, Program->Position,
2268 "Vertex program specified position invariance and wrote vertex position");
2269 _mesa_error (ctx, GL_INVALID_OPERATION,
2270 "Vertex program specified position invariance and wrote vertex position");
2271 }*/
2272
2273 /* And then the mask.
2274 * w,a -> bit 0
2275 * z,b -> bit 1
2276 * y,g -> bit 2
2277 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002278 *
2279 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002280 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002281 tmp = (GLint) *(*inst)++;
2282 *WriteMask = (((tmp>>3) & 0x1) |
2283 ((tmp>>1) & 0x2) |
2284 ((tmp<<1) & 0x4) |
2285 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002286
2287 return 0;
2288}
2289
2290
2291/**
2292 * Handle the parsing of a address register
2293 *
2294 * \param Index - The register index we write to
2295 *
2296 * \return 0 on sucess, 1 on error
2297 */
2298static GLuint
2299parse_address_reg (GLcontext * ctx, GLubyte ** inst,
2300 struct var_cache **vc_head,
2301 struct arb_program *Program, GLint * Index)
2302{
2303 struct var_cache *dst;
2304 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002305
2306 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002307
2308 dst = parse_string (inst, vc_head, Program, &result);
2309 Program->Position = parse_position (inst);
2310
2311 /* If the name has never been added to our symbol table, we're hosed */
2312 if (!result) {
2313 _mesa_set_program_error (ctx, Program->Position, "Undefined variable");
2314 _mesa_error (ctx, GL_INVALID_OPERATION, "Undefined variable: %s",
2315 dst->name);
2316 return 1;
2317 }
2318
2319 if (dst->type != vt_address) {
2320 _mesa_set_program_error (ctx, Program->Position,
2321 "Variable is not of type ADDRESS");
2322 _mesa_error (ctx, GL_INVALID_OPERATION,
2323 "Variable: %s is not of type ADDRESS", dst->name);
2324 return 1;
2325 }
2326
2327 return 0;
2328}
2329
Brian Paul8e8fa632005-07-01 02:03:33 +00002330#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002331/**
2332 * Handle the parsing out of a masked address register
2333 *
2334 * \param Index - The register index we write to
2335 * \param WriteMask - The mask controlling which components we write (1->write)
2336 *
2337 * \return 0 on sucess, 1 on error
2338 */
2339static GLuint
2340parse_masked_address_reg (GLcontext * ctx, GLubyte ** inst,
2341 struct var_cache **vc_head,
2342 struct arb_program *Program, GLint * Index,
2343 GLboolean * WriteMask)
2344{
2345 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2346 return 1;
2347
2348 /* This should be 0x8 */
2349 (*inst)++;
2350
2351 /* Writemask of .x is implied */
2352 WriteMask[0] = 1;
2353 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2354
2355 return 0;
2356}
Brian Paul8e8fa632005-07-01 02:03:33 +00002357#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002358
2359/**
2360 * Parse out a swizzle mask.
2361 *
Brian Paul32df89e2005-10-29 18:26:43 +00002362 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002363 *
2364 * The len parameter allows us to grab 4 components for a vector
2365 * swizzle, or just 1 component for a scalar src register selection
2366 */
Brian Paul32df89e2005-10-29 18:26:43 +00002367static void
2368parse_swizzle_mask(GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002369{
Brian Paul32df89e2005-10-29 18:26:43 +00002370 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002371
Brian Paul32df89e2005-10-29 18:26:43 +00002372 for (i = 0; i < 4; i++)
2373 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002374
Brian Paul32df89e2005-10-29 18:26:43 +00002375 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002376 switch (*(*inst)++) {
2377 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002378 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002379 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002380 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002381 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002382 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002383 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002384 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002385 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002386 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002387 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002388 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002389 default:
2390 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2391 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002392 }
2393 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002394}
2395
Jouk Jansen40322e12004-04-05 08:50:36 +00002396
Brian Paul32df89e2005-10-29 18:26:43 +00002397/**
2398 * Parse an extended swizzle mask which is a sequence of
2399 * four x/y/z/w/0/1 tokens.
2400 * \return swizzle four swizzle values
2401 * \return negateMask four element bitfield
2402 */
2403static void
2404parse_extended_swizzle_mask(GLubyte **inst, GLubyte swizzle[4],
2405 GLubyte *negateMask)
2406{
2407 GLint i;
2408
2409 *negateMask = 0x0;
2410 for (i = 0; i < 4; i++) {
2411 GLubyte swz;
2412 if (parse_sign(inst) == -1)
2413 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002414
2415 swz = *(*inst)++;
2416
2417 switch (swz) {
2418 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002419 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002420 break;
2421 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002422 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002423 break;
2424 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002425 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002426 break;
2427 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002428 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002429 break;
2430 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002431 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002432 break;
2433 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002434 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002435 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002436 default:
2437 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2438 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002439 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002440 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002441}
2442
2443
2444static GLuint
2445parse_src_reg (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002446 struct arb_program *Program,
2447 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002448 GLboolean *IsRelOffset )
2449{
2450 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002451 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002452 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002453
Keith Whitwell7c26b612005-04-21 14:46:57 +00002454 *IsRelOffset = 0;
2455
Jouk Jansen40322e12004-04-05 08:50:36 +00002456 /* And the binding for the src */
2457 switch (*(*inst)++) {
2458 case REGISTER_ATTRIB:
2459 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002460 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002461 return 1;
2462 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002463 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002464
2465 /* We need to insert a dummy variable into the var_cache so we can
2466 * catch generic vertex attrib aliasing errors
2467 */
2468 var_cache_create(&src);
2469 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002470 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002471 src->attrib_binding = binding;
2472 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002473 var_cache_append(vc_head, src);
2474 if (generic_attrib_check(*vc_head)) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002475 const char *msg = "Cannot use both a generic vertex attribute "
2476 "and a specific attribute of the same type";
2477 _mesa_set_program_error (ctx, Program->Position, msg);
2478 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002479 return 1;
2480 }
2481 break;
2482
2483 case REGISTER_PARAM:
2484 switch (**inst) {
2485 case PARAM_ARRAY_ELEMENT:
2486 (*inst)++;
2487 src = parse_string (inst, vc_head, Program, &found);
2488 Program->Position = parse_position (inst);
2489
2490 if (!found) {
2491 _mesa_set_program_error (ctx, Program->Position,
2492 "2: Undefined variable");
2493 _mesa_error (ctx, GL_INVALID_OPERATION,
2494 "2: Undefined variable: %s", src->name);
2495 return 1;
2496 }
2497
Brian Paul95801792005-12-06 15:41:43 +00002498 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002499
2500 switch (*(*inst)++) {
2501 case ARRAY_INDEX_ABSOLUTE:
2502 offset = parse_integer (inst, Program);
2503
2504 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002505 || (offset >= (int)src->param_binding_length)) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002506 _mesa_set_program_error (ctx, Program->Position,
2507 "Index out of range");
2508 _mesa_error (ctx, GL_INVALID_OPERATION,
2509 "Index %d out of range for %s", offset,
2510 src->name);
2511 return 1;
2512 }
2513
2514 *Index = src->param_binding_begin + offset;
2515 break;
2516
2517 case ARRAY_INDEX_RELATIVE:
2518 {
2519 GLint addr_reg_idx, rel_off;
2520
2521 /* First, grab the address regiseter */
2522 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2523 return 1;
2524
2525 /* And the .x */
2526 ((*inst)++);
2527 ((*inst)++);
2528 ((*inst)++);
2529 ((*inst)++);
2530
2531 /* Then the relative offset */
2532 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2533
2534 /* And store it properly */
2535 *Index = src->param_binding_begin + rel_off;
2536 *IsRelOffset = 1;
2537 }
2538 break;
2539 }
2540 break;
2541
2542 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002543 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2544 return 1;
2545
Brian Paul95801792005-12-06 15:41:43 +00002546 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002547 *Index = src->param_binding_begin;
2548 break;
2549 }
2550 break;
2551
2552 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002553 src = parse_string (inst, vc_head, Program, &found);
2554 Program->Position = parse_position (inst);
2555
2556 /* If the name has never been added to our symbol table, we're hosed */
2557 if (!found) {
2558 _mesa_set_program_error (ctx, Program->Position,
2559 "3: Undefined variable");
2560 _mesa_error (ctx, GL_INVALID_OPERATION, "3: Undefined variable: %s",
2561 src->name);
2562 return 1;
2563 }
2564
2565 switch (src->type) {
2566 case vt_attrib:
2567 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002568 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002569 break;
2570
2571 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2572 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002573 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002574 *Index = src->param_binding_begin;
2575 break;
2576
2577 case vt_temp:
2578 *File = PROGRAM_TEMPORARY;
2579 *Index = src->temp_binding;
2580 break;
2581
2582 /* If the var type is vt_output no go */
2583 default:
2584 _mesa_set_program_error (ctx, Program->Position,
2585 "destination register is read only");
2586 _mesa_error (ctx, GL_INVALID_OPERATION,
2587 "destination register is read only: %s",
2588 src->name);
2589 return 1;
2590 }
2591 break;
2592
2593 default:
2594 _mesa_set_program_error (ctx, Program->Position,
2595 "Unknown token in parse_src_reg");
2596 _mesa_error (ctx, GL_INVALID_OPERATION,
2597 "Unknown token in parse_src_reg");
2598 return 1;
2599 }
2600
2601 return 0;
2602}
2603
2604/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002605 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002606 */
2607static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00002608parse_fp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
2609 struct var_cache **vc_head,
2610 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002611 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002612{
Brian Paul7aebaf32005-10-30 21:23:23 +00002613 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002614 GLint index;
2615 GLboolean negate;
2616 GLubyte swizzle[4];
2617 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002618
Jouk Jansen40322e12004-04-05 08:50:36 +00002619 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002620 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002621
2622 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002623 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002624 return 1;
2625
2626 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002627 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002628
Brian Paul32df89e2005-10-29 18:26:43 +00002629 reg->File = file;
2630 reg->Index = index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002631 reg->Abs = 0; /* NV only */
2632 reg->NegateAbs = 0; /* NV only */
Brian Paul32df89e2005-10-29 18:26:43 +00002633 reg->NegateBase = negate;
2634 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002635 return 0;
2636}
2637
Jouk Jansen40322e12004-04-05 08:50:36 +00002638
Keith Whitwell7c26b612005-04-21 14:46:57 +00002639static GLuint
2640parse_fp_dst_reg(GLcontext * ctx, GLubyte ** inst,
2641 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002642 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002643{
Brian Paul7aebaf32005-10-30 21:23:23 +00002644 GLint mask;
2645 GLuint idx;
2646 enum register_file file;
2647
Keith Whitwell7c26b612005-04-21 14:46:57 +00002648 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002649 return 1;
2650
Keith Whitwell7c26b612005-04-21 14:46:57 +00002651 reg->CondMask = 0; /* NV only */
2652 reg->CondSwizzle = 0; /* NV only */
2653 reg->File = file;
2654 reg->Index = idx;
2655 reg->WriteMask = mask;
2656 return 0;
2657}
2658
2659
Brian Paul7aebaf32005-10-30 21:23:23 +00002660/**
2661 * Parse fragment program scalar src register.
2662 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002663static GLuint
2664parse_fp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002665 struct var_cache **vc_head,
2666 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002667 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002668{
Brian Paul7aebaf32005-10-30 21:23:23 +00002669 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002670 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002671 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002672 GLubyte Swizzle[4];
2673 GLboolean IsRelOffset;
2674
2675 /* Grab the sign */
2676 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2677
2678 /* And the src reg */
2679 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2680 return 1;
2681
2682 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002683 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002684
Keith Whitwell7c26b612005-04-21 14:46:57 +00002685 reg->File = File;
2686 reg->Index = Index;
2687 reg->Abs = 0; /* NV only */
2688 reg->NegateAbs = 0; /* NV only */
2689 reg->NegateBase = Negate;
2690 reg->Swizzle = (Swizzle[0] << 0);
2691
Jouk Jansen40322e12004-04-05 08:50:36 +00002692 return 0;
2693}
2694
Keith Whitwell7c26b612005-04-21 14:46:57 +00002695
Jouk Jansen40322e12004-04-05 08:50:36 +00002696/**
2697 * This is a big mother that handles getting opcodes into the instruction
2698 * and handling the src & dst registers for fragment program instructions
2699 */
2700static GLuint
2701parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2702 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002703 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002704{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002705 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002706 GLuint texcoord;
2707 GLubyte instClass, type, code;
2708 GLboolean rel;
2709
Brian Paul7e807512005-11-05 17:10:45 +00002710 _mesa_init_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002711
2712 /* Record the position in the program string for debugging */
2713 fp->StringPos = Program->Position;
2714
2715 /* OP_ALU_INST or OP_TEX_INST */
2716 instClass = *(*inst)++;
2717
2718 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2719 * OP_TEX_{SAMPLE, KIL}
2720 */
2721 type = *(*inst)++;
2722
2723 /* The actual opcode name */
2724 code = *(*inst)++;
2725
2726 /* Increment the correct count */
2727 switch (instClass) {
2728 case OP_ALU_INST:
2729 Program->NumAluInstructions++;
2730 break;
2731 case OP_TEX_INST:
2732 Program->NumTexInstructions++;
2733 break;
2734 }
2735
Jouk Jansen40322e12004-04-05 08:50:36 +00002736 switch (type) {
2737 case OP_ALU_VECTOR:
2738 switch (code) {
2739 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002740 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002741 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002742 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002743 break;
2744
2745 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002746 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002748 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002749 break;
2750
2751 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002752 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002754 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002755 break;
2756
2757 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002758 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002759 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002760 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002761 break;
2762
2763 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002764 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002765 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002766 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 break;
2768 }
2769
Keith Whitwell7c26b612005-04-21 14:46:57 +00002770 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002771 return 1;
2772
Keith Whitwell7c26b612005-04-21 14:46:57 +00002773 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002774 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 break;
2776
2777 case OP_ALU_SCALAR:
2778 switch (code) {
2779 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002780 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002781 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002782 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 break;
2784
2785 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002786 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002788 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002789 break;
2790
2791 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002792 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002794 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002795 break;
2796
2797 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002798 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002799 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002800 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002801 break;
2802
2803 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002804 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002805 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002806 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 break;
2808
2809 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002810 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002811 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002812 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 break;
2814
2815 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002816 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002817 case OP_SCS:
2818
Brian Paul7e807512005-11-05 17:10:45 +00002819 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002820 break;
2821 }
2822
Keith Whitwell7c26b612005-04-21 14:46:57 +00002823 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002824 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002825
2826 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002827 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002828 break;
2829
2830 case OP_ALU_BINSC:
2831 switch (code) {
2832 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002833 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002834 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002835 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002836 break;
2837 }
2838
Keith Whitwell7c26b612005-04-21 14:46:57 +00002839 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002840 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002841
Jouk Jansen40322e12004-04-05 08:50:36 +00002842 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002843 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 }
2846 break;
2847
2848
2849 case OP_ALU_BIN:
2850 switch (code) {
2851 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002852 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002854 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 break;
2856
2857 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002858 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002859 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002860 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002861 break;
2862
2863 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002864 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002865 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002866 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002867 break;
2868
2869 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002870 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002871 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002872 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002873 break;
2874
2875 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002876 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002877 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002878 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002879 break;
2880
2881 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002882 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002884 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 break;
2886
2887 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002888 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002890 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 break;
2892
2893 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002894 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002895 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002896 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 break;
2898
2899 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002900 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002901 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002902 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002903 break;
2904
2905 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002906 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002907 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002908 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002909 break;
2910
2911 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002912 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002913 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002914 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002915 break;
2916
2917 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002918 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002919 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002920 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002921 break;
2922 }
2923
Keith Whitwell7c26b612005-04-21 14:46:57 +00002924 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002925 return 1;
2926 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002927 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2928 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002929 }
2930 break;
2931
2932 case OP_ALU_TRI:
2933 switch (code) {
2934 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002935 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002937 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002938 break;
2939
2940 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002941 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002942 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002943 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002944 break;
2945
2946 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002947 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002948 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002949 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002950 break;
2951 }
2952
Keith Whitwell7c26b612005-04-21 14:46:57 +00002953 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002954 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002955
Jouk Jansen40322e12004-04-05 08:50:36 +00002956 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002957 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2958 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002959 }
2960 break;
2961
2962 case OP_ALU_SWZ:
2963 switch (code) {
2964 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002965 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002966 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002967 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002968 break;
2969 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002970 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002971 return 1;
2972
Keith Whitwell7c26b612005-04-21 14:46:57 +00002973 {
Brian Paul32df89e2005-10-29 18:26:43 +00002974 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002975 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002976 enum register_file file;
2977 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002978
Brian Paul32df89e2005-10-29 18:26:43 +00002979 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002980 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002981 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2982 fp->SrcReg[0].File = file;
2983 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002984 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002985 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2986 swizzle[1],
2987 swizzle[2],
2988 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002989 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002990 break;
2991
2992 case OP_TEX_SAMPLE:
2993 switch (code) {
2994 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002995 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002996 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002997 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002998 break;
2999
3000 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003001 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003002 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003003 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003004 break;
3005
3006 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003007 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003008 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003009 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003010 break;
3011 }
3012
Keith Whitwell7c26b612005-04-21 14:46:57 +00003013 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003014 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003015
3016 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003017 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003018
3019 /* texImageUnit */
3020 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3021 return 1;
3022 fp->TexSrcUnit = texcoord;
3023
3024 /* texTarget */
3025 switch (*(*inst)++) {
3026 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003027 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003028 break;
3029 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003030 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003031 break;
3032 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003033 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003034 break;
3035 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003036 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003037 break;
3038 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003039 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003040 break;
3041 case TEXTARGET_SHADOW1D:
3042 case TEXTARGET_SHADOW2D:
3043 case TEXTARGET_SHADOWRECT:
3044 /* TODO ARB_fragment_program_shadow code */
3045 break;
3046 }
Brian Paul7e807512005-11-05 17:10:45 +00003047 Program->TexturesUsed[texcoord] |= (1<<fp->TexSrcTarget);
Jouk Jansen40322e12004-04-05 08:50:36 +00003048 break;
3049
3050 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003051 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003052 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003053 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003054 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003055 break;
3056 }
3057
3058 return 0;
3059}
3060
Keith Whitwell7c26b612005-04-21 14:46:57 +00003061static GLuint
3062parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3063 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003064 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003065{
Brian Paul7aebaf32005-10-30 21:23:23 +00003066 GLint mask;
3067 GLuint idx;
3068 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003069
3070 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3071 return 1;
3072
3073 reg->File = file;
3074 reg->Index = idx;
3075 reg->WriteMask = mask;
3076 return 0;
3077}
3078
3079/**
3080 * Handle the parsing out of a masked address register
3081 *
3082 * \param Index - The register index we write to
3083 * \param WriteMask - The mask controlling which components we write (1->write)
3084 *
3085 * \return 0 on sucess, 1 on error
3086 */
3087static GLuint
3088parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3089 struct var_cache **vc_head,
3090 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003091 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003092{
3093 GLint idx;
3094
3095 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3096 return 1;
3097
3098 /* This should be 0x8 */
3099 (*inst)++;
3100
3101 reg->File = PROGRAM_ADDRESS;
3102 reg->Index = idx;
3103
3104 /* Writemask of .x is implied */
3105 reg->WriteMask = 0x1;
3106 return 0;
3107}
3108
3109/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003110 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003111 */
3112static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003113parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3114 struct var_cache **vc_head,
3115 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003116 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003117{
Brian Paul7aebaf32005-10-30 21:23:23 +00003118 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003119 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003120 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003121 GLubyte swizzle[4];
3122 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003123
3124 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003125 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003126
3127 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003128 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003129 return 1;
3130
3131 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003132 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003133
Brian Paul32df89e2005-10-29 18:26:43 +00003134 reg->File = file;
3135 reg->Index = index;
3136 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3137 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003138 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003139 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003140 return 0;
3141}
3142
3143
3144static GLuint
3145parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003146 struct var_cache **vc_head,
3147 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003148 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003149{
Brian Paul7aebaf32005-10-30 21:23:23 +00003150 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003151 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003152 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003153 GLubyte Swizzle[4];
3154 GLboolean IsRelOffset;
3155
3156 /* Grab the sign */
3157 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3158
3159 /* And the src reg */
3160 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3161 return 1;
3162
3163 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003164 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003165
3166 reg->File = File;
3167 reg->Index = Index;
3168 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003169 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003170 reg->RelAddr = IsRelOffset;
3171 return 0;
3172}
3173
3174
Jouk Jansen40322e12004-04-05 08:50:36 +00003175/**
3176 * This is a big mother that handles getting opcodes into the instruction
3177 * and handling the src & dst registers for vertex program instructions
3178 */
3179static GLuint
3180parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3181 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003182 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003183{
3184 GLint a;
3185 GLubyte type, code;
3186
3187 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3188 type = *(*inst)++;
3189
3190 /* The actual opcode name */
3191 code = *(*inst)++;
3192
Brian Paul7e807512005-11-05 17:10:45 +00003193 _mesa_init_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003194 /* Record the position in the program string for debugging */
3195 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003196
3197 switch (type) {
3198 /* XXX: */
3199 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003200 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003201
3202 /* Remember to set SrcReg.RelAddr; */
3203
3204 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003205 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003206 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003207
Jouk Jansen40322e12004-04-05 08:50:36 +00003208 vp->DstReg.File = PROGRAM_ADDRESS;
3209
3210 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003211 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003212 return 1;
3213
3214 break;
3215
3216 case OP_ALU_VECTOR:
3217 switch (code) {
3218 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003219 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003220 break;
3221 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003222 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003223 break;
3224 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003225 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003226 break;
3227 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003228 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003229 break;
3230 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003231 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003232 break;
3233 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003234
3235 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003236 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003237
3238 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003239 return 1;
3240 break;
3241
3242 case OP_ALU_SCALAR:
3243 switch (code) {
3244 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003245 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003246 break;
3247 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003248 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003249 break;
3250 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003251 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003252 break;
3253 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003254 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003255 break;
3256 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003257 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003258 break;
3259 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003260 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003261 break;
3262 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003263 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003264 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003265
3266 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003267 return 1;
3268 break;
3269
3270 case OP_ALU_BINSC:
3271 switch (code) {
3272 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003273 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003274 break;
3275 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003276 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003277 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003278
Jouk Jansen40322e12004-04-05 08:50:36 +00003279 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003280 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003281 return 1;
3282 }
3283 break;
3284
3285 case OP_ALU_BIN:
3286 switch (code) {
3287 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003288 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003289 break;
3290 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003291 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003292 break;
3293 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003294 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003295 break;
3296 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003297 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003298 break;
3299 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003300 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003301 break;
3302 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003303 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003304 break;
3305 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003306 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003307 break;
3308 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003309 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003310 break;
3311 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003312 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003313 break;
3314 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003315 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003316 break;
3317 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003318 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003319 break;
3320 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003321 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003322 break;
3323 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003324 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003325 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003326
Jouk Jansen40322e12004-04-05 08:50:36 +00003327 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003328 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003329 return 1;
3330 }
3331 break;
3332
3333 case OP_ALU_TRI:
3334 switch (code) {
3335 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003336 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003337 break;
3338 }
3339
Keith Whitwell7c26b612005-04-21 14:46:57 +00003340 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003341 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003342
Jouk Jansen40322e12004-04-05 08:50:36 +00003343 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003344 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003345 return 1;
3346 }
3347 break;
3348
3349 case OP_ALU_SWZ:
3350 switch (code) {
3351 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003352 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003353 break;
3354 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003355 {
Brian Paul32df89e2005-10-29 18:26:43 +00003356 GLubyte swizzle[4];
3357 GLubyte negateMask;
3358 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003359 enum register_file file;
3360 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003361
Keith Whitwell7c26b612005-04-21 14:46:57 +00003362 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3363 return 1;
3364
Brian Paul32df89e2005-10-29 18:26:43 +00003365 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003366 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003367 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3368 vp->SrcReg[0].File = file;
3369 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003370 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003371 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3372 swizzle[1],
3373 swizzle[2],
3374 swizzle[3]);
3375 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003376 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003377 break;
3378 }
3379 return 0;
3380}
3381
3382#if DEBUG_PARSING
3383
3384static GLvoid
3385print_state_token (GLint token)
3386{
3387 switch (token) {
3388 case STATE_MATERIAL:
3389 fprintf (stderr, "STATE_MATERIAL ");
3390 break;
3391 case STATE_LIGHT:
3392 fprintf (stderr, "STATE_LIGHT ");
3393 break;
3394
3395 case STATE_LIGHTMODEL_AMBIENT:
3396 fprintf (stderr, "STATE_AMBIENT ");
3397 break;
3398
3399 case STATE_LIGHTMODEL_SCENECOLOR:
3400 fprintf (stderr, "STATE_SCENECOLOR ");
3401 break;
3402
3403 case STATE_LIGHTPROD:
3404 fprintf (stderr, "STATE_LIGHTPROD ");
3405 break;
3406
3407 case STATE_TEXGEN:
3408 fprintf (stderr, "STATE_TEXGEN ");
3409 break;
3410
3411 case STATE_FOG_COLOR:
3412 fprintf (stderr, "STATE_FOG_COLOR ");
3413 break;
3414
3415 case STATE_FOG_PARAMS:
3416 fprintf (stderr, "STATE_FOG_PARAMS ");
3417 break;
3418
3419 case STATE_CLIPPLANE:
3420 fprintf (stderr, "STATE_CLIPPLANE ");
3421 break;
3422
3423 case STATE_POINT_SIZE:
3424 fprintf (stderr, "STATE_POINT_SIZE ");
3425 break;
3426
3427 case STATE_POINT_ATTENUATION:
3428 fprintf (stderr, "STATE_ATTENUATION ");
3429 break;
3430
3431 case STATE_MATRIX:
3432 fprintf (stderr, "STATE_MATRIX ");
3433 break;
3434
3435 case STATE_MODELVIEW:
3436 fprintf (stderr, "STATE_MODELVIEW ");
3437 break;
3438
3439 case STATE_PROJECTION:
3440 fprintf (stderr, "STATE_PROJECTION ");
3441 break;
3442
3443 case STATE_MVP:
3444 fprintf (stderr, "STATE_MVP ");
3445 break;
3446
3447 case STATE_TEXTURE:
3448 fprintf (stderr, "STATE_TEXTURE ");
3449 break;
3450
3451 case STATE_PROGRAM:
3452 fprintf (stderr, "STATE_PROGRAM ");
3453 break;
3454
3455 case STATE_MATRIX_INVERSE:
3456 fprintf (stderr, "STATE_INVERSE ");
3457 break;
3458
3459 case STATE_MATRIX_TRANSPOSE:
3460 fprintf (stderr, "STATE_TRANSPOSE ");
3461 break;
3462
3463 case STATE_MATRIX_INVTRANS:
3464 fprintf (stderr, "STATE_INVTRANS ");
3465 break;
3466
3467 case STATE_AMBIENT:
3468 fprintf (stderr, "STATE_AMBIENT ");
3469 break;
3470
3471 case STATE_DIFFUSE:
3472 fprintf (stderr, "STATE_DIFFUSE ");
3473 break;
3474
3475 case STATE_SPECULAR:
3476 fprintf (stderr, "STATE_SPECULAR ");
3477 break;
3478
3479 case STATE_EMISSION:
3480 fprintf (stderr, "STATE_EMISSION ");
3481 break;
3482
3483 case STATE_SHININESS:
3484 fprintf (stderr, "STATE_SHININESS ");
3485 break;
3486
3487 case STATE_HALF:
3488 fprintf (stderr, "STATE_HALF ");
3489 break;
3490
3491 case STATE_POSITION:
3492 fprintf (stderr, "STATE_POSITION ");
3493 break;
3494
3495 case STATE_ATTENUATION:
3496 fprintf (stderr, "STATE_ATTENUATION ");
3497 break;
3498
3499 case STATE_SPOT_DIRECTION:
3500 fprintf (stderr, "STATE_DIRECTION ");
3501 break;
3502
3503 case STATE_TEXGEN_EYE_S:
3504 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3505 break;
3506
3507 case STATE_TEXGEN_EYE_T:
3508 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3509 break;
3510
3511 case STATE_TEXGEN_EYE_R:
3512 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3513 break;
3514
3515 case STATE_TEXGEN_EYE_Q:
3516 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3517 break;
3518
3519 case STATE_TEXGEN_OBJECT_S:
3520 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3521 break;
3522
3523 case STATE_TEXGEN_OBJECT_T:
3524 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3525 break;
3526
3527 case STATE_TEXGEN_OBJECT_R:
3528 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3529 break;
3530
3531 case STATE_TEXGEN_OBJECT_Q:
3532 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3533 break;
3534
3535 case STATE_TEXENV_COLOR:
3536 fprintf (stderr, "STATE_TEXENV_COLOR ");
3537 break;
3538
3539 case STATE_DEPTH_RANGE:
3540 fprintf (stderr, "STATE_DEPTH_RANGE ");
3541 break;
3542
3543 case STATE_VERTEX_PROGRAM:
3544 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3545 break;
3546
3547 case STATE_FRAGMENT_PROGRAM:
3548 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3549 break;
3550
3551 case STATE_ENV:
3552 fprintf (stderr, "STATE_ENV ");
3553 break;
3554
3555 case STATE_LOCAL:
3556 fprintf (stderr, "STATE_LOCAL ");
3557 break;
3558
3559 }
3560 fprintf (stderr, "[%d] ", token);
3561}
3562
3563
3564static GLvoid
3565debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3566 struct arb_program *Program)
3567{
3568 struct var_cache *vc;
3569 GLint a, b;
3570
3571 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3572
3573 /* First of all, print out the contents of the var_cache */
3574 vc = vc_head;
3575 while (vc) {
3576 fprintf (stderr, "[%x]\n", vc);
3577 switch (vc->type) {
3578 case vt_none:
3579 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3580 break;
3581 case vt_attrib:
3582 fprintf (stderr, "ATTRIB %s\n", vc->name);
3583 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3584 break;
3585 case vt_param:
3586 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3587 vc->param_binding_begin, vc->param_binding_length);
3588 b = vc->param_binding_begin;
3589 for (a = 0; a < vc->param_binding_length; a++) {
3590 fprintf (stderr, "%s\n",
3591 Program->Parameters->Parameters[a + b].Name);
3592 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3593 print_state_token (Program->Parameters->Parameters[a + b].
3594 StateIndexes[0]);
3595 print_state_token (Program->Parameters->Parameters[a + b].
3596 StateIndexes[1]);
3597 print_state_token (Program->Parameters->Parameters[a + b].
3598 StateIndexes[2]);
3599 print_state_token (Program->Parameters->Parameters[a + b].
3600 StateIndexes[3]);
3601 print_state_token (Program->Parameters->Parameters[a + b].
3602 StateIndexes[4]);
3603 print_state_token (Program->Parameters->Parameters[a + b].
3604 StateIndexes[5]);
3605 }
3606 else
3607 fprintf (stderr, "%f %f %f %f\n",
3608 Program->Parameters->Parameters[a + b].Values[0],
3609 Program->Parameters->Parameters[a + b].Values[1],
3610 Program->Parameters->Parameters[a + b].Values[2],
3611 Program->Parameters->Parameters[a + b].Values[3]);
3612 }
3613 break;
3614 case vt_temp:
3615 fprintf (stderr, "TEMP %s\n", vc->name);
3616 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3617 break;
3618 case vt_output:
3619 fprintf (stderr, "OUTPUT %s\n", vc->name);
3620 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3621 break;
3622 case vt_alias:
3623 fprintf (stderr, "ALIAS %s\n", vc->name);
3624 fprintf (stderr, " binding: 0x%x (%s)\n",
3625 vc->alias_binding, vc->alias_binding->name);
3626 break;
3627 }
3628 vc = vc->next;
3629 }
3630}
3631
Brian Paul72030e02005-11-03 03:30:34 +00003632#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003633
3634
3635/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003636 * The main loop for parsing a fragment or vertex program
3637 *
Brian Paul72030e02005-11-03 03:30:34 +00003638 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003639 */
Brian Paul72030e02005-11-03 03:30:34 +00003640static GLint
Brian Paul8c41a142005-11-19 15:36:28 +00003641parse_instructions(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
Brian Paul45703642005-10-29 15:52:31 +00003642 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003643{
Brian Paul72030e02005-11-03 03:30:34 +00003644 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3645 ? ctx->Const.FragmentProgram.MaxInstructions
3646 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003647 GLint err = 0;
3648
Brian Paul72030e02005-11-03 03:30:34 +00003649 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3650
Jouk Jansen40322e12004-04-05 08:50:36 +00003651 Program->MajorVersion = (GLuint) * inst++;
3652 Program->MinorVersion = (GLuint) * inst++;
3653
3654 while (*inst != END) {
3655 switch (*inst++) {
3656
3657 case OPTION:
3658 switch (*inst++) {
3659 case ARB_PRECISION_HINT_FASTEST:
3660 Program->PrecisionOption = GL_FASTEST;
3661 break;
3662
3663 case ARB_PRECISION_HINT_NICEST:
3664 Program->PrecisionOption = GL_NICEST;
3665 break;
3666
3667 case ARB_FOG_EXP:
3668 Program->FogOption = GL_EXP;
3669 break;
3670
3671 case ARB_FOG_EXP2:
3672 Program->FogOption = GL_EXP2;
3673 break;
3674
3675 case ARB_FOG_LINEAR:
3676 Program->FogOption = GL_LINEAR;
3677 break;
3678
3679 case ARB_POSITION_INVARIANT:
3680 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003681 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003682 break;
3683
3684 case ARB_FRAGMENT_PROGRAM_SHADOW:
3685 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3686 /* TODO ARB_fragment_program_shadow code */
3687 }
3688 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003689
3690 case ARB_DRAW_BUFFERS:
3691 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3692 /* do nothing for now */
3693 }
3694 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003695 }
3696 break;
3697
3698 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003699 /* check length */
3700 if (Program->Base.NumInstructions + 1 >= maxInst) {
3701 const char *msg = "Max instruction count exceeded";
3702 _mesa_set_program_error(ctx, Program->Position, msg);
3703 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3704 return 1;
3705 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003706 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003707 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003708 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003709 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003710 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003711 }
3712 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003713 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003714 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003715 }
3716
Brian Paul72030e02005-11-03 03:30:34 +00003717 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 Program->Base.NumInstructions++;
3719 break;
3720
3721 case DECLARATION:
3722 err = parse_declaration (ctx, &inst, vc_head, Program);
3723 break;
3724
3725 default:
3726 break;
3727 }
3728
3729 if (err)
3730 break;
3731 }
3732
3733 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003734 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003735 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul8c41a142005-11-19 15:36:28 +00003736 _mesa_init_instruction(Program->Base.Instructions + numInst);
3737 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003738 /* YYY Wrong Position in program, whatever, at least not random -> crash
3739 Program->Position = parse_position (&inst);
3740 */
Brian Paul8c41a142005-11-19 15:36:28 +00003741 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003742 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003743 Program->Base.NumInstructions++;
3744
Brian Paul05051032005-11-01 04:36:33 +00003745 /*
3746 * Initialize native counts to logical counts. The device driver may
3747 * change them if program is translated into a hardware program.
3748 */
3749 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3750 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3751 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3752 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3753 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003754
Jouk Jansen40322e12004-04-05 08:50:36 +00003755 return err;
3756}
3757
Brian Paul7aebaf32005-10-30 21:23:23 +00003758
Jouk Jansen40322e12004-04-05 08:50:36 +00003759/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003760__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003761#include "grammar_syn.h"
3762;
3763
Brian Paul7aebaf32005-10-30 21:23:23 +00003764
Brian Paul8c41a142005-11-19 15:36:28 +00003765/**
3766 * Set a grammar parameter.
3767 * \param name the grammar parameter
3768 * \param value the new parameter value
3769 * \return 0 if OK, 1 if error
3770 */
3771static int
3772set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003773{
3774 char error_msg[300];
3775 GLint error_pos;
3776
Brian Paul8c41a142005-11-19 15:36:28 +00003777 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003778 return 0;
3779
3780 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3781 _mesa_set_program_error (ctx, error_pos, error_msg);
3782 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3783 return 1;
3784}
3785
Brian Paul8c41a142005-11-19 15:36:28 +00003786
3787/**
3788 * Enable support for the given language option in the parser.
3789 * \return 1 if OK, 0 if error
3790 */
3791static int
3792enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003793{
Brian Paul8c41a142005-11-19 15:36:28 +00003794 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003795}
3796
Brian Paul8c41a142005-11-19 15:36:28 +00003797
3798/**
3799 * Enable parser extensions based on which OpenGL extensions are supported
3800 * by this rendering context.
3801 *
3802 * \return GL_TRUE if OK, GL_FALSE if error.
3803 */
3804static GLboolean
3805enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003806{
Brian Paul8c41a142005-11-19 15:36:28 +00003807#if 0
3808 /* These are not supported at this time */
3809 if ((ctx->Extensions.ARB_vertex_blend ||
3810 ctx->Extensions.EXT_vertex_weighting)
3811 && !enable_ext(ctx, id, "point_parameters"))
3812 return GL_FALSE;
3813 if (ctx->Extensions.ARB_matrix_palette
3814 && !enable_ext(ctx, id, "matrix_palette"))
3815 return GL_FALSE;
3816 if (ctx->Extensions.ARB_fragment_program_shadow
3817 && !enable_ext(ctx, id, "fragment_program_shadow"))
3818 return GL_FALSE;
3819#endif
3820 if (ctx->Extensions.EXT_point_parameters
3821 && !enable_ext(ctx, id, "point_parameters"))
3822 return GL_FALSE;
3823 if (ctx->Extensions.EXT_secondary_color
3824 && !enable_ext(ctx, id, "secondary_color"))
3825 return GL_FALSE;
3826 if (ctx->Extensions.EXT_fog_coord
3827 && !enable_ext(ctx, id, "fog_coord"))
3828 return GL_FALSE;
3829 if (ctx->Extensions.NV_texture_rectangle
3830 && !enable_ext(ctx, id, "texture_rectangle"))
3831 return GL_FALSE;
3832 if (ctx->Extensions.ARB_draw_buffers
3833 && !enable_ext(ctx, id, "draw_buffers"))
3834 return GL_FALSE;
3835
3836 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003837}
3838
Brian Paul8c41a142005-11-19 15:36:28 +00003839
Jouk Jansen40322e12004-04-05 08:50:36 +00003840/**
3841 * This kicks everything off.
3842 *
3843 * \param ctx - The GL Context
3844 * \param str - The program string
3845 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003846 * \param program - The arb_program struct to return all the parsed info in
3847 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003848 */
Brian Paul8c41a142005-11-19 15:36:28 +00003849static GLboolean
3850_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3851 const GLubyte *str, GLsizei len,
3852 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003853{
3854 GLint a, err, error_pos;
3855 char error_msg[300];
3856 GLuint parsed_len;
3857 struct var_cache *vc_head;
3858 grammar arbprogram_syn_id;
3859 GLubyte *parsed, *inst;
3860 GLubyte *strz = NULL;
3861 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3862
Brian Paul8c41a142005-11-19 15:36:28 +00003863 /* set the program target before parsing */
3864 program->Base.Target = target;
3865
Brian Paul7f76b8f2004-09-10 01:05:39 +00003866 /* Reset error state */
3867 _mesa_set_program_error(ctx, -1, NULL);
3868
Brian Paul8c41a142005-11-19 15:36:28 +00003869 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003870 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003871 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003872 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003873 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003874
3875 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3876 if (grammar_syn_id == 0) {
3877 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003878 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003879 _mesa_set_program_error (ctx, error_pos, error_msg);
3880 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003881 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003882 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003883 }
3884
Brian Paul8c41a142005-11-19 15:36:28 +00003885 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3886 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003887
Brian Paul49a80ca2006-04-28 15:40:11 +00003888 /* 'parsed' is unused here */
3889 _mesa_free (parsed);
3890 parsed = NULL;
3891
Brian Paul45703642005-10-29 15:52:31 +00003892 /* NOTE: we can't destroy grammar_syn_id right here because
3893 * grammar_destroy() can reset the last error
3894 */
Brian Paul8c41a142005-11-19 15:36:28 +00003895 if (err) {
3896 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003897 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3898 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003899 _mesa_error (ctx, GL_INVALID_OPERATION,
3900 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003901 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003902 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003903 }
3904
3905 grammar_destroy (grammar_syn_id);
3906
3907 arbprogram_syn_is_ok = 1;
3908 }
3909
3910 /* create the grammar object */
3911 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3912 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003913 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003914 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3915 _mesa_set_program_error (ctx, error_pos, error_msg);
3916 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003917 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003918 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003919 }
3920
3921 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003922 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003923 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3924 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003925 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003926 }
3927
Brian Paul8c41a142005-11-19 15:36:28 +00003928 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3929 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003930 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003931 }
3932
3933 /* check for NULL character occurences */
3934 {
Brian Paul8c41a142005-11-19 15:36:28 +00003935 GLint i;
3936 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003937 if (str[i] == '\0') {
3938 _mesa_set_program_error (ctx, i, "invalid character");
Brian Paul8c41a142005-11-19 15:36:28 +00003939 _mesa_error (ctx, GL_INVALID_OPERATION,
3940 "glProgramStringARB(illegal character)");
Jouk Jansen40322e12004-04-05 08:50:36 +00003941 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003942 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003943 }
Brian Paul8c41a142005-11-19 15:36:28 +00003944 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003945 }
3946
3947 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003948 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003949 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003950 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3951 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003952 return GL_FALSE;
3953 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003954 _mesa_memcpy (strz, str, len);
3955 strz[len] = '\0';
3956
Michal Krolb80bc052004-10-21 14:09:54 +00003957 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003958 err = !grammar_fast_check(arbprogram_syn_id, strz,
3959 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003960
3961 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003962 if (err) {
3963 _mesa_free(strz);
Brian Paul49a80ca2006-04-28 15:40:11 +00003964 _mesa_free(parsed);
Jouk Jansen40322e12004-04-05 08:50:36 +00003965 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3966 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003967 _mesa_error (ctx, GL_INVALID_OPERATION,
3968 "glProgramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003969
3970 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003971#if DEBUG_PARSING
3972 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003973 int line, col;
3974 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003975 fprintf(stderr, "program: %s\n", (char *) strz);
3976 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
3977 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003978 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3979 } while (0)
3980#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003981
Jouk Jansen40322e12004-04-05 08:50:36 +00003982 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003983 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003984 }
3985
Jouk Jansen40322e12004-04-05 08:50:36 +00003986 grammar_destroy (arbprogram_syn_id);
3987
Brian Paul8c41a142005-11-19 15:36:28 +00003988 /*
3989 * Program string is syntactically correct at this point
3990 * Parse the tokenized version of the program now, generating
3991 * vertex/fragment program instructions.
3992 */
3993
Jouk Jansen40322e12004-04-05 08:50:36 +00003994 /* Initialize the arb_program struct */
3995 program->Base.String = strz;
Brian Paul8c41a142005-11-19 15:36:28 +00003996 program->Base.Instructions = (struct prog_instruction *)
3997 _mesa_malloc(MAX_INSTRUCTIONS * sizeof(struct prog_instruction));
Jouk Jansen40322e12004-04-05 08:50:36 +00003998 program->Base.NumInstructions =
3999 program->Base.NumTemporaries =
4000 program->Base.NumParameters =
4001 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00004002 program->Base.Parameters = _mesa_new_parameter_list ();
4003 program->Base.InputsRead = 0x0;
4004 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004005 program->Position = 0;
4006 program->MajorVersion = program->MinorVersion = 0;
4007 program->PrecisionOption = GL_DONT_CARE;
4008 program->FogOption = GL_NONE;
4009 program->HintPositionInvariant = GL_FALSE;
4010 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00004011 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004012 program->NumAluInstructions =
4013 program->NumTexInstructions =
4014 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00004015 program->UsesKill = 0;
4016
Jouk Jansen40322e12004-04-05 08:50:36 +00004017 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00004018 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004019
4020 /* Start examining the tokens in the array */
4021 inst = parsed;
4022
4023 /* Check the grammer rev */
4024 if (*inst++ != REVISION) {
4025 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00004026 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul45cd2f92005-11-03 02:25:10 +00004027 "glProgramStringARB(Grammar version mismatch)");
Brian Paul45703642005-10-29 15:52:31 +00004028 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004029 }
4030 else {
Michal Krolb80bc052004-10-21 14:09:54 +00004031 /* ignore program target */
4032 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00004033 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00004034 }
4035
4036 /*debug_variables(ctx, vc_head, program); */
4037
4038 /* We're done with the parsed binary array */
4039 var_cache_destroy (&vc_head);
4040
4041 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00004042
Brian Paul8c41a142005-11-19 15:36:28 +00004043 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4044 * to size [ap.Base.NumInstructions].
4045 */
4046 program->Base.Instructions = (struct prog_instruction *)
4047 _mesa_realloc(program->Base.Instructions,
4048 MAX_INSTRUCTIONS * sizeof(struct prog_instruction),/*orig*/
4049 program->Base.NumInstructions * sizeof(struct prog_instruction));
4050
Brian Paul45703642005-10-29 15:52:31 +00004051 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004052}
Brian Paul8c41a142005-11-19 15:36:28 +00004053
4054
4055
4056void
4057_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4058 const GLvoid *str, GLsizei len,
4059 struct fragment_program *program)
4060{
4061 struct arb_program ap;
4062 GLuint i;
4063
4064 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004065 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004066 /* Error in the program. Just return. */
4067 return;
4068 }
4069
4070 /* Copy the relevant contents of the arb_program struct into the
4071 * fragment_program struct.
4072 */
4073 program->Base.String = ap.Base.String;
4074 program->Base.NumInstructions = ap.Base.NumInstructions;
4075 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4076 program->Base.NumParameters = ap.Base.NumParameters;
4077 program->Base.NumAttributes = ap.Base.NumAttributes;
4078 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4079 program->NumAluInstructions = ap.NumAluInstructions;
4080 program->NumTexInstructions = ap.NumTexInstructions;
4081 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004082 program->NumNativeAluInstructions = ap.NumAluInstructions;
4083 program->NumNativeTexInstructions = ap.NumTexInstructions;
4084 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004085 program->Base.InputsRead = ap.Base.InputsRead;
4086 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4087 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4088 program->TexturesUsed[i] = ap.TexturesUsed[i];
4089 program->FogOption = ap.FogOption;
4090
4091 if (program->Base.Instructions)
4092 _mesa_free(program->Base.Instructions);
4093 program->Base.Instructions = ap.Base.Instructions;
4094
4095 if (program->Base.Parameters)
4096 _mesa_free_parameter_list(program->Base.Parameters);
4097 program->Base.Parameters = ap.Base.Parameters;
4098
4099#if DEBUG_FP
4100 _mesa_print_program(&program.Base);
4101#endif
4102}
4103
4104
4105
4106/**
4107 * Parse the vertex program string. If success, update the given
4108 * vertex_program object with the new program. Else, leave the vertex_program
4109 * object unchanged.
4110 */
4111void
4112_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4113 const GLvoid *str, GLsizei len,
4114 struct vertex_program *program)
4115{
4116 struct arb_program ap;
4117
4118 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4119
Brian Paul95801792005-12-06 15:41:43 +00004120 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004121 /* Error in the program. Just return. */
4122 return;
4123 }
4124
4125 /* Copy the relevant contents of the arb_program struct into the
4126 * vertex_program struct.
4127 */
4128 program->Base.String = ap.Base.String;
4129 program->Base.NumInstructions = ap.Base.NumInstructions;
4130 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4131 program->Base.NumParameters = ap.Base.NumParameters;
4132 program->Base.NumAttributes = ap.Base.NumAttributes;
4133 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4134 program->Base.InputsRead = ap.Base.InputsRead;
4135 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4136 program->IsPositionInvariant = ap.HintPositionInvariant;
4137
4138 if (program->Base.Instructions)
4139 _mesa_free(program->Base.Instructions);
4140 program->Base.Instructions = ap.Base.Instructions;
4141
4142 if (program->Base.Parameters)
4143 _mesa_free_parameter_list(program->Base.Parameters);
4144 program->Base.Parameters = ap.Base.Parameters;
4145
4146#if DEBUG_VP
4147 _mesa_print_program(&program->Base);
4148#endif
4149}