blob: b52525e669fa75ffee8ed7a01082dab7850ec6de [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{
521 GLubyte *name;
522 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);
644 va->name = i;
645
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{
831 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000832 return 0;
833}
834
835/**
836 * \param color 0 if color type is primary, 1 if color type is secondary
837 * \return 0 on sucess, 1 on error
838 */
839static GLuint
840parse_color_type (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
841 GLint * color)
842{
Brian Paula6c423d2004-08-25 15:59:48 +0000843 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000844 *color = *(*inst)++ != COLOR_PRIMARY;
845 return 0;
846}
847
848/**
849 * Get an integer corresponding to a generic vertex attribute.
850 *
851 * \return 0 on sucess, 1 on error
852 */
853static GLuint
854parse_generic_attrib_num(GLcontext *ctx, GLubyte ** inst,
855 struct arb_program *Program, GLuint *attrib)
856{
Brian Paulbd997cd2004-07-20 21:12:56 +0000857 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000858
Michal Krolbb38cad2006-04-11 11:41:11 +0000859 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000860 {
861 _mesa_set_program_error (ctx, Program->Position,
862 "Invalid generic vertex attribute index");
863 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid generic vertex attribute index");
864
865 return 1;
866 }
867
Brian Paulbd997cd2004-07-20 21:12:56 +0000868 *attrib = (GLuint) i;
869
Jouk Jansen40322e12004-04-05 08:50:36 +0000870 return 0;
871}
872
873
874/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000875 * \param color The index of the color buffer to write into
876 * \return 0 on sucess, 1 on error
877 */
878static GLuint
879parse_output_color_num (GLcontext * ctx, GLubyte ** inst,
880 struct arb_program *Program, GLuint * color)
881{
882 GLint i = parse_integer (inst, Program);
883
884 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
885 _mesa_set_program_error (ctx, Program->Position,
886 "Invalid draw buffer index");
887 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid draw buffer index");
888 return 1;
889 }
890
891 *color = (GLuint) i;
892 return 0;
893}
894
895
896/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000897 * \param coord The texture unit index
898 * \return 0 on sucess, 1 on error
899 */
900static GLuint
901parse_texcoord_num (GLcontext * ctx, GLubyte ** inst,
902 struct arb_program *Program, GLuint * coord)
903{
Brian Paulbd997cd2004-07-20 21:12:56 +0000904 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000905
Brian Paula6c423d2004-08-25 15:59:48 +0000906 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000907 _mesa_set_program_error (ctx, Program->Position,
908 "Invalid texture unit index");
909 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid texture unit index");
910 return 1;
911 }
912
Brian Paulbd997cd2004-07-20 21:12:56 +0000913 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000914 return 0;
915}
916
917/**
918 * \param coord The weight index
919 * \return 0 on sucess, 1 on error
920 */
921static GLuint
922parse_weight_num (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
923 GLint * coord)
924{
925 *coord = parse_integer (inst, Program);
926
927 if ((*coord < 0) || (*coord >= 1)) {
928 _mesa_set_program_error (ctx, Program->Position,
929 "Invalid weight index");
930 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid weight index");
931 return 1;
932 }
933
934 return 0;
935}
936
937/**
938 * \param coord The clip plane index
939 * \return 0 on sucess, 1 on error
940 */
941static GLuint
942parse_clipplane_num (GLcontext * ctx, GLubyte ** inst,
943 struct arb_program *Program, GLint * coord)
944{
945 *coord = parse_integer (inst, Program);
946
947 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
948 _mesa_set_program_error (ctx, Program->Position,
949 "Invalid clip plane index");
950 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid clip plane index");
951 return 1;
952 }
953
954 return 0;
955}
956
957
958/**
959 * \return 0 on front face, 1 on back face
960 */
961static GLuint
962parse_face_type (GLubyte ** inst)
963{
964 switch (*(*inst)++) {
965 case FACE_FRONT:
966 return 0;
967
968 case FACE_BACK:
969 return 1;
970 }
971 return 0;
972}
973
974
975/**
976 * Given a matrix and a modifier token on the binary array, return tokens
977 * that _mesa_fetch_state() [program.c] can understand.
978 *
979 * \param matrix - the matrix we are talking about
980 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
981 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
982 * \return 0 on sucess, 1 on failure
983 */
984static GLuint
985parse_matrix (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
986 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
987{
988 GLubyte mat = *(*inst)++;
989
990 *matrix_idx = 0;
991
992 switch (mat) {
993 case MATRIX_MODELVIEW:
994 *matrix = STATE_MODELVIEW;
995 *matrix_idx = parse_integer (inst, Program);
996 if (*matrix_idx > 0) {
997 _mesa_set_program_error (ctx, Program->Position,
998 "ARB_vertex_blend not supported\n");
999 _mesa_error (ctx, GL_INVALID_OPERATION,
1000 "ARB_vertex_blend not supported\n");
1001 return 1;
1002 }
1003 break;
1004
1005 case MATRIX_PROJECTION:
1006 *matrix = STATE_PROJECTION;
1007 break;
1008
1009 case MATRIX_MVP:
1010 *matrix = STATE_MVP;
1011 break;
1012
1013 case MATRIX_TEXTURE:
1014 *matrix = STATE_TEXTURE;
1015 *matrix_idx = parse_integer (inst, Program);
1016 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
1017 _mesa_set_program_error (ctx, Program->Position,
1018 "Invalid Texture Unit");
1019 _mesa_error (ctx, GL_INVALID_OPERATION,
1020 "Invalid Texture Unit: %d", *matrix_idx);
1021 return 1;
1022 }
1023 break;
1024
1025 /* This is not currently supported (ARB_matrix_palette) */
1026 case MATRIX_PALETTE:
1027 *matrix_idx = parse_integer (inst, Program);
1028 _mesa_set_program_error (ctx, Program->Position,
1029 "ARB_matrix_palette not supported\n");
1030 _mesa_error (ctx, GL_INVALID_OPERATION,
1031 "ARB_matrix_palette not supported\n");
1032 return 1;
1033 break;
1034
1035 case MATRIX_PROGRAM:
1036 *matrix = STATE_PROGRAM;
1037 *matrix_idx = parse_integer (inst, Program);
1038 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
1039 _mesa_set_program_error (ctx, Program->Position,
1040 "Invalid Program Matrix");
1041 _mesa_error (ctx, GL_INVALID_OPERATION,
1042 "Invalid Program Matrix: %d", *matrix_idx);
1043 return 1;
1044 }
1045 break;
1046 }
1047
1048 switch (*(*inst)++) {
1049 case MATRIX_MODIFIER_IDENTITY:
1050 *matrix_modifier = 0;
1051 break;
1052 case MATRIX_MODIFIER_INVERSE:
1053 *matrix_modifier = STATE_MATRIX_INVERSE;
1054 break;
1055 case MATRIX_MODIFIER_TRANSPOSE:
1056 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1057 break;
1058 case MATRIX_MODIFIER_INVTRANS:
1059 *matrix_modifier = STATE_MATRIX_INVTRANS;
1060 break;
1061 }
1062
1063 return 0;
1064}
1065
1066
1067/**
1068 * This parses a state string (rather, the binary version of it) into
1069 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1070 *
1071 * \param inst - the start in the binary arry to start working from
1072 * \param state_tokens - the storage for the 6-token state description
1073 * \return - 0 on sucess, 1 on error
1074 */
1075static GLuint
1076parse_state_single_item (GLcontext * ctx, GLubyte ** inst,
1077 struct arb_program *Program, GLint * state_tokens)
1078{
1079 switch (*(*inst)++) {
1080 case STATE_MATERIAL_PARSER:
1081 state_tokens[0] = STATE_MATERIAL;
1082 state_tokens[1] = parse_face_type (inst);
1083 switch (*(*inst)++) {
1084 case MATERIAL_AMBIENT:
1085 state_tokens[2] = STATE_AMBIENT;
1086 break;
1087 case MATERIAL_DIFFUSE:
1088 state_tokens[2] = STATE_DIFFUSE;
1089 break;
1090 case MATERIAL_SPECULAR:
1091 state_tokens[2] = STATE_SPECULAR;
1092 break;
1093 case MATERIAL_EMISSION:
1094 state_tokens[2] = STATE_EMISSION;
1095 break;
1096 case MATERIAL_SHININESS:
1097 state_tokens[2] = STATE_SHININESS;
1098 break;
1099 }
1100 break;
1101
1102 case STATE_LIGHT_PARSER:
1103 state_tokens[0] = STATE_LIGHT;
1104 state_tokens[1] = parse_integer (inst, Program);
1105
1106 /* Check the value of state_tokens[1] against the # of lights */
1107 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1108 _mesa_set_program_error (ctx, Program->Position,
1109 "Invalid Light Number");
1110 _mesa_error (ctx, GL_INVALID_OPERATION,
1111 "Invalid Light Number: %d", state_tokens[1]);
1112 return 1;
1113 }
1114
1115 switch (*(*inst)++) {
1116 case LIGHT_AMBIENT:
1117 state_tokens[2] = STATE_AMBIENT;
1118 break;
1119 case LIGHT_DIFFUSE:
1120 state_tokens[2] = STATE_DIFFUSE;
1121 break;
1122 case LIGHT_SPECULAR:
1123 state_tokens[2] = STATE_SPECULAR;
1124 break;
1125 case LIGHT_POSITION:
1126 state_tokens[2] = STATE_POSITION;
1127 break;
1128 case LIGHT_ATTENUATION:
1129 state_tokens[2] = STATE_ATTENUATION;
1130 break;
1131 case LIGHT_HALF:
1132 state_tokens[2] = STATE_HALF;
1133 break;
1134 case LIGHT_SPOT_DIRECTION:
1135 state_tokens[2] = STATE_SPOT_DIRECTION;
1136 break;
1137 }
1138 break;
1139
1140 case STATE_LIGHT_MODEL:
1141 switch (*(*inst)++) {
1142 case LIGHT_MODEL_AMBIENT:
1143 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1144 break;
1145 case LIGHT_MODEL_SCENECOLOR:
1146 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1147 state_tokens[1] = parse_face_type (inst);
1148 break;
1149 }
1150 break;
1151
1152 case STATE_LIGHT_PROD:
1153 state_tokens[0] = STATE_LIGHTPROD;
1154 state_tokens[1] = parse_integer (inst, Program);
1155
1156 /* Check the value of state_tokens[1] against the # of lights */
1157 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1158 _mesa_set_program_error (ctx, Program->Position,
1159 "Invalid Light Number");
1160 _mesa_error (ctx, GL_INVALID_OPERATION,
1161 "Invalid Light Number: %d", state_tokens[1]);
1162 return 1;
1163 }
1164
1165 state_tokens[2] = parse_face_type (inst);
1166 switch (*(*inst)++) {
1167 case LIGHT_PROD_AMBIENT:
1168 state_tokens[3] = STATE_AMBIENT;
1169 break;
1170 case LIGHT_PROD_DIFFUSE:
1171 state_tokens[3] = STATE_DIFFUSE;
1172 break;
1173 case LIGHT_PROD_SPECULAR:
1174 state_tokens[3] = STATE_SPECULAR;
1175 break;
1176 }
1177 break;
1178
1179
1180 case STATE_FOG:
1181 switch (*(*inst)++) {
1182 case FOG_COLOR:
1183 state_tokens[0] = STATE_FOG_COLOR;
1184 break;
1185 case FOG_PARAMS:
1186 state_tokens[0] = STATE_FOG_PARAMS;
1187 break;
1188 }
1189 break;
1190
1191 case STATE_TEX_ENV:
1192 state_tokens[1] = parse_integer (inst, Program);
1193 switch (*(*inst)++) {
1194 case TEX_ENV_COLOR:
1195 state_tokens[0] = STATE_TEXENV_COLOR;
1196 break;
1197 }
1198 break;
1199
1200 case STATE_TEX_GEN:
1201 {
1202 GLuint type, coord;
1203
1204 state_tokens[0] = STATE_TEXGEN;
1205 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1206
1207 if (parse_texcoord_num (ctx, inst, Program, &coord))
1208 return 1;
1209 state_tokens[1] = coord;
1210
1211 /* EYE or OBJECT */
1212 type = *(*inst++);
1213
1214 /* 0 - s, 1 - t, 2 - r, 3 - q */
1215 coord = *(*inst++);
1216
1217 if (type == TEX_GEN_EYE) {
1218 switch (coord) {
1219 case COMPONENT_X:
1220 state_tokens[2] = STATE_TEXGEN_EYE_S;
1221 break;
1222 case COMPONENT_Y:
1223 state_tokens[2] = STATE_TEXGEN_EYE_T;
1224 break;
1225 case COMPONENT_Z:
1226 state_tokens[2] = STATE_TEXGEN_EYE_R;
1227 break;
1228 case COMPONENT_W:
1229 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1230 break;
1231 }
1232 }
1233 else {
1234 switch (coord) {
1235 case COMPONENT_X:
1236 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1237 break;
1238 case COMPONENT_Y:
1239 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1240 break;
1241 case COMPONENT_Z:
1242 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1243 break;
1244 case COMPONENT_W:
1245 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1246 break;
1247 }
1248 }
1249 }
1250 break;
1251
1252 case STATE_DEPTH:
1253 switch (*(*inst)++) {
1254 case DEPTH_RANGE:
1255 state_tokens[0] = STATE_DEPTH_RANGE;
1256 break;
1257 }
1258 break;
1259
1260 case STATE_CLIP_PLANE:
1261 state_tokens[0] = STATE_CLIPPLANE;
1262 state_tokens[1] = parse_integer (inst, Program);
1263 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1264 return 1;
1265 break;
1266
1267 case STATE_POINT:
1268 switch (*(*inst++)) {
1269 case POINT_SIZE:
1270 state_tokens[0] = STATE_POINT_SIZE;
1271 break;
1272
1273 case POINT_ATTENUATION:
1274 state_tokens[0] = STATE_POINT_ATTENUATION;
1275 break;
1276 }
1277 break;
1278
1279 /* XXX: I think this is the correct format for a matrix row */
1280 case STATE_MATRIX_ROWS:
1281 state_tokens[0] = STATE_MATRIX;
1282 if (parse_matrix
1283 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1284 &state_tokens[5]))
1285 return 1;
1286
1287 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1288
1289 if ((**inst) != 0) { /* Either the last row, 0 */
1290 state_tokens[4] = parse_integer (inst, Program);
1291 if (state_tokens[4] < state_tokens[3]) {
1292 _mesa_set_program_error (ctx, Program->Position,
1293 "Second matrix index less than the first");
1294 _mesa_error (ctx, GL_INVALID_OPERATION,
1295 "Second matrix index (%d) less than the first (%d)",
1296 state_tokens[4], state_tokens[3]);
1297 return 1;
1298 }
1299 }
1300 else {
1301 state_tokens[4] = state_tokens[3];
1302 (*inst)++;
1303 }
1304 break;
1305 }
1306
1307 return 0;
1308}
1309
1310/**
1311 * This parses a state string (rather, the binary version of it) into
1312 * a 6-token similar for the state fetching code in program.c
1313 *
1314 * One might ask, why fetch these parameters into just like you fetch
1315 * state when they are already stored in other places?
1316 *
1317 * Because of array offsets -> We can stick env/local parameters in the
1318 * middle of a parameter array and then index someplace into the array
1319 * when we execute.
1320 *
1321 * One optimization might be to only do this for the cases where the
1322 * env/local parameters end up inside of an array, and leave the
1323 * single parameters (or arrays of pure env/local pareameters) in their
1324 * respective register files.
1325 *
1326 * For ENV parameters, the format is:
1327 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1328 * state_tokens[1] = STATE_ENV
1329 * state_tokens[2] = the parameter index
1330 *
1331 * for LOCAL parameters, the format is:
1332 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1333 * state_tokens[1] = STATE_LOCAL
1334 * state_tokens[2] = the parameter index
1335 *
1336 * \param inst - the start in the binary arry to start working from
1337 * \param state_tokens - the storage for the 6-token state description
1338 * \return - 0 on sucess, 1 on failure
1339 */
1340static GLuint
1341parse_program_single_item (GLcontext * ctx, GLubyte ** inst,
1342 struct arb_program *Program, GLint * state_tokens)
1343{
1344 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1345 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1346 else
1347 state_tokens[0] = STATE_VERTEX_PROGRAM;
1348
1349
1350 switch (*(*inst)++) {
1351 case PROGRAM_PARAM_ENV:
1352 state_tokens[1] = STATE_ENV;
1353 state_tokens[2] = parse_integer (inst, Program);
1354
1355 /* Check state_tokens[2] against the number of ENV parameters available */
1356 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001357 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001358 ||
1359 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001360 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001361 _mesa_set_program_error (ctx, Program->Position,
1362 "Invalid Program Env Parameter");
1363 _mesa_error (ctx, GL_INVALID_OPERATION,
1364 "Invalid Program Env Parameter: %d",
1365 state_tokens[2]);
1366 return 1;
1367 }
1368
1369 break;
1370
1371 case PROGRAM_PARAM_LOCAL:
1372 state_tokens[1] = STATE_LOCAL;
1373 state_tokens[2] = parse_integer (inst, Program);
1374
1375 /* Check state_tokens[2] against the number of LOCAL parameters available */
1376 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001377 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001378 ||
1379 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001380 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001381 _mesa_set_program_error (ctx, Program->Position,
1382 "Invalid Program Local Parameter");
1383 _mesa_error (ctx, GL_INVALID_OPERATION,
1384 "Invalid Program Local Parameter: %d",
1385 state_tokens[2]);
1386 return 1;
1387 }
1388 break;
1389 }
1390
1391 return 0;
1392}
1393
1394/**
1395 * For ARB_vertex_program, programs are not allowed to use both an explicit
1396 * vertex attribute and a generic vertex attribute corresponding to the same
1397 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1398 *
1399 * This will walk our var_cache and make sure that nobody does anything fishy.
1400 *
1401 * \return 0 on sucess, 1 on error
1402 */
1403static GLuint
1404generic_attrib_check(struct var_cache *vc_head)
1405{
1406 int a;
1407 struct var_cache *curr;
1408 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1409 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1410
1411 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1412 explicitAttrib[a] = GL_FALSE;
1413 genericAttrib[a] = GL_FALSE;
1414 }
1415
1416 curr = vc_head;
1417 while (curr) {
1418 if (curr->type == vt_attrib) {
1419 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001420 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001421 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001422 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001423 }
1424
1425 curr = curr->next;
1426 }
1427
1428 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1429 if ((explicitAttrib[a]) && (genericAttrib[a]))
1430 return 1;
1431 }
1432
1433 return 0;
1434}
1435
1436/**
1437 * This will handle the binding side of an ATTRIB var declaration
1438 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001439 * \param inputReg returns the input register index, one of the
1440 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001441 * \return returns 0 on sucess, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001442 */
1443static GLuint
Brian Paul18e7c5c2005-10-30 21:46:00 +00001444parse_attrib_binding(GLcontext * ctx, GLubyte ** inst,
1445 struct arb_program *Program,
1446 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001447{
Jouk Jansen40322e12004-04-05 08:50:36 +00001448 GLint err = 0;
1449
1450 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001451
Jouk Jansen40322e12004-04-05 08:50:36 +00001452 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1453 switch (*(*inst)++) {
1454 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001455 {
1456 GLint coord;
1457 err = parse_color_type (ctx, inst, Program, &coord);
1458 *inputReg = FRAG_ATTRIB_COL0 + coord;
1459 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001460 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001461 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001462 {
1463 GLuint texcoord;
1464 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1465 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1466 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001467 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001468 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001469 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001470 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001471 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001472 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001473 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 default:
1475 err = 1;
1476 break;
1477 }
1478 }
1479 else {
1480 switch (*(*inst)++) {
1481 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001482 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001483 break;
1484
1485 case VERTEX_ATTRIB_WEIGHT:
1486 {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001487 const char *msg = "ARB_vertex_blend not supported";
Jouk Jansen40322e12004-04-05 08:50:36 +00001488 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001489 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001490 *inputReg = VERT_ATTRIB_WEIGHT;
1491 _mesa_set_program_error(ctx, Program->Position, msg);
1492 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001493 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001494 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001495
1496 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001497 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001498 break;
1499
1500 case VERTEX_ATTRIB_COLOR:
1501 {
1502 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001503 err = parse_color_type (ctx, inst, Program, &color);
1504 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001505 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001506 }
1507 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001508 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001509 }
1510 }
1511 break;
1512
1513 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001514 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001515 break;
1516
1517 case VERTEX_ATTRIB_TEXCOORD:
1518 {
1519 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001520 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001521 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001522 }
1523 break;
1524
Jouk Jansen40322e12004-04-05 08:50:36 +00001525 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001526 /* Not supported at this time */
1527 {
1528 const char *msg = "ARB_palette_matrix not supported";
1529 parse_integer (inst, Program);
1530 _mesa_set_program_error (ctx, Program->Position, msg);
1531 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
1532 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001533 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001534
1535 case VERTEX_ATTRIB_GENERIC:
1536 {
1537 GLuint attrib;
Jouk Jansen40322e12004-04-05 08:50:36 +00001538 if (!parse_generic_attrib_num(ctx, inst, Program, &attrib)) {
1539 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001540 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1541 * attributes do not alias the conventional vertex
1542 * attributes.
1543 */
1544 if (attrib > 0)
1545 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001546 }
1547 }
1548 break;
1549
1550 default:
1551 err = 1;
1552 break;
1553 }
1554 }
1555
1556 /* Can this even happen? */
1557 if (err) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001558 const char *msg = "Bad attribute binding";
1559 _mesa_set_program_error(ctx, Program->Position, msg);
1560 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001561 }
1562
Brian Paulde997602005-11-12 17:53:14 +00001563 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001564
1565 return err;
1566}
1567
Brian Paul7aebaf32005-10-30 21:23:23 +00001568
Jouk Jansen40322e12004-04-05 08:50:36 +00001569/**
1570 * This translates between a binary token for an output variable type
1571 * and the mesa token for the same thing.
1572 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001573 * \param inst The parsed tokens
1574 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001575 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001576 */
1577static GLuint
Brian Paul7aebaf32005-10-30 21:23:23 +00001578parse_result_binding(GLcontext *ctx, GLubyte **inst,
1579 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001580{
Brian Paul7aebaf32005-10-30 21:23:23 +00001581 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001582
Brian Paul7aebaf32005-10-30 21:23:23 +00001583 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001584 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001585 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001586 GLuint out_color;
1587
Brian Paulbe76b7f2004-10-04 14:40:05 +00001588 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001589 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001590 */
1591 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001592 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001593 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001594 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001595 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001596 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1597 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001598 }
1599 break;
1600
1601 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001602 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001603 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001604 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001605 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001606 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001607 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001608 GLint color_type;
1609 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001610 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1611 if (err)
1612 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001613
Jouk Jansen40322e12004-04-05 08:50:36 +00001614 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001615 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001616 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001617 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001618 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001619 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001620 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 }
1622 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001623 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001624 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001625 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001626 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001627 }
1628 /* primary color */
1629 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001630 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001631 }
1632 }
1633 }
1634 break;
1635
1636 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001637 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001638 break;
1639
1640 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001641 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001642 break;
1643
1644 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001645 {
1646 GLuint unit;
1647 if (parse_texcoord_num (ctx, inst, Program, &unit))
1648 return 1;
1649 *outputReg = VERT_RESULT_TEX0 + unit;
1650 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001651 break;
1652 }
1653
Brian Paulde997602005-11-12 17:53:14 +00001654 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001655
1656 return 0;
1657}
1658
Brian Paul7aebaf32005-10-30 21:23:23 +00001659
Jouk Jansen40322e12004-04-05 08:50:36 +00001660/**
1661 * This handles the declaration of ATTRIB variables
1662 *
1663 * XXX: Still needs
1664 * parse_vert_attrib_binding(), or something like that
1665 *
1666 * \return 0 on sucess, 1 on error
1667 */
1668static GLint
1669parse_attrib (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1670 struct arb_program *Program)
1671{
1672 GLuint found;
1673 char *error_msg;
1674 struct var_cache *attrib_var;
1675
1676 attrib_var = parse_string (inst, vc_head, Program, &found);
1677 Program->Position = parse_position (inst);
1678 if (found) {
1679 error_msg = (char *)
1680 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1681 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1682 attrib_var->name);
1683
1684 _mesa_set_program_error (ctx, Program->Position, error_msg);
1685 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1686
1687 _mesa_free (error_msg);
1688 return 1;
1689 }
1690
1691 attrib_var->type = vt_attrib;
1692
Brian Paul7aebaf32005-10-30 21:23:23 +00001693 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001694 &attrib_var->attrib_is_generic))
1695 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001696
Brian Paul7aebaf32005-10-30 21:23:23 +00001697 if (generic_attrib_check(*vc_head)) {
1698 _mesa_set_program_error(ctx, Program->Position,
1699 "Cannot use both a generic vertex attribute "
1700 "and a specific attribute of the same type");
1701 _mesa_error(ctx, GL_INVALID_OPERATION,
1702 "Cannot use both a generic vertex attribute and a specific "
1703 "attribute of the same type");
1704 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001705 }
1706
1707 Program->Base.NumAttributes++;
1708 return 0;
1709}
1710
1711/**
1712 * \param use -- TRUE if we're called when declaring implicit parameters,
1713 * FALSE if we're declaraing variables. This has to do with
1714 * if we get a signed or unsigned float for scalar constants
1715 */
1716static GLuint
1717parse_param_elements (GLcontext * ctx, GLubyte ** inst,
1718 struct var_cache *param_var,
1719 struct arb_program *Program, GLboolean use)
1720{
1721 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001722 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001723 GLint state_tokens[6];
1724 GLfloat const_values[4];
1725
Jouk Jansen40322e12004-04-05 08:50:36 +00001726 switch (*(*inst)++) {
1727 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001728 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1729 return 1;
1730
1731 /* If we adding STATE_MATRIX that has multiple rows, we need to
1732 * unroll it and call _mesa_add_state_reference() for each row
1733 */
1734 if ((state_tokens[0] == STATE_MATRIX)
1735 && (state_tokens[3] != state_tokens[4])) {
1736 GLint row;
1737 GLint first_row = state_tokens[3];
1738 GLint last_row = state_tokens[4];
1739
1740 for (row = first_row; row <= last_row; row++) {
1741 state_tokens[3] = state_tokens[4] = row;
1742
Brian Paulde997602005-11-12 17:53:14 +00001743 idx = _mesa_add_state_reference(Program->Base.Parameters,
1744 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001745 if (param_var->param_binding_begin == ~0U)
1746 param_var->param_binding_begin = idx;
1747 param_var->param_binding_length++;
1748 Program->Base.NumParameters++;
1749 }
1750 }
1751 else {
Brian Paulde997602005-11-12 17:53:14 +00001752 idx = _mesa_add_state_reference(Program->Base.Parameters,
1753 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001754 if (param_var->param_binding_begin == ~0U)
1755 param_var->param_binding_begin = idx;
1756 param_var->param_binding_length++;
1757 Program->Base.NumParameters++;
1758 }
1759 break;
1760
1761 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001762 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1763 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001764 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001765 if (param_var->param_binding_begin == ~0U)
1766 param_var->param_binding_begin = idx;
1767 param_var->param_binding_length++;
1768 Program->Base.NumParameters++;
1769
1770 /* Check if there is more: 0 -> we're done, else its an integer */
1771 if (**inst) {
1772 GLuint out_of_range, new_idx;
1773 GLuint start_idx = state_tokens[2] + 1;
1774 GLuint end_idx = parse_integer (inst, Program);
1775
1776 out_of_range = 0;
1777 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1778 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001779 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001780 || ((state_tokens[1] == STATE_LOCAL)
1781 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001782 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001783 out_of_range = 1;
1784 }
1785 else {
1786 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001787 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001788 || ((state_tokens[1] == STATE_LOCAL)
1789 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001790 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001791 out_of_range = 1;
1792 }
1793 if (out_of_range) {
1794 _mesa_set_program_error (ctx, Program->Position,
1795 "Invalid Program Parameter");
1796 _mesa_error (ctx, GL_INVALID_OPERATION,
1797 "Invalid Program Parameter: %d", end_idx);
1798 return 1;
1799 }
1800
1801 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1802 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001803 idx = _mesa_add_state_reference(Program->Base.Parameters,
1804 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001805 param_var->param_binding_length++;
1806 Program->Base.NumParameters++;
1807 }
1808 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001809 else {
1810 (*inst)++;
1811 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001812 break;
1813
1814 case PARAM_CONSTANT:
1815 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001816 idx = _mesa_add_named_constant(Program->Base.Parameters,
1817 (char *) param_var->name,
1818 const_values);
Jouk Jansen40322e12004-04-05 08:50:36 +00001819 if (param_var->param_binding_begin == ~0U)
1820 param_var->param_binding_begin = idx;
1821 param_var->param_binding_length++;
1822 Program->Base.NumParameters++;
1823 break;
1824
1825 default:
Brian Paul7aebaf32005-10-30 21:23:23 +00001826 _mesa_set_program_error(ctx, Program->Position,
1827 "Unexpected token in parse_param_elements()");
1828 _mesa_error(ctx, GL_INVALID_OPERATION,
1829 "Unexpected token in parse_param_elements()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001830 return 1;
1831 }
1832
1833 /* Make sure we haven't blown past our parameter limits */
1834 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1835 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001836 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001837 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1838 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001839 ctx->Const.FragmentProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001840 _mesa_set_program_error (ctx, Program->Position,
1841 "Too many parameter variables");
1842 _mesa_error (ctx, GL_INVALID_OPERATION, "Too many parameter variables");
1843 return 1;
1844 }
1845
1846 return err;
1847}
1848
Brian Paul7aebaf32005-10-30 21:23:23 +00001849
Jouk Jansen40322e12004-04-05 08:50:36 +00001850/**
1851 * This picks out PARAM program parameter bindings.
1852 *
1853 * XXX: This needs to be stressed & tested
1854 *
1855 * \return 0 on sucess, 1 on error
1856 */
1857static GLuint
1858parse_param (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1859 struct arb_program *Program)
1860{
Brian Paulbd997cd2004-07-20 21:12:56 +00001861 GLuint found, err;
1862 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001863 struct var_cache *param_var;
1864
1865 err = 0;
1866 param_var = parse_string (inst, vc_head, Program, &found);
1867 Program->Position = parse_position (inst);
1868
1869 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001870 char *error_msg = (char *)
1871 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001872 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1873 param_var->name);
1874
1875 _mesa_set_program_error (ctx, Program->Position, error_msg);
1876 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1877
1878 _mesa_free (error_msg);
1879 return 1;
1880 }
1881
1882 specified_length = parse_integer (inst, Program);
1883
1884 if (specified_length < 0) {
1885 _mesa_set_program_error (ctx, Program->Position,
1886 "Negative parameter array length");
1887 _mesa_error (ctx, GL_INVALID_OPERATION,
1888 "Negative parameter array length: %d", specified_length);
1889 return 1;
1890 }
1891
1892 param_var->type = vt_param;
1893 param_var->param_binding_length = 0;
1894
1895 /* Right now, everything is shoved into the main state register file.
1896 *
1897 * In the future, it would be nice to leave things ENV/LOCAL params
1898 * in their respective register files, if possible
1899 */
1900 param_var->param_binding_type = PROGRAM_STATE_VAR;
1901
1902 /* Remember to:
1903 * * - add each guy to the parameter list
1904 * * - increment the param_var->param_binding_len
1905 * * - store the param_var->param_binding_begin for the first one
1906 * * - compare the actual len to the specified len at the end
1907 */
1908 while (**inst != PARAM_NULL) {
1909 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1910 return 1;
1911 }
1912
1913 /* Test array length here! */
1914 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001915 if (specified_length != (int)param_var->param_binding_length) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001916 const char *msg
1917 = "Declared parameter array length does not match parameter list";
1918 _mesa_set_program_error(ctx, Program->Position, msg);
1919 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001920 }
1921 }
1922
1923 (*inst)++;
1924
1925 return 0;
1926}
1927
1928/**
1929 *
1930 */
1931static GLuint
1932parse_param_use (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1933 struct arb_program *Program, struct var_cache **new_var)
1934{
1935 struct var_cache *param_var;
1936
1937 /* First, insert a dummy entry into the var_cache */
1938 var_cache_create (&param_var);
1939 param_var->name = (GLubyte *) _mesa_strdup (" ");
1940 param_var->type = vt_param;
1941
1942 param_var->param_binding_length = 0;
1943 /* Don't fill in binding_begin; We use the default value of -1
1944 * to tell if its already initialized, elsewhere.
1945 *
1946 * param_var->param_binding_begin = 0;
1947 */
1948 param_var->param_binding_type = PROGRAM_STATE_VAR;
1949
1950 var_cache_append (vc_head, param_var);
1951
1952 /* Then fill it with juicy parameter goodness */
1953 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1954 return 1;
1955
1956 *new_var = param_var;
1957
1958 return 0;
1959}
1960
1961
1962/**
1963 * This handles the declaration of TEMP variables
1964 *
1965 * \return 0 on sucess, 1 on error
1966 */
1967static GLuint
1968parse_temp (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1969 struct arb_program *Program)
1970{
1971 GLuint found;
1972 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001973
1974 while (**inst != 0) {
1975 temp_var = parse_string (inst, vc_head, Program, &found);
1976 Program->Position = parse_position (inst);
1977 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001978 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001979 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1980 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1981 temp_var->name);
1982
1983 _mesa_set_program_error (ctx, Program->Position, error_msg);
1984 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1985
1986 _mesa_free (error_msg);
1987 return 1;
1988 }
1989
1990 temp_var->type = vt_temp;
1991
1992 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1993 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001994 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001995 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1996 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001997 ctx->Const.VertexProgram.MaxTemps))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001998 _mesa_set_program_error (ctx, Program->Position,
1999 "Too many TEMP variables declared");
2000 _mesa_error (ctx, GL_INVALID_OPERATION,
2001 "Too many TEMP variables declared");
2002 return 1;
2003 }
2004
2005 temp_var->temp_binding = Program->Base.NumTemporaries;
2006 Program->Base.NumTemporaries++;
2007 }
2008 (*inst)++;
2009
2010 return 0;
2011}
2012
2013/**
2014 * This handles variables of the OUTPUT variety
2015 *
2016 * \return 0 on sucess, 1 on error
2017 */
2018static GLuint
2019parse_output (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2020 struct arb_program *Program)
2021{
2022 GLuint found;
2023 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002024 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002025
2026 output_var = parse_string (inst, vc_head, Program, &found);
2027 Program->Position = parse_position (inst);
2028 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002029 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002030 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2031 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2032 output_var->name);
2033
2034 _mesa_set_program_error (ctx, Program->Position, error_msg);
2035 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2036
2037 _mesa_free (error_msg);
2038 return 1;
2039 }
2040
2041 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002042
2043 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2044 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002045}
2046
2047/**
2048 * This handles variables of the ALIAS kind
2049 *
2050 * \return 0 on sucess, 1 on error
2051 */
2052static GLuint
2053parse_alias (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2054 struct arb_program *Program)
2055{
2056 GLuint found;
2057 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002058
2059 temp_var = parse_string (inst, vc_head, Program, &found);
2060 Program->Position = parse_position (inst);
2061
2062 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002063 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002064 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2065 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2066 temp_var->name);
2067
2068 _mesa_set_program_error (ctx, Program->Position, error_msg);
2069 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2070
2071 _mesa_free (error_msg);
2072 return 1;
2073 }
2074
2075 temp_var->type = vt_alias;
2076 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2077 Program->Position = parse_position (inst);
2078
2079 if (!found)
2080 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002081 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002082 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2083 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2084 temp_var->alias_binding->name);
2085
2086 _mesa_set_program_error (ctx, Program->Position, error_msg);
2087 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2088
2089 _mesa_free (error_msg);
2090 return 1;
2091 }
2092
2093 return 0;
2094}
2095
2096/**
2097 * This handles variables of the ADDRESS kind
2098 *
2099 * \return 0 on sucess, 1 on error
2100 */
2101static GLuint
2102parse_address (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2103 struct arb_program *Program)
2104{
2105 GLuint found;
2106 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002107
2108 while (**inst != 0) {
2109 temp_var = parse_string (inst, vc_head, Program, &found);
2110 Program->Position = parse_position (inst);
2111 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002112 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002113 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2114 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2115 temp_var->name);
2116
2117 _mesa_set_program_error (ctx, Program->Position, error_msg);
2118 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2119
2120 _mesa_free (error_msg);
2121 return 1;
2122 }
2123
2124 temp_var->type = vt_address;
2125
2126 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002127 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002128 const char *msg = "Too many ADDRESS variables declared";
2129 _mesa_set_program_error(ctx, Program->Position, msg);
2130
2131 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002132 return 1;
2133 }
2134
2135 temp_var->address_binding = Program->Base.NumAddressRegs;
2136 Program->Base.NumAddressRegs++;
2137 }
2138 (*inst)++;
2139
2140 return 0;
2141}
2142
2143/**
2144 * Parse a program declaration
2145 *
2146 * \return 0 on sucess, 1 on error
2147 */
2148static GLint
2149parse_declaration (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2150 struct arb_program *Program)
2151{
2152 GLint err = 0;
2153
2154 switch (*(*inst)++) {
2155 case ADDRESS:
2156 err = parse_address (ctx, inst, vc_head, Program);
2157 break;
2158
2159 case ALIAS:
2160 err = parse_alias (ctx, inst, vc_head, Program);
2161 break;
2162
2163 case ATTRIB:
2164 err = parse_attrib (ctx, inst, vc_head, Program);
2165 break;
2166
2167 case OUTPUT:
2168 err = parse_output (ctx, inst, vc_head, Program);
2169 break;
2170
2171 case PARAM:
2172 err = parse_param (ctx, inst, vc_head, Program);
2173 break;
2174
2175 case TEMP:
2176 err = parse_temp (ctx, inst, vc_head, Program);
2177 break;
2178 }
2179
2180 return err;
2181}
2182
2183/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002184 * Handle the parsing out of a masked destination register, either for a
2185 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002186 *
2187 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002188 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002189 * position invariant
2190 *
2191 * \param File - The register file we write to
2192 * \param Index - The register index we write to
2193 * \param WriteMask - The mask controlling which components we write (1->write)
2194 *
2195 * \return 0 on sucess, 1 on error
2196 */
2197static GLuint
2198parse_masked_dst_reg (GLcontext * ctx, GLubyte ** inst,
2199 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002200 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002201{
Brian Paul7aebaf32005-10-30 21:23:23 +00002202 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002203 struct var_cache *dst;
2204
2205 /* We either have a result register specified, or a
2206 * variable that may or may not be writable
2207 */
2208 switch (*(*inst)++) {
2209 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002210 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002211 return 1;
2212 *File = PROGRAM_OUTPUT;
2213 break;
2214
2215 case REGISTER_ESTABLISHED_NAME:
2216 dst = parse_string (inst, vc_head, Program, &result);
2217 Program->Position = parse_position (inst);
2218
2219 /* If the name has never been added to our symbol table, we're hosed */
2220 if (!result) {
2221 _mesa_set_program_error (ctx, Program->Position,
2222 "0: Undefined variable");
2223 _mesa_error (ctx, GL_INVALID_OPERATION, "0: Undefined variable: %s",
2224 dst->name);
2225 return 1;
2226 }
2227
2228 switch (dst->type) {
2229 case vt_output:
2230 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002231 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002232 break;
2233
2234 case vt_temp:
2235 *File = PROGRAM_TEMPORARY;
2236 *Index = dst->temp_binding;
2237 break;
2238
2239 /* If the var type is not vt_output or vt_temp, no go */
2240 default:
2241 _mesa_set_program_error (ctx, Program->Position,
2242 "Destination register is read only");
2243 _mesa_error (ctx, GL_INVALID_OPERATION,
2244 "Destination register is read only: %s",
2245 dst->name);
2246 return 1;
2247 }
2248 break;
2249
2250 default:
2251 _mesa_set_program_error (ctx, Program->Position,
2252 "Unexpected opcode in parse_masked_dst_reg()");
2253 _mesa_error (ctx, GL_INVALID_OPERATION,
2254 "Unexpected opcode in parse_masked_dst_reg()");
2255 return 1;
2256 }
2257
2258
2259 /* Position invariance test */
2260 /* This test is done now in syntax portion - when position invariance OPTION
2261 is specified, "result.position" rule is disabled so there is no way
2262 to write the position
2263 */
2264 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2265 (*Index == 0)) {
2266 _mesa_set_program_error (ctx, Program->Position,
2267 "Vertex program specified position invariance and wrote vertex position");
2268 _mesa_error (ctx, GL_INVALID_OPERATION,
2269 "Vertex program specified position invariance and wrote vertex position");
2270 }*/
2271
2272 /* And then the mask.
2273 * w,a -> bit 0
2274 * z,b -> bit 1
2275 * y,g -> bit 2
2276 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002277 *
2278 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002279 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002280 tmp = (GLint) *(*inst)++;
2281 *WriteMask = (((tmp>>3) & 0x1) |
2282 ((tmp>>1) & 0x2) |
2283 ((tmp<<1) & 0x4) |
2284 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002285
2286 return 0;
2287}
2288
2289
2290/**
2291 * Handle the parsing of a address register
2292 *
2293 * \param Index - The register index we write to
2294 *
2295 * \return 0 on sucess, 1 on error
2296 */
2297static GLuint
2298parse_address_reg (GLcontext * ctx, GLubyte ** inst,
2299 struct var_cache **vc_head,
2300 struct arb_program *Program, GLint * Index)
2301{
2302 struct var_cache *dst;
2303 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002304
2305 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002306
2307 dst = parse_string (inst, vc_head, Program, &result);
2308 Program->Position = parse_position (inst);
2309
2310 /* If the name has never been added to our symbol table, we're hosed */
2311 if (!result) {
2312 _mesa_set_program_error (ctx, Program->Position, "Undefined variable");
2313 _mesa_error (ctx, GL_INVALID_OPERATION, "Undefined variable: %s",
2314 dst->name);
2315 return 1;
2316 }
2317
2318 if (dst->type != vt_address) {
2319 _mesa_set_program_error (ctx, Program->Position,
2320 "Variable is not of type ADDRESS");
2321 _mesa_error (ctx, GL_INVALID_OPERATION,
2322 "Variable: %s is not of type ADDRESS", dst->name);
2323 return 1;
2324 }
2325
2326 return 0;
2327}
2328
Brian Paul8e8fa632005-07-01 02:03:33 +00002329#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002330/**
2331 * Handle the parsing out of a masked address register
2332 *
2333 * \param Index - The register index we write to
2334 * \param WriteMask - The mask controlling which components we write (1->write)
2335 *
2336 * \return 0 on sucess, 1 on error
2337 */
2338static GLuint
2339parse_masked_address_reg (GLcontext * ctx, GLubyte ** inst,
2340 struct var_cache **vc_head,
2341 struct arb_program *Program, GLint * Index,
2342 GLboolean * WriteMask)
2343{
2344 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2345 return 1;
2346
2347 /* This should be 0x8 */
2348 (*inst)++;
2349
2350 /* Writemask of .x is implied */
2351 WriteMask[0] = 1;
2352 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2353
2354 return 0;
2355}
Brian Paul8e8fa632005-07-01 02:03:33 +00002356#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002357
2358/**
2359 * Parse out a swizzle mask.
2360 *
Brian Paul32df89e2005-10-29 18:26:43 +00002361 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002362 *
2363 * The len parameter allows us to grab 4 components for a vector
2364 * swizzle, or just 1 component for a scalar src register selection
2365 */
Brian Paul32df89e2005-10-29 18:26:43 +00002366static void
2367parse_swizzle_mask(GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002368{
Brian Paul32df89e2005-10-29 18:26:43 +00002369 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002370
Brian Paul32df89e2005-10-29 18:26:43 +00002371 for (i = 0; i < 4; i++)
2372 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002373
Brian Paul32df89e2005-10-29 18:26:43 +00002374 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002375 switch (*(*inst)++) {
2376 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002377 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002378 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002379 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002380 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002381 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002382 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002383 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002384 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002385 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002386 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002388 default:
2389 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2390 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002391 }
2392 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002393}
2394
Jouk Jansen40322e12004-04-05 08:50:36 +00002395
Brian Paul32df89e2005-10-29 18:26:43 +00002396/**
2397 * Parse an extended swizzle mask which is a sequence of
2398 * four x/y/z/w/0/1 tokens.
2399 * \return swizzle four swizzle values
2400 * \return negateMask four element bitfield
2401 */
2402static void
2403parse_extended_swizzle_mask(GLubyte **inst, GLubyte swizzle[4],
2404 GLubyte *negateMask)
2405{
2406 GLint i;
2407
2408 *negateMask = 0x0;
2409 for (i = 0; i < 4; i++) {
2410 GLubyte swz;
2411 if (parse_sign(inst) == -1)
2412 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002413
2414 swz = *(*inst)++;
2415
2416 switch (swz) {
2417 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002418 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002419 break;
2420 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002421 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002422 break;
2423 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002424 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002425 break;
2426 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002427 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002428 break;
2429 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002430 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002431 break;
2432 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002433 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002434 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002435 default:
2436 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2437 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002438 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002439 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002440}
2441
2442
2443static GLuint
2444parse_src_reg (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002445 struct arb_program *Program,
2446 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002447 GLboolean *IsRelOffset )
2448{
2449 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002450 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002451 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002452
Keith Whitwell7c26b612005-04-21 14:46:57 +00002453 *IsRelOffset = 0;
2454
Jouk Jansen40322e12004-04-05 08:50:36 +00002455 /* And the binding for the src */
2456 switch (*(*inst)++) {
2457 case REGISTER_ATTRIB:
2458 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002459 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002460 return 1;
2461 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002462 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002463
2464 /* We need to insert a dummy variable into the var_cache so we can
2465 * catch generic vertex attrib aliasing errors
2466 */
2467 var_cache_create(&src);
2468 src->type = vt_attrib;
2469 src->name = (GLubyte *)_mesa_strdup("Dummy Attrib Variable");
Brian Paul18e7c5c2005-10-30 21:46:00 +00002470 src->attrib_binding = binding;
2471 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002472 var_cache_append(vc_head, src);
2473 if (generic_attrib_check(*vc_head)) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002474 const char *msg = "Cannot use both a generic vertex attribute "
2475 "and a specific attribute of the same type";
2476 _mesa_set_program_error (ctx, Program->Position, msg);
2477 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002478 return 1;
2479 }
2480 break;
2481
2482 case REGISTER_PARAM:
2483 switch (**inst) {
2484 case PARAM_ARRAY_ELEMENT:
2485 (*inst)++;
2486 src = parse_string (inst, vc_head, Program, &found);
2487 Program->Position = parse_position (inst);
2488
2489 if (!found) {
2490 _mesa_set_program_error (ctx, Program->Position,
2491 "2: Undefined variable");
2492 _mesa_error (ctx, GL_INVALID_OPERATION,
2493 "2: Undefined variable: %s", src->name);
2494 return 1;
2495 }
2496
Brian Paul95801792005-12-06 15:41:43 +00002497 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002498
2499 switch (*(*inst)++) {
2500 case ARRAY_INDEX_ABSOLUTE:
2501 offset = parse_integer (inst, Program);
2502
2503 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002504 || (offset >= (int)src->param_binding_length)) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002505 _mesa_set_program_error (ctx, Program->Position,
2506 "Index out of range");
2507 _mesa_error (ctx, GL_INVALID_OPERATION,
2508 "Index %d out of range for %s", offset,
2509 src->name);
2510 return 1;
2511 }
2512
2513 *Index = src->param_binding_begin + offset;
2514 break;
2515
2516 case ARRAY_INDEX_RELATIVE:
2517 {
2518 GLint addr_reg_idx, rel_off;
2519
2520 /* First, grab the address regiseter */
2521 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2522 return 1;
2523
2524 /* And the .x */
2525 ((*inst)++);
2526 ((*inst)++);
2527 ((*inst)++);
2528 ((*inst)++);
2529
2530 /* Then the relative offset */
2531 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2532
2533 /* And store it properly */
2534 *Index = src->param_binding_begin + rel_off;
2535 *IsRelOffset = 1;
2536 }
2537 break;
2538 }
2539 break;
2540
2541 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002542 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2543 return 1;
2544
Brian Paul95801792005-12-06 15:41:43 +00002545 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002546 *Index = src->param_binding_begin;
2547 break;
2548 }
2549 break;
2550
2551 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002552 src = parse_string (inst, vc_head, Program, &found);
2553 Program->Position = parse_position (inst);
2554
2555 /* If the name has never been added to our symbol table, we're hosed */
2556 if (!found) {
2557 _mesa_set_program_error (ctx, Program->Position,
2558 "3: Undefined variable");
2559 _mesa_error (ctx, GL_INVALID_OPERATION, "3: Undefined variable: %s",
2560 src->name);
2561 return 1;
2562 }
2563
2564 switch (src->type) {
2565 case vt_attrib:
2566 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002567 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002568 break;
2569
2570 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2571 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002572 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002573 *Index = src->param_binding_begin;
2574 break;
2575
2576 case vt_temp:
2577 *File = PROGRAM_TEMPORARY;
2578 *Index = src->temp_binding;
2579 break;
2580
2581 /* If the var type is vt_output no go */
2582 default:
2583 _mesa_set_program_error (ctx, Program->Position,
2584 "destination register is read only");
2585 _mesa_error (ctx, GL_INVALID_OPERATION,
2586 "destination register is read only: %s",
2587 src->name);
2588 return 1;
2589 }
2590 break;
2591
2592 default:
2593 _mesa_set_program_error (ctx, Program->Position,
2594 "Unknown token in parse_src_reg");
2595 _mesa_error (ctx, GL_INVALID_OPERATION,
2596 "Unknown token in parse_src_reg");
2597 return 1;
2598 }
2599
2600 return 0;
2601}
2602
2603/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002604 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002605 */
2606static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00002607parse_fp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
2608 struct var_cache **vc_head,
2609 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002610 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002611{
Brian Paul7aebaf32005-10-30 21:23:23 +00002612 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002613 GLint index;
2614 GLboolean negate;
2615 GLubyte swizzle[4];
2616 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002617
Jouk Jansen40322e12004-04-05 08:50:36 +00002618 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002619 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002620
2621 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002622 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002623 return 1;
2624
2625 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002626 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002627
Brian Paul32df89e2005-10-29 18:26:43 +00002628 reg->File = file;
2629 reg->Index = index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002630 reg->Abs = 0; /* NV only */
2631 reg->NegateAbs = 0; /* NV only */
Brian Paul32df89e2005-10-29 18:26:43 +00002632 reg->NegateBase = negate;
2633 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002634 return 0;
2635}
2636
Jouk Jansen40322e12004-04-05 08:50:36 +00002637
Keith Whitwell7c26b612005-04-21 14:46:57 +00002638static GLuint
2639parse_fp_dst_reg(GLcontext * ctx, GLubyte ** inst,
2640 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002641 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002642{
Brian Paul7aebaf32005-10-30 21:23:23 +00002643 GLint mask;
2644 GLuint idx;
2645 enum register_file file;
2646
Keith Whitwell7c26b612005-04-21 14:46:57 +00002647 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002648 return 1;
2649
Keith Whitwell7c26b612005-04-21 14:46:57 +00002650 reg->CondMask = 0; /* NV only */
2651 reg->CondSwizzle = 0; /* NV only */
2652 reg->File = file;
2653 reg->Index = idx;
2654 reg->WriteMask = mask;
2655 return 0;
2656}
2657
2658
Brian Paul7aebaf32005-10-30 21:23:23 +00002659/**
2660 * Parse fragment program scalar src register.
2661 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002662static GLuint
2663parse_fp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002664 struct var_cache **vc_head,
2665 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002666 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002667{
Brian Paul7aebaf32005-10-30 21:23:23 +00002668 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002669 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002670 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002671 GLubyte Swizzle[4];
2672 GLboolean IsRelOffset;
2673
2674 /* Grab the sign */
2675 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2676
2677 /* And the src reg */
2678 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2679 return 1;
2680
2681 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002682 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002683
Keith Whitwell7c26b612005-04-21 14:46:57 +00002684 reg->File = File;
2685 reg->Index = Index;
2686 reg->Abs = 0; /* NV only */
2687 reg->NegateAbs = 0; /* NV only */
2688 reg->NegateBase = Negate;
2689 reg->Swizzle = (Swizzle[0] << 0);
2690
Jouk Jansen40322e12004-04-05 08:50:36 +00002691 return 0;
2692}
2693
Keith Whitwell7c26b612005-04-21 14:46:57 +00002694
Jouk Jansen40322e12004-04-05 08:50:36 +00002695/**
2696 * This is a big mother that handles getting opcodes into the instruction
2697 * and handling the src & dst registers for fragment program instructions
2698 */
2699static GLuint
2700parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2701 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002702 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002703{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002704 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002705 GLuint texcoord;
2706 GLubyte instClass, type, code;
2707 GLboolean rel;
2708
Brian Paul7e807512005-11-05 17:10:45 +00002709 _mesa_init_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002710
2711 /* Record the position in the program string for debugging */
2712 fp->StringPos = Program->Position;
2713
2714 /* OP_ALU_INST or OP_TEX_INST */
2715 instClass = *(*inst)++;
2716
2717 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2718 * OP_TEX_{SAMPLE, KIL}
2719 */
2720 type = *(*inst)++;
2721
2722 /* The actual opcode name */
2723 code = *(*inst)++;
2724
2725 /* Increment the correct count */
2726 switch (instClass) {
2727 case OP_ALU_INST:
2728 Program->NumAluInstructions++;
2729 break;
2730 case OP_TEX_INST:
2731 Program->NumTexInstructions++;
2732 break;
2733 }
2734
Jouk Jansen40322e12004-04-05 08:50:36 +00002735 switch (type) {
2736 case OP_ALU_VECTOR:
2737 switch (code) {
2738 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002739 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002740 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002741 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002742 break;
2743
2744 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002745 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002746 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002747 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002748 break;
2749
2750 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002751 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002752 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002753 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 break;
2755
2756 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002757 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002758 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002759 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 break;
2761
2762 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002763 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002764 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002765 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002766 break;
2767 }
2768
Keith Whitwell7c26b612005-04-21 14:46:57 +00002769 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002770 return 1;
2771
Keith Whitwell7c26b612005-04-21 14:46:57 +00002772 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002773 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002774 break;
2775
2776 case OP_ALU_SCALAR:
2777 switch (code) {
2778 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002779 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002780 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002781 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 break;
2783
2784 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002785 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002786 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002787 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002788 break;
2789
2790 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002791 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002792 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002793 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002794 break;
2795
2796 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002797 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002798 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002799 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 break;
2801
2802 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002803 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002804 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002805 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002806 break;
2807
2808 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002809 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002810 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002811 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002812 break;
2813
2814 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002815 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002816 case OP_SCS:
2817
Brian Paul7e807512005-11-05 17:10:45 +00002818 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 break;
2820 }
2821
Keith Whitwell7c26b612005-04-21 14:46:57 +00002822 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002823 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002824
2825 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002826 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002827 break;
2828
2829 case OP_ALU_BINSC:
2830 switch (code) {
2831 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002832 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002833 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002834 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 break;
2836 }
2837
Keith Whitwell7c26b612005-04-21 14:46:57 +00002838 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002839 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002840
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002842 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002843 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 }
2845 break;
2846
2847
2848 case OP_ALU_BIN:
2849 switch (code) {
2850 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002851 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002852 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002853 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002854 break;
2855
2856 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002857 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002858 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002859 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002860 break;
2861
2862 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002863 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002864 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002865 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002866 break;
2867
2868 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002869 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002870 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002871 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002872 break;
2873
2874 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002875 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002876 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002877 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002878 break;
2879
2880 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002881 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002882 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002883 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002884 break;
2885
2886 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002887 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002888 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002889 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002890 break;
2891
2892 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002893 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002894 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002895 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002896 break;
2897
2898 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002899 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002900 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002901 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002902 break;
2903
2904 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002905 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002906 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002907 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 break;
2909
2910 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002911 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002912 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002913 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 break;
2915
2916 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002917 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002918 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002919 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002920 break;
2921 }
2922
Keith Whitwell7c26b612005-04-21 14:46:57 +00002923 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002924 return 1;
2925 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002926 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2927 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002928 }
2929 break;
2930
2931 case OP_ALU_TRI:
2932 switch (code) {
2933 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002934 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002935 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002936 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002937 break;
2938
2939 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002940 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002941 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002942 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002943 break;
2944
2945 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002946 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002947 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002948 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002949 break;
2950 }
2951
Keith Whitwell7c26b612005-04-21 14:46:57 +00002952 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002953 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002954
Jouk Jansen40322e12004-04-05 08:50:36 +00002955 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002956 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2957 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002958 }
2959 break;
2960
2961 case OP_ALU_SWZ:
2962 switch (code) {
2963 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002964 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002965 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002966 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002967 break;
2968 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002969 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002970 return 1;
2971
Keith Whitwell7c26b612005-04-21 14:46:57 +00002972 {
Brian Paul32df89e2005-10-29 18:26:43 +00002973 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002974 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002975 enum register_file file;
2976 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002977
Brian Paul32df89e2005-10-29 18:26:43 +00002978 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002979 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002980 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2981 fp->SrcReg[0].File = file;
2982 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002983 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002984 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2985 swizzle[1],
2986 swizzle[2],
2987 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002988 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002989 break;
2990
2991 case OP_TEX_SAMPLE:
2992 switch (code) {
2993 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002994 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002995 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002996 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002997 break;
2998
2999 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003000 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003001 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003002 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003003 break;
3004
3005 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003006 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003007 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003008 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 break;
3010 }
3011
Keith Whitwell7c26b612005-04-21 14:46:57 +00003012 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003013 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003014
3015 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003016 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003017
3018 /* texImageUnit */
3019 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3020 return 1;
3021 fp->TexSrcUnit = texcoord;
3022
3023 /* texTarget */
3024 switch (*(*inst)++) {
3025 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003026 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003027 break;
3028 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003029 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003030 break;
3031 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003032 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003033 break;
3034 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003035 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003036 break;
3037 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003038 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003039 break;
3040 case TEXTARGET_SHADOW1D:
3041 case TEXTARGET_SHADOW2D:
3042 case TEXTARGET_SHADOWRECT:
3043 /* TODO ARB_fragment_program_shadow code */
3044 break;
3045 }
Brian Paul7e807512005-11-05 17:10:45 +00003046 Program->TexturesUsed[texcoord] |= (1<<fp->TexSrcTarget);
Jouk Jansen40322e12004-04-05 08:50:36 +00003047 break;
3048
3049 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003050 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003051 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003052 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003053 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003054 break;
3055 }
3056
3057 return 0;
3058}
3059
Keith Whitwell7c26b612005-04-21 14:46:57 +00003060static GLuint
3061parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3062 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003063 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003064{
Brian Paul7aebaf32005-10-30 21:23:23 +00003065 GLint mask;
3066 GLuint idx;
3067 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003068
3069 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3070 return 1;
3071
3072 reg->File = file;
3073 reg->Index = idx;
3074 reg->WriteMask = mask;
3075 return 0;
3076}
3077
3078/**
3079 * Handle the parsing out of a masked address register
3080 *
3081 * \param Index - The register index we write to
3082 * \param WriteMask - The mask controlling which components we write (1->write)
3083 *
3084 * \return 0 on sucess, 1 on error
3085 */
3086static GLuint
3087parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3088 struct var_cache **vc_head,
3089 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003090 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003091{
3092 GLint idx;
3093
3094 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3095 return 1;
3096
3097 /* This should be 0x8 */
3098 (*inst)++;
3099
3100 reg->File = PROGRAM_ADDRESS;
3101 reg->Index = idx;
3102
3103 /* Writemask of .x is implied */
3104 reg->WriteMask = 0x1;
3105 return 0;
3106}
3107
3108/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003109 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003110 */
3111static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003112parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3113 struct var_cache **vc_head,
3114 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003115 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003116{
Brian Paul7aebaf32005-10-30 21:23:23 +00003117 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003118 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003119 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003120 GLubyte swizzle[4];
3121 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003122
3123 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003124 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003125
3126 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003127 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003128 return 1;
3129
3130 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003131 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003132
Brian Paul32df89e2005-10-29 18:26:43 +00003133 reg->File = file;
3134 reg->Index = index;
3135 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3136 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003137 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003138 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003139 return 0;
3140}
3141
3142
3143static GLuint
3144parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003145 struct var_cache **vc_head,
3146 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003147 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003148{
Brian Paul7aebaf32005-10-30 21:23:23 +00003149 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003150 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003151 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003152 GLubyte Swizzle[4];
3153 GLboolean IsRelOffset;
3154
3155 /* Grab the sign */
3156 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3157
3158 /* And the src reg */
3159 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3160 return 1;
3161
3162 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003163 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003164
3165 reg->File = File;
3166 reg->Index = Index;
3167 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003168 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003169 reg->RelAddr = IsRelOffset;
3170 return 0;
3171}
3172
3173
Jouk Jansen40322e12004-04-05 08:50:36 +00003174/**
3175 * This is a big mother that handles getting opcodes into the instruction
3176 * and handling the src & dst registers for vertex program instructions
3177 */
3178static GLuint
3179parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3180 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003181 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003182{
3183 GLint a;
3184 GLubyte type, code;
3185
3186 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3187 type = *(*inst)++;
3188
3189 /* The actual opcode name */
3190 code = *(*inst)++;
3191
Brian Paul7e807512005-11-05 17:10:45 +00003192 _mesa_init_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003193 /* Record the position in the program string for debugging */
3194 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003195
3196 switch (type) {
3197 /* XXX: */
3198 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003199 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003200
3201 /* Remember to set SrcReg.RelAddr; */
3202
3203 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003204 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003205 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003206
Jouk Jansen40322e12004-04-05 08:50:36 +00003207 vp->DstReg.File = PROGRAM_ADDRESS;
3208
3209 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003210 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003211 return 1;
3212
3213 break;
3214
3215 case OP_ALU_VECTOR:
3216 switch (code) {
3217 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003218 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003219 break;
3220 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003221 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003222 break;
3223 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003224 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003225 break;
3226 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003227 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 break;
3229 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003230 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003231 break;
3232 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003233
3234 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003236
3237 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003238 return 1;
3239 break;
3240
3241 case OP_ALU_SCALAR:
3242 switch (code) {
3243 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003244 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 break;
3246 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003247 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 break;
3249 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003250 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 break;
3252 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003253 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 break;
3255 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003256 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003257 break;
3258 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003259 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003260 break;
3261 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003262 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003263 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003264
3265 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003266 return 1;
3267 break;
3268
3269 case OP_ALU_BINSC:
3270 switch (code) {
3271 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003272 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003273 break;
3274 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003275 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003277
Jouk Jansen40322e12004-04-05 08:50:36 +00003278 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003279 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003280 return 1;
3281 }
3282 break;
3283
3284 case OP_ALU_BIN:
3285 switch (code) {
3286 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003287 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 break;
3289 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003290 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003291 break;
3292 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003293 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 break;
3295 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003296 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 break;
3298 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003299 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 break;
3301 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003302 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003303 break;
3304 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003305 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003306 break;
3307 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003308 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 break;
3310 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003311 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003312 break;
3313 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003314 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003315 break;
3316 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003317 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003318 break;
3319 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003320 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003321 break;
3322 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003323 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003324 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003325
Jouk Jansen40322e12004-04-05 08:50:36 +00003326 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003327 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003328 return 1;
3329 }
3330 break;
3331
3332 case OP_ALU_TRI:
3333 switch (code) {
3334 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003335 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003336 break;
3337 }
3338
Keith Whitwell7c26b612005-04-21 14:46:57 +00003339 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003340 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003341
Jouk Jansen40322e12004-04-05 08:50:36 +00003342 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003343 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003344 return 1;
3345 }
3346 break;
3347
3348 case OP_ALU_SWZ:
3349 switch (code) {
3350 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003351 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003352 break;
3353 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003354 {
Brian Paul32df89e2005-10-29 18:26:43 +00003355 GLubyte swizzle[4];
3356 GLubyte negateMask;
3357 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003358 enum register_file file;
3359 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003360
Keith Whitwell7c26b612005-04-21 14:46:57 +00003361 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3362 return 1;
3363
Brian Paul32df89e2005-10-29 18:26:43 +00003364 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003365 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003366 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3367 vp->SrcReg[0].File = file;
3368 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003369 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003370 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3371 swizzle[1],
3372 swizzle[2],
3373 swizzle[3]);
3374 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003375 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003376 break;
3377 }
3378 return 0;
3379}
3380
3381#if DEBUG_PARSING
3382
3383static GLvoid
3384print_state_token (GLint token)
3385{
3386 switch (token) {
3387 case STATE_MATERIAL:
3388 fprintf (stderr, "STATE_MATERIAL ");
3389 break;
3390 case STATE_LIGHT:
3391 fprintf (stderr, "STATE_LIGHT ");
3392 break;
3393
3394 case STATE_LIGHTMODEL_AMBIENT:
3395 fprintf (stderr, "STATE_AMBIENT ");
3396 break;
3397
3398 case STATE_LIGHTMODEL_SCENECOLOR:
3399 fprintf (stderr, "STATE_SCENECOLOR ");
3400 break;
3401
3402 case STATE_LIGHTPROD:
3403 fprintf (stderr, "STATE_LIGHTPROD ");
3404 break;
3405
3406 case STATE_TEXGEN:
3407 fprintf (stderr, "STATE_TEXGEN ");
3408 break;
3409
3410 case STATE_FOG_COLOR:
3411 fprintf (stderr, "STATE_FOG_COLOR ");
3412 break;
3413
3414 case STATE_FOG_PARAMS:
3415 fprintf (stderr, "STATE_FOG_PARAMS ");
3416 break;
3417
3418 case STATE_CLIPPLANE:
3419 fprintf (stderr, "STATE_CLIPPLANE ");
3420 break;
3421
3422 case STATE_POINT_SIZE:
3423 fprintf (stderr, "STATE_POINT_SIZE ");
3424 break;
3425
3426 case STATE_POINT_ATTENUATION:
3427 fprintf (stderr, "STATE_ATTENUATION ");
3428 break;
3429
3430 case STATE_MATRIX:
3431 fprintf (stderr, "STATE_MATRIX ");
3432 break;
3433
3434 case STATE_MODELVIEW:
3435 fprintf (stderr, "STATE_MODELVIEW ");
3436 break;
3437
3438 case STATE_PROJECTION:
3439 fprintf (stderr, "STATE_PROJECTION ");
3440 break;
3441
3442 case STATE_MVP:
3443 fprintf (stderr, "STATE_MVP ");
3444 break;
3445
3446 case STATE_TEXTURE:
3447 fprintf (stderr, "STATE_TEXTURE ");
3448 break;
3449
3450 case STATE_PROGRAM:
3451 fprintf (stderr, "STATE_PROGRAM ");
3452 break;
3453
3454 case STATE_MATRIX_INVERSE:
3455 fprintf (stderr, "STATE_INVERSE ");
3456 break;
3457
3458 case STATE_MATRIX_TRANSPOSE:
3459 fprintf (stderr, "STATE_TRANSPOSE ");
3460 break;
3461
3462 case STATE_MATRIX_INVTRANS:
3463 fprintf (stderr, "STATE_INVTRANS ");
3464 break;
3465
3466 case STATE_AMBIENT:
3467 fprintf (stderr, "STATE_AMBIENT ");
3468 break;
3469
3470 case STATE_DIFFUSE:
3471 fprintf (stderr, "STATE_DIFFUSE ");
3472 break;
3473
3474 case STATE_SPECULAR:
3475 fprintf (stderr, "STATE_SPECULAR ");
3476 break;
3477
3478 case STATE_EMISSION:
3479 fprintf (stderr, "STATE_EMISSION ");
3480 break;
3481
3482 case STATE_SHININESS:
3483 fprintf (stderr, "STATE_SHININESS ");
3484 break;
3485
3486 case STATE_HALF:
3487 fprintf (stderr, "STATE_HALF ");
3488 break;
3489
3490 case STATE_POSITION:
3491 fprintf (stderr, "STATE_POSITION ");
3492 break;
3493
3494 case STATE_ATTENUATION:
3495 fprintf (stderr, "STATE_ATTENUATION ");
3496 break;
3497
3498 case STATE_SPOT_DIRECTION:
3499 fprintf (stderr, "STATE_DIRECTION ");
3500 break;
3501
3502 case STATE_TEXGEN_EYE_S:
3503 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3504 break;
3505
3506 case STATE_TEXGEN_EYE_T:
3507 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3508 break;
3509
3510 case STATE_TEXGEN_EYE_R:
3511 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3512 break;
3513
3514 case STATE_TEXGEN_EYE_Q:
3515 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3516 break;
3517
3518 case STATE_TEXGEN_OBJECT_S:
3519 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3520 break;
3521
3522 case STATE_TEXGEN_OBJECT_T:
3523 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3524 break;
3525
3526 case STATE_TEXGEN_OBJECT_R:
3527 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3528 break;
3529
3530 case STATE_TEXGEN_OBJECT_Q:
3531 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3532 break;
3533
3534 case STATE_TEXENV_COLOR:
3535 fprintf (stderr, "STATE_TEXENV_COLOR ");
3536 break;
3537
3538 case STATE_DEPTH_RANGE:
3539 fprintf (stderr, "STATE_DEPTH_RANGE ");
3540 break;
3541
3542 case STATE_VERTEX_PROGRAM:
3543 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3544 break;
3545
3546 case STATE_FRAGMENT_PROGRAM:
3547 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3548 break;
3549
3550 case STATE_ENV:
3551 fprintf (stderr, "STATE_ENV ");
3552 break;
3553
3554 case STATE_LOCAL:
3555 fprintf (stderr, "STATE_LOCAL ");
3556 break;
3557
3558 }
3559 fprintf (stderr, "[%d] ", token);
3560}
3561
3562
3563static GLvoid
3564debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3565 struct arb_program *Program)
3566{
3567 struct var_cache *vc;
3568 GLint a, b;
3569
3570 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3571
3572 /* First of all, print out the contents of the var_cache */
3573 vc = vc_head;
3574 while (vc) {
3575 fprintf (stderr, "[%x]\n", vc);
3576 switch (vc->type) {
3577 case vt_none:
3578 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3579 break;
3580 case vt_attrib:
3581 fprintf (stderr, "ATTRIB %s\n", vc->name);
3582 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3583 break;
3584 case vt_param:
3585 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3586 vc->param_binding_begin, vc->param_binding_length);
3587 b = vc->param_binding_begin;
3588 for (a = 0; a < vc->param_binding_length; a++) {
3589 fprintf (stderr, "%s\n",
3590 Program->Parameters->Parameters[a + b].Name);
3591 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3592 print_state_token (Program->Parameters->Parameters[a + b].
3593 StateIndexes[0]);
3594 print_state_token (Program->Parameters->Parameters[a + b].
3595 StateIndexes[1]);
3596 print_state_token (Program->Parameters->Parameters[a + b].
3597 StateIndexes[2]);
3598 print_state_token (Program->Parameters->Parameters[a + b].
3599 StateIndexes[3]);
3600 print_state_token (Program->Parameters->Parameters[a + b].
3601 StateIndexes[4]);
3602 print_state_token (Program->Parameters->Parameters[a + b].
3603 StateIndexes[5]);
3604 }
3605 else
3606 fprintf (stderr, "%f %f %f %f\n",
3607 Program->Parameters->Parameters[a + b].Values[0],
3608 Program->Parameters->Parameters[a + b].Values[1],
3609 Program->Parameters->Parameters[a + b].Values[2],
3610 Program->Parameters->Parameters[a + b].Values[3]);
3611 }
3612 break;
3613 case vt_temp:
3614 fprintf (stderr, "TEMP %s\n", vc->name);
3615 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3616 break;
3617 case vt_output:
3618 fprintf (stderr, "OUTPUT %s\n", vc->name);
3619 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3620 break;
3621 case vt_alias:
3622 fprintf (stderr, "ALIAS %s\n", vc->name);
3623 fprintf (stderr, " binding: 0x%x (%s)\n",
3624 vc->alias_binding, vc->alias_binding->name);
3625 break;
3626 }
3627 vc = vc->next;
3628 }
3629}
3630
Brian Paul72030e02005-11-03 03:30:34 +00003631#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003632
3633
3634/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003635 * The main loop for parsing a fragment or vertex program
3636 *
Brian Paul72030e02005-11-03 03:30:34 +00003637 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003638 */
Brian Paul72030e02005-11-03 03:30:34 +00003639static GLint
Brian Paul8c41a142005-11-19 15:36:28 +00003640parse_instructions(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
Brian Paul45703642005-10-29 15:52:31 +00003641 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003642{
Brian Paul72030e02005-11-03 03:30:34 +00003643 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3644 ? ctx->Const.FragmentProgram.MaxInstructions
3645 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003646 GLint err = 0;
3647
Brian Paul72030e02005-11-03 03:30:34 +00003648 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3649
Jouk Jansen40322e12004-04-05 08:50:36 +00003650 Program->MajorVersion = (GLuint) * inst++;
3651 Program->MinorVersion = (GLuint) * inst++;
3652
3653 while (*inst != END) {
3654 switch (*inst++) {
3655
3656 case OPTION:
3657 switch (*inst++) {
3658 case ARB_PRECISION_HINT_FASTEST:
3659 Program->PrecisionOption = GL_FASTEST;
3660 break;
3661
3662 case ARB_PRECISION_HINT_NICEST:
3663 Program->PrecisionOption = GL_NICEST;
3664 break;
3665
3666 case ARB_FOG_EXP:
3667 Program->FogOption = GL_EXP;
3668 break;
3669
3670 case ARB_FOG_EXP2:
3671 Program->FogOption = GL_EXP2;
3672 break;
3673
3674 case ARB_FOG_LINEAR:
3675 Program->FogOption = GL_LINEAR;
3676 break;
3677
3678 case ARB_POSITION_INVARIANT:
3679 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003680 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003681 break;
3682
3683 case ARB_FRAGMENT_PROGRAM_SHADOW:
3684 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3685 /* TODO ARB_fragment_program_shadow code */
3686 }
3687 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003688
3689 case ARB_DRAW_BUFFERS:
3690 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3691 /* do nothing for now */
3692 }
3693 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003694 }
3695 break;
3696
3697 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003698 /* check length */
3699 if (Program->Base.NumInstructions + 1 >= maxInst) {
3700 const char *msg = "Max instruction count exceeded";
3701 _mesa_set_program_error(ctx, Program->Position, msg);
3702 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3703 return 1;
3704 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003705 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003706 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003707 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003708 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003709 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003710 }
3711 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003712 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003713 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003714 }
3715
Brian Paul72030e02005-11-03 03:30:34 +00003716 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 Program->Base.NumInstructions++;
3718 break;
3719
3720 case DECLARATION:
3721 err = parse_declaration (ctx, &inst, vc_head, Program);
3722 break;
3723
3724 default:
3725 break;
3726 }
3727
3728 if (err)
3729 break;
3730 }
3731
3732 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003733 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003734 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul8c41a142005-11-19 15:36:28 +00003735 _mesa_init_instruction(Program->Base.Instructions + numInst);
3736 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003737 /* YYY Wrong Position in program, whatever, at least not random -> crash
3738 Program->Position = parse_position (&inst);
3739 */
Brian Paul8c41a142005-11-19 15:36:28 +00003740 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003741 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003742 Program->Base.NumInstructions++;
3743
Brian Paul05051032005-11-01 04:36:33 +00003744 /*
3745 * Initialize native counts to logical counts. The device driver may
3746 * change them if program is translated into a hardware program.
3747 */
3748 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3749 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3750 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3751 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3752 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003753
Jouk Jansen40322e12004-04-05 08:50:36 +00003754 return err;
3755}
3756
Brian Paul7aebaf32005-10-30 21:23:23 +00003757
Jouk Jansen40322e12004-04-05 08:50:36 +00003758/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003759__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003760#include "grammar_syn.h"
3761;
3762
Brian Paul7aebaf32005-10-30 21:23:23 +00003763
Brian Paul8c41a142005-11-19 15:36:28 +00003764/**
3765 * Set a grammar parameter.
3766 * \param name the grammar parameter
3767 * \param value the new parameter value
3768 * \return 0 if OK, 1 if error
3769 */
3770static int
3771set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003772{
3773 char error_msg[300];
3774 GLint error_pos;
3775
Brian Paul8c41a142005-11-19 15:36:28 +00003776 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003777 return 0;
3778
3779 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3780 _mesa_set_program_error (ctx, error_pos, error_msg);
3781 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3782 return 1;
3783}
3784
Brian Paul8c41a142005-11-19 15:36:28 +00003785
3786/**
3787 * Enable support for the given language option in the parser.
3788 * \return 1 if OK, 0 if error
3789 */
3790static int
3791enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003792{
Brian Paul8c41a142005-11-19 15:36:28 +00003793 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003794}
3795
Brian Paul8c41a142005-11-19 15:36:28 +00003796
3797/**
3798 * Enable parser extensions based on which OpenGL extensions are supported
3799 * by this rendering context.
3800 *
3801 * \return GL_TRUE if OK, GL_FALSE if error.
3802 */
3803static GLboolean
3804enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003805{
Brian Paul8c41a142005-11-19 15:36:28 +00003806#if 0
3807 /* These are not supported at this time */
3808 if ((ctx->Extensions.ARB_vertex_blend ||
3809 ctx->Extensions.EXT_vertex_weighting)
3810 && !enable_ext(ctx, id, "point_parameters"))
3811 return GL_FALSE;
3812 if (ctx->Extensions.ARB_matrix_palette
3813 && !enable_ext(ctx, id, "matrix_palette"))
3814 return GL_FALSE;
3815 if (ctx->Extensions.ARB_fragment_program_shadow
3816 && !enable_ext(ctx, id, "fragment_program_shadow"))
3817 return GL_FALSE;
3818#endif
3819 if (ctx->Extensions.EXT_point_parameters
3820 && !enable_ext(ctx, id, "point_parameters"))
3821 return GL_FALSE;
3822 if (ctx->Extensions.EXT_secondary_color
3823 && !enable_ext(ctx, id, "secondary_color"))
3824 return GL_FALSE;
3825 if (ctx->Extensions.EXT_fog_coord
3826 && !enable_ext(ctx, id, "fog_coord"))
3827 return GL_FALSE;
3828 if (ctx->Extensions.NV_texture_rectangle
3829 && !enable_ext(ctx, id, "texture_rectangle"))
3830 return GL_FALSE;
3831 if (ctx->Extensions.ARB_draw_buffers
3832 && !enable_ext(ctx, id, "draw_buffers"))
3833 return GL_FALSE;
3834
3835 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003836}
3837
Brian Paul8c41a142005-11-19 15:36:28 +00003838
Jouk Jansen40322e12004-04-05 08:50:36 +00003839/**
3840 * This kicks everything off.
3841 *
3842 * \param ctx - The GL Context
3843 * \param str - The program string
3844 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003845 * \param program - The arb_program struct to return all the parsed info in
3846 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003847 */
Brian Paul8c41a142005-11-19 15:36:28 +00003848static GLboolean
3849_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3850 const GLubyte *str, GLsizei len,
3851 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003852{
3853 GLint a, err, error_pos;
3854 char error_msg[300];
3855 GLuint parsed_len;
3856 struct var_cache *vc_head;
3857 grammar arbprogram_syn_id;
3858 GLubyte *parsed, *inst;
3859 GLubyte *strz = NULL;
3860 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3861
Brian Paul8c41a142005-11-19 15:36:28 +00003862 /* set the program target before parsing */
3863 program->Base.Target = target;
3864
Brian Paul7f76b8f2004-09-10 01:05:39 +00003865 /* Reset error state */
3866 _mesa_set_program_error(ctx, -1, NULL);
3867
Brian Paul8c41a142005-11-19 15:36:28 +00003868 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003869 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003870 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003871 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003872 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003873
3874 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3875 if (grammar_syn_id == 0) {
3876 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003877 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003878 _mesa_set_program_error (ctx, error_pos, error_msg);
3879 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003880 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003881 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003882 }
3883
Brian Paul8c41a142005-11-19 15:36:28 +00003884 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3885 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003886
Brian Paul49a80ca2006-04-28 15:40:11 +00003887 /* 'parsed' is unused here */
3888 _mesa_free (parsed);
3889 parsed = NULL;
3890
Brian Paul45703642005-10-29 15:52:31 +00003891 /* NOTE: we can't destroy grammar_syn_id right here because
3892 * grammar_destroy() can reset the last error
3893 */
Brian Paul8c41a142005-11-19 15:36:28 +00003894 if (err) {
3895 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003896 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3897 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003898 _mesa_error (ctx, GL_INVALID_OPERATION,
3899 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003900 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003901 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003902 }
3903
3904 grammar_destroy (grammar_syn_id);
3905
3906 arbprogram_syn_is_ok = 1;
3907 }
3908
3909 /* create the grammar object */
3910 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3911 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003912 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003913 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3914 _mesa_set_program_error (ctx, error_pos, error_msg);
3915 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003916 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003917 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003918 }
3919
3920 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003921 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003922 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3923 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003924 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003925 }
3926
Brian Paul8c41a142005-11-19 15:36:28 +00003927 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3928 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003929 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003930 }
3931
3932 /* check for NULL character occurences */
3933 {
Brian Paul8c41a142005-11-19 15:36:28 +00003934 GLint i;
3935 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003936 if (str[i] == '\0') {
3937 _mesa_set_program_error (ctx, i, "invalid character");
Brian Paul8c41a142005-11-19 15:36:28 +00003938 _mesa_error (ctx, GL_INVALID_OPERATION,
3939 "glProgramStringARB(illegal character)");
Jouk Jansen40322e12004-04-05 08:50:36 +00003940 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003941 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003942 }
Brian Paul8c41a142005-11-19 15:36:28 +00003943 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003944 }
3945
3946 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003947 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003948 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003949 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3950 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003951 return GL_FALSE;
3952 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003953 _mesa_memcpy (strz, str, len);
3954 strz[len] = '\0';
3955
Michal Krolb80bc052004-10-21 14:09:54 +00003956 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003957 err = !grammar_fast_check(arbprogram_syn_id, strz,
3958 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003959
3960 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003961 if (err) {
3962 _mesa_free(strz);
Brian Paul49a80ca2006-04-28 15:40:11 +00003963 _mesa_free(parsed);
Jouk Jansen40322e12004-04-05 08:50:36 +00003964 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3965 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003966 _mesa_error (ctx, GL_INVALID_OPERATION,
3967 "glProgramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003968
3969 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003970#if DEBUG_PARSING
3971 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003972 int line, col;
3973 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003974 fprintf(stderr, "program: %s\n", (char *) strz);
3975 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
3976 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003977 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3978 } while (0)
3979#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003980
Jouk Jansen40322e12004-04-05 08:50:36 +00003981 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003982 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003983 }
3984
Jouk Jansen40322e12004-04-05 08:50:36 +00003985 grammar_destroy (arbprogram_syn_id);
3986
Brian Paul8c41a142005-11-19 15:36:28 +00003987 /*
3988 * Program string is syntactically correct at this point
3989 * Parse the tokenized version of the program now, generating
3990 * vertex/fragment program instructions.
3991 */
3992
Jouk Jansen40322e12004-04-05 08:50:36 +00003993 /* Initialize the arb_program struct */
3994 program->Base.String = strz;
Brian Paul8c41a142005-11-19 15:36:28 +00003995 program->Base.Instructions = (struct prog_instruction *)
3996 _mesa_malloc(MAX_INSTRUCTIONS * sizeof(struct prog_instruction));
Jouk Jansen40322e12004-04-05 08:50:36 +00003997 program->Base.NumInstructions =
3998 program->Base.NumTemporaries =
3999 program->Base.NumParameters =
4000 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00004001 program->Base.Parameters = _mesa_new_parameter_list ();
4002 program->Base.InputsRead = 0x0;
4003 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004004 program->Position = 0;
4005 program->MajorVersion = program->MinorVersion = 0;
4006 program->PrecisionOption = GL_DONT_CARE;
4007 program->FogOption = GL_NONE;
4008 program->HintPositionInvariant = GL_FALSE;
4009 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00004010 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004011 program->NumAluInstructions =
4012 program->NumTexInstructions =
4013 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00004014 program->UsesKill = 0;
4015
Jouk Jansen40322e12004-04-05 08:50:36 +00004016 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00004017 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004018
4019 /* Start examining the tokens in the array */
4020 inst = parsed;
4021
4022 /* Check the grammer rev */
4023 if (*inst++ != REVISION) {
4024 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00004025 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul45cd2f92005-11-03 02:25:10 +00004026 "glProgramStringARB(Grammar version mismatch)");
Brian Paul45703642005-10-29 15:52:31 +00004027 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004028 }
4029 else {
Michal Krolb80bc052004-10-21 14:09:54 +00004030 /* ignore program target */
4031 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00004032 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00004033 }
4034
4035 /*debug_variables(ctx, vc_head, program); */
4036
4037 /* We're done with the parsed binary array */
4038 var_cache_destroy (&vc_head);
4039
4040 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00004041
Brian Paul8c41a142005-11-19 15:36:28 +00004042 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4043 * to size [ap.Base.NumInstructions].
4044 */
4045 program->Base.Instructions = (struct prog_instruction *)
4046 _mesa_realloc(program->Base.Instructions,
4047 MAX_INSTRUCTIONS * sizeof(struct prog_instruction),/*orig*/
4048 program->Base.NumInstructions * sizeof(struct prog_instruction));
4049
Brian Paul45703642005-10-29 15:52:31 +00004050 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004051}
Brian Paul8c41a142005-11-19 15:36:28 +00004052
4053
4054
4055void
4056_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4057 const GLvoid *str, GLsizei len,
4058 struct fragment_program *program)
4059{
4060 struct arb_program ap;
4061 GLuint i;
4062
4063 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004064 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004065 /* Error in the program. Just return. */
4066 return;
4067 }
4068
4069 /* Copy the relevant contents of the arb_program struct into the
4070 * fragment_program struct.
4071 */
4072 program->Base.String = ap.Base.String;
4073 program->Base.NumInstructions = ap.Base.NumInstructions;
4074 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4075 program->Base.NumParameters = ap.Base.NumParameters;
4076 program->Base.NumAttributes = ap.Base.NumAttributes;
4077 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4078 program->NumAluInstructions = ap.NumAluInstructions;
4079 program->NumTexInstructions = ap.NumTexInstructions;
4080 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004081 program->NumNativeAluInstructions = ap.NumAluInstructions;
4082 program->NumNativeTexInstructions = ap.NumTexInstructions;
4083 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004084 program->Base.InputsRead = ap.Base.InputsRead;
4085 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4086 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4087 program->TexturesUsed[i] = ap.TexturesUsed[i];
4088 program->FogOption = ap.FogOption;
4089
4090 if (program->Base.Instructions)
4091 _mesa_free(program->Base.Instructions);
4092 program->Base.Instructions = ap.Base.Instructions;
4093
4094 if (program->Base.Parameters)
4095 _mesa_free_parameter_list(program->Base.Parameters);
4096 program->Base.Parameters = ap.Base.Parameters;
4097
4098#if DEBUG_FP
4099 _mesa_print_program(&program.Base);
4100#endif
4101}
4102
4103
4104
4105/**
4106 * Parse the vertex program string. If success, update the given
4107 * vertex_program object with the new program. Else, leave the vertex_program
4108 * object unchanged.
4109 */
4110void
4111_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4112 const GLvoid *str, GLsizei len,
4113 struct vertex_program *program)
4114{
4115 struct arb_program ap;
4116
4117 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4118
Brian Paul95801792005-12-06 15:41:43 +00004119 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004120 /* Error in the program. Just return. */
4121 return;
4122 }
4123
4124 /* Copy the relevant contents of the arb_program struct into the
4125 * vertex_program struct.
4126 */
4127 program->Base.String = ap.Base.String;
4128 program->Base.NumInstructions = ap.Base.NumInstructions;
4129 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4130 program->Base.NumParameters = ap.Base.NumParameters;
4131 program->Base.NumAttributes = ap.Base.NumAttributes;
4132 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4133 program->Base.InputsRead = ap.Base.InputsRead;
4134 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4135 program->IsPositionInvariant = ap.HintPositionInvariant;
4136
4137 if (program->Base.Instructions)
4138 _mesa_free(program->Base.Instructions);
4139 program->Base.Instructions = ap.Base.Instructions;
4140
4141 if (program->Base.Parameters)
4142 _mesa_free_parameter_list(program->Base.Parameters);
4143 program->Base.Parameters = ap.Base.Parameters;
4144
4145#if DEBUG_VP
4146 _mesa_print_program(&program->Base);
4147#endif
4148}