blob: 66ea96b4d3802504a6ae20cf4711ebf51212224a [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian Paul54cfe692005-10-21 15:22:36 +00003 * Version: 6.5
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Michal Krolbb38cad2006-04-11 11:41:11 +00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
Jouk Jansen40322e12004-04-05 08:50:36 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#define DEBUG_PARSING 0
26
27/**
28 * \file arbprogparse.c
29 * ARB_*_program parser core
30 * \author Karl Rasche
31 */
32
Jouk Jansen40322e12004-04-05 08:50:36 +000033#include "glheader.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000034#include "imports.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000035#include "arbprogparse.h"
36#include "grammar_mesa.h"
Brian Paul32df89e2005-10-29 18:26:43 +000037#include "program.h"
Brian Paul8c41a142005-11-19 15:36:28 +000038#include "context.h"
39#include "mtypes.h"
40#include "program_instruction.h"
41
42
43#define MAX_INSTRUCTIONS 256
44
45
46/**
47 * This is basically a union of the vertex_program and fragment_program
48 * structs that we can use to parse the program into
49 *
50 * XXX we can probably get rid of this entirely someday.
51 */
52struct arb_program
53{
54 struct program Base;
55
56 GLuint Position; /* Just used for error reporting while parsing */
57 GLuint MajorVersion;
58 GLuint MinorVersion;
59
60 /* ARB_vertex_progmra options */
61 GLboolean HintPositionInvariant;
62
63 /* ARB_fragment_progmra options */
64 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
65 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
66
67 /* ARB_fragment_program specifics */
68 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
69 GLuint NumAluInstructions;
70 GLuint NumTexInstructions;
71 GLuint NumTexIndirections;
72
73 GLboolean UsesKill;
74};
75
Ian Romanick9bdfee32005-07-18 12:31:24 +000076
Alan Hourihane38b317d2004-12-14 09:11:52 +000077#ifndef __extension__
78#if !defined(__GNUC__) || (__GNUC__ < 2) || \
79 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
Brian Paula6c423d2004-08-25 15:59:48 +000080# define __extension__
81#endif
Alan Hourihane38b317d2004-12-14 09:11:52 +000082#endif
Brian Paula6c423d2004-08-25 15:59:48 +000083
Jouk Jansen40322e12004-04-05 08:50:36 +000084/* TODO:
85 * Fragment Program Stuff:
86 * -----------------------------------------------------
87 *
88 * - things from Michal's email
89 * + overflow on atoi
90 * + not-overflowing floats (don't use parse_integer..)
91 * + can remove range checking in arbparse.c
92 *
93 * - check all limits of number of various variables
94 * + parameters
95 *
96 * - test! test! test!
97 *
98 * Vertex Program Stuff:
99 * -----------------------------------------------------
100 * - Optimize param array usage and count limits correctly, see spec,
101 * section 2.14.3.7
102 * + Record if an array is reference absolutly or relatively (or both)
103 * + For absolute arrays, store a bitmap of accesses
104 * + For single parameters, store an access flag
105 * + After parsing, make a parameter cleanup and merging pass, where
106 * relative arrays are layed out first, followed by abs arrays, and
107 * finally single state.
108 * + Remap offsets for param src and dst registers
109 * + Now we can properly count parameter usage
110 *
111 * - Multiple state binding errors in param arrays (see spec, just before
112 * section 2.14.3.3)
113 * - grep for XXX
114 *
115 * Mesa Stuff
116 * -----------------------------------------------------
117 * - User clipping planes vs. PositionInvariant
118 * - Is it sufficient to just multiply by the mvp to transform in the
119 * PositionInvariant case? Or do we need something more involved?
120 *
121 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
122 * - fetch state listed in program_parameters list
123 * + WTF should this go???
124 * + currently in nvvertexec.c and s_nvfragprog.c
125 *
126 * - allow for multiple address registers (and fetch address regs properly)
127 *
128 * Cosmetic Stuff
129 * -----------------------------------------------------
130 * - remove any leftover unused grammer.c stuff (dict_ ?)
131 * - fix grammer.c error handling so its not static
132 * - #ifdef around stuff pertaining to extentions
133 *
134 * Outstanding Questions:
135 * -----------------------------------------------------
136 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
137 * what gets hacked off because of this:
138 * + VERTEX_ATTRIB_MATRIXINDEX
139 * + VERTEX_ATTRIB_WEIGHT
140 * + MATRIX_MODELVIEW
141 * + MATRIX_PALETTE
142 *
143 * - When can we fetch env/local params from their own register files, and
144 * when to we have to fetch them into the main state register file?
145 * (think arrays)
146 *
147 * Grammar Changes:
148 * -----------------------------------------------------
149 */
150
151/* Changes since moving the file to shader directory
152
1532004-III-4 ------------------------------------------------------------
154- added #include "grammar_mesa.h"
155- removed grammar specific code part (it resides now in grammar.c)
156- added GL_ARB_fragment_program_shadow tokens
157- modified #include "arbparse_syn.h"
158- major changes inside _mesa_parse_arb_program()
159- check the program string for '\0' characters
160- copy the program string to a one-byte-longer location to have
161 it null-terminated
162- position invariance test (not writing to result.position) moved
163 to syntax part
164*/
165
166typedef GLubyte *production;
167
168/**
169 * This is the text describing the rules to parse the grammar
170 */
Brian Paula6c423d2004-08-25 15:59:48 +0000171__extension__ static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000172#include "arbprogram_syn.h"
173;
174
175/**
176 * These should match up with the values defined in arbprogram.syn
177 */
178
179/*
180 Changes:
181 - changed and merged V_* and F_* opcode values to OP_*.
182 - added GL_ARB_fragment_program_shadow specific tokens (michal)
183*/
Michal Krolb80bc052004-10-21 14:09:54 +0000184#define REVISION 0x09
Jouk Jansen40322e12004-04-05 08:50:36 +0000185
186/* program type */
187#define FRAGMENT_PROGRAM 0x01
188#define VERTEX_PROGRAM 0x02
189
190/* program section */
191#define OPTION 0x01
192#define INSTRUCTION 0x02
193#define DECLARATION 0x03
194#define END 0x04
195
Michal Krolb80bc052004-10-21 14:09:54 +0000196/* GL_ARB_fragment_program option */
197#define ARB_PRECISION_HINT_FASTEST 0x00
198#define ARB_PRECISION_HINT_NICEST 0x01
199#define ARB_FOG_EXP 0x02
200#define ARB_FOG_EXP2 0x03
201#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000202
Michal Krolb80bc052004-10-21 14:09:54 +0000203/* GL_ARB_vertex_program option */
204#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000205
Michal Krolb80bc052004-10-21 14:09:54 +0000206/* GL_ARB_fragment_program_shadow option */
207#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000208
Michal Krolb80bc052004-10-21 14:09:54 +0000209/* GL_ARB_draw_buffers option */
210#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000211
Jouk Jansen40322e12004-04-05 08:50:36 +0000212/* GL_ARB_fragment_program instruction class */
213#define OP_ALU_INST 0x00
214#define OP_TEX_INST 0x01
215
216/* GL_ARB_vertex_program instruction class */
217/* OP_ALU_INST */
218
219/* GL_ARB_fragment_program instruction type */
220#define OP_ALU_VECTOR 0x00
221#define OP_ALU_SCALAR 0x01
222#define OP_ALU_BINSC 0x02
223#define OP_ALU_BIN 0x03
224#define OP_ALU_TRI 0x04
225#define OP_ALU_SWZ 0x05
226#define OP_TEX_SAMPLE 0x06
227#define OP_TEX_KIL 0x07
228
229/* GL_ARB_vertex_program instruction type */
230#define OP_ALU_ARL 0x08
231/* OP_ALU_VECTOR */
232/* OP_ALU_SCALAR */
233/* OP_ALU_BINSC */
234/* OP_ALU_BIN */
235/* OP_ALU_TRI */
236/* OP_ALU_SWZ */
237
238/* GL_ARB_fragment_program instruction code */
239#define OP_ABS 0x00
240#define OP_ABS_SAT 0x1B
241#define OP_FLR 0x09
242#define OP_FLR_SAT 0x26
243#define OP_FRC 0x0A
244#define OP_FRC_SAT 0x27
245#define OP_LIT 0x0C
246#define OP_LIT_SAT 0x2A
247#define OP_MOV 0x11
248#define OP_MOV_SAT 0x30
249#define OP_COS 0x1F
250#define OP_COS_SAT 0x20
251#define OP_EX2 0x07
252#define OP_EX2_SAT 0x25
253#define OP_LG2 0x0B
254#define OP_LG2_SAT 0x29
255#define OP_RCP 0x14
256#define OP_RCP_SAT 0x33
257#define OP_RSQ 0x15
258#define OP_RSQ_SAT 0x34
259#define OP_SIN 0x38
260#define OP_SIN_SAT 0x39
261#define OP_SCS 0x35
262#define OP_SCS_SAT 0x36
263#define OP_POW 0x13
264#define OP_POW_SAT 0x32
265#define OP_ADD 0x01
266#define OP_ADD_SAT 0x1C
267#define OP_DP3 0x03
268#define OP_DP3_SAT 0x21
269#define OP_DP4 0x04
270#define OP_DP4_SAT 0x22
271#define OP_DPH 0x05
272#define OP_DPH_SAT 0x23
273#define OP_DST 0x06
274#define OP_DST_SAT 0x24
275#define OP_MAX 0x0F
276#define OP_MAX_SAT 0x2E
277#define OP_MIN 0x10
278#define OP_MIN_SAT 0x2F
279#define OP_MUL 0x12
280#define OP_MUL_SAT 0x31
281#define OP_SGE 0x16
282#define OP_SGE_SAT 0x37
283#define OP_SLT 0x17
284#define OP_SLT_SAT 0x3A
285#define OP_SUB 0x18
286#define OP_SUB_SAT 0x3B
287#define OP_XPD 0x1A
288#define OP_XPD_SAT 0x43
289#define OP_CMP 0x1D
290#define OP_CMP_SAT 0x1E
291#define OP_LRP 0x2B
292#define OP_LRP_SAT 0x2C
293#define OP_MAD 0x0E
294#define OP_MAD_SAT 0x2D
295#define OP_SWZ 0x19
296#define OP_SWZ_SAT 0x3C
297#define OP_TEX 0x3D
298#define OP_TEX_SAT 0x3E
299#define OP_TXB 0x3F
300#define OP_TXB_SAT 0x40
301#define OP_TXP 0x41
302#define OP_TXP_SAT 0x42
303#define OP_KIL 0x28
304
305/* GL_ARB_vertex_program instruction code */
306#define OP_ARL 0x02
307/* OP_ABS */
308/* OP_FLR */
309/* OP_FRC */
310/* OP_LIT */
311/* OP_MOV */
312/* OP_EX2 */
313#define OP_EXP 0x08
314/* OP_LG2 */
315#define OP_LOG 0x0D
316/* OP_RCP */
317/* OP_RSQ */
318/* OP_POW */
319/* OP_ADD */
320/* OP_DP3 */
321/* OP_DP4 */
322/* OP_DPH */
323/* OP_DST */
324/* OP_MAX */
325/* OP_MIN */
326/* OP_MUL */
327/* OP_SGE */
328/* OP_SLT */
329/* OP_SUB */
330/* OP_XPD */
331/* OP_MAD */
332/* OP_SWZ */
333
334/* fragment attribute binding */
335#define FRAGMENT_ATTRIB_COLOR 0x01
336#define FRAGMENT_ATTRIB_TEXCOORD 0x02
337#define FRAGMENT_ATTRIB_FOGCOORD 0x03
338#define FRAGMENT_ATTRIB_POSITION 0x04
339
340/* vertex attribute binding */
341#define VERTEX_ATTRIB_POSITION 0x01
342#define VERTEX_ATTRIB_WEIGHT 0x02
343#define VERTEX_ATTRIB_NORMAL 0x03
344#define VERTEX_ATTRIB_COLOR 0x04
345#define VERTEX_ATTRIB_FOGCOORD 0x05
346#define VERTEX_ATTRIB_TEXCOORD 0x06
347#define VERTEX_ATTRIB_MATRIXINDEX 0x07
348#define VERTEX_ATTRIB_GENERIC 0x08
349
350/* fragment result binding */
351#define FRAGMENT_RESULT_COLOR 0x01
352#define FRAGMENT_RESULT_DEPTH 0x02
353
354/* vertex result binding */
355#define VERTEX_RESULT_POSITION 0x01
356#define VERTEX_RESULT_COLOR 0x02
357#define VERTEX_RESULT_FOGCOORD 0x03
358#define VERTEX_RESULT_POINTSIZE 0x04
359#define VERTEX_RESULT_TEXCOORD 0x05
360
361/* texture target */
362#define TEXTARGET_1D 0x01
363#define TEXTARGET_2D 0x02
364#define TEXTARGET_3D 0x03
365#define TEXTARGET_RECT 0x04
366#define TEXTARGET_CUBE 0x05
367/* GL_ARB_fragment_program_shadow */
368#define TEXTARGET_SHADOW1D 0x06
369#define TEXTARGET_SHADOW2D 0x07
370#define TEXTARGET_SHADOWRECT 0x08
371
372/* face type */
373#define FACE_FRONT 0x00
374#define FACE_BACK 0x01
375
376/* color type */
377#define COLOR_PRIMARY 0x00
378#define COLOR_SECONDARY 0x01
379
380/* component */
381#define COMPONENT_X 0x00
382#define COMPONENT_Y 0x01
383#define COMPONENT_Z 0x02
384#define COMPONENT_W 0x03
385#define COMPONENT_0 0x04
386#define COMPONENT_1 0x05
387
388/* array index type */
389#define ARRAY_INDEX_ABSOLUTE 0x00
390#define ARRAY_INDEX_RELATIVE 0x01
391
392/* matrix name */
393#define MATRIX_MODELVIEW 0x01
394#define MATRIX_PROJECTION 0x02
395#define MATRIX_MVP 0x03
396#define MATRIX_TEXTURE 0x04
397#define MATRIX_PALETTE 0x05
398#define MATRIX_PROGRAM 0x06
399
400/* matrix modifier */
401#define MATRIX_MODIFIER_IDENTITY 0x00
402#define MATRIX_MODIFIER_INVERSE 0x01
403#define MATRIX_MODIFIER_TRANSPOSE 0x02
404#define MATRIX_MODIFIER_INVTRANS 0x03
405
406/* constant type */
407#define CONSTANT_SCALAR 0x01
408#define CONSTANT_VECTOR 0x02
409
410/* program param type */
411#define PROGRAM_PARAM_ENV 0x01
412#define PROGRAM_PARAM_LOCAL 0x02
413
414/* register type */
415#define REGISTER_ATTRIB 0x01
416#define REGISTER_PARAM 0x02
417#define REGISTER_RESULT 0x03
418#define REGISTER_ESTABLISHED_NAME 0x04
419
420/* param binding */
421#define PARAM_NULL 0x00
422#define PARAM_ARRAY_ELEMENT 0x01
423#define PARAM_STATE_ELEMENT 0x02
424#define PARAM_PROGRAM_ELEMENT 0x03
425#define PARAM_PROGRAM_ELEMENTS 0x04
426#define PARAM_CONSTANT 0x05
427
428/* param state property */
429#define STATE_MATERIAL_PARSER 0x01
430#define STATE_LIGHT_PARSER 0x02
431#define STATE_LIGHT_MODEL 0x03
432#define STATE_LIGHT_PROD 0x04
433#define STATE_FOG 0x05
434#define STATE_MATRIX_ROWS 0x06
435/* GL_ARB_fragment_program */
436#define STATE_TEX_ENV 0x07
437#define STATE_DEPTH 0x08
438/* GL_ARB_vertex_program */
439#define STATE_TEX_GEN 0x09
440#define STATE_CLIP_PLANE 0x0A
441#define STATE_POINT 0x0B
442
443/* state material property */
444#define MATERIAL_AMBIENT 0x01
445#define MATERIAL_DIFFUSE 0x02
446#define MATERIAL_SPECULAR 0x03
447#define MATERIAL_EMISSION 0x04
448#define MATERIAL_SHININESS 0x05
449
450/* state light property */
451#define LIGHT_AMBIENT 0x01
452#define LIGHT_DIFFUSE 0x02
453#define LIGHT_SPECULAR 0x03
454#define LIGHT_POSITION 0x04
455#define LIGHT_ATTENUATION 0x05
456#define LIGHT_HALF 0x06
457#define LIGHT_SPOT_DIRECTION 0x07
458
459/* state light model property */
460#define LIGHT_MODEL_AMBIENT 0x01
461#define LIGHT_MODEL_SCENECOLOR 0x02
462
463/* state light product property */
464#define LIGHT_PROD_AMBIENT 0x01
465#define LIGHT_PROD_DIFFUSE 0x02
466#define LIGHT_PROD_SPECULAR 0x03
467
468/* state texture environment property */
469#define TEX_ENV_COLOR 0x01
470
471/* state texture generation coord property */
472#define TEX_GEN_EYE 0x01
473#define TEX_GEN_OBJECT 0x02
474
475/* state fog property */
476#define FOG_COLOR 0x01
477#define FOG_PARAMS 0x02
478
479/* state depth property */
480#define DEPTH_RANGE 0x01
481
482/* state point parameters property */
483#define POINT_SIZE 0x01
484#define POINT_ATTENUATION 0x02
485
486/* declaration */
487#define ATTRIB 0x01
488#define PARAM 0x02
489#define TEMP 0x03
490#define OUTPUT 0x04
491#define ALIAS 0x05
492/* GL_ARB_vertex_program */
493#define ADDRESS 0x06
494
495/*-----------------------------------------------------------------------
496 * From here on down is the semantic checking portion
497 *
498 */
499
500/**
501 * Variable Table Handling functions
502 */
503typedef enum
504{
505 vt_none,
506 vt_address,
507 vt_attrib,
508 vt_param,
509 vt_temp,
510 vt_output,
511 vt_alias
512} var_type;
513
514
Brian Paul7aebaf32005-10-30 21:23:23 +0000515/**
516 * Setting an explicit field for each of the binding properties is a bit
517 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000518 */
519struct var_cache
520{
Brian Paul6a769d92006-04-28 15:42:15 +0000521 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000522 var_type type;
523 GLuint address_binding; /* The index of the address register we should
524 * be using */
525 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000526 GLuint attrib_is_generic; /* If the attrib was specified through a generic
527 * vertex attrib */
528 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000529 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000530 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
531 * that this is aliased to */
532 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
533 * PROGRAM_ENV_PARAM} */
534 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
535 * the tokens representing our bound state (or constants)
536 * start */
537 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
538 * we take up with our state tokens or constants. Note that
539 * this is _not_ the same as the number of param registers
540 * we eventually use */
541 struct var_cache *next;
542};
543
544static GLvoid
545var_cache_create (struct var_cache **va)
546{
547 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
548 if (*va) {
549 (**va).name = NULL;
550 (**va).type = vt_none;
551 (**va).attrib_binding = ~0;
552 (**va).attrib_is_generic = 0;
553 (**va).temp_binding = ~0;
554 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000555 (**va).param_binding_type = ~0;
556 (**va).param_binding_begin = ~0;
557 (**va).param_binding_length = ~0;
558 (**va).alias_binding = NULL;
559 (**va).next = NULL;
560 }
561}
562
563static GLvoid
564var_cache_destroy (struct var_cache **va)
565{
566 if (*va) {
567 var_cache_destroy (&(**va).next);
568 _mesa_free (*va);
569 *va = NULL;
570 }
571}
572
573static GLvoid
574var_cache_append (struct var_cache **va, struct var_cache *nv)
575{
576 if (*va)
577 var_cache_append (&(**va).next, nv);
578 else
579 *va = nv;
580}
581
582static struct var_cache *
583var_cache_find (struct var_cache *va, GLubyte * name)
584{
Brian Paul0a360cf2005-01-17 01:21:03 +0000585 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000586
587 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000588 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000589 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000590 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000591 return va;
592 }
593
594 va = va->next;
595 }
596
597 return NULL;
598}
599
600/**
601 * constructs an integer from 4 GLubytes in LE format
602 */
603static GLuint
604parse_position (GLubyte ** inst)
605{
606 GLuint value;
607
608 value = (GLuint) (*(*inst)++);
609 value += (GLuint) (*(*inst)++) * 0x100;
610 value += (GLuint) (*(*inst)++) * 0x10000;
611 value += (GLuint) (*(*inst)++) * 0x1000000;
612
613 return value;
614}
615
616/**
617 * This will, given a string, lookup the string as a variable name in the
618 * var cache. If the name is found, the var cache node corresponding to the
619 * var name is returned. If it is not found, a new entry is allocated
620 *
621 * \param I Points into the binary array where the string identifier begins
622 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
623 * \return The location on the var_cache corresponding the the string starting at I
624 */
625static struct var_cache *
626parse_string (GLubyte ** inst, struct var_cache **vc_head,
627 struct arb_program *Program, GLuint * found)
628{
629 GLubyte *i = *inst;
630 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000631 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000632
633 *inst += _mesa_strlen ((char *) i) + 1;
634
635 va = var_cache_find (*vc_head, i);
636
637 if (va) {
638 *found = 1;
639 return va;
640 }
641
642 *found = 0;
643 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000644 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000645
646 var_cache_append (vc_head, va);
647
648 return va;
649}
650
651static char *
652parse_string_without_adding (GLubyte ** inst, struct arb_program *Program)
653{
654 GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000655 (void) Program;
656
Jouk Jansen40322e12004-04-05 08:50:36 +0000657 *inst += _mesa_strlen ((char *) i) + 1;
658
659 return (char *) i;
660}
661
662/**
Brian Paul05908952004-06-08 15:20:23 +0000663 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000664 */
Brian Paul05908952004-06-08 15:20:23 +0000665static GLint
Jouk Jansen40322e12004-04-05 08:50:36 +0000666parse_sign (GLubyte ** inst)
667{
668 /*return *(*inst)++ != '+'; */
669
670 if (**inst == '-') {
671 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000672 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000673 }
674 else if (**inst == '+') {
675 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000676 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000677 }
678
Brian Paul05908952004-06-08 15:20:23 +0000679 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000680}
681
682/**
683 * parses and returns signed integer
684 */
685static GLint
686parse_integer (GLubyte ** inst, struct arb_program *Program)
687{
688 GLint sign;
689 GLint value;
690
691 /* check if *inst points to '+' or '-'
692 * if yes, grab the sign and increment *inst
693 */
694 sign = parse_sign (inst);
695
696 /* now check if *inst points to 0
697 * if yes, increment the *inst and return the default value
698 */
699 if (**inst == 0) {
700 (*inst)++;
701 return 0;
702 }
703
704 /* parse the integer as you normally would do it */
705 value = _mesa_atoi (parse_string_without_adding (inst, Program));
706
707 /* now, after terminating 0 there is a position
708 * to parse it - parse_position()
709 */
710 Program->Position = parse_position (inst);
711
Brian Paul05908952004-06-08 15:20:23 +0000712 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000713}
714
715/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000716 Accumulate this string of digits, and return them as
717 a large integer represented in floating point (for range).
718 If scale is not NULL, also accumulates a power-of-ten
719 integer scale factor that represents the number of digits
720 in the string.
721*/
722static GLdouble
723parse_float_string(GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
724{
725 GLdouble value = 0.0;
726 GLdouble oscale = 1.0;
727
728 if (**inst == 0) { /* this string of digits is empty-- do nothing */
729 (*inst)++;
730 }
731 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000732 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000733 GLubyte digit = *((*inst)++);
734 value = value * 10.0 + (GLint) (digit - '0');
735 oscale *= 10.0;
736 }
737 assert(**inst == 0); /* integer string should end with 0 */
738 (*inst)++; /* skip over terminating 0 */
739 Program->Position = parse_position(inst); /* skip position (from integer) */
740 }
741 if (scale)
742 *scale = oscale;
743 return value;
744}
745
746/**
747 Parse an unsigned floating-point number from this stream of tokenized
748 characters. Example floating-point formats supported:
749 12.34
750 12
751 0.34
752 .34
753 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000754 */
755static GLfloat
756parse_float (GLubyte ** inst, struct arb_program *Program)
757{
Brian Paul1ff8f502005-02-16 15:08:29 +0000758 GLint exponent;
759 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000760
Brian Paul1ff8f502005-02-16 15:08:29 +0000761 whole = parse_float_string(inst, Program, 0);
762 fraction = parse_float_string(inst, Program, &fracScale);
763
764 /* Parse signed exponent */
765 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000766
Brian Paul1ff8f502005-02-16 15:08:29 +0000767 /* Assemble parts of floating-point number: */
768 return (GLfloat) ((whole + fraction / fracScale) *
769 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000770}
771
772
773/**
774 */
775static GLfloat
776parse_signed_float (GLubyte ** inst, struct arb_program *Program)
777{
Brian Paul05908952004-06-08 15:20:23 +0000778 GLint sign = parse_sign (inst);
779 GLfloat value = parse_float (inst, Program);
780 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000781}
782
783/**
784 * This picks out a constant value from the parsed array. The constant vector is r
785 * returned in the *values array, which should be of length 4.
786 *
787 * \param values - The 4 component vector with the constant value in it
788 */
789static GLvoid
790parse_constant (GLubyte ** inst, GLfloat *values, struct arb_program *Program,
791 GLboolean use)
792{
793 GLuint components, i;
794
795
796 switch (*(*inst)++) {
797 case CONSTANT_SCALAR:
798 if (use == GL_TRUE) {
799 values[0] =
800 values[1] =
801 values[2] = values[3] = parse_float (inst, Program);
802 }
803 else {
804 values[0] =
805 values[1] =
806 values[2] = values[3] = parse_signed_float (inst, Program);
807 }
808
809 break;
810 case CONSTANT_VECTOR:
811 values[0] = values[1] = values[2] = 0;
812 values[3] = 1;
813 components = *(*inst)++;
814 for (i = 0; i < components; i++) {
815 values[i] = parse_signed_float (inst, Program);
816 }
817 break;
818 }
819}
820
821/**
822 * \param offset The offset from the address register that we should
823 * address
824 *
825 * \return 0 on sucess, 1 on error
826 */
827static GLuint
828parse_relative_offset (GLcontext *ctx, GLubyte **inst, struct arb_program *Program,
829 GLint *offset)
830{
Brian Paul6a769d92006-04-28 15:42:15 +0000831 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000832 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000833 return 0;
834}
835
836/**
837 * \param color 0 if color type is primary, 1 if color type is secondary
838 * \return 0 on sucess, 1 on error
839 */
840static GLuint
841parse_color_type (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
842 GLint * color)
843{
Brian Paula6c423d2004-08-25 15:59:48 +0000844 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000845 *color = *(*inst)++ != COLOR_PRIMARY;
846 return 0;
847}
848
849/**
850 * Get an integer corresponding to a generic vertex attribute.
851 *
852 * \return 0 on sucess, 1 on error
853 */
854static GLuint
855parse_generic_attrib_num(GLcontext *ctx, GLubyte ** inst,
856 struct arb_program *Program, GLuint *attrib)
857{
Brian Paulbd997cd2004-07-20 21:12:56 +0000858 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000859
Michal Krolbb38cad2006-04-11 11:41:11 +0000860 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000861 {
862 _mesa_set_program_error (ctx, Program->Position,
863 "Invalid generic vertex attribute index");
864 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid generic vertex attribute index");
865
866 return 1;
867 }
868
Brian Paulbd997cd2004-07-20 21:12:56 +0000869 *attrib = (GLuint) i;
870
Jouk Jansen40322e12004-04-05 08:50:36 +0000871 return 0;
872}
873
874
875/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000876 * \param color The index of the color buffer to write into
877 * \return 0 on sucess, 1 on error
878 */
879static GLuint
880parse_output_color_num (GLcontext * ctx, GLubyte ** inst,
881 struct arb_program *Program, GLuint * color)
882{
883 GLint i = parse_integer (inst, Program);
884
885 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
886 _mesa_set_program_error (ctx, Program->Position,
887 "Invalid draw buffer index");
888 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid draw buffer index");
889 return 1;
890 }
891
892 *color = (GLuint) i;
893 return 0;
894}
895
896
897/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000898 * \param coord The texture unit index
899 * \return 0 on sucess, 1 on error
900 */
901static GLuint
902parse_texcoord_num (GLcontext * ctx, GLubyte ** inst,
903 struct arb_program *Program, GLuint * coord)
904{
Brian Paulbd997cd2004-07-20 21:12:56 +0000905 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000906
Brian Paula6c423d2004-08-25 15:59:48 +0000907 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000908 _mesa_set_program_error (ctx, Program->Position,
909 "Invalid texture unit index");
910 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid texture unit index");
911 return 1;
912 }
913
Brian Paulbd997cd2004-07-20 21:12:56 +0000914 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000915 return 0;
916}
917
918/**
919 * \param coord The weight index
920 * \return 0 on sucess, 1 on error
921 */
922static GLuint
923parse_weight_num (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
924 GLint * coord)
925{
926 *coord = parse_integer (inst, Program);
927
928 if ((*coord < 0) || (*coord >= 1)) {
929 _mesa_set_program_error (ctx, Program->Position,
930 "Invalid weight index");
931 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid weight index");
932 return 1;
933 }
934
935 return 0;
936}
937
938/**
939 * \param coord The clip plane index
940 * \return 0 on sucess, 1 on error
941 */
942static GLuint
943parse_clipplane_num (GLcontext * ctx, GLubyte ** inst,
944 struct arb_program *Program, GLint * coord)
945{
946 *coord = parse_integer (inst, Program);
947
948 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
949 _mesa_set_program_error (ctx, Program->Position,
950 "Invalid clip plane index");
951 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid clip plane index");
952 return 1;
953 }
954
955 return 0;
956}
957
958
959/**
960 * \return 0 on front face, 1 on back face
961 */
962static GLuint
963parse_face_type (GLubyte ** inst)
964{
965 switch (*(*inst)++) {
966 case FACE_FRONT:
967 return 0;
968
969 case FACE_BACK:
970 return 1;
971 }
972 return 0;
973}
974
975
976/**
977 * Given a matrix and a modifier token on the binary array, return tokens
978 * that _mesa_fetch_state() [program.c] can understand.
979 *
980 * \param matrix - the matrix we are talking about
981 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
982 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
983 * \return 0 on sucess, 1 on failure
984 */
985static GLuint
986parse_matrix (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
987 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
988{
989 GLubyte mat = *(*inst)++;
990
991 *matrix_idx = 0;
992
993 switch (mat) {
994 case MATRIX_MODELVIEW:
995 *matrix = STATE_MODELVIEW;
996 *matrix_idx = parse_integer (inst, Program);
997 if (*matrix_idx > 0) {
998 _mesa_set_program_error (ctx, Program->Position,
999 "ARB_vertex_blend not supported\n");
1000 _mesa_error (ctx, GL_INVALID_OPERATION,
1001 "ARB_vertex_blend not supported\n");
1002 return 1;
1003 }
1004 break;
1005
1006 case MATRIX_PROJECTION:
1007 *matrix = STATE_PROJECTION;
1008 break;
1009
1010 case MATRIX_MVP:
1011 *matrix = STATE_MVP;
1012 break;
1013
1014 case MATRIX_TEXTURE:
1015 *matrix = STATE_TEXTURE;
1016 *matrix_idx = parse_integer (inst, Program);
1017 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
1018 _mesa_set_program_error (ctx, Program->Position,
1019 "Invalid Texture Unit");
1020 _mesa_error (ctx, GL_INVALID_OPERATION,
1021 "Invalid Texture Unit: %d", *matrix_idx);
1022 return 1;
1023 }
1024 break;
1025
1026 /* This is not currently supported (ARB_matrix_palette) */
1027 case MATRIX_PALETTE:
1028 *matrix_idx = parse_integer (inst, Program);
1029 _mesa_set_program_error (ctx, Program->Position,
1030 "ARB_matrix_palette not supported\n");
1031 _mesa_error (ctx, GL_INVALID_OPERATION,
1032 "ARB_matrix_palette not supported\n");
1033 return 1;
1034 break;
1035
1036 case MATRIX_PROGRAM:
1037 *matrix = STATE_PROGRAM;
1038 *matrix_idx = parse_integer (inst, Program);
1039 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
1040 _mesa_set_program_error (ctx, Program->Position,
1041 "Invalid Program Matrix");
1042 _mesa_error (ctx, GL_INVALID_OPERATION,
1043 "Invalid Program Matrix: %d", *matrix_idx);
1044 return 1;
1045 }
1046 break;
1047 }
1048
1049 switch (*(*inst)++) {
1050 case MATRIX_MODIFIER_IDENTITY:
1051 *matrix_modifier = 0;
1052 break;
1053 case MATRIX_MODIFIER_INVERSE:
1054 *matrix_modifier = STATE_MATRIX_INVERSE;
1055 break;
1056 case MATRIX_MODIFIER_TRANSPOSE:
1057 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1058 break;
1059 case MATRIX_MODIFIER_INVTRANS:
1060 *matrix_modifier = STATE_MATRIX_INVTRANS;
1061 break;
1062 }
1063
1064 return 0;
1065}
1066
1067
1068/**
1069 * This parses a state string (rather, the binary version of it) into
1070 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1071 *
1072 * \param inst - the start in the binary arry to start working from
1073 * \param state_tokens - the storage for the 6-token state description
1074 * \return - 0 on sucess, 1 on error
1075 */
1076static GLuint
1077parse_state_single_item (GLcontext * ctx, GLubyte ** inst,
1078 struct arb_program *Program, GLint * state_tokens)
1079{
1080 switch (*(*inst)++) {
1081 case STATE_MATERIAL_PARSER:
1082 state_tokens[0] = STATE_MATERIAL;
1083 state_tokens[1] = parse_face_type (inst);
1084 switch (*(*inst)++) {
1085 case MATERIAL_AMBIENT:
1086 state_tokens[2] = STATE_AMBIENT;
1087 break;
1088 case MATERIAL_DIFFUSE:
1089 state_tokens[2] = STATE_DIFFUSE;
1090 break;
1091 case MATERIAL_SPECULAR:
1092 state_tokens[2] = STATE_SPECULAR;
1093 break;
1094 case MATERIAL_EMISSION:
1095 state_tokens[2] = STATE_EMISSION;
1096 break;
1097 case MATERIAL_SHININESS:
1098 state_tokens[2] = STATE_SHININESS;
1099 break;
1100 }
1101 break;
1102
1103 case STATE_LIGHT_PARSER:
1104 state_tokens[0] = STATE_LIGHT;
1105 state_tokens[1] = parse_integer (inst, Program);
1106
1107 /* Check the value of state_tokens[1] against the # of lights */
1108 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1109 _mesa_set_program_error (ctx, Program->Position,
1110 "Invalid Light Number");
1111 _mesa_error (ctx, GL_INVALID_OPERATION,
1112 "Invalid Light Number: %d", state_tokens[1]);
1113 return 1;
1114 }
1115
1116 switch (*(*inst)++) {
1117 case LIGHT_AMBIENT:
1118 state_tokens[2] = STATE_AMBIENT;
1119 break;
1120 case LIGHT_DIFFUSE:
1121 state_tokens[2] = STATE_DIFFUSE;
1122 break;
1123 case LIGHT_SPECULAR:
1124 state_tokens[2] = STATE_SPECULAR;
1125 break;
1126 case LIGHT_POSITION:
1127 state_tokens[2] = STATE_POSITION;
1128 break;
1129 case LIGHT_ATTENUATION:
1130 state_tokens[2] = STATE_ATTENUATION;
1131 break;
1132 case LIGHT_HALF:
1133 state_tokens[2] = STATE_HALF;
1134 break;
1135 case LIGHT_SPOT_DIRECTION:
1136 state_tokens[2] = STATE_SPOT_DIRECTION;
1137 break;
1138 }
1139 break;
1140
1141 case STATE_LIGHT_MODEL:
1142 switch (*(*inst)++) {
1143 case LIGHT_MODEL_AMBIENT:
1144 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1145 break;
1146 case LIGHT_MODEL_SCENECOLOR:
1147 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1148 state_tokens[1] = parse_face_type (inst);
1149 break;
1150 }
1151 break;
1152
1153 case STATE_LIGHT_PROD:
1154 state_tokens[0] = STATE_LIGHTPROD;
1155 state_tokens[1] = parse_integer (inst, Program);
1156
1157 /* Check the value of state_tokens[1] against the # of lights */
1158 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1159 _mesa_set_program_error (ctx, Program->Position,
1160 "Invalid Light Number");
1161 _mesa_error (ctx, GL_INVALID_OPERATION,
1162 "Invalid Light Number: %d", state_tokens[1]);
1163 return 1;
1164 }
1165
1166 state_tokens[2] = parse_face_type (inst);
1167 switch (*(*inst)++) {
1168 case LIGHT_PROD_AMBIENT:
1169 state_tokens[3] = STATE_AMBIENT;
1170 break;
1171 case LIGHT_PROD_DIFFUSE:
1172 state_tokens[3] = STATE_DIFFUSE;
1173 break;
1174 case LIGHT_PROD_SPECULAR:
1175 state_tokens[3] = STATE_SPECULAR;
1176 break;
1177 }
1178 break;
1179
1180
1181 case STATE_FOG:
1182 switch (*(*inst)++) {
1183 case FOG_COLOR:
1184 state_tokens[0] = STATE_FOG_COLOR;
1185 break;
1186 case FOG_PARAMS:
1187 state_tokens[0] = STATE_FOG_PARAMS;
1188 break;
1189 }
1190 break;
1191
1192 case STATE_TEX_ENV:
1193 state_tokens[1] = parse_integer (inst, Program);
1194 switch (*(*inst)++) {
1195 case TEX_ENV_COLOR:
1196 state_tokens[0] = STATE_TEXENV_COLOR;
1197 break;
1198 }
1199 break;
1200
1201 case STATE_TEX_GEN:
1202 {
1203 GLuint type, coord;
1204
1205 state_tokens[0] = STATE_TEXGEN;
1206 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1207
1208 if (parse_texcoord_num (ctx, inst, Program, &coord))
1209 return 1;
1210 state_tokens[1] = coord;
1211
1212 /* EYE or OBJECT */
1213 type = *(*inst++);
1214
1215 /* 0 - s, 1 - t, 2 - r, 3 - q */
1216 coord = *(*inst++);
1217
1218 if (type == TEX_GEN_EYE) {
1219 switch (coord) {
1220 case COMPONENT_X:
1221 state_tokens[2] = STATE_TEXGEN_EYE_S;
1222 break;
1223 case COMPONENT_Y:
1224 state_tokens[2] = STATE_TEXGEN_EYE_T;
1225 break;
1226 case COMPONENT_Z:
1227 state_tokens[2] = STATE_TEXGEN_EYE_R;
1228 break;
1229 case COMPONENT_W:
1230 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1231 break;
1232 }
1233 }
1234 else {
1235 switch (coord) {
1236 case COMPONENT_X:
1237 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1238 break;
1239 case COMPONENT_Y:
1240 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1241 break;
1242 case COMPONENT_Z:
1243 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1244 break;
1245 case COMPONENT_W:
1246 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1247 break;
1248 }
1249 }
1250 }
1251 break;
1252
1253 case STATE_DEPTH:
1254 switch (*(*inst)++) {
1255 case DEPTH_RANGE:
1256 state_tokens[0] = STATE_DEPTH_RANGE;
1257 break;
1258 }
1259 break;
1260
1261 case STATE_CLIP_PLANE:
1262 state_tokens[0] = STATE_CLIPPLANE;
1263 state_tokens[1] = parse_integer (inst, Program);
1264 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1265 return 1;
1266 break;
1267
1268 case STATE_POINT:
1269 switch (*(*inst++)) {
1270 case POINT_SIZE:
1271 state_tokens[0] = STATE_POINT_SIZE;
1272 break;
1273
1274 case POINT_ATTENUATION:
1275 state_tokens[0] = STATE_POINT_ATTENUATION;
1276 break;
1277 }
1278 break;
1279
1280 /* XXX: I think this is the correct format for a matrix row */
1281 case STATE_MATRIX_ROWS:
1282 state_tokens[0] = STATE_MATRIX;
1283 if (parse_matrix
1284 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1285 &state_tokens[5]))
1286 return 1;
1287
1288 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1289
1290 if ((**inst) != 0) { /* Either the last row, 0 */
1291 state_tokens[4] = parse_integer (inst, Program);
1292 if (state_tokens[4] < state_tokens[3]) {
1293 _mesa_set_program_error (ctx, Program->Position,
1294 "Second matrix index less than the first");
1295 _mesa_error (ctx, GL_INVALID_OPERATION,
1296 "Second matrix index (%d) less than the first (%d)",
1297 state_tokens[4], state_tokens[3]);
1298 return 1;
1299 }
1300 }
1301 else {
1302 state_tokens[4] = state_tokens[3];
1303 (*inst)++;
1304 }
1305 break;
1306 }
1307
1308 return 0;
1309}
1310
1311/**
1312 * This parses a state string (rather, the binary version of it) into
1313 * a 6-token similar for the state fetching code in program.c
1314 *
1315 * One might ask, why fetch these parameters into just like you fetch
1316 * state when they are already stored in other places?
1317 *
1318 * Because of array offsets -> We can stick env/local parameters in the
1319 * middle of a parameter array and then index someplace into the array
1320 * when we execute.
1321 *
1322 * One optimization might be to only do this for the cases where the
1323 * env/local parameters end up inside of an array, and leave the
1324 * single parameters (or arrays of pure env/local pareameters) in their
1325 * respective register files.
1326 *
1327 * For ENV parameters, the format is:
1328 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1329 * state_tokens[1] = STATE_ENV
1330 * state_tokens[2] = the parameter index
1331 *
1332 * for LOCAL parameters, the format is:
1333 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1334 * state_tokens[1] = STATE_LOCAL
1335 * state_tokens[2] = the parameter index
1336 *
1337 * \param inst - the start in the binary arry to start working from
1338 * \param state_tokens - the storage for the 6-token state description
1339 * \return - 0 on sucess, 1 on failure
1340 */
1341static GLuint
1342parse_program_single_item (GLcontext * ctx, GLubyte ** inst,
1343 struct arb_program *Program, GLint * state_tokens)
1344{
1345 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1346 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1347 else
1348 state_tokens[0] = STATE_VERTEX_PROGRAM;
1349
1350
1351 switch (*(*inst)++) {
1352 case PROGRAM_PARAM_ENV:
1353 state_tokens[1] = STATE_ENV;
1354 state_tokens[2] = parse_integer (inst, Program);
1355
1356 /* Check state_tokens[2] against the number of ENV parameters available */
1357 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001358 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001359 ||
1360 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001361 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001362 _mesa_set_program_error (ctx, Program->Position,
1363 "Invalid Program Env Parameter");
1364 _mesa_error (ctx, GL_INVALID_OPERATION,
1365 "Invalid Program Env Parameter: %d",
1366 state_tokens[2]);
1367 return 1;
1368 }
1369
1370 break;
1371
1372 case PROGRAM_PARAM_LOCAL:
1373 state_tokens[1] = STATE_LOCAL;
1374 state_tokens[2] = parse_integer (inst, Program);
1375
1376 /* Check state_tokens[2] against the number of LOCAL parameters available */
1377 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001378 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001379 ||
1380 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001381 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001382 _mesa_set_program_error (ctx, Program->Position,
1383 "Invalid Program Local Parameter");
1384 _mesa_error (ctx, GL_INVALID_OPERATION,
1385 "Invalid Program Local Parameter: %d",
1386 state_tokens[2]);
1387 return 1;
1388 }
1389 break;
1390 }
1391
1392 return 0;
1393}
1394
1395/**
1396 * For ARB_vertex_program, programs are not allowed to use both an explicit
1397 * vertex attribute and a generic vertex attribute corresponding to the same
1398 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1399 *
1400 * This will walk our var_cache and make sure that nobody does anything fishy.
1401 *
1402 * \return 0 on sucess, 1 on error
1403 */
1404static GLuint
1405generic_attrib_check(struct var_cache *vc_head)
1406{
1407 int a;
1408 struct var_cache *curr;
1409 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1410 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1411
1412 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1413 explicitAttrib[a] = GL_FALSE;
1414 genericAttrib[a] = GL_FALSE;
1415 }
1416
1417 curr = vc_head;
1418 while (curr) {
1419 if (curr->type == vt_attrib) {
1420 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001421 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001422 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001423 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001424 }
1425
1426 curr = curr->next;
1427 }
1428
1429 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1430 if ((explicitAttrib[a]) && (genericAttrib[a]))
1431 return 1;
1432 }
1433
1434 return 0;
1435}
1436
1437/**
1438 * This will handle the binding side of an ATTRIB var declaration
1439 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001440 * \param inputReg returns the input register index, one of the
1441 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001442 * \return returns 0 on sucess, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001443 */
1444static GLuint
Brian Paul18e7c5c2005-10-30 21:46:00 +00001445parse_attrib_binding(GLcontext * ctx, GLubyte ** inst,
1446 struct arb_program *Program,
1447 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001448{
Jouk Jansen40322e12004-04-05 08:50:36 +00001449 GLint err = 0;
1450
1451 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001452
Jouk Jansen40322e12004-04-05 08:50:36 +00001453 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1454 switch (*(*inst)++) {
1455 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001456 {
1457 GLint coord;
1458 err = parse_color_type (ctx, inst, Program, &coord);
1459 *inputReg = FRAG_ATTRIB_COL0 + coord;
1460 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001461 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001462 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001463 {
1464 GLuint texcoord;
1465 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1466 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1467 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001468 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001469 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001470 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001471 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001473 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001475 default:
1476 err = 1;
1477 break;
1478 }
1479 }
1480 else {
1481 switch (*(*inst)++) {
1482 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001483 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001484 break;
1485
1486 case VERTEX_ATTRIB_WEIGHT:
1487 {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001488 const char *msg = "ARB_vertex_blend not supported";
Jouk Jansen40322e12004-04-05 08:50:36 +00001489 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001490 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001491 *inputReg = VERT_ATTRIB_WEIGHT;
1492 _mesa_set_program_error(ctx, Program->Position, msg);
1493 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001494 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001495 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001496
1497 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001498 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001499 break;
1500
1501 case VERTEX_ATTRIB_COLOR:
1502 {
1503 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001504 err = parse_color_type (ctx, inst, Program, &color);
1505 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001506 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001507 }
1508 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001509 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001510 }
1511 }
1512 break;
1513
1514 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001515 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001516 break;
1517
1518 case VERTEX_ATTRIB_TEXCOORD:
1519 {
1520 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001521 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001522 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001523 }
1524 break;
1525
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001527 /* Not supported at this time */
1528 {
1529 const char *msg = "ARB_palette_matrix not supported";
1530 parse_integer (inst, Program);
1531 _mesa_set_program_error (ctx, Program->Position, msg);
1532 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
1533 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001534 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001535
1536 case VERTEX_ATTRIB_GENERIC:
1537 {
1538 GLuint attrib;
Jouk Jansen40322e12004-04-05 08:50:36 +00001539 if (!parse_generic_attrib_num(ctx, inst, Program, &attrib)) {
1540 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001541 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1542 * attributes do not alias the conventional vertex
1543 * attributes.
1544 */
1545 if (attrib > 0)
1546 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001547 else
1548 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001549 }
1550 }
1551 break;
1552
1553 default:
1554 err = 1;
1555 break;
1556 }
1557 }
1558
1559 /* Can this even happen? */
1560 if (err) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001561 const char *msg = "Bad attribute binding";
1562 _mesa_set_program_error(ctx, Program->Position, msg);
1563 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001564 }
1565
Brian Paulde997602005-11-12 17:53:14 +00001566 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001567
1568 return err;
1569}
1570
Brian Paul7aebaf32005-10-30 21:23:23 +00001571
Jouk Jansen40322e12004-04-05 08:50:36 +00001572/**
1573 * This translates between a binary token for an output variable type
1574 * and the mesa token for the same thing.
1575 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001576 * \param inst The parsed tokens
1577 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001578 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001579 */
1580static GLuint
Brian Paul7aebaf32005-10-30 21:23:23 +00001581parse_result_binding(GLcontext *ctx, GLubyte **inst,
1582 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001583{
Brian Paul7aebaf32005-10-30 21:23:23 +00001584 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001585
Brian Paul7aebaf32005-10-30 21:23:23 +00001586 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001587 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001588 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001589 GLuint out_color;
1590
Brian Paulbe76b7f2004-10-04 14:40:05 +00001591 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001592 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001593 */
1594 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001595 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001596 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001597 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001598 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001599 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1600 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001601 }
1602 break;
1603
1604 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001605 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001606 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001607 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001608 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001609 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001610 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001611 GLint color_type;
1612 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001613 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1614 if (err)
1615 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001616
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001618 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001619 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001620 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001622 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001623 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 }
1625 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001627 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001629 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001630 }
1631 /* primary color */
1632 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001633 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001634 }
1635 }
1636 }
1637 break;
1638
1639 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001640 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 break;
1642
1643 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001644 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001645 break;
1646
1647 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001648 {
1649 GLuint unit;
1650 if (parse_texcoord_num (ctx, inst, Program, &unit))
1651 return 1;
1652 *outputReg = VERT_RESULT_TEX0 + unit;
1653 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001654 break;
1655 }
1656
Brian Paulde997602005-11-12 17:53:14 +00001657 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001658
1659 return 0;
1660}
1661
Brian Paul7aebaf32005-10-30 21:23:23 +00001662
Jouk Jansen40322e12004-04-05 08:50:36 +00001663/**
1664 * This handles the declaration of ATTRIB variables
1665 *
1666 * XXX: Still needs
1667 * parse_vert_attrib_binding(), or something like that
1668 *
1669 * \return 0 on sucess, 1 on error
1670 */
1671static GLint
1672parse_attrib (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1673 struct arb_program *Program)
1674{
1675 GLuint found;
1676 char *error_msg;
1677 struct var_cache *attrib_var;
1678
1679 attrib_var = parse_string (inst, vc_head, Program, &found);
1680 Program->Position = parse_position (inst);
1681 if (found) {
1682 error_msg = (char *)
1683 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1684 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1685 attrib_var->name);
1686
1687 _mesa_set_program_error (ctx, Program->Position, error_msg);
1688 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1689
1690 _mesa_free (error_msg);
1691 return 1;
1692 }
1693
1694 attrib_var->type = vt_attrib;
1695
Brian Paul7aebaf32005-10-30 21:23:23 +00001696 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001697 &attrib_var->attrib_is_generic))
1698 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001699
Brian Paul7aebaf32005-10-30 21:23:23 +00001700 if (generic_attrib_check(*vc_head)) {
1701 _mesa_set_program_error(ctx, Program->Position,
1702 "Cannot use both a generic vertex attribute "
1703 "and a specific attribute of the same type");
1704 _mesa_error(ctx, GL_INVALID_OPERATION,
1705 "Cannot use both a generic vertex attribute and a specific "
1706 "attribute of the same type");
1707 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001708 }
1709
1710 Program->Base.NumAttributes++;
1711 return 0;
1712}
1713
1714/**
1715 * \param use -- TRUE if we're called when declaring implicit parameters,
1716 * FALSE if we're declaraing variables. This has to do with
1717 * if we get a signed or unsigned float for scalar constants
1718 */
1719static GLuint
1720parse_param_elements (GLcontext * ctx, GLubyte ** inst,
1721 struct var_cache *param_var,
1722 struct arb_program *Program, GLboolean use)
1723{
1724 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001725 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001726 GLint state_tokens[6];
1727 GLfloat const_values[4];
1728
Jouk Jansen40322e12004-04-05 08:50:36 +00001729 switch (*(*inst)++) {
1730 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001731 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1732 return 1;
1733
1734 /* If we adding STATE_MATRIX that has multiple rows, we need to
1735 * unroll it and call _mesa_add_state_reference() for each row
1736 */
1737 if ((state_tokens[0] == STATE_MATRIX)
1738 && (state_tokens[3] != state_tokens[4])) {
1739 GLint row;
1740 GLint first_row = state_tokens[3];
1741 GLint last_row = state_tokens[4];
1742
1743 for (row = first_row; row <= last_row; row++) {
1744 state_tokens[3] = state_tokens[4] = row;
1745
Brian Paulde997602005-11-12 17:53:14 +00001746 idx = _mesa_add_state_reference(Program->Base.Parameters,
1747 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001748 if (param_var->param_binding_begin == ~0U)
1749 param_var->param_binding_begin = idx;
1750 param_var->param_binding_length++;
1751 Program->Base.NumParameters++;
1752 }
1753 }
1754 else {
Brian Paulde997602005-11-12 17:53:14 +00001755 idx = _mesa_add_state_reference(Program->Base.Parameters,
1756 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001757 if (param_var->param_binding_begin == ~0U)
1758 param_var->param_binding_begin = idx;
1759 param_var->param_binding_length++;
1760 Program->Base.NumParameters++;
1761 }
1762 break;
1763
1764 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001765 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1766 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001767 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001768 if (param_var->param_binding_begin == ~0U)
1769 param_var->param_binding_begin = idx;
1770 param_var->param_binding_length++;
1771 Program->Base.NumParameters++;
1772
1773 /* Check if there is more: 0 -> we're done, else its an integer */
1774 if (**inst) {
1775 GLuint out_of_range, new_idx;
1776 GLuint start_idx = state_tokens[2] + 1;
1777 GLuint end_idx = parse_integer (inst, Program);
1778
1779 out_of_range = 0;
1780 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1781 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001782 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001783 || ((state_tokens[1] == STATE_LOCAL)
1784 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001785 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001786 out_of_range = 1;
1787 }
1788 else {
1789 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001790 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001791 || ((state_tokens[1] == STATE_LOCAL)
1792 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001793 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001794 out_of_range = 1;
1795 }
1796 if (out_of_range) {
1797 _mesa_set_program_error (ctx, Program->Position,
1798 "Invalid Program Parameter");
1799 _mesa_error (ctx, GL_INVALID_OPERATION,
1800 "Invalid Program Parameter: %d", end_idx);
1801 return 1;
1802 }
1803
1804 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1805 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001806 idx = _mesa_add_state_reference(Program->Base.Parameters,
1807 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001808 param_var->param_binding_length++;
1809 Program->Base.NumParameters++;
1810 }
1811 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001812 else {
1813 (*inst)++;
1814 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001815 break;
1816
1817 case PARAM_CONSTANT:
1818 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001819 idx = _mesa_add_named_constant(Program->Base.Parameters,
1820 (char *) param_var->name,
1821 const_values);
Jouk Jansen40322e12004-04-05 08:50:36 +00001822 if (param_var->param_binding_begin == ~0U)
1823 param_var->param_binding_begin = idx;
1824 param_var->param_binding_length++;
1825 Program->Base.NumParameters++;
1826 break;
1827
1828 default:
Brian Paul7aebaf32005-10-30 21:23:23 +00001829 _mesa_set_program_error(ctx, Program->Position,
1830 "Unexpected token in parse_param_elements()");
1831 _mesa_error(ctx, GL_INVALID_OPERATION,
1832 "Unexpected token in parse_param_elements()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001833 return 1;
1834 }
1835
1836 /* Make sure we haven't blown past our parameter limits */
1837 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1838 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001839 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001840 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1841 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001842 ctx->Const.FragmentProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001843 _mesa_set_program_error (ctx, Program->Position,
1844 "Too many parameter variables");
1845 _mesa_error (ctx, GL_INVALID_OPERATION, "Too many parameter variables");
1846 return 1;
1847 }
1848
1849 return err;
1850}
1851
Brian Paul7aebaf32005-10-30 21:23:23 +00001852
Jouk Jansen40322e12004-04-05 08:50:36 +00001853/**
1854 * This picks out PARAM program parameter bindings.
1855 *
1856 * XXX: This needs to be stressed & tested
1857 *
1858 * \return 0 on sucess, 1 on error
1859 */
1860static GLuint
1861parse_param (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1862 struct arb_program *Program)
1863{
Brian Paulbd997cd2004-07-20 21:12:56 +00001864 GLuint found, err;
1865 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001866 struct var_cache *param_var;
1867
1868 err = 0;
1869 param_var = parse_string (inst, vc_head, Program, &found);
1870 Program->Position = parse_position (inst);
1871
1872 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001873 char *error_msg = (char *)
1874 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001875 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1876 param_var->name);
1877
1878 _mesa_set_program_error (ctx, Program->Position, error_msg);
1879 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1880
1881 _mesa_free (error_msg);
1882 return 1;
1883 }
1884
1885 specified_length = parse_integer (inst, Program);
1886
1887 if (specified_length < 0) {
1888 _mesa_set_program_error (ctx, Program->Position,
1889 "Negative parameter array length");
1890 _mesa_error (ctx, GL_INVALID_OPERATION,
1891 "Negative parameter array length: %d", specified_length);
1892 return 1;
1893 }
1894
1895 param_var->type = vt_param;
1896 param_var->param_binding_length = 0;
1897
1898 /* Right now, everything is shoved into the main state register file.
1899 *
1900 * In the future, it would be nice to leave things ENV/LOCAL params
1901 * in their respective register files, if possible
1902 */
1903 param_var->param_binding_type = PROGRAM_STATE_VAR;
1904
1905 /* Remember to:
1906 * * - add each guy to the parameter list
1907 * * - increment the param_var->param_binding_len
1908 * * - store the param_var->param_binding_begin for the first one
1909 * * - compare the actual len to the specified len at the end
1910 */
1911 while (**inst != PARAM_NULL) {
1912 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1913 return 1;
1914 }
1915
1916 /* Test array length here! */
1917 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001918 if (specified_length != (int)param_var->param_binding_length) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001919 const char *msg
1920 = "Declared parameter array length does not match parameter list";
1921 _mesa_set_program_error(ctx, Program->Position, msg);
1922 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001923 }
1924 }
1925
1926 (*inst)++;
1927
1928 return 0;
1929}
1930
1931/**
1932 *
1933 */
1934static GLuint
1935parse_param_use (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1936 struct arb_program *Program, struct var_cache **new_var)
1937{
1938 struct var_cache *param_var;
1939
1940 /* First, insert a dummy entry into the var_cache */
1941 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001942 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001943 param_var->type = vt_param;
1944
1945 param_var->param_binding_length = 0;
1946 /* Don't fill in binding_begin; We use the default value of -1
1947 * to tell if its already initialized, elsewhere.
1948 *
1949 * param_var->param_binding_begin = 0;
1950 */
1951 param_var->param_binding_type = PROGRAM_STATE_VAR;
1952
1953 var_cache_append (vc_head, param_var);
1954
1955 /* Then fill it with juicy parameter goodness */
1956 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1957 return 1;
1958
1959 *new_var = param_var;
1960
1961 return 0;
1962}
1963
1964
1965/**
1966 * This handles the declaration of TEMP variables
1967 *
1968 * \return 0 on sucess, 1 on error
1969 */
1970static GLuint
1971parse_temp (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1972 struct arb_program *Program)
1973{
1974 GLuint found;
1975 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001976
1977 while (**inst != 0) {
1978 temp_var = parse_string (inst, vc_head, Program, &found);
1979 Program->Position = parse_position (inst);
1980 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001981 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001982 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1983 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1984 temp_var->name);
1985
1986 _mesa_set_program_error (ctx, Program->Position, error_msg);
1987 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1988
1989 _mesa_free (error_msg);
1990 return 1;
1991 }
1992
1993 temp_var->type = vt_temp;
1994
1995 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1996 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001997 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001998 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1999 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002000 ctx->Const.VertexProgram.MaxTemps))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002001 _mesa_set_program_error (ctx, Program->Position,
2002 "Too many TEMP variables declared");
2003 _mesa_error (ctx, GL_INVALID_OPERATION,
2004 "Too many TEMP variables declared");
2005 return 1;
2006 }
2007
2008 temp_var->temp_binding = Program->Base.NumTemporaries;
2009 Program->Base.NumTemporaries++;
2010 }
2011 (*inst)++;
2012
2013 return 0;
2014}
2015
2016/**
2017 * This handles variables of the OUTPUT variety
2018 *
2019 * \return 0 on sucess, 1 on error
2020 */
2021static GLuint
2022parse_output (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2023 struct arb_program *Program)
2024{
2025 GLuint found;
2026 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002027 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002028
2029 output_var = parse_string (inst, vc_head, Program, &found);
2030 Program->Position = parse_position (inst);
2031 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002032 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002033 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2034 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2035 output_var->name);
2036
2037 _mesa_set_program_error (ctx, Program->Position, error_msg);
2038 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2039
2040 _mesa_free (error_msg);
2041 return 1;
2042 }
2043
2044 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002045
2046 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2047 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002048}
2049
2050/**
2051 * This handles variables of the ALIAS kind
2052 *
2053 * \return 0 on sucess, 1 on error
2054 */
2055static GLuint
2056parse_alias (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2057 struct arb_program *Program)
2058{
2059 GLuint found;
2060 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002061
2062 temp_var = parse_string (inst, vc_head, Program, &found);
2063 Program->Position = parse_position (inst);
2064
2065 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002066 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002067 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2068 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2069 temp_var->name);
2070
2071 _mesa_set_program_error (ctx, Program->Position, error_msg);
2072 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2073
2074 _mesa_free (error_msg);
2075 return 1;
2076 }
2077
2078 temp_var->type = vt_alias;
2079 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2080 Program->Position = parse_position (inst);
2081
2082 if (!found)
2083 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002084 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002085 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2086 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2087 temp_var->alias_binding->name);
2088
2089 _mesa_set_program_error (ctx, Program->Position, error_msg);
2090 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2091
2092 _mesa_free (error_msg);
2093 return 1;
2094 }
2095
2096 return 0;
2097}
2098
2099/**
2100 * This handles variables of the ADDRESS kind
2101 *
2102 * \return 0 on sucess, 1 on error
2103 */
2104static GLuint
2105parse_address (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2106 struct arb_program *Program)
2107{
2108 GLuint found;
2109 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002110
2111 while (**inst != 0) {
2112 temp_var = parse_string (inst, vc_head, Program, &found);
2113 Program->Position = parse_position (inst);
2114 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002115 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002116 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2117 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2118 temp_var->name);
2119
2120 _mesa_set_program_error (ctx, Program->Position, error_msg);
2121 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2122
2123 _mesa_free (error_msg);
2124 return 1;
2125 }
2126
2127 temp_var->type = vt_address;
2128
2129 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002130 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002131 const char *msg = "Too many ADDRESS variables declared";
2132 _mesa_set_program_error(ctx, Program->Position, msg);
2133
2134 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002135 return 1;
2136 }
2137
2138 temp_var->address_binding = Program->Base.NumAddressRegs;
2139 Program->Base.NumAddressRegs++;
2140 }
2141 (*inst)++;
2142
2143 return 0;
2144}
2145
2146/**
2147 * Parse a program declaration
2148 *
2149 * \return 0 on sucess, 1 on error
2150 */
2151static GLint
2152parse_declaration (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2153 struct arb_program *Program)
2154{
2155 GLint err = 0;
2156
2157 switch (*(*inst)++) {
2158 case ADDRESS:
2159 err = parse_address (ctx, inst, vc_head, Program);
2160 break;
2161
2162 case ALIAS:
2163 err = parse_alias (ctx, inst, vc_head, Program);
2164 break;
2165
2166 case ATTRIB:
2167 err = parse_attrib (ctx, inst, vc_head, Program);
2168 break;
2169
2170 case OUTPUT:
2171 err = parse_output (ctx, inst, vc_head, Program);
2172 break;
2173
2174 case PARAM:
2175 err = parse_param (ctx, inst, vc_head, Program);
2176 break;
2177
2178 case TEMP:
2179 err = parse_temp (ctx, inst, vc_head, Program);
2180 break;
2181 }
2182
2183 return err;
2184}
2185
2186/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002187 * Handle the parsing out of a masked destination register, either for a
2188 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002189 *
2190 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002191 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002192 * position invariant
2193 *
2194 * \param File - The register file we write to
2195 * \param Index - The register index we write to
2196 * \param WriteMask - The mask controlling which components we write (1->write)
2197 *
2198 * \return 0 on sucess, 1 on error
2199 */
2200static GLuint
2201parse_masked_dst_reg (GLcontext * ctx, GLubyte ** inst,
2202 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002203 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002204{
Brian Paul7aebaf32005-10-30 21:23:23 +00002205 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002206 struct var_cache *dst;
2207
2208 /* We either have a result register specified, or a
2209 * variable that may or may not be writable
2210 */
2211 switch (*(*inst)++) {
2212 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002213 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002214 return 1;
2215 *File = PROGRAM_OUTPUT;
2216 break;
2217
2218 case REGISTER_ESTABLISHED_NAME:
2219 dst = parse_string (inst, vc_head, Program, &result);
2220 Program->Position = parse_position (inst);
2221
2222 /* If the name has never been added to our symbol table, we're hosed */
2223 if (!result) {
2224 _mesa_set_program_error (ctx, Program->Position,
2225 "0: Undefined variable");
2226 _mesa_error (ctx, GL_INVALID_OPERATION, "0: Undefined variable: %s",
2227 dst->name);
2228 return 1;
2229 }
2230
2231 switch (dst->type) {
2232 case vt_output:
2233 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002234 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002235 break;
2236
2237 case vt_temp:
2238 *File = PROGRAM_TEMPORARY;
2239 *Index = dst->temp_binding;
2240 break;
2241
2242 /* If the var type is not vt_output or vt_temp, no go */
2243 default:
2244 _mesa_set_program_error (ctx, Program->Position,
2245 "Destination register is read only");
2246 _mesa_error (ctx, GL_INVALID_OPERATION,
2247 "Destination register is read only: %s",
2248 dst->name);
2249 return 1;
2250 }
2251 break;
2252
2253 default:
2254 _mesa_set_program_error (ctx, Program->Position,
2255 "Unexpected opcode in parse_masked_dst_reg()");
2256 _mesa_error (ctx, GL_INVALID_OPERATION,
2257 "Unexpected opcode in parse_masked_dst_reg()");
2258 return 1;
2259 }
2260
2261
2262 /* Position invariance test */
2263 /* This test is done now in syntax portion - when position invariance OPTION
2264 is specified, "result.position" rule is disabled so there is no way
2265 to write the position
2266 */
2267 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2268 (*Index == 0)) {
2269 _mesa_set_program_error (ctx, Program->Position,
2270 "Vertex program specified position invariance and wrote vertex position");
2271 _mesa_error (ctx, GL_INVALID_OPERATION,
2272 "Vertex program specified position invariance and wrote vertex position");
2273 }*/
2274
2275 /* And then the mask.
2276 * w,a -> bit 0
2277 * z,b -> bit 1
2278 * y,g -> bit 2
2279 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002280 *
2281 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002282 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002283 tmp = (GLint) *(*inst)++;
2284 *WriteMask = (((tmp>>3) & 0x1) |
2285 ((tmp>>1) & 0x2) |
2286 ((tmp<<1) & 0x4) |
2287 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002288
2289 return 0;
2290}
2291
2292
2293/**
2294 * Handle the parsing of a address register
2295 *
2296 * \param Index - The register index we write to
2297 *
2298 * \return 0 on sucess, 1 on error
2299 */
2300static GLuint
2301parse_address_reg (GLcontext * ctx, GLubyte ** inst,
2302 struct var_cache **vc_head,
2303 struct arb_program *Program, GLint * Index)
2304{
2305 struct var_cache *dst;
2306 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002307
2308 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002309
2310 dst = parse_string (inst, vc_head, Program, &result);
2311 Program->Position = parse_position (inst);
2312
2313 /* If the name has never been added to our symbol table, we're hosed */
2314 if (!result) {
2315 _mesa_set_program_error (ctx, Program->Position, "Undefined variable");
2316 _mesa_error (ctx, GL_INVALID_OPERATION, "Undefined variable: %s",
2317 dst->name);
2318 return 1;
2319 }
2320
2321 if (dst->type != vt_address) {
2322 _mesa_set_program_error (ctx, Program->Position,
2323 "Variable is not of type ADDRESS");
2324 _mesa_error (ctx, GL_INVALID_OPERATION,
2325 "Variable: %s is not of type ADDRESS", dst->name);
2326 return 1;
2327 }
2328
2329 return 0;
2330}
2331
Brian Paul8e8fa632005-07-01 02:03:33 +00002332#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002333/**
2334 * Handle the parsing out of a masked address register
2335 *
2336 * \param Index - The register index we write to
2337 * \param WriteMask - The mask controlling which components we write (1->write)
2338 *
2339 * \return 0 on sucess, 1 on error
2340 */
2341static GLuint
2342parse_masked_address_reg (GLcontext * ctx, GLubyte ** inst,
2343 struct var_cache **vc_head,
2344 struct arb_program *Program, GLint * Index,
2345 GLboolean * WriteMask)
2346{
2347 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2348 return 1;
2349
2350 /* This should be 0x8 */
2351 (*inst)++;
2352
2353 /* Writemask of .x is implied */
2354 WriteMask[0] = 1;
2355 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2356
2357 return 0;
2358}
Brian Paul8e8fa632005-07-01 02:03:33 +00002359#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002360
2361/**
2362 * Parse out a swizzle mask.
2363 *
Brian Paul32df89e2005-10-29 18:26:43 +00002364 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002365 *
2366 * The len parameter allows us to grab 4 components for a vector
2367 * swizzle, or just 1 component for a scalar src register selection
2368 */
Brian Paul32df89e2005-10-29 18:26:43 +00002369static void
2370parse_swizzle_mask(GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002371{
Brian Paul32df89e2005-10-29 18:26:43 +00002372 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002373
Brian Paul32df89e2005-10-29 18:26:43 +00002374 for (i = 0; i < 4; i++)
2375 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002376
Brian Paul32df89e2005-10-29 18:26:43 +00002377 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002378 switch (*(*inst)++) {
2379 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002380 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002381 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002382 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002383 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002384 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002385 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002386 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002388 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002389 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002390 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002391 default:
2392 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2393 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002394 }
2395 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002396}
2397
Jouk Jansen40322e12004-04-05 08:50:36 +00002398
Brian Paul32df89e2005-10-29 18:26:43 +00002399/**
2400 * Parse an extended swizzle mask which is a sequence of
2401 * four x/y/z/w/0/1 tokens.
2402 * \return swizzle four swizzle values
2403 * \return negateMask four element bitfield
2404 */
2405static void
2406parse_extended_swizzle_mask(GLubyte **inst, GLubyte swizzle[4],
2407 GLubyte *negateMask)
2408{
2409 GLint i;
2410
2411 *negateMask = 0x0;
2412 for (i = 0; i < 4; i++) {
2413 GLubyte swz;
2414 if (parse_sign(inst) == -1)
2415 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002416
2417 swz = *(*inst)++;
2418
2419 switch (swz) {
2420 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002421 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002422 break;
2423 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002424 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002425 break;
2426 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002427 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002428 break;
2429 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002430 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002431 break;
2432 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002433 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002434 break;
2435 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002436 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002437 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002438 default:
2439 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2440 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002441 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002442 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002443}
2444
2445
2446static GLuint
2447parse_src_reg (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002448 struct arb_program *Program,
2449 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002450 GLboolean *IsRelOffset )
2451{
2452 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002453 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002454 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002455
Keith Whitwell7c26b612005-04-21 14:46:57 +00002456 *IsRelOffset = 0;
2457
Jouk Jansen40322e12004-04-05 08:50:36 +00002458 /* And the binding for the src */
2459 switch (*(*inst)++) {
2460 case REGISTER_ATTRIB:
2461 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002462 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002463 return 1;
2464 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002465 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002466
2467 /* We need to insert a dummy variable into the var_cache so we can
2468 * catch generic vertex attrib aliasing errors
2469 */
2470 var_cache_create(&src);
2471 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002472 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002473 src->attrib_binding = binding;
2474 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002475 var_cache_append(vc_head, src);
2476 if (generic_attrib_check(*vc_head)) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002477 const char *msg = "Cannot use both a generic vertex attribute "
2478 "and a specific attribute of the same type";
2479 _mesa_set_program_error (ctx, Program->Position, msg);
2480 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002481 return 1;
2482 }
2483 break;
2484
2485 case REGISTER_PARAM:
2486 switch (**inst) {
2487 case PARAM_ARRAY_ELEMENT:
2488 (*inst)++;
2489 src = parse_string (inst, vc_head, Program, &found);
2490 Program->Position = parse_position (inst);
2491
2492 if (!found) {
2493 _mesa_set_program_error (ctx, Program->Position,
2494 "2: Undefined variable");
2495 _mesa_error (ctx, GL_INVALID_OPERATION,
2496 "2: Undefined variable: %s", src->name);
2497 return 1;
2498 }
2499
Brian Paul95801792005-12-06 15:41:43 +00002500 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002501
2502 switch (*(*inst)++) {
2503 case ARRAY_INDEX_ABSOLUTE:
2504 offset = parse_integer (inst, Program);
2505
2506 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002507 || (offset >= (int)src->param_binding_length)) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002508 _mesa_set_program_error (ctx, Program->Position,
2509 "Index out of range");
2510 _mesa_error (ctx, GL_INVALID_OPERATION,
2511 "Index %d out of range for %s", offset,
2512 src->name);
2513 return 1;
2514 }
2515
2516 *Index = src->param_binding_begin + offset;
2517 break;
2518
2519 case ARRAY_INDEX_RELATIVE:
2520 {
2521 GLint addr_reg_idx, rel_off;
2522
2523 /* First, grab the address regiseter */
2524 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2525 return 1;
2526
2527 /* And the .x */
2528 ((*inst)++);
2529 ((*inst)++);
2530 ((*inst)++);
2531 ((*inst)++);
2532
2533 /* Then the relative offset */
2534 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2535
2536 /* And store it properly */
2537 *Index = src->param_binding_begin + rel_off;
2538 *IsRelOffset = 1;
2539 }
2540 break;
2541 }
2542 break;
2543
2544 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002545 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2546 return 1;
2547
Brian Paul95801792005-12-06 15:41:43 +00002548 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002549 *Index = src->param_binding_begin;
2550 break;
2551 }
2552 break;
2553
2554 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002555 src = parse_string (inst, vc_head, Program, &found);
2556 Program->Position = parse_position (inst);
2557
2558 /* If the name has never been added to our symbol table, we're hosed */
2559 if (!found) {
2560 _mesa_set_program_error (ctx, Program->Position,
2561 "3: Undefined variable");
2562 _mesa_error (ctx, GL_INVALID_OPERATION, "3: Undefined variable: %s",
2563 src->name);
2564 return 1;
2565 }
2566
2567 switch (src->type) {
2568 case vt_attrib:
2569 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002570 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002571 break;
2572
2573 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2574 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002575 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002576 *Index = src->param_binding_begin;
2577 break;
2578
2579 case vt_temp:
2580 *File = PROGRAM_TEMPORARY;
2581 *Index = src->temp_binding;
2582 break;
2583
2584 /* If the var type is vt_output no go */
2585 default:
2586 _mesa_set_program_error (ctx, Program->Position,
2587 "destination register is read only");
2588 _mesa_error (ctx, GL_INVALID_OPERATION,
2589 "destination register is read only: %s",
2590 src->name);
2591 return 1;
2592 }
2593 break;
2594
2595 default:
2596 _mesa_set_program_error (ctx, Program->Position,
2597 "Unknown token in parse_src_reg");
2598 _mesa_error (ctx, GL_INVALID_OPERATION,
2599 "Unknown token in parse_src_reg");
2600 return 1;
2601 }
2602
2603 return 0;
2604}
2605
2606/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002607 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002608 */
2609static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00002610parse_fp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
2611 struct var_cache **vc_head,
2612 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002613 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002614{
Brian Paul7aebaf32005-10-30 21:23:23 +00002615 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002616 GLint index;
2617 GLboolean negate;
2618 GLubyte swizzle[4];
2619 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002620
Jouk Jansen40322e12004-04-05 08:50:36 +00002621 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002622 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002623
2624 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002625 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002626 return 1;
2627
2628 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002629 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002630
Brian Paul32df89e2005-10-29 18:26:43 +00002631 reg->File = file;
2632 reg->Index = index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002633 reg->Abs = 0; /* NV only */
2634 reg->NegateAbs = 0; /* NV only */
Brian Paul32df89e2005-10-29 18:26:43 +00002635 reg->NegateBase = negate;
2636 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002637 return 0;
2638}
2639
Jouk Jansen40322e12004-04-05 08:50:36 +00002640
Keith Whitwell7c26b612005-04-21 14:46:57 +00002641static GLuint
2642parse_fp_dst_reg(GLcontext * ctx, GLubyte ** inst,
2643 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002644 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002645{
Brian Paul7aebaf32005-10-30 21:23:23 +00002646 GLint mask;
2647 GLuint idx;
2648 enum register_file file;
2649
Keith Whitwell7c26b612005-04-21 14:46:57 +00002650 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002651 return 1;
2652
Keith Whitwell7c26b612005-04-21 14:46:57 +00002653 reg->CondMask = 0; /* NV only */
2654 reg->CondSwizzle = 0; /* NV only */
2655 reg->File = file;
2656 reg->Index = idx;
2657 reg->WriteMask = mask;
2658 return 0;
2659}
2660
2661
Brian Paul7aebaf32005-10-30 21:23:23 +00002662/**
2663 * Parse fragment program scalar src register.
2664 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002665static GLuint
2666parse_fp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002667 struct var_cache **vc_head,
2668 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002669 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002670{
Brian Paul7aebaf32005-10-30 21:23:23 +00002671 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002672 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002673 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002674 GLubyte Swizzle[4];
2675 GLboolean IsRelOffset;
2676
2677 /* Grab the sign */
2678 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2679
2680 /* And the src reg */
2681 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2682 return 1;
2683
2684 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002685 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002686
Keith Whitwell7c26b612005-04-21 14:46:57 +00002687 reg->File = File;
2688 reg->Index = Index;
2689 reg->Abs = 0; /* NV only */
2690 reg->NegateAbs = 0; /* NV only */
2691 reg->NegateBase = Negate;
2692 reg->Swizzle = (Swizzle[0] << 0);
2693
Jouk Jansen40322e12004-04-05 08:50:36 +00002694 return 0;
2695}
2696
Keith Whitwell7c26b612005-04-21 14:46:57 +00002697
Jouk Jansen40322e12004-04-05 08:50:36 +00002698/**
2699 * This is a big mother that handles getting opcodes into the instruction
2700 * and handling the src & dst registers for fragment program instructions
2701 */
2702static GLuint
2703parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2704 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002705 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002706{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002707 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002708 GLuint texcoord;
2709 GLubyte instClass, type, code;
2710 GLboolean rel;
2711
Brian Paul7e807512005-11-05 17:10:45 +00002712 _mesa_init_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002713
2714 /* Record the position in the program string for debugging */
2715 fp->StringPos = Program->Position;
2716
2717 /* OP_ALU_INST or OP_TEX_INST */
2718 instClass = *(*inst)++;
2719
2720 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2721 * OP_TEX_{SAMPLE, KIL}
2722 */
2723 type = *(*inst)++;
2724
2725 /* The actual opcode name */
2726 code = *(*inst)++;
2727
2728 /* Increment the correct count */
2729 switch (instClass) {
2730 case OP_ALU_INST:
2731 Program->NumAluInstructions++;
2732 break;
2733 case OP_TEX_INST:
2734 Program->NumTexInstructions++;
2735 break;
2736 }
2737
Jouk Jansen40322e12004-04-05 08:50:36 +00002738 switch (type) {
2739 case OP_ALU_VECTOR:
2740 switch (code) {
2741 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002742 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002743 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002744 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002745 break;
2746
2747 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002748 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002749 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002750 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 break;
2752
2753 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002754 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002755 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002756 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002757 break;
2758
2759 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002760 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002761 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002762 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002763 break;
2764
2765 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002766 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002768 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002769 break;
2770 }
2771
Keith Whitwell7c26b612005-04-21 14:46:57 +00002772 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002773 return 1;
2774
Keith Whitwell7c26b612005-04-21 14:46:57 +00002775 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002776 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002777 break;
2778
2779 case OP_ALU_SCALAR:
2780 switch (code) {
2781 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002782 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002784 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 break;
2786
2787 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002788 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002789 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002790 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 break;
2792
2793 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002794 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002795 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002796 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 break;
2798
2799 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002800 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002801 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002802 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002803 break;
2804
2805 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002806 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002808 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002809 break;
2810
2811 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002812 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002814 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002815 break;
2816
2817 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002818 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 case OP_SCS:
2820
Brian Paul7e807512005-11-05 17:10:45 +00002821 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002822 break;
2823 }
2824
Keith Whitwell7c26b612005-04-21 14:46:57 +00002825 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002826 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002827
2828 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002829 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002830 break;
2831
2832 case OP_ALU_BINSC:
2833 switch (code) {
2834 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002835 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002836 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002837 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002838 break;
2839 }
2840
Keith Whitwell7c26b612005-04-21 14:46:57 +00002841 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002842 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002843
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002845 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002846 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 }
2848 break;
2849
2850
2851 case OP_ALU_BIN:
2852 switch (code) {
2853 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002854 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002856 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002857 break;
2858
2859 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002860 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002861 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002862 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 break;
2864
2865 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002866 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002867 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002868 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002869 break;
2870
2871 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002872 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002873 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002874 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002875 break;
2876
2877 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002878 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002879 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002880 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002881 break;
2882
2883 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002884 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002886 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002887 break;
2888
2889 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002890 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002892 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 break;
2894
2895 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002896 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002898 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 break;
2900
2901 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002902 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002903 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002904 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002905 break;
2906
2907 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002908 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002909 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002910 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002911 break;
2912
2913 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002914 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002915 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002916 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002917 break;
2918
2919 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002920 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002921 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002922 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002923 break;
2924 }
2925
Keith Whitwell7c26b612005-04-21 14:46:57 +00002926 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002927 return 1;
2928 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002929 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2930 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002931 }
2932 break;
2933
2934 case OP_ALU_TRI:
2935 switch (code) {
2936 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002937 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002938 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002939 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002940 break;
2941
2942 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002943 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002944 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002945 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002946 break;
2947
2948 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002949 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002950 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002951 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002952 break;
2953 }
2954
Keith Whitwell7c26b612005-04-21 14:46:57 +00002955 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002956 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002957
Jouk Jansen40322e12004-04-05 08:50:36 +00002958 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002959 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2960 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002961 }
2962 break;
2963
2964 case OP_ALU_SWZ:
2965 switch (code) {
2966 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002967 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002968 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002969 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002970 break;
2971 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002972 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002973 return 1;
2974
Keith Whitwell7c26b612005-04-21 14:46:57 +00002975 {
Brian Paul32df89e2005-10-29 18:26:43 +00002976 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002977 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002978 enum register_file file;
2979 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002980
Brian Paul32df89e2005-10-29 18:26:43 +00002981 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002982 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002983 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2984 fp->SrcReg[0].File = file;
2985 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002986 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002987 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2988 swizzle[1],
2989 swizzle[2],
2990 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002991 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002992 break;
2993
2994 case OP_TEX_SAMPLE:
2995 switch (code) {
2996 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002997 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002998 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002999 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003000 break;
3001
3002 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003003 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003004 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003005 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003006 break;
3007
3008 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003009 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003010 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003011 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003012 break;
3013 }
3014
Keith Whitwell7c26b612005-04-21 14:46:57 +00003015 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003016 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003017
3018 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003019 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003020
3021 /* texImageUnit */
3022 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3023 return 1;
3024 fp->TexSrcUnit = texcoord;
3025
3026 /* texTarget */
3027 switch (*(*inst)++) {
3028 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003029 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003030 break;
3031 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003032 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003033 break;
3034 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003035 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003036 break;
3037 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003038 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003039 break;
3040 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003041 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003042 break;
3043 case TEXTARGET_SHADOW1D:
3044 case TEXTARGET_SHADOW2D:
3045 case TEXTARGET_SHADOWRECT:
3046 /* TODO ARB_fragment_program_shadow code */
3047 break;
3048 }
Brian Paul7e807512005-11-05 17:10:45 +00003049 Program->TexturesUsed[texcoord] |= (1<<fp->TexSrcTarget);
Jouk Jansen40322e12004-04-05 08:50:36 +00003050 break;
3051
3052 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003053 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003054 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003055 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003056 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003057 break;
3058 }
3059
3060 return 0;
3061}
3062
Keith Whitwell7c26b612005-04-21 14:46:57 +00003063static GLuint
3064parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3065 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003066 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003067{
Brian Paul7aebaf32005-10-30 21:23:23 +00003068 GLint mask;
3069 GLuint idx;
3070 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003071
3072 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3073 return 1;
3074
3075 reg->File = file;
3076 reg->Index = idx;
3077 reg->WriteMask = mask;
3078 return 0;
3079}
3080
3081/**
3082 * Handle the parsing out of a masked address register
3083 *
3084 * \param Index - The register index we write to
3085 * \param WriteMask - The mask controlling which components we write (1->write)
3086 *
3087 * \return 0 on sucess, 1 on error
3088 */
3089static GLuint
3090parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3091 struct var_cache **vc_head,
3092 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003093 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003094{
3095 GLint idx;
3096
3097 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3098 return 1;
3099
3100 /* This should be 0x8 */
3101 (*inst)++;
3102
3103 reg->File = PROGRAM_ADDRESS;
3104 reg->Index = idx;
3105
3106 /* Writemask of .x is implied */
3107 reg->WriteMask = 0x1;
3108 return 0;
3109}
3110
3111/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003112 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003113 */
3114static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003115parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3116 struct var_cache **vc_head,
3117 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003118 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003119{
Brian Paul7aebaf32005-10-30 21:23:23 +00003120 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003121 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003122 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003123 GLubyte swizzle[4];
3124 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003125
3126 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003127 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003128
3129 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003130 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003131 return 1;
3132
3133 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003134 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003135
Brian Paul32df89e2005-10-29 18:26:43 +00003136 reg->File = file;
3137 reg->Index = index;
3138 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3139 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003140 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003141 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003142 return 0;
3143}
3144
3145
3146static GLuint
3147parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003148 struct var_cache **vc_head,
3149 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003150 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003151{
Brian Paul7aebaf32005-10-30 21:23:23 +00003152 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003153 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003154 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003155 GLubyte Swizzle[4];
3156 GLboolean IsRelOffset;
3157
3158 /* Grab the sign */
3159 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3160
3161 /* And the src reg */
3162 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3163 return 1;
3164
3165 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003166 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003167
3168 reg->File = File;
3169 reg->Index = Index;
3170 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003171 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003172 reg->RelAddr = IsRelOffset;
3173 return 0;
3174}
3175
3176
Jouk Jansen40322e12004-04-05 08:50:36 +00003177/**
3178 * This is a big mother that handles getting opcodes into the instruction
3179 * and handling the src & dst registers for vertex program instructions
3180 */
3181static GLuint
3182parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3183 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003184 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003185{
3186 GLint a;
3187 GLubyte type, code;
3188
3189 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3190 type = *(*inst)++;
3191
3192 /* The actual opcode name */
3193 code = *(*inst)++;
3194
Brian Paul7e807512005-11-05 17:10:45 +00003195 _mesa_init_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003196 /* Record the position in the program string for debugging */
3197 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003198
3199 switch (type) {
3200 /* XXX: */
3201 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003202 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003203
3204 /* Remember to set SrcReg.RelAddr; */
3205
3206 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003207 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003208 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003209
Jouk Jansen40322e12004-04-05 08:50:36 +00003210 vp->DstReg.File = PROGRAM_ADDRESS;
3211
3212 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003213 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003214 return 1;
3215
3216 break;
3217
3218 case OP_ALU_VECTOR:
3219 switch (code) {
3220 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003221 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003222 break;
3223 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003224 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003225 break;
3226 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003227 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 break;
3229 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003230 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003231 break;
3232 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003233 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003234 break;
3235 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003236
3237 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003238 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003239
3240 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003241 return 1;
3242 break;
3243
3244 case OP_ALU_SCALAR:
3245 switch (code) {
3246 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003247 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 break;
3249 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003250 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 break;
3252 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003253 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 break;
3255 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003256 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003257 break;
3258 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003259 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003260 break;
3261 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003262 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003263 break;
3264 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003265 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003266 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003267
3268 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003269 return 1;
3270 break;
3271
3272 case OP_ALU_BINSC:
3273 switch (code) {
3274 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003275 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 break;
3277 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003278 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003279 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003280
Jouk Jansen40322e12004-04-05 08:50:36 +00003281 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003282 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003283 return 1;
3284 }
3285 break;
3286
3287 case OP_ALU_BIN:
3288 switch (code) {
3289 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003290 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003291 break;
3292 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003293 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 break;
3295 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003296 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 break;
3298 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003299 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 break;
3301 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003302 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003303 break;
3304 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003305 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003306 break;
3307 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003308 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 break;
3310 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003311 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003312 break;
3313 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003314 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003315 break;
3316 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003317 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003318 break;
3319 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003320 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003321 break;
3322 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003323 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003324 break;
3325 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003326 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003327 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003328
Jouk Jansen40322e12004-04-05 08:50:36 +00003329 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003330 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003331 return 1;
3332 }
3333 break;
3334
3335 case OP_ALU_TRI:
3336 switch (code) {
3337 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003338 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003339 break;
3340 }
3341
Keith Whitwell7c26b612005-04-21 14:46:57 +00003342 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003343 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003344
Jouk Jansen40322e12004-04-05 08:50:36 +00003345 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003346 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003347 return 1;
3348 }
3349 break;
3350
3351 case OP_ALU_SWZ:
3352 switch (code) {
3353 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003354 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003355 break;
3356 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003357 {
Brian Paul32df89e2005-10-29 18:26:43 +00003358 GLubyte swizzle[4];
3359 GLubyte negateMask;
3360 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003361 enum register_file file;
3362 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003363
Keith Whitwell7c26b612005-04-21 14:46:57 +00003364 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3365 return 1;
3366
Brian Paul32df89e2005-10-29 18:26:43 +00003367 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003368 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003369 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3370 vp->SrcReg[0].File = file;
3371 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003372 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003373 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3374 swizzle[1],
3375 swizzle[2],
3376 swizzle[3]);
3377 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003378 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003379 break;
3380 }
3381 return 0;
3382}
3383
3384#if DEBUG_PARSING
3385
3386static GLvoid
3387print_state_token (GLint token)
3388{
3389 switch (token) {
3390 case STATE_MATERIAL:
3391 fprintf (stderr, "STATE_MATERIAL ");
3392 break;
3393 case STATE_LIGHT:
3394 fprintf (stderr, "STATE_LIGHT ");
3395 break;
3396
3397 case STATE_LIGHTMODEL_AMBIENT:
3398 fprintf (stderr, "STATE_AMBIENT ");
3399 break;
3400
3401 case STATE_LIGHTMODEL_SCENECOLOR:
3402 fprintf (stderr, "STATE_SCENECOLOR ");
3403 break;
3404
3405 case STATE_LIGHTPROD:
3406 fprintf (stderr, "STATE_LIGHTPROD ");
3407 break;
3408
3409 case STATE_TEXGEN:
3410 fprintf (stderr, "STATE_TEXGEN ");
3411 break;
3412
3413 case STATE_FOG_COLOR:
3414 fprintf (stderr, "STATE_FOG_COLOR ");
3415 break;
3416
3417 case STATE_FOG_PARAMS:
3418 fprintf (stderr, "STATE_FOG_PARAMS ");
3419 break;
3420
3421 case STATE_CLIPPLANE:
3422 fprintf (stderr, "STATE_CLIPPLANE ");
3423 break;
3424
3425 case STATE_POINT_SIZE:
3426 fprintf (stderr, "STATE_POINT_SIZE ");
3427 break;
3428
3429 case STATE_POINT_ATTENUATION:
3430 fprintf (stderr, "STATE_ATTENUATION ");
3431 break;
3432
3433 case STATE_MATRIX:
3434 fprintf (stderr, "STATE_MATRIX ");
3435 break;
3436
3437 case STATE_MODELVIEW:
3438 fprintf (stderr, "STATE_MODELVIEW ");
3439 break;
3440
3441 case STATE_PROJECTION:
3442 fprintf (stderr, "STATE_PROJECTION ");
3443 break;
3444
3445 case STATE_MVP:
3446 fprintf (stderr, "STATE_MVP ");
3447 break;
3448
3449 case STATE_TEXTURE:
3450 fprintf (stderr, "STATE_TEXTURE ");
3451 break;
3452
3453 case STATE_PROGRAM:
3454 fprintf (stderr, "STATE_PROGRAM ");
3455 break;
3456
3457 case STATE_MATRIX_INVERSE:
3458 fprintf (stderr, "STATE_INVERSE ");
3459 break;
3460
3461 case STATE_MATRIX_TRANSPOSE:
3462 fprintf (stderr, "STATE_TRANSPOSE ");
3463 break;
3464
3465 case STATE_MATRIX_INVTRANS:
3466 fprintf (stderr, "STATE_INVTRANS ");
3467 break;
3468
3469 case STATE_AMBIENT:
3470 fprintf (stderr, "STATE_AMBIENT ");
3471 break;
3472
3473 case STATE_DIFFUSE:
3474 fprintf (stderr, "STATE_DIFFUSE ");
3475 break;
3476
3477 case STATE_SPECULAR:
3478 fprintf (stderr, "STATE_SPECULAR ");
3479 break;
3480
3481 case STATE_EMISSION:
3482 fprintf (stderr, "STATE_EMISSION ");
3483 break;
3484
3485 case STATE_SHININESS:
3486 fprintf (stderr, "STATE_SHININESS ");
3487 break;
3488
3489 case STATE_HALF:
3490 fprintf (stderr, "STATE_HALF ");
3491 break;
3492
3493 case STATE_POSITION:
3494 fprintf (stderr, "STATE_POSITION ");
3495 break;
3496
3497 case STATE_ATTENUATION:
3498 fprintf (stderr, "STATE_ATTENUATION ");
3499 break;
3500
3501 case STATE_SPOT_DIRECTION:
3502 fprintf (stderr, "STATE_DIRECTION ");
3503 break;
3504
3505 case STATE_TEXGEN_EYE_S:
3506 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3507 break;
3508
3509 case STATE_TEXGEN_EYE_T:
3510 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3511 break;
3512
3513 case STATE_TEXGEN_EYE_R:
3514 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3515 break;
3516
3517 case STATE_TEXGEN_EYE_Q:
3518 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3519 break;
3520
3521 case STATE_TEXGEN_OBJECT_S:
3522 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3523 break;
3524
3525 case STATE_TEXGEN_OBJECT_T:
3526 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3527 break;
3528
3529 case STATE_TEXGEN_OBJECT_R:
3530 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3531 break;
3532
3533 case STATE_TEXGEN_OBJECT_Q:
3534 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3535 break;
3536
3537 case STATE_TEXENV_COLOR:
3538 fprintf (stderr, "STATE_TEXENV_COLOR ");
3539 break;
3540
3541 case STATE_DEPTH_RANGE:
3542 fprintf (stderr, "STATE_DEPTH_RANGE ");
3543 break;
3544
3545 case STATE_VERTEX_PROGRAM:
3546 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3547 break;
3548
3549 case STATE_FRAGMENT_PROGRAM:
3550 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3551 break;
3552
3553 case STATE_ENV:
3554 fprintf (stderr, "STATE_ENV ");
3555 break;
3556
3557 case STATE_LOCAL:
3558 fprintf (stderr, "STATE_LOCAL ");
3559 break;
3560
3561 }
3562 fprintf (stderr, "[%d] ", token);
3563}
3564
3565
3566static GLvoid
3567debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3568 struct arb_program *Program)
3569{
3570 struct var_cache *vc;
3571 GLint a, b;
3572
3573 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3574
3575 /* First of all, print out the contents of the var_cache */
3576 vc = vc_head;
3577 while (vc) {
3578 fprintf (stderr, "[%x]\n", vc);
3579 switch (vc->type) {
3580 case vt_none:
3581 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3582 break;
3583 case vt_attrib:
3584 fprintf (stderr, "ATTRIB %s\n", vc->name);
3585 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3586 break;
3587 case vt_param:
3588 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3589 vc->param_binding_begin, vc->param_binding_length);
3590 b = vc->param_binding_begin;
3591 for (a = 0; a < vc->param_binding_length; a++) {
3592 fprintf (stderr, "%s\n",
3593 Program->Parameters->Parameters[a + b].Name);
3594 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3595 print_state_token (Program->Parameters->Parameters[a + b].
3596 StateIndexes[0]);
3597 print_state_token (Program->Parameters->Parameters[a + b].
3598 StateIndexes[1]);
3599 print_state_token (Program->Parameters->Parameters[a + b].
3600 StateIndexes[2]);
3601 print_state_token (Program->Parameters->Parameters[a + b].
3602 StateIndexes[3]);
3603 print_state_token (Program->Parameters->Parameters[a + b].
3604 StateIndexes[4]);
3605 print_state_token (Program->Parameters->Parameters[a + b].
3606 StateIndexes[5]);
3607 }
3608 else
3609 fprintf (stderr, "%f %f %f %f\n",
3610 Program->Parameters->Parameters[a + b].Values[0],
3611 Program->Parameters->Parameters[a + b].Values[1],
3612 Program->Parameters->Parameters[a + b].Values[2],
3613 Program->Parameters->Parameters[a + b].Values[3]);
3614 }
3615 break;
3616 case vt_temp:
3617 fprintf (stderr, "TEMP %s\n", vc->name);
3618 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3619 break;
3620 case vt_output:
3621 fprintf (stderr, "OUTPUT %s\n", vc->name);
3622 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3623 break;
3624 case vt_alias:
3625 fprintf (stderr, "ALIAS %s\n", vc->name);
3626 fprintf (stderr, " binding: 0x%x (%s)\n",
3627 vc->alias_binding, vc->alias_binding->name);
3628 break;
3629 }
3630 vc = vc->next;
3631 }
3632}
3633
Brian Paul72030e02005-11-03 03:30:34 +00003634#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003635
3636
3637/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003638 * The main loop for parsing a fragment or vertex program
3639 *
Brian Paul72030e02005-11-03 03:30:34 +00003640 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003641 */
Brian Paul72030e02005-11-03 03:30:34 +00003642static GLint
Brian Paul8c41a142005-11-19 15:36:28 +00003643parse_instructions(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
Brian Paul45703642005-10-29 15:52:31 +00003644 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003645{
Brian Paul72030e02005-11-03 03:30:34 +00003646 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3647 ? ctx->Const.FragmentProgram.MaxInstructions
3648 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003649 GLint err = 0;
3650
Brian Paul72030e02005-11-03 03:30:34 +00003651 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3652
Jouk Jansen40322e12004-04-05 08:50:36 +00003653 Program->MajorVersion = (GLuint) * inst++;
3654 Program->MinorVersion = (GLuint) * inst++;
3655
3656 while (*inst != END) {
3657 switch (*inst++) {
3658
3659 case OPTION:
3660 switch (*inst++) {
3661 case ARB_PRECISION_HINT_FASTEST:
3662 Program->PrecisionOption = GL_FASTEST;
3663 break;
3664
3665 case ARB_PRECISION_HINT_NICEST:
3666 Program->PrecisionOption = GL_NICEST;
3667 break;
3668
3669 case ARB_FOG_EXP:
3670 Program->FogOption = GL_EXP;
3671 break;
3672
3673 case ARB_FOG_EXP2:
3674 Program->FogOption = GL_EXP2;
3675 break;
3676
3677 case ARB_FOG_LINEAR:
3678 Program->FogOption = GL_LINEAR;
3679 break;
3680
3681 case ARB_POSITION_INVARIANT:
3682 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003683 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003684 break;
3685
3686 case ARB_FRAGMENT_PROGRAM_SHADOW:
3687 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3688 /* TODO ARB_fragment_program_shadow code */
3689 }
3690 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003691
3692 case ARB_DRAW_BUFFERS:
3693 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3694 /* do nothing for now */
3695 }
3696 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003697 }
3698 break;
3699
3700 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003701 /* check length */
3702 if (Program->Base.NumInstructions + 1 >= maxInst) {
3703 const char *msg = "Max instruction count exceeded";
3704 _mesa_set_program_error(ctx, Program->Position, msg);
3705 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3706 return 1;
3707 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003708 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003709 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003710 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003711 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003712 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003713 }
3714 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003715 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003716 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 }
3718
Brian Paul72030e02005-11-03 03:30:34 +00003719 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003720 Program->Base.NumInstructions++;
3721 break;
3722
3723 case DECLARATION:
3724 err = parse_declaration (ctx, &inst, vc_head, Program);
3725 break;
3726
3727 default:
3728 break;
3729 }
3730
3731 if (err)
3732 break;
3733 }
3734
3735 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003736 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003737 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul8c41a142005-11-19 15:36:28 +00003738 _mesa_init_instruction(Program->Base.Instructions + numInst);
3739 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003740 /* YYY Wrong Position in program, whatever, at least not random -> crash
3741 Program->Position = parse_position (&inst);
3742 */
Brian Paul8c41a142005-11-19 15:36:28 +00003743 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003744 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003745 Program->Base.NumInstructions++;
3746
Brian Paul05051032005-11-01 04:36:33 +00003747 /*
3748 * Initialize native counts to logical counts. The device driver may
3749 * change them if program is translated into a hardware program.
3750 */
3751 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3752 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3753 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3754 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3755 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003756
Jouk Jansen40322e12004-04-05 08:50:36 +00003757 return err;
3758}
3759
Brian Paul7aebaf32005-10-30 21:23:23 +00003760
Jouk Jansen40322e12004-04-05 08:50:36 +00003761/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003762__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003763#include "grammar_syn.h"
3764;
3765
Brian Paul7aebaf32005-10-30 21:23:23 +00003766
Brian Paul8c41a142005-11-19 15:36:28 +00003767/**
3768 * Set a grammar parameter.
3769 * \param name the grammar parameter
3770 * \param value the new parameter value
3771 * \return 0 if OK, 1 if error
3772 */
3773static int
3774set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003775{
3776 char error_msg[300];
3777 GLint error_pos;
3778
Brian Paul8c41a142005-11-19 15:36:28 +00003779 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003780 return 0;
3781
3782 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3783 _mesa_set_program_error (ctx, error_pos, error_msg);
3784 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3785 return 1;
3786}
3787
Brian Paul8c41a142005-11-19 15:36:28 +00003788
3789/**
3790 * Enable support for the given language option in the parser.
3791 * \return 1 if OK, 0 if error
3792 */
3793static int
3794enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003795{
Brian Paul8c41a142005-11-19 15:36:28 +00003796 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003797}
3798
Brian Paul8c41a142005-11-19 15:36:28 +00003799
3800/**
3801 * Enable parser extensions based on which OpenGL extensions are supported
3802 * by this rendering context.
3803 *
3804 * \return GL_TRUE if OK, GL_FALSE if error.
3805 */
3806static GLboolean
3807enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003808{
Brian Paul8c41a142005-11-19 15:36:28 +00003809#if 0
3810 /* These are not supported at this time */
3811 if ((ctx->Extensions.ARB_vertex_blend ||
3812 ctx->Extensions.EXT_vertex_weighting)
3813 && !enable_ext(ctx, id, "point_parameters"))
3814 return GL_FALSE;
3815 if (ctx->Extensions.ARB_matrix_palette
3816 && !enable_ext(ctx, id, "matrix_palette"))
3817 return GL_FALSE;
3818 if (ctx->Extensions.ARB_fragment_program_shadow
3819 && !enable_ext(ctx, id, "fragment_program_shadow"))
3820 return GL_FALSE;
3821#endif
3822 if (ctx->Extensions.EXT_point_parameters
3823 && !enable_ext(ctx, id, "point_parameters"))
3824 return GL_FALSE;
3825 if (ctx->Extensions.EXT_secondary_color
3826 && !enable_ext(ctx, id, "secondary_color"))
3827 return GL_FALSE;
3828 if (ctx->Extensions.EXT_fog_coord
3829 && !enable_ext(ctx, id, "fog_coord"))
3830 return GL_FALSE;
3831 if (ctx->Extensions.NV_texture_rectangle
3832 && !enable_ext(ctx, id, "texture_rectangle"))
3833 return GL_FALSE;
3834 if (ctx->Extensions.ARB_draw_buffers
3835 && !enable_ext(ctx, id, "draw_buffers"))
3836 return GL_FALSE;
3837
3838 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003839}
3840
Brian Paul8c41a142005-11-19 15:36:28 +00003841
Jouk Jansen40322e12004-04-05 08:50:36 +00003842/**
3843 * This kicks everything off.
3844 *
3845 * \param ctx - The GL Context
3846 * \param str - The program string
3847 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003848 * \param program - The arb_program struct to return all the parsed info in
3849 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003850 */
Brian Paul8c41a142005-11-19 15:36:28 +00003851static GLboolean
3852_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3853 const GLubyte *str, GLsizei len,
3854 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003855{
3856 GLint a, err, error_pos;
3857 char error_msg[300];
3858 GLuint parsed_len;
3859 struct var_cache *vc_head;
3860 grammar arbprogram_syn_id;
3861 GLubyte *parsed, *inst;
3862 GLubyte *strz = NULL;
3863 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3864
Brian Paul8c41a142005-11-19 15:36:28 +00003865 /* set the program target before parsing */
3866 program->Base.Target = target;
3867
Brian Paul7f76b8f2004-09-10 01:05:39 +00003868 /* Reset error state */
3869 _mesa_set_program_error(ctx, -1, NULL);
3870
Brian Paul8c41a142005-11-19 15:36:28 +00003871 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003872 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003873 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003874 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003875 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003876
3877 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3878 if (grammar_syn_id == 0) {
3879 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003880 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003881 _mesa_set_program_error (ctx, error_pos, error_msg);
3882 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003883 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003884 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003885 }
3886
Brian Paul8c41a142005-11-19 15:36:28 +00003887 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3888 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003889
Brian Paul49a80ca2006-04-28 15:40:11 +00003890 /* 'parsed' is unused here */
3891 _mesa_free (parsed);
3892 parsed = NULL;
3893
Brian Paul45703642005-10-29 15:52:31 +00003894 /* NOTE: we can't destroy grammar_syn_id right here because
3895 * grammar_destroy() can reset the last error
3896 */
Brian Paul8c41a142005-11-19 15:36:28 +00003897 if (err) {
3898 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003899 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3900 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003901 _mesa_error (ctx, GL_INVALID_OPERATION,
3902 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003903 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003904 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003905 }
3906
3907 grammar_destroy (grammar_syn_id);
3908
3909 arbprogram_syn_is_ok = 1;
3910 }
3911
3912 /* create the grammar object */
3913 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3914 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003915 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003916 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3917 _mesa_set_program_error (ctx, error_pos, error_msg);
3918 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003919 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003920 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003921 }
3922
3923 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003924 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003925 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3926 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003927 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003928 }
3929
Brian Paul8c41a142005-11-19 15:36:28 +00003930 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3931 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003932 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003933 }
3934
3935 /* check for NULL character occurences */
3936 {
Brian Paul8c41a142005-11-19 15:36:28 +00003937 GLint i;
3938 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003939 if (str[i] == '\0') {
3940 _mesa_set_program_error (ctx, i, "invalid character");
Brian Paul8c41a142005-11-19 15:36:28 +00003941 _mesa_error (ctx, GL_INVALID_OPERATION,
3942 "glProgramStringARB(illegal character)");
Jouk Jansen40322e12004-04-05 08:50:36 +00003943 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003944 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003945 }
Brian Paul8c41a142005-11-19 15:36:28 +00003946 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003947 }
3948
3949 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003950 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003951 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003952 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3953 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003954 return GL_FALSE;
3955 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003956 _mesa_memcpy (strz, str, len);
3957 strz[len] = '\0';
3958
Michal Krolb80bc052004-10-21 14:09:54 +00003959 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003960 err = !grammar_fast_check(arbprogram_syn_id, strz,
3961 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003962
3963 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003964 if (err) {
3965 _mesa_free(strz);
Brian Paul49a80ca2006-04-28 15:40:11 +00003966 _mesa_free(parsed);
Jouk Jansen40322e12004-04-05 08:50:36 +00003967 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3968 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003969 _mesa_error (ctx, GL_INVALID_OPERATION,
3970 "glProgramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003971
3972 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003973#if DEBUG_PARSING
3974 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003975 int line, col;
3976 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003977 fprintf(stderr, "program: %s\n", (char *) strz);
3978 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
3979 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003980 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3981 } while (0)
3982#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003983
Jouk Jansen40322e12004-04-05 08:50:36 +00003984 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003985 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003986 }
3987
Jouk Jansen40322e12004-04-05 08:50:36 +00003988 grammar_destroy (arbprogram_syn_id);
3989
Brian Paul8c41a142005-11-19 15:36:28 +00003990 /*
3991 * Program string is syntactically correct at this point
3992 * Parse the tokenized version of the program now, generating
3993 * vertex/fragment program instructions.
3994 */
3995
Jouk Jansen40322e12004-04-05 08:50:36 +00003996 /* Initialize the arb_program struct */
3997 program->Base.String = strz;
Brian Paul8c41a142005-11-19 15:36:28 +00003998 program->Base.Instructions = (struct prog_instruction *)
3999 _mesa_malloc(MAX_INSTRUCTIONS * sizeof(struct prog_instruction));
Jouk Jansen40322e12004-04-05 08:50:36 +00004000 program->Base.NumInstructions =
4001 program->Base.NumTemporaries =
4002 program->Base.NumParameters =
4003 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00004004 program->Base.Parameters = _mesa_new_parameter_list ();
4005 program->Base.InputsRead = 0x0;
4006 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004007 program->Position = 0;
4008 program->MajorVersion = program->MinorVersion = 0;
4009 program->PrecisionOption = GL_DONT_CARE;
4010 program->FogOption = GL_NONE;
4011 program->HintPositionInvariant = GL_FALSE;
4012 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00004013 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004014 program->NumAluInstructions =
4015 program->NumTexInstructions =
4016 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00004017 program->UsesKill = 0;
4018
Jouk Jansen40322e12004-04-05 08:50:36 +00004019 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00004020 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004021
4022 /* Start examining the tokens in the array */
4023 inst = parsed;
4024
4025 /* Check the grammer rev */
4026 if (*inst++ != REVISION) {
4027 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00004028 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul45cd2f92005-11-03 02:25:10 +00004029 "glProgramStringARB(Grammar version mismatch)");
Brian Paul45703642005-10-29 15:52:31 +00004030 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004031 }
4032 else {
Michal Krolb80bc052004-10-21 14:09:54 +00004033 /* ignore program target */
4034 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00004035 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00004036 }
4037
4038 /*debug_variables(ctx, vc_head, program); */
4039
4040 /* We're done with the parsed binary array */
4041 var_cache_destroy (&vc_head);
4042
4043 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00004044
Brian Paul8c41a142005-11-19 15:36:28 +00004045 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4046 * to size [ap.Base.NumInstructions].
4047 */
4048 program->Base.Instructions = (struct prog_instruction *)
4049 _mesa_realloc(program->Base.Instructions,
4050 MAX_INSTRUCTIONS * sizeof(struct prog_instruction),/*orig*/
4051 program->Base.NumInstructions * sizeof(struct prog_instruction));
4052
Brian Paul45703642005-10-29 15:52:31 +00004053 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004054}
Brian Paul8c41a142005-11-19 15:36:28 +00004055
4056
4057
4058void
4059_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4060 const GLvoid *str, GLsizei len,
4061 struct fragment_program *program)
4062{
4063 struct arb_program ap;
4064 GLuint i;
4065
4066 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004067 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004068 /* Error in the program. Just return. */
4069 return;
4070 }
4071
4072 /* Copy the relevant contents of the arb_program struct into the
4073 * fragment_program struct.
4074 */
4075 program->Base.String = ap.Base.String;
4076 program->Base.NumInstructions = ap.Base.NumInstructions;
4077 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4078 program->Base.NumParameters = ap.Base.NumParameters;
4079 program->Base.NumAttributes = ap.Base.NumAttributes;
4080 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4081 program->NumAluInstructions = ap.NumAluInstructions;
4082 program->NumTexInstructions = ap.NumTexInstructions;
4083 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004084 program->NumNativeAluInstructions = ap.NumAluInstructions;
4085 program->NumNativeTexInstructions = ap.NumTexInstructions;
4086 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004087 program->Base.InputsRead = ap.Base.InputsRead;
4088 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4089 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4090 program->TexturesUsed[i] = ap.TexturesUsed[i];
4091 program->FogOption = ap.FogOption;
4092
4093 if (program->Base.Instructions)
4094 _mesa_free(program->Base.Instructions);
4095 program->Base.Instructions = ap.Base.Instructions;
4096
4097 if (program->Base.Parameters)
4098 _mesa_free_parameter_list(program->Base.Parameters);
4099 program->Base.Parameters = ap.Base.Parameters;
4100
4101#if DEBUG_FP
4102 _mesa_print_program(&program.Base);
4103#endif
4104}
4105
4106
4107
4108/**
4109 * Parse the vertex program string. If success, update the given
4110 * vertex_program object with the new program. Else, leave the vertex_program
4111 * object unchanged.
4112 */
4113void
4114_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4115 const GLvoid *str, GLsizei len,
4116 struct vertex_program *program)
4117{
4118 struct arb_program ap;
4119
4120 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4121
Brian Paul95801792005-12-06 15:41:43 +00004122 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004123 /* Error in the program. Just return. */
4124 return;
4125 }
4126
4127 /* Copy the relevant contents of the arb_program struct into the
4128 * vertex_program struct.
4129 */
4130 program->Base.String = ap.Base.String;
4131 program->Base.NumInstructions = ap.Base.NumInstructions;
4132 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4133 program->Base.NumParameters = ap.Base.NumParameters;
4134 program->Base.NumAttributes = ap.Base.NumAttributes;
4135 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4136 program->Base.InputsRead = ap.Base.InputsRead;
4137 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4138 program->IsPositionInvariant = ap.HintPositionInvariant;
4139
4140 if (program->Base.Instructions)
4141 _mesa_free(program->Base.Instructions);
4142 program->Base.Instructions = ap.Base.Instructions;
4143
4144 if (program->Base.Parameters)
4145 _mesa_free_parameter_list(program->Base.Parameters);
4146 program->Base.Parameters = ap.Base.Parameters;
4147
4148#if DEBUG_VP
4149 _mesa_print_program(&program->Base);
4150#endif
4151}