blob: 4edd921b160ae39034932549af4d7c169d42817e [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian Paula7543902006-08-24 21:58:32 +00003 * Version: 6.5.1
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"
Brian Paul6211a142006-08-24 23:37:13 +000039#include "macros.h"
Brian Paul8c41a142005-11-19 15:36:28 +000040#include "mtypes.h"
41#include "program_instruction.h"
42
43
Brian Paul6211a142006-08-24 23:37:13 +000044/* For ARB programs, use the NV instruction limits */
45#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
46 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000047
48
49/**
50 * This is basically a union of the vertex_program and fragment_program
51 * structs that we can use to parse the program into
52 *
53 * XXX we can probably get rid of this entirely someday.
54 */
55struct arb_program
56{
Brian Paul122629f2006-07-20 16:49:57 +000057 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000058
59 GLuint Position; /* Just used for error reporting while parsing */
60 GLuint MajorVersion;
61 GLuint MinorVersion;
62
63 /* ARB_vertex_progmra options */
64 GLboolean HintPositionInvariant;
65
66 /* ARB_fragment_progmra options */
67 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
68 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
69
70 /* ARB_fragment_program specifics */
71 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
72 GLuint NumAluInstructions;
73 GLuint NumTexInstructions;
74 GLuint NumTexIndirections;
75
76 GLboolean UsesKill;
77};
78
Ian Romanick9bdfee32005-07-18 12:31:24 +000079
Alan Hourihane38b317d2004-12-14 09:11:52 +000080#ifndef __extension__
81#if !defined(__GNUC__) || (__GNUC__ < 2) || \
82 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
Brian Paula6c423d2004-08-25 15:59:48 +000083# define __extension__
84#endif
Alan Hourihane38b317d2004-12-14 09:11:52 +000085#endif
Brian Paula6c423d2004-08-25 15:59:48 +000086
Jouk Jansen40322e12004-04-05 08:50:36 +000087/* TODO:
88 * Fragment Program Stuff:
89 * -----------------------------------------------------
90 *
91 * - things from Michal's email
92 * + overflow on atoi
93 * + not-overflowing floats (don't use parse_integer..)
94 * + can remove range checking in arbparse.c
95 *
96 * - check all limits of number of various variables
97 * + parameters
98 *
99 * - test! test! test!
100 *
101 * Vertex Program Stuff:
102 * -----------------------------------------------------
103 * - Optimize param array usage and count limits correctly, see spec,
104 * section 2.14.3.7
105 * + Record if an array is reference absolutly or relatively (or both)
106 * + For absolute arrays, store a bitmap of accesses
107 * + For single parameters, store an access flag
108 * + After parsing, make a parameter cleanup and merging pass, where
109 * relative arrays are layed out first, followed by abs arrays, and
110 * finally single state.
111 * + Remap offsets for param src and dst registers
112 * + Now we can properly count parameter usage
113 *
114 * - Multiple state binding errors in param arrays (see spec, just before
115 * section 2.14.3.3)
116 * - grep for XXX
117 *
118 * Mesa Stuff
119 * -----------------------------------------------------
120 * - User clipping planes vs. PositionInvariant
121 * - Is it sufficient to just multiply by the mvp to transform in the
122 * PositionInvariant case? Or do we need something more involved?
123 *
124 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
125 * - fetch state listed in program_parameters list
126 * + WTF should this go???
127 * + currently in nvvertexec.c and s_nvfragprog.c
128 *
129 * - allow for multiple address registers (and fetch address regs properly)
130 *
131 * Cosmetic Stuff
132 * -----------------------------------------------------
133 * - remove any leftover unused grammer.c stuff (dict_ ?)
134 * - fix grammer.c error handling so its not static
135 * - #ifdef around stuff pertaining to extentions
136 *
137 * Outstanding Questions:
138 * -----------------------------------------------------
139 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
140 * what gets hacked off because of this:
141 * + VERTEX_ATTRIB_MATRIXINDEX
142 * + VERTEX_ATTRIB_WEIGHT
143 * + MATRIX_MODELVIEW
144 * + MATRIX_PALETTE
145 *
146 * - When can we fetch env/local params from their own register files, and
147 * when to we have to fetch them into the main state register file?
148 * (think arrays)
149 *
150 * Grammar Changes:
151 * -----------------------------------------------------
152 */
153
154/* Changes since moving the file to shader directory
155
1562004-III-4 ------------------------------------------------------------
157- added #include "grammar_mesa.h"
158- removed grammar specific code part (it resides now in grammar.c)
159- added GL_ARB_fragment_program_shadow tokens
160- modified #include "arbparse_syn.h"
161- major changes inside _mesa_parse_arb_program()
162- check the program string for '\0' characters
163- copy the program string to a one-byte-longer location to have
164 it null-terminated
165- position invariance test (not writing to result.position) moved
166 to syntax part
167*/
168
169typedef GLubyte *production;
170
171/**
172 * This is the text describing the rules to parse the grammar
173 */
Brian Paula6c423d2004-08-25 15:59:48 +0000174__extension__ static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000175#include "arbprogram_syn.h"
176;
177
178/**
179 * These should match up with the values defined in arbprogram.syn
180 */
181
182/*
183 Changes:
184 - changed and merged V_* and F_* opcode values to OP_*.
185 - added GL_ARB_fragment_program_shadow specific tokens (michal)
186*/
Michal Krolb80bc052004-10-21 14:09:54 +0000187#define REVISION 0x09
Jouk Jansen40322e12004-04-05 08:50:36 +0000188
189/* program type */
190#define FRAGMENT_PROGRAM 0x01
191#define VERTEX_PROGRAM 0x02
192
193/* program section */
194#define OPTION 0x01
195#define INSTRUCTION 0x02
196#define DECLARATION 0x03
197#define END 0x04
198
Michal Krolb80bc052004-10-21 14:09:54 +0000199/* GL_ARB_fragment_program option */
200#define ARB_PRECISION_HINT_FASTEST 0x00
201#define ARB_PRECISION_HINT_NICEST 0x01
202#define ARB_FOG_EXP 0x02
203#define ARB_FOG_EXP2 0x03
204#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000205
Michal Krolb80bc052004-10-21 14:09:54 +0000206/* GL_ARB_vertex_program option */
207#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000208
Michal Krolb80bc052004-10-21 14:09:54 +0000209/* GL_ARB_fragment_program_shadow option */
210#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000211
Michal Krolb80bc052004-10-21 14:09:54 +0000212/* GL_ARB_draw_buffers option */
213#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000214
Jouk Jansen40322e12004-04-05 08:50:36 +0000215/* GL_ARB_fragment_program instruction class */
216#define OP_ALU_INST 0x00
217#define OP_TEX_INST 0x01
218
219/* GL_ARB_vertex_program instruction class */
220/* OP_ALU_INST */
221
222/* GL_ARB_fragment_program instruction type */
223#define OP_ALU_VECTOR 0x00
224#define OP_ALU_SCALAR 0x01
225#define OP_ALU_BINSC 0x02
226#define OP_ALU_BIN 0x03
227#define OP_ALU_TRI 0x04
228#define OP_ALU_SWZ 0x05
229#define OP_TEX_SAMPLE 0x06
230#define OP_TEX_KIL 0x07
231
232/* GL_ARB_vertex_program instruction type */
233#define OP_ALU_ARL 0x08
234/* OP_ALU_VECTOR */
235/* OP_ALU_SCALAR */
236/* OP_ALU_BINSC */
237/* OP_ALU_BIN */
238/* OP_ALU_TRI */
239/* OP_ALU_SWZ */
240
241/* GL_ARB_fragment_program instruction code */
242#define OP_ABS 0x00
243#define OP_ABS_SAT 0x1B
244#define OP_FLR 0x09
245#define OP_FLR_SAT 0x26
246#define OP_FRC 0x0A
247#define OP_FRC_SAT 0x27
248#define OP_LIT 0x0C
249#define OP_LIT_SAT 0x2A
250#define OP_MOV 0x11
251#define OP_MOV_SAT 0x30
252#define OP_COS 0x1F
253#define OP_COS_SAT 0x20
254#define OP_EX2 0x07
255#define OP_EX2_SAT 0x25
256#define OP_LG2 0x0B
257#define OP_LG2_SAT 0x29
258#define OP_RCP 0x14
259#define OP_RCP_SAT 0x33
260#define OP_RSQ 0x15
261#define OP_RSQ_SAT 0x34
262#define OP_SIN 0x38
263#define OP_SIN_SAT 0x39
264#define OP_SCS 0x35
265#define OP_SCS_SAT 0x36
266#define OP_POW 0x13
267#define OP_POW_SAT 0x32
268#define OP_ADD 0x01
269#define OP_ADD_SAT 0x1C
270#define OP_DP3 0x03
271#define OP_DP3_SAT 0x21
272#define OP_DP4 0x04
273#define OP_DP4_SAT 0x22
274#define OP_DPH 0x05
275#define OP_DPH_SAT 0x23
276#define OP_DST 0x06
277#define OP_DST_SAT 0x24
278#define OP_MAX 0x0F
279#define OP_MAX_SAT 0x2E
280#define OP_MIN 0x10
281#define OP_MIN_SAT 0x2F
282#define OP_MUL 0x12
283#define OP_MUL_SAT 0x31
284#define OP_SGE 0x16
285#define OP_SGE_SAT 0x37
286#define OP_SLT 0x17
287#define OP_SLT_SAT 0x3A
288#define OP_SUB 0x18
289#define OP_SUB_SAT 0x3B
290#define OP_XPD 0x1A
291#define OP_XPD_SAT 0x43
292#define OP_CMP 0x1D
293#define OP_CMP_SAT 0x1E
294#define OP_LRP 0x2B
295#define OP_LRP_SAT 0x2C
296#define OP_MAD 0x0E
297#define OP_MAD_SAT 0x2D
298#define OP_SWZ 0x19
299#define OP_SWZ_SAT 0x3C
300#define OP_TEX 0x3D
301#define OP_TEX_SAT 0x3E
302#define OP_TXB 0x3F
303#define OP_TXB_SAT 0x40
304#define OP_TXP 0x41
305#define OP_TXP_SAT 0x42
306#define OP_KIL 0x28
307
308/* GL_ARB_vertex_program instruction code */
309#define OP_ARL 0x02
310/* OP_ABS */
311/* OP_FLR */
312/* OP_FRC */
313/* OP_LIT */
314/* OP_MOV */
315/* OP_EX2 */
316#define OP_EXP 0x08
317/* OP_LG2 */
318#define OP_LOG 0x0D
319/* OP_RCP */
320/* OP_RSQ */
321/* OP_POW */
322/* OP_ADD */
323/* OP_DP3 */
324/* OP_DP4 */
325/* OP_DPH */
326/* OP_DST */
327/* OP_MAX */
328/* OP_MIN */
329/* OP_MUL */
330/* OP_SGE */
331/* OP_SLT */
332/* OP_SUB */
333/* OP_XPD */
334/* OP_MAD */
335/* OP_SWZ */
336
337/* fragment attribute binding */
338#define FRAGMENT_ATTRIB_COLOR 0x01
339#define FRAGMENT_ATTRIB_TEXCOORD 0x02
340#define FRAGMENT_ATTRIB_FOGCOORD 0x03
341#define FRAGMENT_ATTRIB_POSITION 0x04
342
343/* vertex attribute binding */
344#define VERTEX_ATTRIB_POSITION 0x01
345#define VERTEX_ATTRIB_WEIGHT 0x02
346#define VERTEX_ATTRIB_NORMAL 0x03
347#define VERTEX_ATTRIB_COLOR 0x04
348#define VERTEX_ATTRIB_FOGCOORD 0x05
349#define VERTEX_ATTRIB_TEXCOORD 0x06
350#define VERTEX_ATTRIB_MATRIXINDEX 0x07
351#define VERTEX_ATTRIB_GENERIC 0x08
352
353/* fragment result binding */
354#define FRAGMENT_RESULT_COLOR 0x01
355#define FRAGMENT_RESULT_DEPTH 0x02
356
357/* vertex result binding */
358#define VERTEX_RESULT_POSITION 0x01
359#define VERTEX_RESULT_COLOR 0x02
360#define VERTEX_RESULT_FOGCOORD 0x03
361#define VERTEX_RESULT_POINTSIZE 0x04
362#define VERTEX_RESULT_TEXCOORD 0x05
363
364/* texture target */
365#define TEXTARGET_1D 0x01
366#define TEXTARGET_2D 0x02
367#define TEXTARGET_3D 0x03
368#define TEXTARGET_RECT 0x04
369#define TEXTARGET_CUBE 0x05
370/* GL_ARB_fragment_program_shadow */
371#define TEXTARGET_SHADOW1D 0x06
372#define TEXTARGET_SHADOW2D 0x07
373#define TEXTARGET_SHADOWRECT 0x08
374
375/* face type */
376#define FACE_FRONT 0x00
377#define FACE_BACK 0x01
378
379/* color type */
380#define COLOR_PRIMARY 0x00
381#define COLOR_SECONDARY 0x01
382
383/* component */
384#define COMPONENT_X 0x00
385#define COMPONENT_Y 0x01
386#define COMPONENT_Z 0x02
387#define COMPONENT_W 0x03
388#define COMPONENT_0 0x04
389#define COMPONENT_1 0x05
390
391/* array index type */
392#define ARRAY_INDEX_ABSOLUTE 0x00
393#define ARRAY_INDEX_RELATIVE 0x01
394
395/* matrix name */
396#define MATRIX_MODELVIEW 0x01
397#define MATRIX_PROJECTION 0x02
398#define MATRIX_MVP 0x03
399#define MATRIX_TEXTURE 0x04
400#define MATRIX_PALETTE 0x05
401#define MATRIX_PROGRAM 0x06
402
403/* matrix modifier */
404#define MATRIX_MODIFIER_IDENTITY 0x00
405#define MATRIX_MODIFIER_INVERSE 0x01
406#define MATRIX_MODIFIER_TRANSPOSE 0x02
407#define MATRIX_MODIFIER_INVTRANS 0x03
408
409/* constant type */
410#define CONSTANT_SCALAR 0x01
411#define CONSTANT_VECTOR 0x02
412
413/* program param type */
414#define PROGRAM_PARAM_ENV 0x01
415#define PROGRAM_PARAM_LOCAL 0x02
416
417/* register type */
418#define REGISTER_ATTRIB 0x01
419#define REGISTER_PARAM 0x02
420#define REGISTER_RESULT 0x03
421#define REGISTER_ESTABLISHED_NAME 0x04
422
423/* param binding */
424#define PARAM_NULL 0x00
425#define PARAM_ARRAY_ELEMENT 0x01
426#define PARAM_STATE_ELEMENT 0x02
427#define PARAM_PROGRAM_ELEMENT 0x03
428#define PARAM_PROGRAM_ELEMENTS 0x04
429#define PARAM_CONSTANT 0x05
430
431/* param state property */
432#define STATE_MATERIAL_PARSER 0x01
433#define STATE_LIGHT_PARSER 0x02
434#define STATE_LIGHT_MODEL 0x03
435#define STATE_LIGHT_PROD 0x04
436#define STATE_FOG 0x05
437#define STATE_MATRIX_ROWS 0x06
438/* GL_ARB_fragment_program */
439#define STATE_TEX_ENV 0x07
440#define STATE_DEPTH 0x08
441/* GL_ARB_vertex_program */
442#define STATE_TEX_GEN 0x09
443#define STATE_CLIP_PLANE 0x0A
444#define STATE_POINT 0x0B
445
446/* state material property */
447#define MATERIAL_AMBIENT 0x01
448#define MATERIAL_DIFFUSE 0x02
449#define MATERIAL_SPECULAR 0x03
450#define MATERIAL_EMISSION 0x04
451#define MATERIAL_SHININESS 0x05
452
453/* state light property */
454#define LIGHT_AMBIENT 0x01
455#define LIGHT_DIFFUSE 0x02
456#define LIGHT_SPECULAR 0x03
457#define LIGHT_POSITION 0x04
458#define LIGHT_ATTENUATION 0x05
459#define LIGHT_HALF 0x06
460#define LIGHT_SPOT_DIRECTION 0x07
461
462/* state light model property */
463#define LIGHT_MODEL_AMBIENT 0x01
464#define LIGHT_MODEL_SCENECOLOR 0x02
465
466/* state light product property */
467#define LIGHT_PROD_AMBIENT 0x01
468#define LIGHT_PROD_DIFFUSE 0x02
469#define LIGHT_PROD_SPECULAR 0x03
470
471/* state texture environment property */
472#define TEX_ENV_COLOR 0x01
473
474/* state texture generation coord property */
475#define TEX_GEN_EYE 0x01
476#define TEX_GEN_OBJECT 0x02
477
478/* state fog property */
479#define FOG_COLOR 0x01
480#define FOG_PARAMS 0x02
481
482/* state depth property */
483#define DEPTH_RANGE 0x01
484
485/* state point parameters property */
486#define POINT_SIZE 0x01
487#define POINT_ATTENUATION 0x02
488
489/* declaration */
490#define ATTRIB 0x01
491#define PARAM 0x02
492#define TEMP 0x03
493#define OUTPUT 0x04
494#define ALIAS 0x05
495/* GL_ARB_vertex_program */
496#define ADDRESS 0x06
497
498/*-----------------------------------------------------------------------
499 * From here on down is the semantic checking portion
500 *
501 */
502
503/**
504 * Variable Table Handling functions
505 */
506typedef enum
507{
508 vt_none,
509 vt_address,
510 vt_attrib,
511 vt_param,
512 vt_temp,
513 vt_output,
514 vt_alias
515} var_type;
516
517
Brian Paul7aebaf32005-10-30 21:23:23 +0000518/**
519 * Setting an explicit field for each of the binding properties is a bit
520 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000521 */
522struct var_cache
523{
Brian Paul6a769d92006-04-28 15:42:15 +0000524 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000525 var_type type;
526 GLuint address_binding; /* The index of the address register we should
527 * be using */
528 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000529 GLuint attrib_is_generic; /* If the attrib was specified through a generic
530 * vertex attrib */
531 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000532 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000533 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
534 * that this is aliased to */
535 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
536 * PROGRAM_ENV_PARAM} */
537 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
538 * the tokens representing our bound state (or constants)
539 * start */
540 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
541 * we take up with our state tokens or constants. Note that
542 * this is _not_ the same as the number of param registers
543 * we eventually use */
544 struct var_cache *next;
545};
546
547static GLvoid
548var_cache_create (struct var_cache **va)
549{
550 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
551 if (*va) {
552 (**va).name = NULL;
553 (**va).type = vt_none;
554 (**va).attrib_binding = ~0;
555 (**va).attrib_is_generic = 0;
556 (**va).temp_binding = ~0;
557 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000558 (**va).param_binding_type = ~0;
559 (**va).param_binding_begin = ~0;
560 (**va).param_binding_length = ~0;
561 (**va).alias_binding = NULL;
562 (**va).next = NULL;
563 }
564}
565
566static GLvoid
567var_cache_destroy (struct var_cache **va)
568{
569 if (*va) {
570 var_cache_destroy (&(**va).next);
571 _mesa_free (*va);
572 *va = NULL;
573 }
574}
575
576static GLvoid
577var_cache_append (struct var_cache **va, struct var_cache *nv)
578{
579 if (*va)
580 var_cache_append (&(**va).next, nv);
581 else
582 *va = nv;
583}
584
585static struct var_cache *
586var_cache_find (struct var_cache *va, GLubyte * name)
587{
Brian Paul0a360cf2005-01-17 01:21:03 +0000588 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000589
590 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000591 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000592 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000593 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000594 return va;
595 }
596
597 va = va->next;
598 }
599
600 return NULL;
601}
602
603/**
604 * constructs an integer from 4 GLubytes in LE format
605 */
606static GLuint
607parse_position (GLubyte ** inst)
608{
609 GLuint value;
610
611 value = (GLuint) (*(*inst)++);
612 value += (GLuint) (*(*inst)++) * 0x100;
613 value += (GLuint) (*(*inst)++) * 0x10000;
614 value += (GLuint) (*(*inst)++) * 0x1000000;
615
616 return value;
617}
618
619/**
620 * This will, given a string, lookup the string as a variable name in the
621 * var cache. If the name is found, the var cache node corresponding to the
622 * var name is returned. If it is not found, a new entry is allocated
623 *
624 * \param I Points into the binary array where the string identifier begins
625 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
626 * \return The location on the var_cache corresponding the the string starting at I
627 */
628static struct var_cache *
629parse_string (GLubyte ** inst, struct var_cache **vc_head,
630 struct arb_program *Program, GLuint * found)
631{
632 GLubyte *i = *inst;
633 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000634 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000635
636 *inst += _mesa_strlen ((char *) i) + 1;
637
638 va = var_cache_find (*vc_head, i);
639
640 if (va) {
641 *found = 1;
642 return va;
643 }
644
645 *found = 0;
646 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000647 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000648
649 var_cache_append (vc_head, va);
650
651 return va;
652}
653
654static char *
655parse_string_without_adding (GLubyte ** inst, struct arb_program *Program)
656{
657 GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000658 (void) Program;
659
Jouk Jansen40322e12004-04-05 08:50:36 +0000660 *inst += _mesa_strlen ((char *) i) + 1;
661
662 return (char *) i;
663}
664
665/**
Brian Paul05908952004-06-08 15:20:23 +0000666 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000667 */
Brian Paul05908952004-06-08 15:20:23 +0000668static GLint
Jouk Jansen40322e12004-04-05 08:50:36 +0000669parse_sign (GLubyte ** inst)
670{
671 /*return *(*inst)++ != '+'; */
672
673 if (**inst == '-') {
674 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000675 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000676 }
677 else if (**inst == '+') {
678 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000679 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000680 }
681
Brian Paul05908952004-06-08 15:20:23 +0000682 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000683}
684
685/**
686 * parses and returns signed integer
687 */
688static GLint
689parse_integer (GLubyte ** inst, struct arb_program *Program)
690{
691 GLint sign;
692 GLint value;
693
694 /* check if *inst points to '+' or '-'
695 * if yes, grab the sign and increment *inst
696 */
697 sign = parse_sign (inst);
698
699 /* now check if *inst points to 0
700 * if yes, increment the *inst and return the default value
701 */
702 if (**inst == 0) {
703 (*inst)++;
704 return 0;
705 }
706
707 /* parse the integer as you normally would do it */
708 value = _mesa_atoi (parse_string_without_adding (inst, Program));
709
710 /* now, after terminating 0 there is a position
711 * to parse it - parse_position()
712 */
713 Program->Position = parse_position (inst);
714
Brian Paul05908952004-06-08 15:20:23 +0000715 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000716}
717
718/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000719 Accumulate this string of digits, and return them as
720 a large integer represented in floating point (for range).
721 If scale is not NULL, also accumulates a power-of-ten
722 integer scale factor that represents the number of digits
723 in the string.
724*/
725static GLdouble
726parse_float_string(GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
727{
728 GLdouble value = 0.0;
729 GLdouble oscale = 1.0;
730
731 if (**inst == 0) { /* this string of digits is empty-- do nothing */
732 (*inst)++;
733 }
734 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000735 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000736 GLubyte digit = *((*inst)++);
737 value = value * 10.0 + (GLint) (digit - '0');
738 oscale *= 10.0;
739 }
740 assert(**inst == 0); /* integer string should end with 0 */
741 (*inst)++; /* skip over terminating 0 */
742 Program->Position = parse_position(inst); /* skip position (from integer) */
743 }
744 if (scale)
745 *scale = oscale;
746 return value;
747}
748
749/**
750 Parse an unsigned floating-point number from this stream of tokenized
751 characters. Example floating-point formats supported:
752 12.34
753 12
754 0.34
755 .34
756 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000757 */
758static GLfloat
759parse_float (GLubyte ** inst, struct arb_program *Program)
760{
Brian Paul1ff8f502005-02-16 15:08:29 +0000761 GLint exponent;
762 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000763
Brian Paul1ff8f502005-02-16 15:08:29 +0000764 whole = parse_float_string(inst, Program, 0);
765 fraction = parse_float_string(inst, Program, &fracScale);
766
767 /* Parse signed exponent */
768 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000769
Brian Paul1ff8f502005-02-16 15:08:29 +0000770 /* Assemble parts of floating-point number: */
771 return (GLfloat) ((whole + fraction / fracScale) *
772 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000773}
774
775
776/**
777 */
778static GLfloat
779parse_signed_float (GLubyte ** inst, struct arb_program *Program)
780{
Brian Paul05908952004-06-08 15:20:23 +0000781 GLint sign = parse_sign (inst);
782 GLfloat value = parse_float (inst, Program);
783 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000784}
785
786/**
787 * This picks out a constant value from the parsed array. The constant vector is r
788 * returned in the *values array, which should be of length 4.
789 *
790 * \param values - The 4 component vector with the constant value in it
791 */
792static GLvoid
793parse_constant (GLubyte ** inst, GLfloat *values, struct arb_program *Program,
794 GLboolean use)
795{
796 GLuint components, i;
797
798
799 switch (*(*inst)++) {
800 case CONSTANT_SCALAR:
801 if (use == GL_TRUE) {
802 values[0] =
803 values[1] =
804 values[2] = values[3] = parse_float (inst, Program);
805 }
806 else {
807 values[0] =
808 values[1] =
809 values[2] = values[3] = parse_signed_float (inst, Program);
810 }
811
812 break;
813 case CONSTANT_VECTOR:
814 values[0] = values[1] = values[2] = 0;
815 values[3] = 1;
816 components = *(*inst)++;
817 for (i = 0; i < components; i++) {
818 values[i] = parse_signed_float (inst, Program);
819 }
820 break;
821 }
822}
823
824/**
825 * \param offset The offset from the address register that we should
826 * address
827 *
828 * \return 0 on sucess, 1 on error
829 */
830static GLuint
831parse_relative_offset (GLcontext *ctx, GLubyte **inst, struct arb_program *Program,
832 GLint *offset)
833{
Brian Paul6a769d92006-04-28 15:42:15 +0000834 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000835 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000836 return 0;
837}
838
839/**
840 * \param color 0 if color type is primary, 1 if color type is secondary
841 * \return 0 on sucess, 1 on error
842 */
843static GLuint
844parse_color_type (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
845 GLint * color)
846{
Brian Paula6c423d2004-08-25 15:59:48 +0000847 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000848 *color = *(*inst)++ != COLOR_PRIMARY;
849 return 0;
850}
851
852/**
853 * Get an integer corresponding to a generic vertex attribute.
854 *
855 * \return 0 on sucess, 1 on error
856 */
857static GLuint
858parse_generic_attrib_num(GLcontext *ctx, GLubyte ** inst,
859 struct arb_program *Program, GLuint *attrib)
860{
Brian Paulbd997cd2004-07-20 21:12:56 +0000861 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000862
Michal Krolbb38cad2006-04-11 11:41:11 +0000863 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000864 {
865 _mesa_set_program_error (ctx, Program->Position,
866 "Invalid generic vertex attribute index");
867 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid generic vertex attribute index");
868
869 return 1;
870 }
871
Brian Paulbd997cd2004-07-20 21:12:56 +0000872 *attrib = (GLuint) i;
873
Jouk Jansen40322e12004-04-05 08:50:36 +0000874 return 0;
875}
876
877
878/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000879 * \param color The index of the color buffer to write into
880 * \return 0 on sucess, 1 on error
881 */
882static GLuint
883parse_output_color_num (GLcontext * ctx, GLubyte ** inst,
884 struct arb_program *Program, GLuint * color)
885{
886 GLint i = parse_integer (inst, Program);
887
888 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
889 _mesa_set_program_error (ctx, Program->Position,
890 "Invalid draw buffer index");
891 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid draw buffer index");
892 return 1;
893 }
894
895 *color = (GLuint) i;
896 return 0;
897}
898
899
900/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000901 * \param coord The texture unit index
902 * \return 0 on sucess, 1 on error
903 */
904static GLuint
905parse_texcoord_num (GLcontext * ctx, GLubyte ** inst,
906 struct arb_program *Program, GLuint * coord)
907{
Brian Paulbd997cd2004-07-20 21:12:56 +0000908 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000909
Brian Paula6c423d2004-08-25 15:59:48 +0000910 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000911 _mesa_set_program_error (ctx, Program->Position,
912 "Invalid texture unit index");
913 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid texture unit index");
914 return 1;
915 }
916
Brian Paulbd997cd2004-07-20 21:12:56 +0000917 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000918 return 0;
919}
920
921/**
922 * \param coord The weight index
923 * \return 0 on sucess, 1 on error
924 */
925static GLuint
926parse_weight_num (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
927 GLint * coord)
928{
929 *coord = parse_integer (inst, Program);
930
931 if ((*coord < 0) || (*coord >= 1)) {
932 _mesa_set_program_error (ctx, Program->Position,
933 "Invalid weight index");
934 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid weight index");
935 return 1;
936 }
937
938 return 0;
939}
940
941/**
942 * \param coord The clip plane index
943 * \return 0 on sucess, 1 on error
944 */
945static GLuint
946parse_clipplane_num (GLcontext * ctx, GLubyte ** inst,
947 struct arb_program *Program, GLint * coord)
948{
949 *coord = parse_integer (inst, Program);
950
951 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
952 _mesa_set_program_error (ctx, Program->Position,
953 "Invalid clip plane index");
954 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid clip plane index");
955 return 1;
956 }
957
958 return 0;
959}
960
961
962/**
963 * \return 0 on front face, 1 on back face
964 */
965static GLuint
966parse_face_type (GLubyte ** inst)
967{
968 switch (*(*inst)++) {
969 case FACE_FRONT:
970 return 0;
971
972 case FACE_BACK:
973 return 1;
974 }
975 return 0;
976}
977
978
979/**
980 * Given a matrix and a modifier token on the binary array, return tokens
981 * that _mesa_fetch_state() [program.c] can understand.
982 *
983 * \param matrix - the matrix we are talking about
984 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
985 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
986 * \return 0 on sucess, 1 on failure
987 */
988static GLuint
989parse_matrix (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
990 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
991{
992 GLubyte mat = *(*inst)++;
993
994 *matrix_idx = 0;
995
996 switch (mat) {
997 case MATRIX_MODELVIEW:
998 *matrix = STATE_MODELVIEW;
999 *matrix_idx = parse_integer (inst, Program);
1000 if (*matrix_idx > 0) {
1001 _mesa_set_program_error (ctx, Program->Position,
1002 "ARB_vertex_blend not supported\n");
1003 _mesa_error (ctx, GL_INVALID_OPERATION,
1004 "ARB_vertex_blend not supported\n");
1005 return 1;
1006 }
1007 break;
1008
1009 case MATRIX_PROJECTION:
1010 *matrix = STATE_PROJECTION;
1011 break;
1012
1013 case MATRIX_MVP:
1014 *matrix = STATE_MVP;
1015 break;
1016
1017 case MATRIX_TEXTURE:
1018 *matrix = STATE_TEXTURE;
1019 *matrix_idx = parse_integer (inst, Program);
1020 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
1021 _mesa_set_program_error (ctx, Program->Position,
1022 "Invalid Texture Unit");
1023 _mesa_error (ctx, GL_INVALID_OPERATION,
1024 "Invalid Texture Unit: %d", *matrix_idx);
1025 return 1;
1026 }
1027 break;
1028
1029 /* This is not currently supported (ARB_matrix_palette) */
1030 case MATRIX_PALETTE:
1031 *matrix_idx = parse_integer (inst, Program);
1032 _mesa_set_program_error (ctx, Program->Position,
1033 "ARB_matrix_palette not supported\n");
1034 _mesa_error (ctx, GL_INVALID_OPERATION,
1035 "ARB_matrix_palette not supported\n");
1036 return 1;
1037 break;
1038
1039 case MATRIX_PROGRAM:
1040 *matrix = STATE_PROGRAM;
1041 *matrix_idx = parse_integer (inst, Program);
1042 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
1043 _mesa_set_program_error (ctx, Program->Position,
1044 "Invalid Program Matrix");
1045 _mesa_error (ctx, GL_INVALID_OPERATION,
1046 "Invalid Program Matrix: %d", *matrix_idx);
1047 return 1;
1048 }
1049 break;
1050 }
1051
1052 switch (*(*inst)++) {
1053 case MATRIX_MODIFIER_IDENTITY:
1054 *matrix_modifier = 0;
1055 break;
1056 case MATRIX_MODIFIER_INVERSE:
1057 *matrix_modifier = STATE_MATRIX_INVERSE;
1058 break;
1059 case MATRIX_MODIFIER_TRANSPOSE:
1060 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1061 break;
1062 case MATRIX_MODIFIER_INVTRANS:
1063 *matrix_modifier = STATE_MATRIX_INVTRANS;
1064 break;
1065 }
1066
1067 return 0;
1068}
1069
1070
1071/**
1072 * This parses a state string (rather, the binary version of it) into
1073 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1074 *
1075 * \param inst - the start in the binary arry to start working from
1076 * \param state_tokens - the storage for the 6-token state description
1077 * \return - 0 on sucess, 1 on error
1078 */
1079static GLuint
1080parse_state_single_item (GLcontext * ctx, GLubyte ** inst,
1081 struct arb_program *Program, GLint * state_tokens)
1082{
1083 switch (*(*inst)++) {
1084 case STATE_MATERIAL_PARSER:
1085 state_tokens[0] = STATE_MATERIAL;
1086 state_tokens[1] = parse_face_type (inst);
1087 switch (*(*inst)++) {
1088 case MATERIAL_AMBIENT:
1089 state_tokens[2] = STATE_AMBIENT;
1090 break;
1091 case MATERIAL_DIFFUSE:
1092 state_tokens[2] = STATE_DIFFUSE;
1093 break;
1094 case MATERIAL_SPECULAR:
1095 state_tokens[2] = STATE_SPECULAR;
1096 break;
1097 case MATERIAL_EMISSION:
1098 state_tokens[2] = STATE_EMISSION;
1099 break;
1100 case MATERIAL_SHININESS:
1101 state_tokens[2] = STATE_SHININESS;
1102 break;
1103 }
1104 break;
1105
1106 case STATE_LIGHT_PARSER:
1107 state_tokens[0] = STATE_LIGHT;
1108 state_tokens[1] = parse_integer (inst, Program);
1109
1110 /* Check the value of state_tokens[1] against the # of lights */
1111 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1112 _mesa_set_program_error (ctx, Program->Position,
1113 "Invalid Light Number");
1114 _mesa_error (ctx, GL_INVALID_OPERATION,
1115 "Invalid Light Number: %d", state_tokens[1]);
1116 return 1;
1117 }
1118
1119 switch (*(*inst)++) {
1120 case LIGHT_AMBIENT:
1121 state_tokens[2] = STATE_AMBIENT;
1122 break;
1123 case LIGHT_DIFFUSE:
1124 state_tokens[2] = STATE_DIFFUSE;
1125 break;
1126 case LIGHT_SPECULAR:
1127 state_tokens[2] = STATE_SPECULAR;
1128 break;
1129 case LIGHT_POSITION:
1130 state_tokens[2] = STATE_POSITION;
1131 break;
1132 case LIGHT_ATTENUATION:
1133 state_tokens[2] = STATE_ATTENUATION;
1134 break;
1135 case LIGHT_HALF:
1136 state_tokens[2] = STATE_HALF;
1137 break;
1138 case LIGHT_SPOT_DIRECTION:
1139 state_tokens[2] = STATE_SPOT_DIRECTION;
1140 break;
1141 }
1142 break;
1143
1144 case STATE_LIGHT_MODEL:
1145 switch (*(*inst)++) {
1146 case LIGHT_MODEL_AMBIENT:
1147 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1148 break;
1149 case LIGHT_MODEL_SCENECOLOR:
1150 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1151 state_tokens[1] = parse_face_type (inst);
1152 break;
1153 }
1154 break;
1155
1156 case STATE_LIGHT_PROD:
1157 state_tokens[0] = STATE_LIGHTPROD;
1158 state_tokens[1] = parse_integer (inst, Program);
1159
1160 /* Check the value of state_tokens[1] against the # of lights */
1161 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1162 _mesa_set_program_error (ctx, Program->Position,
1163 "Invalid Light Number");
1164 _mesa_error (ctx, GL_INVALID_OPERATION,
1165 "Invalid Light Number: %d", state_tokens[1]);
1166 return 1;
1167 }
1168
1169 state_tokens[2] = parse_face_type (inst);
1170 switch (*(*inst)++) {
1171 case LIGHT_PROD_AMBIENT:
1172 state_tokens[3] = STATE_AMBIENT;
1173 break;
1174 case LIGHT_PROD_DIFFUSE:
1175 state_tokens[3] = STATE_DIFFUSE;
1176 break;
1177 case LIGHT_PROD_SPECULAR:
1178 state_tokens[3] = STATE_SPECULAR;
1179 break;
1180 }
1181 break;
1182
1183
1184 case STATE_FOG:
1185 switch (*(*inst)++) {
1186 case FOG_COLOR:
1187 state_tokens[0] = STATE_FOG_COLOR;
1188 break;
1189 case FOG_PARAMS:
1190 state_tokens[0] = STATE_FOG_PARAMS;
1191 break;
1192 }
1193 break;
1194
1195 case STATE_TEX_ENV:
1196 state_tokens[1] = parse_integer (inst, Program);
1197 switch (*(*inst)++) {
1198 case TEX_ENV_COLOR:
1199 state_tokens[0] = STATE_TEXENV_COLOR;
1200 break;
1201 }
1202 break;
1203
1204 case STATE_TEX_GEN:
1205 {
1206 GLuint type, coord;
1207
1208 state_tokens[0] = STATE_TEXGEN;
1209 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1210
1211 if (parse_texcoord_num (ctx, inst, Program, &coord))
1212 return 1;
1213 state_tokens[1] = coord;
1214
1215 /* EYE or OBJECT */
1216 type = *(*inst++);
1217
1218 /* 0 - s, 1 - t, 2 - r, 3 - q */
1219 coord = *(*inst++);
1220
1221 if (type == TEX_GEN_EYE) {
1222 switch (coord) {
1223 case COMPONENT_X:
1224 state_tokens[2] = STATE_TEXGEN_EYE_S;
1225 break;
1226 case COMPONENT_Y:
1227 state_tokens[2] = STATE_TEXGEN_EYE_T;
1228 break;
1229 case COMPONENT_Z:
1230 state_tokens[2] = STATE_TEXGEN_EYE_R;
1231 break;
1232 case COMPONENT_W:
1233 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1234 break;
1235 }
1236 }
1237 else {
1238 switch (coord) {
1239 case COMPONENT_X:
1240 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1241 break;
1242 case COMPONENT_Y:
1243 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1244 break;
1245 case COMPONENT_Z:
1246 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1247 break;
1248 case COMPONENT_W:
1249 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1250 break;
1251 }
1252 }
1253 }
1254 break;
1255
1256 case STATE_DEPTH:
1257 switch (*(*inst)++) {
1258 case DEPTH_RANGE:
1259 state_tokens[0] = STATE_DEPTH_RANGE;
1260 break;
1261 }
1262 break;
1263
1264 case STATE_CLIP_PLANE:
1265 state_tokens[0] = STATE_CLIPPLANE;
1266 state_tokens[1] = parse_integer (inst, Program);
1267 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1268 return 1;
1269 break;
1270
1271 case STATE_POINT:
1272 switch (*(*inst++)) {
1273 case POINT_SIZE:
1274 state_tokens[0] = STATE_POINT_SIZE;
1275 break;
1276
1277 case POINT_ATTENUATION:
1278 state_tokens[0] = STATE_POINT_ATTENUATION;
1279 break;
1280 }
1281 break;
1282
1283 /* XXX: I think this is the correct format for a matrix row */
1284 case STATE_MATRIX_ROWS:
1285 state_tokens[0] = STATE_MATRIX;
1286 if (parse_matrix
1287 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1288 &state_tokens[5]))
1289 return 1;
1290
1291 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1292
1293 if ((**inst) != 0) { /* Either the last row, 0 */
1294 state_tokens[4] = parse_integer (inst, Program);
1295 if (state_tokens[4] < state_tokens[3]) {
1296 _mesa_set_program_error (ctx, Program->Position,
1297 "Second matrix index less than the first");
1298 _mesa_error (ctx, GL_INVALID_OPERATION,
1299 "Second matrix index (%d) less than the first (%d)",
1300 state_tokens[4], state_tokens[3]);
1301 return 1;
1302 }
1303 }
1304 else {
1305 state_tokens[4] = state_tokens[3];
1306 (*inst)++;
1307 }
1308 break;
1309 }
1310
1311 return 0;
1312}
1313
1314/**
1315 * This parses a state string (rather, the binary version of it) into
1316 * a 6-token similar for the state fetching code in program.c
1317 *
1318 * One might ask, why fetch these parameters into just like you fetch
1319 * state when they are already stored in other places?
1320 *
1321 * Because of array offsets -> We can stick env/local parameters in the
1322 * middle of a parameter array and then index someplace into the array
1323 * when we execute.
1324 *
1325 * One optimization might be to only do this for the cases where the
1326 * env/local parameters end up inside of an array, and leave the
1327 * single parameters (or arrays of pure env/local pareameters) in their
1328 * respective register files.
1329 *
1330 * For ENV parameters, the format is:
1331 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1332 * state_tokens[1] = STATE_ENV
1333 * state_tokens[2] = the parameter index
1334 *
1335 * for LOCAL parameters, the format is:
1336 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1337 * state_tokens[1] = STATE_LOCAL
1338 * state_tokens[2] = the parameter index
1339 *
1340 * \param inst - the start in the binary arry to start working from
1341 * \param state_tokens - the storage for the 6-token state description
1342 * \return - 0 on sucess, 1 on failure
1343 */
1344static GLuint
1345parse_program_single_item (GLcontext * ctx, GLubyte ** inst,
1346 struct arb_program *Program, GLint * state_tokens)
1347{
1348 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1349 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1350 else
1351 state_tokens[0] = STATE_VERTEX_PROGRAM;
1352
1353
1354 switch (*(*inst)++) {
1355 case PROGRAM_PARAM_ENV:
1356 state_tokens[1] = STATE_ENV;
1357 state_tokens[2] = parse_integer (inst, Program);
1358
1359 /* Check state_tokens[2] against the number of ENV parameters available */
1360 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001361 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001362 ||
1363 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001364 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001365 _mesa_set_program_error (ctx, Program->Position,
1366 "Invalid Program Env Parameter");
1367 _mesa_error (ctx, GL_INVALID_OPERATION,
1368 "Invalid Program Env Parameter: %d",
1369 state_tokens[2]);
1370 return 1;
1371 }
1372
1373 break;
1374
1375 case PROGRAM_PARAM_LOCAL:
1376 state_tokens[1] = STATE_LOCAL;
1377 state_tokens[2] = parse_integer (inst, Program);
1378
1379 /* Check state_tokens[2] against the number of LOCAL parameters available */
1380 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001381 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001382 ||
1383 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001384 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001385 _mesa_set_program_error (ctx, Program->Position,
1386 "Invalid Program Local Parameter");
1387 _mesa_error (ctx, GL_INVALID_OPERATION,
1388 "Invalid Program Local Parameter: %d",
1389 state_tokens[2]);
1390 return 1;
1391 }
1392 break;
1393 }
1394
1395 return 0;
1396}
1397
1398/**
1399 * For ARB_vertex_program, programs are not allowed to use both an explicit
1400 * vertex attribute and a generic vertex attribute corresponding to the same
1401 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1402 *
1403 * This will walk our var_cache and make sure that nobody does anything fishy.
1404 *
1405 * \return 0 on sucess, 1 on error
1406 */
1407static GLuint
1408generic_attrib_check(struct var_cache *vc_head)
1409{
1410 int a;
1411 struct var_cache *curr;
1412 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1413 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1414
1415 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1416 explicitAttrib[a] = GL_FALSE;
1417 genericAttrib[a] = GL_FALSE;
1418 }
1419
1420 curr = vc_head;
1421 while (curr) {
1422 if (curr->type == vt_attrib) {
1423 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001424 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001425 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001426 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001427 }
1428
1429 curr = curr->next;
1430 }
1431
1432 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1433 if ((explicitAttrib[a]) && (genericAttrib[a]))
1434 return 1;
1435 }
1436
1437 return 0;
1438}
1439
1440/**
1441 * This will handle the binding side of an ATTRIB var declaration
1442 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001443 * \param inputReg returns the input register index, one of the
1444 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001445 * \return returns 0 on sucess, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001446 */
1447static GLuint
Brian Paul18e7c5c2005-10-30 21:46:00 +00001448parse_attrib_binding(GLcontext * ctx, GLubyte ** inst,
1449 struct arb_program *Program,
1450 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001451{
Jouk Jansen40322e12004-04-05 08:50:36 +00001452 GLint err = 0;
1453
1454 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001455
Jouk Jansen40322e12004-04-05 08:50:36 +00001456 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1457 switch (*(*inst)++) {
1458 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001459 {
1460 GLint coord;
1461 err = parse_color_type (ctx, inst, Program, &coord);
1462 *inputReg = FRAG_ATTRIB_COL0 + coord;
1463 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001464 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001465 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001466 {
1467 GLuint texcoord;
1468 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1469 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1470 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001471 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001473 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001475 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001476 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001477 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001478 default:
1479 err = 1;
1480 break;
1481 }
1482 }
1483 else {
1484 switch (*(*inst)++) {
1485 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001486 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001487 break;
1488
1489 case VERTEX_ATTRIB_WEIGHT:
1490 {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001491 const char *msg = "ARB_vertex_blend not supported";
Jouk Jansen40322e12004-04-05 08:50:36 +00001492 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001493 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001494 *inputReg = VERT_ATTRIB_WEIGHT;
1495 _mesa_set_program_error(ctx, Program->Position, msg);
1496 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001497 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001498 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001499
1500 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001501 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001502 break;
1503
1504 case VERTEX_ATTRIB_COLOR:
1505 {
1506 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001507 err = parse_color_type (ctx, inst, Program, &color);
1508 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001509 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001510 }
1511 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001512 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001513 }
1514 }
1515 break;
1516
1517 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001518 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001519 break;
1520
1521 case VERTEX_ATTRIB_TEXCOORD:
1522 {
1523 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001524 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001525 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 }
1527 break;
1528
Jouk Jansen40322e12004-04-05 08:50:36 +00001529 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001530 /* Not supported at this time */
1531 {
1532 const char *msg = "ARB_palette_matrix not supported";
1533 parse_integer (inst, Program);
1534 _mesa_set_program_error (ctx, Program->Position, msg);
1535 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
1536 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001537 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001538
1539 case VERTEX_ATTRIB_GENERIC:
1540 {
1541 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001542
1543 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
1544
1545 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001546 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001547 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1548 * attributes do not alias the conventional vertex
1549 * attributes.
1550 */
1551 if (attrib > 0)
1552 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001553 else
1554 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001555 }
1556 }
1557 break;
1558
1559 default:
1560 err = 1;
1561 break;
1562 }
1563 }
1564
Jouk Jansen40322e12004-04-05 08:50:36 +00001565 if (err) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001566 const char *msg = "Bad attribute binding";
1567 _mesa_set_program_error(ctx, Program->Position, msg);
1568 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001569 }
1570
Brian Paulde997602005-11-12 17:53:14 +00001571 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001572
1573 return err;
1574}
1575
Brian Paul7aebaf32005-10-30 21:23:23 +00001576
Jouk Jansen40322e12004-04-05 08:50:36 +00001577/**
1578 * This translates between a binary token for an output variable type
1579 * and the mesa token for the same thing.
1580 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001581 * \param inst The parsed tokens
1582 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001583 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001584 */
1585static GLuint
Brian Paul7aebaf32005-10-30 21:23:23 +00001586parse_result_binding(GLcontext *ctx, GLubyte **inst,
1587 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001588{
Brian Paul7aebaf32005-10-30 21:23:23 +00001589 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001590
Brian Paul7aebaf32005-10-30 21:23:23 +00001591 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001592 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001593 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001594 GLuint out_color;
1595
Brian Paulbe76b7f2004-10-04 14:40:05 +00001596 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001597 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001598 */
1599 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001600 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001601 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001602 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001603 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001604 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1605 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001606 }
1607 break;
1608
1609 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001610 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001611 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001612 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001613 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001614 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001615 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001616 GLint color_type;
1617 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001618 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1619 if (err)
1620 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001621
Jouk Jansen40322e12004-04-05 08:50:36 +00001622 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001623 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001625 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001627 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001628 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001629 }
1630 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001631 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001632 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001633 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001634 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001635 }
1636 /* primary color */
1637 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001638 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001639 }
1640 }
1641 }
1642 break;
1643
1644 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001645 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001646 break;
1647
1648 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001649 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001650 break;
1651
1652 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001653 {
1654 GLuint unit;
1655 if (parse_texcoord_num (ctx, inst, Program, &unit))
1656 return 1;
1657 *outputReg = VERT_RESULT_TEX0 + unit;
1658 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001659 break;
1660 }
1661
Brian Paulde997602005-11-12 17:53:14 +00001662 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001663
1664 return 0;
1665}
1666
Brian Paul7aebaf32005-10-30 21:23:23 +00001667
Jouk Jansen40322e12004-04-05 08:50:36 +00001668/**
1669 * This handles the declaration of ATTRIB variables
1670 *
1671 * XXX: Still needs
1672 * parse_vert_attrib_binding(), or something like that
1673 *
1674 * \return 0 on sucess, 1 on error
1675 */
1676static GLint
1677parse_attrib (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1678 struct arb_program *Program)
1679{
1680 GLuint found;
1681 char *error_msg;
1682 struct var_cache *attrib_var;
1683
1684 attrib_var = parse_string (inst, vc_head, Program, &found);
1685 Program->Position = parse_position (inst);
1686 if (found) {
1687 error_msg = (char *)
1688 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1689 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1690 attrib_var->name);
1691
1692 _mesa_set_program_error (ctx, Program->Position, error_msg);
1693 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1694
1695 _mesa_free (error_msg);
1696 return 1;
1697 }
1698
1699 attrib_var->type = vt_attrib;
1700
Brian Paul7aebaf32005-10-30 21:23:23 +00001701 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001702 &attrib_var->attrib_is_generic))
1703 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001704
Brian Paul7aebaf32005-10-30 21:23:23 +00001705 if (generic_attrib_check(*vc_head)) {
1706 _mesa_set_program_error(ctx, Program->Position,
1707 "Cannot use both a generic vertex attribute "
1708 "and a specific attribute of the same type");
1709 _mesa_error(ctx, GL_INVALID_OPERATION,
1710 "Cannot use both a generic vertex attribute and a specific "
1711 "attribute of the same type");
1712 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001713 }
1714
1715 Program->Base.NumAttributes++;
1716 return 0;
1717}
1718
1719/**
1720 * \param use -- TRUE if we're called when declaring implicit parameters,
1721 * FALSE if we're declaraing variables. This has to do with
1722 * if we get a signed or unsigned float for scalar constants
1723 */
1724static GLuint
1725parse_param_elements (GLcontext * ctx, GLubyte ** inst,
1726 struct var_cache *param_var,
1727 struct arb_program *Program, GLboolean use)
1728{
1729 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001730 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001731 GLint state_tokens[6];
1732 GLfloat const_values[4];
1733
Jouk Jansen40322e12004-04-05 08:50:36 +00001734 switch (*(*inst)++) {
1735 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001736 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1737 return 1;
1738
1739 /* If we adding STATE_MATRIX that has multiple rows, we need to
1740 * unroll it and call _mesa_add_state_reference() for each row
1741 */
1742 if ((state_tokens[0] == STATE_MATRIX)
1743 && (state_tokens[3] != state_tokens[4])) {
1744 GLint row;
1745 GLint first_row = state_tokens[3];
1746 GLint last_row = state_tokens[4];
1747
1748 for (row = first_row; row <= last_row; row++) {
1749 state_tokens[3] = state_tokens[4] = row;
1750
Brian Paulde997602005-11-12 17:53:14 +00001751 idx = _mesa_add_state_reference(Program->Base.Parameters,
1752 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001753 if (param_var->param_binding_begin == ~0U)
1754 param_var->param_binding_begin = idx;
1755 param_var->param_binding_length++;
1756 Program->Base.NumParameters++;
1757 }
1758 }
1759 else {
Brian Paulde997602005-11-12 17:53:14 +00001760 idx = _mesa_add_state_reference(Program->Base.Parameters,
1761 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001762 if (param_var->param_binding_begin == ~0U)
1763 param_var->param_binding_begin = idx;
1764 param_var->param_binding_length++;
1765 Program->Base.NumParameters++;
1766 }
1767 break;
1768
1769 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001770 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1771 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001772 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001773 if (param_var->param_binding_begin == ~0U)
1774 param_var->param_binding_begin = idx;
1775 param_var->param_binding_length++;
1776 Program->Base.NumParameters++;
1777
1778 /* Check if there is more: 0 -> we're done, else its an integer */
1779 if (**inst) {
1780 GLuint out_of_range, new_idx;
1781 GLuint start_idx = state_tokens[2] + 1;
1782 GLuint end_idx = parse_integer (inst, Program);
1783
1784 out_of_range = 0;
1785 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1786 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001787 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001788 || ((state_tokens[1] == STATE_LOCAL)
1789 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001790 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001791 out_of_range = 1;
1792 }
1793 else {
1794 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001795 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001796 || ((state_tokens[1] == STATE_LOCAL)
1797 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001798 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001799 out_of_range = 1;
1800 }
1801 if (out_of_range) {
1802 _mesa_set_program_error (ctx, Program->Position,
1803 "Invalid Program Parameter");
1804 _mesa_error (ctx, GL_INVALID_OPERATION,
1805 "Invalid Program Parameter: %d", end_idx);
1806 return 1;
1807 }
1808
1809 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1810 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001811 idx = _mesa_add_state_reference(Program->Base.Parameters,
1812 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001813 param_var->param_binding_length++;
1814 Program->Base.NumParameters++;
1815 }
1816 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001817 else {
1818 (*inst)++;
1819 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001820 break;
1821
1822 case PARAM_CONSTANT:
1823 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001824 idx = _mesa_add_named_constant(Program->Base.Parameters,
1825 (char *) param_var->name,
1826 const_values);
Jouk Jansen40322e12004-04-05 08:50:36 +00001827 if (param_var->param_binding_begin == ~0U)
1828 param_var->param_binding_begin = idx;
1829 param_var->param_binding_length++;
1830 Program->Base.NumParameters++;
1831 break;
1832
1833 default:
Brian Paul7aebaf32005-10-30 21:23:23 +00001834 _mesa_set_program_error(ctx, Program->Position,
1835 "Unexpected token in parse_param_elements()");
1836 _mesa_error(ctx, GL_INVALID_OPERATION,
1837 "Unexpected token in parse_param_elements()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001838 return 1;
1839 }
1840
1841 /* Make sure we haven't blown past our parameter limits */
1842 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1843 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001844 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001845 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1846 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001847 ctx->Const.FragmentProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001848 _mesa_set_program_error (ctx, Program->Position,
1849 "Too many parameter variables");
1850 _mesa_error (ctx, GL_INVALID_OPERATION, "Too many parameter variables");
1851 return 1;
1852 }
1853
1854 return err;
1855}
1856
Brian Paul7aebaf32005-10-30 21:23:23 +00001857
Jouk Jansen40322e12004-04-05 08:50:36 +00001858/**
1859 * This picks out PARAM program parameter bindings.
1860 *
1861 * XXX: This needs to be stressed & tested
1862 *
1863 * \return 0 on sucess, 1 on error
1864 */
1865static GLuint
1866parse_param (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1867 struct arb_program *Program)
1868{
Brian Paulbd997cd2004-07-20 21:12:56 +00001869 GLuint found, err;
1870 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001871 struct var_cache *param_var;
1872
1873 err = 0;
1874 param_var = parse_string (inst, vc_head, Program, &found);
1875 Program->Position = parse_position (inst);
1876
1877 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001878 char *error_msg = (char *)
1879 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001880 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1881 param_var->name);
1882
1883 _mesa_set_program_error (ctx, Program->Position, error_msg);
1884 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1885
1886 _mesa_free (error_msg);
1887 return 1;
1888 }
1889
1890 specified_length = parse_integer (inst, Program);
1891
1892 if (specified_length < 0) {
1893 _mesa_set_program_error (ctx, Program->Position,
1894 "Negative parameter array length");
1895 _mesa_error (ctx, GL_INVALID_OPERATION,
1896 "Negative parameter array length: %d", specified_length);
1897 return 1;
1898 }
1899
1900 param_var->type = vt_param;
1901 param_var->param_binding_length = 0;
1902
1903 /* Right now, everything is shoved into the main state register file.
1904 *
1905 * In the future, it would be nice to leave things ENV/LOCAL params
1906 * in their respective register files, if possible
1907 */
1908 param_var->param_binding_type = PROGRAM_STATE_VAR;
1909
1910 /* Remember to:
1911 * * - add each guy to the parameter list
1912 * * - increment the param_var->param_binding_len
1913 * * - store the param_var->param_binding_begin for the first one
1914 * * - compare the actual len to the specified len at the end
1915 */
1916 while (**inst != PARAM_NULL) {
1917 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1918 return 1;
1919 }
1920
1921 /* Test array length here! */
1922 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001923 if (specified_length != (int)param_var->param_binding_length) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001924 const char *msg
1925 = "Declared parameter array length does not match parameter list";
1926 _mesa_set_program_error(ctx, Program->Position, msg);
1927 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001928 }
1929 }
1930
1931 (*inst)++;
1932
1933 return 0;
1934}
1935
1936/**
1937 *
1938 */
1939static GLuint
1940parse_param_use (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1941 struct arb_program *Program, struct var_cache **new_var)
1942{
1943 struct var_cache *param_var;
1944
1945 /* First, insert a dummy entry into the var_cache */
1946 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001947 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001948 param_var->type = vt_param;
1949
1950 param_var->param_binding_length = 0;
1951 /* Don't fill in binding_begin; We use the default value of -1
1952 * to tell if its already initialized, elsewhere.
1953 *
1954 * param_var->param_binding_begin = 0;
1955 */
1956 param_var->param_binding_type = PROGRAM_STATE_VAR;
1957
1958 var_cache_append (vc_head, param_var);
1959
1960 /* Then fill it with juicy parameter goodness */
1961 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1962 return 1;
1963
1964 *new_var = param_var;
1965
1966 return 0;
1967}
1968
1969
1970/**
1971 * This handles the declaration of TEMP variables
1972 *
1973 * \return 0 on sucess, 1 on error
1974 */
1975static GLuint
1976parse_temp (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1977 struct arb_program *Program)
1978{
1979 GLuint found;
1980 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001981
1982 while (**inst != 0) {
1983 temp_var = parse_string (inst, vc_head, Program, &found);
1984 Program->Position = parse_position (inst);
1985 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001986 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001987 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1988 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1989 temp_var->name);
1990
1991 _mesa_set_program_error (ctx, Program->Position, error_msg);
1992 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1993
1994 _mesa_free (error_msg);
1995 return 1;
1996 }
1997
1998 temp_var->type = vt_temp;
1999
2000 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2001 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002002 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002003 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2004 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002005 ctx->Const.VertexProgram.MaxTemps))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002006 _mesa_set_program_error (ctx, Program->Position,
2007 "Too many TEMP variables declared");
2008 _mesa_error (ctx, GL_INVALID_OPERATION,
2009 "Too many TEMP variables declared");
2010 return 1;
2011 }
2012
2013 temp_var->temp_binding = Program->Base.NumTemporaries;
2014 Program->Base.NumTemporaries++;
2015 }
2016 (*inst)++;
2017
2018 return 0;
2019}
2020
2021/**
2022 * This handles variables of the OUTPUT variety
2023 *
2024 * \return 0 on sucess, 1 on error
2025 */
2026static GLuint
2027parse_output (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2028 struct arb_program *Program)
2029{
2030 GLuint found;
2031 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002032 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002033
2034 output_var = parse_string (inst, vc_head, Program, &found);
2035 Program->Position = parse_position (inst);
2036 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002037 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002038 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2039 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2040 output_var->name);
2041
2042 _mesa_set_program_error (ctx, Program->Position, error_msg);
2043 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2044
2045 _mesa_free (error_msg);
2046 return 1;
2047 }
2048
2049 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002050
2051 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2052 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002053}
2054
2055/**
2056 * This handles variables of the ALIAS kind
2057 *
2058 * \return 0 on sucess, 1 on error
2059 */
2060static GLuint
2061parse_alias (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2062 struct arb_program *Program)
2063{
2064 GLuint found;
2065 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002066
2067 temp_var = parse_string (inst, vc_head, Program, &found);
2068 Program->Position = parse_position (inst);
2069
2070 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002071 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002072 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2073 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2074 temp_var->name);
2075
2076 _mesa_set_program_error (ctx, Program->Position, error_msg);
2077 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2078
2079 _mesa_free (error_msg);
2080 return 1;
2081 }
2082
2083 temp_var->type = vt_alias;
2084 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2085 Program->Position = parse_position (inst);
2086
2087 if (!found)
2088 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002089 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002090 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2091 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2092 temp_var->alias_binding->name);
2093
2094 _mesa_set_program_error (ctx, Program->Position, error_msg);
2095 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2096
2097 _mesa_free (error_msg);
2098 return 1;
2099 }
2100
2101 return 0;
2102}
2103
2104/**
2105 * This handles variables of the ADDRESS kind
2106 *
2107 * \return 0 on sucess, 1 on error
2108 */
2109static GLuint
2110parse_address (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2111 struct arb_program *Program)
2112{
2113 GLuint found;
2114 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002115
2116 while (**inst != 0) {
2117 temp_var = parse_string (inst, vc_head, Program, &found);
2118 Program->Position = parse_position (inst);
2119 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002120 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002121 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2122 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2123 temp_var->name);
2124
2125 _mesa_set_program_error (ctx, Program->Position, error_msg);
2126 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2127
2128 _mesa_free (error_msg);
2129 return 1;
2130 }
2131
2132 temp_var->type = vt_address;
2133
2134 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002135 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002136 const char *msg = "Too many ADDRESS variables declared";
2137 _mesa_set_program_error(ctx, Program->Position, msg);
2138
2139 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002140 return 1;
2141 }
2142
2143 temp_var->address_binding = Program->Base.NumAddressRegs;
2144 Program->Base.NumAddressRegs++;
2145 }
2146 (*inst)++;
2147
2148 return 0;
2149}
2150
2151/**
2152 * Parse a program declaration
2153 *
2154 * \return 0 on sucess, 1 on error
2155 */
2156static GLint
2157parse_declaration (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2158 struct arb_program *Program)
2159{
2160 GLint err = 0;
2161
2162 switch (*(*inst)++) {
2163 case ADDRESS:
2164 err = parse_address (ctx, inst, vc_head, Program);
2165 break;
2166
2167 case ALIAS:
2168 err = parse_alias (ctx, inst, vc_head, Program);
2169 break;
2170
2171 case ATTRIB:
2172 err = parse_attrib (ctx, inst, vc_head, Program);
2173 break;
2174
2175 case OUTPUT:
2176 err = parse_output (ctx, inst, vc_head, Program);
2177 break;
2178
2179 case PARAM:
2180 err = parse_param (ctx, inst, vc_head, Program);
2181 break;
2182
2183 case TEMP:
2184 err = parse_temp (ctx, inst, vc_head, Program);
2185 break;
2186 }
2187
2188 return err;
2189}
2190
2191/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002192 * Handle the parsing out of a masked destination register, either for a
2193 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002194 *
2195 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002196 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002197 * position invariant
2198 *
2199 * \param File - The register file we write to
2200 * \param Index - The register index we write to
2201 * \param WriteMask - The mask controlling which components we write (1->write)
2202 *
2203 * \return 0 on sucess, 1 on error
2204 */
2205static GLuint
2206parse_masked_dst_reg (GLcontext * ctx, GLubyte ** inst,
2207 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002208 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002209{
Brian Paul7aebaf32005-10-30 21:23:23 +00002210 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002211 struct var_cache *dst;
2212
2213 /* We either have a result register specified, or a
2214 * variable that may or may not be writable
2215 */
2216 switch (*(*inst)++) {
2217 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002218 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002219 return 1;
2220 *File = PROGRAM_OUTPUT;
2221 break;
2222
2223 case REGISTER_ESTABLISHED_NAME:
2224 dst = parse_string (inst, vc_head, Program, &result);
2225 Program->Position = parse_position (inst);
2226
2227 /* If the name has never been added to our symbol table, we're hosed */
2228 if (!result) {
2229 _mesa_set_program_error (ctx, Program->Position,
2230 "0: Undefined variable");
2231 _mesa_error (ctx, GL_INVALID_OPERATION, "0: Undefined variable: %s",
2232 dst->name);
2233 return 1;
2234 }
2235
2236 switch (dst->type) {
2237 case vt_output:
2238 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002239 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002240 break;
2241
2242 case vt_temp:
2243 *File = PROGRAM_TEMPORARY;
2244 *Index = dst->temp_binding;
2245 break;
2246
2247 /* If the var type is not vt_output or vt_temp, no go */
2248 default:
2249 _mesa_set_program_error (ctx, Program->Position,
2250 "Destination register is read only");
2251 _mesa_error (ctx, GL_INVALID_OPERATION,
2252 "Destination register is read only: %s",
2253 dst->name);
2254 return 1;
2255 }
2256 break;
2257
2258 default:
2259 _mesa_set_program_error (ctx, Program->Position,
2260 "Unexpected opcode in parse_masked_dst_reg()");
2261 _mesa_error (ctx, GL_INVALID_OPERATION,
2262 "Unexpected opcode in parse_masked_dst_reg()");
2263 return 1;
2264 }
2265
2266
2267 /* Position invariance test */
2268 /* This test is done now in syntax portion - when position invariance OPTION
2269 is specified, "result.position" rule is disabled so there is no way
2270 to write the position
2271 */
2272 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2273 (*Index == 0)) {
2274 _mesa_set_program_error (ctx, Program->Position,
2275 "Vertex program specified position invariance and wrote vertex position");
2276 _mesa_error (ctx, GL_INVALID_OPERATION,
2277 "Vertex program specified position invariance and wrote vertex position");
2278 }*/
2279
2280 /* And then the mask.
2281 * w,a -> bit 0
2282 * z,b -> bit 1
2283 * y,g -> bit 2
2284 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002285 *
2286 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002287 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002288 tmp = (GLint) *(*inst)++;
2289 *WriteMask = (((tmp>>3) & 0x1) |
2290 ((tmp>>1) & 0x2) |
2291 ((tmp<<1) & 0x4) |
2292 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002293
2294 return 0;
2295}
2296
2297
2298/**
2299 * Handle the parsing of a address register
2300 *
2301 * \param Index - The register index we write to
2302 *
2303 * \return 0 on sucess, 1 on error
2304 */
2305static GLuint
2306parse_address_reg (GLcontext * ctx, GLubyte ** inst,
2307 struct var_cache **vc_head,
2308 struct arb_program *Program, GLint * Index)
2309{
2310 struct var_cache *dst;
2311 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002312
2313 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002314
2315 dst = parse_string (inst, vc_head, Program, &result);
2316 Program->Position = parse_position (inst);
2317
2318 /* If the name has never been added to our symbol table, we're hosed */
2319 if (!result) {
2320 _mesa_set_program_error (ctx, Program->Position, "Undefined variable");
2321 _mesa_error (ctx, GL_INVALID_OPERATION, "Undefined variable: %s",
2322 dst->name);
2323 return 1;
2324 }
2325
2326 if (dst->type != vt_address) {
2327 _mesa_set_program_error (ctx, Program->Position,
2328 "Variable is not of type ADDRESS");
2329 _mesa_error (ctx, GL_INVALID_OPERATION,
2330 "Variable: %s is not of type ADDRESS", dst->name);
2331 return 1;
2332 }
2333
2334 return 0;
2335}
2336
Brian Paul8e8fa632005-07-01 02:03:33 +00002337#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002338/**
2339 * Handle the parsing out of a masked address register
2340 *
2341 * \param Index - The register index we write to
2342 * \param WriteMask - The mask controlling which components we write (1->write)
2343 *
2344 * \return 0 on sucess, 1 on error
2345 */
2346static GLuint
2347parse_masked_address_reg (GLcontext * ctx, GLubyte ** inst,
2348 struct var_cache **vc_head,
2349 struct arb_program *Program, GLint * Index,
2350 GLboolean * WriteMask)
2351{
2352 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2353 return 1;
2354
2355 /* This should be 0x8 */
2356 (*inst)++;
2357
2358 /* Writemask of .x is implied */
2359 WriteMask[0] = 1;
2360 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2361
2362 return 0;
2363}
Brian Paul8e8fa632005-07-01 02:03:33 +00002364#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002365
2366/**
2367 * Parse out a swizzle mask.
2368 *
Brian Paul32df89e2005-10-29 18:26:43 +00002369 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002370 *
2371 * The len parameter allows us to grab 4 components for a vector
2372 * swizzle, or just 1 component for a scalar src register selection
2373 */
Brian Paul32df89e2005-10-29 18:26:43 +00002374static void
2375parse_swizzle_mask(GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002376{
Brian Paul32df89e2005-10-29 18:26:43 +00002377 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002378
Brian Paul32df89e2005-10-29 18:26:43 +00002379 for (i = 0; i < 4; i++)
2380 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002381
Brian Paul32df89e2005-10-29 18:26:43 +00002382 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002383 switch (*(*inst)++) {
2384 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002385 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002386 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002388 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002389 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002390 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002391 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002392 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002393 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002394 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002395 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002396 default:
2397 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2398 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002399 }
2400 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002401}
2402
Jouk Jansen40322e12004-04-05 08:50:36 +00002403
Brian Paul32df89e2005-10-29 18:26:43 +00002404/**
2405 * Parse an extended swizzle mask which is a sequence of
2406 * four x/y/z/w/0/1 tokens.
2407 * \return swizzle four swizzle values
2408 * \return negateMask four element bitfield
2409 */
2410static void
2411parse_extended_swizzle_mask(GLubyte **inst, GLubyte swizzle[4],
2412 GLubyte *negateMask)
2413{
2414 GLint i;
2415
2416 *negateMask = 0x0;
2417 for (i = 0; i < 4; i++) {
2418 GLubyte swz;
2419 if (parse_sign(inst) == -1)
2420 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002421
2422 swz = *(*inst)++;
2423
2424 switch (swz) {
2425 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002426 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002427 break;
2428 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002429 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002430 break;
2431 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002432 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002433 break;
2434 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002435 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002436 break;
2437 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002438 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002439 break;
2440 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002441 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002442 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002443 default:
2444 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2445 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002446 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002447 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002448}
2449
2450
2451static GLuint
2452parse_src_reg (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002453 struct arb_program *Program,
2454 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002455 GLboolean *IsRelOffset )
2456{
2457 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002458 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002459 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002460
Keith Whitwell7c26b612005-04-21 14:46:57 +00002461 *IsRelOffset = 0;
2462
Jouk Jansen40322e12004-04-05 08:50:36 +00002463 /* And the binding for the src */
2464 switch (*(*inst)++) {
2465 case REGISTER_ATTRIB:
2466 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002467 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002468 return 1;
2469 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002470 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002471
2472 /* We need to insert a dummy variable into the var_cache so we can
2473 * catch generic vertex attrib aliasing errors
2474 */
2475 var_cache_create(&src);
2476 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002477 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002478 src->attrib_binding = binding;
2479 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002480 var_cache_append(vc_head, src);
2481 if (generic_attrib_check(*vc_head)) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002482 const char *msg = "Cannot use both a generic vertex attribute "
2483 "and a specific attribute of the same type";
2484 _mesa_set_program_error (ctx, Program->Position, msg);
2485 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002486 return 1;
2487 }
2488 break;
2489
2490 case REGISTER_PARAM:
2491 switch (**inst) {
2492 case PARAM_ARRAY_ELEMENT:
2493 (*inst)++;
2494 src = parse_string (inst, vc_head, Program, &found);
2495 Program->Position = parse_position (inst);
2496
2497 if (!found) {
2498 _mesa_set_program_error (ctx, Program->Position,
2499 "2: Undefined variable");
2500 _mesa_error (ctx, GL_INVALID_OPERATION,
2501 "2: Undefined variable: %s", src->name);
2502 return 1;
2503 }
2504
Brian Paul95801792005-12-06 15:41:43 +00002505 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002506
2507 switch (*(*inst)++) {
2508 case ARRAY_INDEX_ABSOLUTE:
2509 offset = parse_integer (inst, Program);
2510
2511 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002512 || (offset >= (int)src->param_binding_length)) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002513 _mesa_set_program_error (ctx, Program->Position,
2514 "Index out of range");
2515 _mesa_error (ctx, GL_INVALID_OPERATION,
2516 "Index %d out of range for %s", offset,
2517 src->name);
2518 return 1;
2519 }
2520
2521 *Index = src->param_binding_begin + offset;
2522 break;
2523
2524 case ARRAY_INDEX_RELATIVE:
2525 {
2526 GLint addr_reg_idx, rel_off;
2527
2528 /* First, grab the address regiseter */
2529 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2530 return 1;
2531
2532 /* And the .x */
2533 ((*inst)++);
2534 ((*inst)++);
2535 ((*inst)++);
2536 ((*inst)++);
2537
2538 /* Then the relative offset */
2539 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2540
2541 /* And store it properly */
2542 *Index = src->param_binding_begin + rel_off;
2543 *IsRelOffset = 1;
2544 }
2545 break;
2546 }
2547 break;
2548
2549 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002550 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2551 return 1;
2552
Brian Paul95801792005-12-06 15:41:43 +00002553 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002554 *Index = src->param_binding_begin;
2555 break;
2556 }
2557 break;
2558
2559 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002560 src = parse_string (inst, vc_head, Program, &found);
2561 Program->Position = parse_position (inst);
2562
2563 /* If the name has never been added to our symbol table, we're hosed */
2564 if (!found) {
2565 _mesa_set_program_error (ctx, Program->Position,
2566 "3: Undefined variable");
2567 _mesa_error (ctx, GL_INVALID_OPERATION, "3: Undefined variable: %s",
2568 src->name);
2569 return 1;
2570 }
2571
2572 switch (src->type) {
2573 case vt_attrib:
2574 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002575 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002576 break;
2577
2578 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2579 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002580 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002581 *Index = src->param_binding_begin;
2582 break;
2583
2584 case vt_temp:
2585 *File = PROGRAM_TEMPORARY;
2586 *Index = src->temp_binding;
2587 break;
2588
2589 /* If the var type is vt_output no go */
2590 default:
2591 _mesa_set_program_error (ctx, Program->Position,
2592 "destination register is read only");
2593 _mesa_error (ctx, GL_INVALID_OPERATION,
2594 "destination register is read only: %s",
2595 src->name);
2596 return 1;
2597 }
2598 break;
2599
2600 default:
2601 _mesa_set_program_error (ctx, Program->Position,
2602 "Unknown token in parse_src_reg");
2603 _mesa_error (ctx, GL_INVALID_OPERATION,
2604 "Unknown token in parse_src_reg");
2605 return 1;
2606 }
2607
2608 return 0;
2609}
2610
2611/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002612 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002613 */
2614static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00002615parse_fp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
2616 struct var_cache **vc_head,
2617 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002618 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002619{
Brian Paul7aebaf32005-10-30 21:23:23 +00002620 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002621 GLint index;
2622 GLboolean negate;
2623 GLubyte swizzle[4];
2624 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002625
Jouk Jansen40322e12004-04-05 08:50:36 +00002626 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002627 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002628
2629 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002630 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002631 return 1;
2632
2633 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002634 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002635
Brian Paul32df89e2005-10-29 18:26:43 +00002636 reg->File = file;
2637 reg->Index = index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002638 reg->Abs = 0; /* NV only */
2639 reg->NegateAbs = 0; /* NV only */
Brian Paul32df89e2005-10-29 18:26:43 +00002640 reg->NegateBase = negate;
2641 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002642 return 0;
2643}
2644
Jouk Jansen40322e12004-04-05 08:50:36 +00002645
Keith Whitwell7c26b612005-04-21 14:46:57 +00002646static GLuint
2647parse_fp_dst_reg(GLcontext * ctx, GLubyte ** inst,
2648 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002649 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002650{
Brian Paul7aebaf32005-10-30 21:23:23 +00002651 GLint mask;
2652 GLuint idx;
2653 enum register_file file;
2654
Keith Whitwell7c26b612005-04-21 14:46:57 +00002655 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002656 return 1;
2657
Keith Whitwell7c26b612005-04-21 14:46:57 +00002658 reg->CondMask = 0; /* NV only */
2659 reg->CondSwizzle = 0; /* NV only */
2660 reg->File = file;
2661 reg->Index = idx;
2662 reg->WriteMask = mask;
2663 return 0;
2664}
2665
2666
Brian Paul7aebaf32005-10-30 21:23:23 +00002667/**
2668 * Parse fragment program scalar src register.
2669 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002670static GLuint
2671parse_fp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002672 struct var_cache **vc_head,
2673 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002674 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002675{
Brian Paul7aebaf32005-10-30 21:23:23 +00002676 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002677 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002678 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002679 GLubyte Swizzle[4];
2680 GLboolean IsRelOffset;
2681
2682 /* Grab the sign */
2683 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2684
2685 /* And the src reg */
2686 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2687 return 1;
2688
2689 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002690 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002691
Keith Whitwell7c26b612005-04-21 14:46:57 +00002692 reg->File = File;
2693 reg->Index = Index;
2694 reg->Abs = 0; /* NV only */
2695 reg->NegateAbs = 0; /* NV only */
2696 reg->NegateBase = Negate;
2697 reg->Swizzle = (Swizzle[0] << 0);
2698
Jouk Jansen40322e12004-04-05 08:50:36 +00002699 return 0;
2700}
2701
Keith Whitwell7c26b612005-04-21 14:46:57 +00002702
Jouk Jansen40322e12004-04-05 08:50:36 +00002703/**
2704 * This is a big mother that handles getting opcodes into the instruction
2705 * and handling the src & dst registers for fragment program instructions
2706 */
2707static GLuint
2708parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2709 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002710 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002711{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002712 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002713 GLuint texcoord;
2714 GLubyte instClass, type, code;
2715 GLboolean rel;
2716
Brian Paul7e807512005-11-05 17:10:45 +00002717 _mesa_init_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002718
2719 /* Record the position in the program string for debugging */
2720 fp->StringPos = Program->Position;
2721
2722 /* OP_ALU_INST or OP_TEX_INST */
2723 instClass = *(*inst)++;
2724
2725 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2726 * OP_TEX_{SAMPLE, KIL}
2727 */
2728 type = *(*inst)++;
2729
2730 /* The actual opcode name */
2731 code = *(*inst)++;
2732
2733 /* Increment the correct count */
2734 switch (instClass) {
2735 case OP_ALU_INST:
2736 Program->NumAluInstructions++;
2737 break;
2738 case OP_TEX_INST:
2739 Program->NumTexInstructions++;
2740 break;
2741 }
2742
Jouk Jansen40322e12004-04-05 08:50:36 +00002743 switch (type) {
2744 case OP_ALU_VECTOR:
2745 switch (code) {
2746 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002747 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002748 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002749 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002750 break;
2751
2752 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002753 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002755 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002756 break;
2757
2758 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002759 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002761 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002762 break;
2763
2764 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002765 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002766 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002767 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002768 break;
2769
2770 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002771 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002772 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002773 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002774 break;
2775 }
2776
Keith Whitwell7c26b612005-04-21 14:46:57 +00002777 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002778 return 1;
2779
Keith Whitwell7c26b612005-04-21 14:46:57 +00002780 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002781 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 break;
2783
2784 case OP_ALU_SCALAR:
2785 switch (code) {
2786 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002787 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002788 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002789 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002790 break;
2791
2792 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002793 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002794 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002795 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002796 break;
2797
2798 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002799 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002801 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002802 break;
2803
2804 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002805 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002806 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002807 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002808 break;
2809
2810 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002811 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002812 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002813 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002814 break;
2815
2816 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002817 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002818 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002819 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002820 break;
2821
2822 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002823 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002824 case OP_SCS:
2825
Brian Paul7e807512005-11-05 17:10:45 +00002826 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002827 break;
2828 }
2829
Keith Whitwell7c26b612005-04-21 14:46:57 +00002830 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002832
2833 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002834 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 break;
2836
2837 case OP_ALU_BINSC:
2838 switch (code) {
2839 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002840 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002842 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002843 break;
2844 }
2845
Keith Whitwell7c26b612005-04-21 14:46:57 +00002846 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002848
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002850 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002851 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002852 }
2853 break;
2854
2855
2856 case OP_ALU_BIN:
2857 switch (code) {
2858 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002859 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002860 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002861 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002862 break;
2863
2864 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002865 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002866 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002867 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002868 break;
2869
2870 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002871 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002872 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002873 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002874 break;
2875
2876 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002877 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002878 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002879 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002880 break;
2881
2882 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002883 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002884 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002885 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002886 break;
2887
2888 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002889 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002890 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002891 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002892 break;
2893
2894 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002895 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002896 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002897 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002898 break;
2899
2900 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002901 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002902 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002903 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002904 break;
2905
2906 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002907 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002909 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002910 break;
2911
2912 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002913 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002915 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002916 break;
2917
2918 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002919 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002920 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002921 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002922 break;
2923
2924 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002925 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002926 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002927 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002928 break;
2929 }
2930
Keith Whitwell7c26b612005-04-21 14:46:57 +00002931 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002932 return 1;
2933 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002934 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2935 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 }
2937 break;
2938
2939 case OP_ALU_TRI:
2940 switch (code) {
2941 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002942 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002943 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002944 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002945 break;
2946
2947 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002948 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002949 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002950 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002951 break;
2952
2953 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002954 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002955 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002956 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002957 break;
2958 }
2959
Keith Whitwell7c26b612005-04-21 14:46:57 +00002960 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002961 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002962
Jouk Jansen40322e12004-04-05 08:50:36 +00002963 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002964 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2965 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002966 }
2967 break;
2968
2969 case OP_ALU_SWZ:
2970 switch (code) {
2971 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002972 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002973 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002974 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002975 break;
2976 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002977 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002978 return 1;
2979
Keith Whitwell7c26b612005-04-21 14:46:57 +00002980 {
Brian Paul32df89e2005-10-29 18:26:43 +00002981 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002982 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002983 enum register_file file;
2984 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002985
Brian Paul32df89e2005-10-29 18:26:43 +00002986 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002987 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002988 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2989 fp->SrcReg[0].File = file;
2990 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002991 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002992 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2993 swizzle[1],
2994 swizzle[2],
2995 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002996 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002997 break;
2998
2999 case OP_TEX_SAMPLE:
3000 switch (code) {
3001 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003002 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003003 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00003004 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003005 break;
3006
3007 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003008 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003010 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003011 break;
3012
3013 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003014 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003015 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003016 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003017 break;
3018 }
3019
Keith Whitwell7c26b612005-04-21 14:46:57 +00003020 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003021 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003022
3023 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003024 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003025
3026 /* texImageUnit */
3027 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3028 return 1;
3029 fp->TexSrcUnit = texcoord;
3030
3031 /* texTarget */
3032 switch (*(*inst)++) {
3033 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003034 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003035 break;
3036 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003037 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003038 break;
3039 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003040 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003041 break;
3042 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003043 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003044 break;
3045 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003046 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003047 break;
3048 case TEXTARGET_SHADOW1D:
3049 case TEXTARGET_SHADOW2D:
3050 case TEXTARGET_SHADOWRECT:
3051 /* TODO ARB_fragment_program_shadow code */
3052 break;
3053 }
Brian Paul7e807512005-11-05 17:10:45 +00003054 Program->TexturesUsed[texcoord] |= (1<<fp->TexSrcTarget);
Jouk Jansen40322e12004-04-05 08:50:36 +00003055 break;
3056
3057 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003058 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003059 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003060 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003061 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003062 break;
3063 }
3064
3065 return 0;
3066}
3067
Keith Whitwell7c26b612005-04-21 14:46:57 +00003068static GLuint
3069parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3070 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003071 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003072{
Brian Paul7aebaf32005-10-30 21:23:23 +00003073 GLint mask;
3074 GLuint idx;
3075 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003076
3077 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3078 return 1;
3079
3080 reg->File = file;
3081 reg->Index = idx;
3082 reg->WriteMask = mask;
3083 return 0;
3084}
3085
3086/**
3087 * Handle the parsing out of a masked address register
3088 *
3089 * \param Index - The register index we write to
3090 * \param WriteMask - The mask controlling which components we write (1->write)
3091 *
3092 * \return 0 on sucess, 1 on error
3093 */
3094static GLuint
3095parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3096 struct var_cache **vc_head,
3097 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003098 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003099{
3100 GLint idx;
3101
3102 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3103 return 1;
3104
3105 /* This should be 0x8 */
3106 (*inst)++;
3107
3108 reg->File = PROGRAM_ADDRESS;
3109 reg->Index = idx;
3110
3111 /* Writemask of .x is implied */
3112 reg->WriteMask = 0x1;
3113 return 0;
3114}
3115
3116/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003117 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003118 */
3119static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003120parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3121 struct var_cache **vc_head,
3122 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003123 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003124{
Brian Paul7aebaf32005-10-30 21:23:23 +00003125 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003126 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003127 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003128 GLubyte swizzle[4];
3129 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003130
3131 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003132 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003133
3134 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003135 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003136 return 1;
3137
3138 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003139 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003140
Brian Paul32df89e2005-10-29 18:26:43 +00003141 reg->File = file;
3142 reg->Index = index;
3143 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3144 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003145 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003146 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003147 return 0;
3148}
3149
3150
3151static GLuint
3152parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003153 struct var_cache **vc_head,
3154 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003155 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003156{
Brian Paul7aebaf32005-10-30 21:23:23 +00003157 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003158 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003159 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003160 GLubyte Swizzle[4];
3161 GLboolean IsRelOffset;
3162
3163 /* Grab the sign */
3164 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3165
3166 /* And the src reg */
3167 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3168 return 1;
3169
3170 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003171 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003172
3173 reg->File = File;
3174 reg->Index = Index;
3175 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003176 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003177 reg->RelAddr = IsRelOffset;
3178 return 0;
3179}
3180
3181
Jouk Jansen40322e12004-04-05 08:50:36 +00003182/**
3183 * This is a big mother that handles getting opcodes into the instruction
3184 * and handling the src & dst registers for vertex program instructions
3185 */
3186static GLuint
3187parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3188 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003189 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003190{
3191 GLint a;
3192 GLubyte type, code;
3193
3194 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3195 type = *(*inst)++;
3196
3197 /* The actual opcode name */
3198 code = *(*inst)++;
3199
Brian Paul7e807512005-11-05 17:10:45 +00003200 _mesa_init_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003201 /* Record the position in the program string for debugging */
3202 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003203
3204 switch (type) {
3205 /* XXX: */
3206 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003207 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003208
3209 /* Remember to set SrcReg.RelAddr; */
3210
3211 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003212 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003213 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003214
Jouk Jansen40322e12004-04-05 08:50:36 +00003215 vp->DstReg.File = PROGRAM_ADDRESS;
3216
3217 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003218 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003219 return 1;
3220
3221 break;
3222
3223 case OP_ALU_VECTOR:
3224 switch (code) {
3225 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003226 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003227 break;
3228 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003229 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003230 break;
3231 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003232 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003233 break;
3234 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003235 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003236 break;
3237 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003238 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003239 break;
3240 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003241
3242 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003243 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003244
3245 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003246 return 1;
3247 break;
3248
3249 case OP_ALU_SCALAR:
3250 switch (code) {
3251 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003252 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003253 break;
3254 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003255 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003256 break;
3257 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003258 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003259 break;
3260 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003261 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003262 break;
3263 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003264 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003265 break;
3266 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003267 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003268 break;
3269 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003270 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003271 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003272
3273 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003274 return 1;
3275 break;
3276
3277 case OP_ALU_BINSC:
3278 switch (code) {
3279 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003280 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003281 break;
3282 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003283 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003284 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003285
Jouk Jansen40322e12004-04-05 08:50:36 +00003286 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003287 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 return 1;
3289 }
3290 break;
3291
3292 case OP_ALU_BIN:
3293 switch (code) {
3294 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003295 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003296 break;
3297 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003298 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003299 break;
3300 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003301 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003302 break;
3303 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003304 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003305 break;
3306 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003307 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003308 break;
3309 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003310 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003311 break;
3312 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003313 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003314 break;
3315 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003316 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003317 break;
3318 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003319 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003320 break;
3321 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003322 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003323 break;
3324 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003325 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003326 break;
3327 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003328 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003329 break;
3330 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003331 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003332 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003333
Jouk Jansen40322e12004-04-05 08:50:36 +00003334 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003335 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003336 return 1;
3337 }
3338 break;
3339
3340 case OP_ALU_TRI:
3341 switch (code) {
3342 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003343 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003344 break;
3345 }
3346
Keith Whitwell7c26b612005-04-21 14:46:57 +00003347 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003348 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003349
Jouk Jansen40322e12004-04-05 08:50:36 +00003350 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003351 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003352 return 1;
3353 }
3354 break;
3355
3356 case OP_ALU_SWZ:
3357 switch (code) {
3358 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003359 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003360 break;
3361 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003362 {
Brian Paul32df89e2005-10-29 18:26:43 +00003363 GLubyte swizzle[4];
3364 GLubyte negateMask;
3365 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003366 enum register_file file;
3367 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003368
Keith Whitwell7c26b612005-04-21 14:46:57 +00003369 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3370 return 1;
3371
Brian Paul32df89e2005-10-29 18:26:43 +00003372 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003373 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003374 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3375 vp->SrcReg[0].File = file;
3376 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003377 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003378 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3379 swizzle[1],
3380 swizzle[2],
3381 swizzle[3]);
3382 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003383 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003384 break;
3385 }
3386 return 0;
3387}
3388
3389#if DEBUG_PARSING
3390
3391static GLvoid
3392print_state_token (GLint token)
3393{
3394 switch (token) {
3395 case STATE_MATERIAL:
3396 fprintf (stderr, "STATE_MATERIAL ");
3397 break;
3398 case STATE_LIGHT:
3399 fprintf (stderr, "STATE_LIGHT ");
3400 break;
3401
3402 case STATE_LIGHTMODEL_AMBIENT:
3403 fprintf (stderr, "STATE_AMBIENT ");
3404 break;
3405
3406 case STATE_LIGHTMODEL_SCENECOLOR:
3407 fprintf (stderr, "STATE_SCENECOLOR ");
3408 break;
3409
3410 case STATE_LIGHTPROD:
3411 fprintf (stderr, "STATE_LIGHTPROD ");
3412 break;
3413
3414 case STATE_TEXGEN:
3415 fprintf (stderr, "STATE_TEXGEN ");
3416 break;
3417
3418 case STATE_FOG_COLOR:
3419 fprintf (stderr, "STATE_FOG_COLOR ");
3420 break;
3421
3422 case STATE_FOG_PARAMS:
3423 fprintf (stderr, "STATE_FOG_PARAMS ");
3424 break;
3425
3426 case STATE_CLIPPLANE:
3427 fprintf (stderr, "STATE_CLIPPLANE ");
3428 break;
3429
3430 case STATE_POINT_SIZE:
3431 fprintf (stderr, "STATE_POINT_SIZE ");
3432 break;
3433
3434 case STATE_POINT_ATTENUATION:
3435 fprintf (stderr, "STATE_ATTENUATION ");
3436 break;
3437
3438 case STATE_MATRIX:
3439 fprintf (stderr, "STATE_MATRIX ");
3440 break;
3441
3442 case STATE_MODELVIEW:
3443 fprintf (stderr, "STATE_MODELVIEW ");
3444 break;
3445
3446 case STATE_PROJECTION:
3447 fprintf (stderr, "STATE_PROJECTION ");
3448 break;
3449
3450 case STATE_MVP:
3451 fprintf (stderr, "STATE_MVP ");
3452 break;
3453
3454 case STATE_TEXTURE:
3455 fprintf (stderr, "STATE_TEXTURE ");
3456 break;
3457
3458 case STATE_PROGRAM:
3459 fprintf (stderr, "STATE_PROGRAM ");
3460 break;
3461
3462 case STATE_MATRIX_INVERSE:
3463 fprintf (stderr, "STATE_INVERSE ");
3464 break;
3465
3466 case STATE_MATRIX_TRANSPOSE:
3467 fprintf (stderr, "STATE_TRANSPOSE ");
3468 break;
3469
3470 case STATE_MATRIX_INVTRANS:
3471 fprintf (stderr, "STATE_INVTRANS ");
3472 break;
3473
3474 case STATE_AMBIENT:
3475 fprintf (stderr, "STATE_AMBIENT ");
3476 break;
3477
3478 case STATE_DIFFUSE:
3479 fprintf (stderr, "STATE_DIFFUSE ");
3480 break;
3481
3482 case STATE_SPECULAR:
3483 fprintf (stderr, "STATE_SPECULAR ");
3484 break;
3485
3486 case STATE_EMISSION:
3487 fprintf (stderr, "STATE_EMISSION ");
3488 break;
3489
3490 case STATE_SHININESS:
3491 fprintf (stderr, "STATE_SHININESS ");
3492 break;
3493
3494 case STATE_HALF:
3495 fprintf (stderr, "STATE_HALF ");
3496 break;
3497
3498 case STATE_POSITION:
3499 fprintf (stderr, "STATE_POSITION ");
3500 break;
3501
3502 case STATE_ATTENUATION:
3503 fprintf (stderr, "STATE_ATTENUATION ");
3504 break;
3505
3506 case STATE_SPOT_DIRECTION:
3507 fprintf (stderr, "STATE_DIRECTION ");
3508 break;
3509
3510 case STATE_TEXGEN_EYE_S:
3511 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3512 break;
3513
3514 case STATE_TEXGEN_EYE_T:
3515 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3516 break;
3517
3518 case STATE_TEXGEN_EYE_R:
3519 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3520 break;
3521
3522 case STATE_TEXGEN_EYE_Q:
3523 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3524 break;
3525
3526 case STATE_TEXGEN_OBJECT_S:
3527 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3528 break;
3529
3530 case STATE_TEXGEN_OBJECT_T:
3531 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3532 break;
3533
3534 case STATE_TEXGEN_OBJECT_R:
3535 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3536 break;
3537
3538 case STATE_TEXGEN_OBJECT_Q:
3539 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3540 break;
3541
3542 case STATE_TEXENV_COLOR:
3543 fprintf (stderr, "STATE_TEXENV_COLOR ");
3544 break;
3545
3546 case STATE_DEPTH_RANGE:
3547 fprintf (stderr, "STATE_DEPTH_RANGE ");
3548 break;
3549
3550 case STATE_VERTEX_PROGRAM:
3551 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3552 break;
3553
3554 case STATE_FRAGMENT_PROGRAM:
3555 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3556 break;
3557
3558 case STATE_ENV:
3559 fprintf (stderr, "STATE_ENV ");
3560 break;
3561
3562 case STATE_LOCAL:
3563 fprintf (stderr, "STATE_LOCAL ");
3564 break;
3565
3566 }
3567 fprintf (stderr, "[%d] ", token);
3568}
3569
3570
3571static GLvoid
3572debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3573 struct arb_program *Program)
3574{
3575 struct var_cache *vc;
3576 GLint a, b;
3577
3578 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3579
3580 /* First of all, print out the contents of the var_cache */
3581 vc = vc_head;
3582 while (vc) {
3583 fprintf (stderr, "[%x]\n", vc);
3584 switch (vc->type) {
3585 case vt_none:
3586 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3587 break;
3588 case vt_attrib:
3589 fprintf (stderr, "ATTRIB %s\n", vc->name);
3590 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3591 break;
3592 case vt_param:
3593 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3594 vc->param_binding_begin, vc->param_binding_length);
3595 b = vc->param_binding_begin;
3596 for (a = 0; a < vc->param_binding_length; a++) {
3597 fprintf (stderr, "%s\n",
3598 Program->Parameters->Parameters[a + b].Name);
3599 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3600 print_state_token (Program->Parameters->Parameters[a + b].
3601 StateIndexes[0]);
3602 print_state_token (Program->Parameters->Parameters[a + b].
3603 StateIndexes[1]);
3604 print_state_token (Program->Parameters->Parameters[a + b].
3605 StateIndexes[2]);
3606 print_state_token (Program->Parameters->Parameters[a + b].
3607 StateIndexes[3]);
3608 print_state_token (Program->Parameters->Parameters[a + b].
3609 StateIndexes[4]);
3610 print_state_token (Program->Parameters->Parameters[a + b].
3611 StateIndexes[5]);
3612 }
3613 else
3614 fprintf (stderr, "%f %f %f %f\n",
3615 Program->Parameters->Parameters[a + b].Values[0],
3616 Program->Parameters->Parameters[a + b].Values[1],
3617 Program->Parameters->Parameters[a + b].Values[2],
3618 Program->Parameters->Parameters[a + b].Values[3]);
3619 }
3620 break;
3621 case vt_temp:
3622 fprintf (stderr, "TEMP %s\n", vc->name);
3623 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3624 break;
3625 case vt_output:
3626 fprintf (stderr, "OUTPUT %s\n", vc->name);
3627 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3628 break;
3629 case vt_alias:
3630 fprintf (stderr, "ALIAS %s\n", vc->name);
3631 fprintf (stderr, " binding: 0x%x (%s)\n",
3632 vc->alias_binding, vc->alias_binding->name);
3633 break;
3634 }
3635 vc = vc->next;
3636 }
3637}
3638
Brian Paul72030e02005-11-03 03:30:34 +00003639#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003640
3641
3642/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003643 * The main loop for parsing a fragment or vertex program
3644 *
Brian Paul72030e02005-11-03 03:30:34 +00003645 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003646 */
Brian Paul72030e02005-11-03 03:30:34 +00003647static GLint
Brian Paul8c41a142005-11-19 15:36:28 +00003648parse_instructions(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
Brian Paul45703642005-10-29 15:52:31 +00003649 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003650{
Brian Paul72030e02005-11-03 03:30:34 +00003651 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3652 ? ctx->Const.FragmentProgram.MaxInstructions
3653 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003654 GLint err = 0;
3655
Brian Paul72030e02005-11-03 03:30:34 +00003656 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3657
Jouk Jansen40322e12004-04-05 08:50:36 +00003658 Program->MajorVersion = (GLuint) * inst++;
3659 Program->MinorVersion = (GLuint) * inst++;
3660
3661 while (*inst != END) {
3662 switch (*inst++) {
3663
3664 case OPTION:
3665 switch (*inst++) {
3666 case ARB_PRECISION_HINT_FASTEST:
3667 Program->PrecisionOption = GL_FASTEST;
3668 break;
3669
3670 case ARB_PRECISION_HINT_NICEST:
3671 Program->PrecisionOption = GL_NICEST;
3672 break;
3673
3674 case ARB_FOG_EXP:
3675 Program->FogOption = GL_EXP;
3676 break;
3677
3678 case ARB_FOG_EXP2:
3679 Program->FogOption = GL_EXP2;
3680 break;
3681
3682 case ARB_FOG_LINEAR:
3683 Program->FogOption = GL_LINEAR;
3684 break;
3685
3686 case ARB_POSITION_INVARIANT:
3687 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003688 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003689 break;
3690
3691 case ARB_FRAGMENT_PROGRAM_SHADOW:
3692 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3693 /* TODO ARB_fragment_program_shadow code */
3694 }
3695 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003696
3697 case ARB_DRAW_BUFFERS:
3698 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3699 /* do nothing for now */
3700 }
3701 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003702 }
3703 break;
3704
3705 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003706 /* check length */
3707 if (Program->Base.NumInstructions + 1 >= maxInst) {
3708 const char *msg = "Max instruction count exceeded";
3709 _mesa_set_program_error(ctx, Program->Position, msg);
3710 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3711 return 1;
3712 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003713 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003714 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003715 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003716 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003717 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 }
3719 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003720 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003721 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003722 }
3723
Brian Paul72030e02005-11-03 03:30:34 +00003724 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003725 Program->Base.NumInstructions++;
3726 break;
3727
3728 case DECLARATION:
3729 err = parse_declaration (ctx, &inst, vc_head, Program);
3730 break;
3731
3732 default:
3733 break;
3734 }
3735
3736 if (err)
3737 break;
3738 }
3739
3740 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003741 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003742 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul8c41a142005-11-19 15:36:28 +00003743 _mesa_init_instruction(Program->Base.Instructions + numInst);
3744 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003745 /* YYY Wrong Position in program, whatever, at least not random -> crash
3746 Program->Position = parse_position (&inst);
3747 */
Brian Paul8c41a142005-11-19 15:36:28 +00003748 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003749 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003750 Program->Base.NumInstructions++;
3751
Brian Paul05051032005-11-01 04:36:33 +00003752 /*
3753 * Initialize native counts to logical counts. The device driver may
3754 * change them if program is translated into a hardware program.
3755 */
3756 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3757 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3758 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3759 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3760 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003761
Jouk Jansen40322e12004-04-05 08:50:36 +00003762 return err;
3763}
3764
Brian Paul7aebaf32005-10-30 21:23:23 +00003765
Jouk Jansen40322e12004-04-05 08:50:36 +00003766/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003767__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003768#include "grammar_syn.h"
3769;
3770
Brian Paul7aebaf32005-10-30 21:23:23 +00003771
Brian Paul8c41a142005-11-19 15:36:28 +00003772/**
3773 * Set a grammar parameter.
3774 * \param name the grammar parameter
3775 * \param value the new parameter value
3776 * \return 0 if OK, 1 if error
3777 */
3778static int
3779set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003780{
3781 char error_msg[300];
3782 GLint error_pos;
3783
Brian Paul8c41a142005-11-19 15:36:28 +00003784 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003785 return 0;
3786
3787 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3788 _mesa_set_program_error (ctx, error_pos, error_msg);
3789 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3790 return 1;
3791}
3792
Brian Paul8c41a142005-11-19 15:36:28 +00003793
3794/**
3795 * Enable support for the given language option in the parser.
3796 * \return 1 if OK, 0 if error
3797 */
3798static int
3799enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003800{
Brian Paul8c41a142005-11-19 15:36:28 +00003801 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003802}
3803
Brian Paul8c41a142005-11-19 15:36:28 +00003804
3805/**
3806 * Enable parser extensions based on which OpenGL extensions are supported
3807 * by this rendering context.
3808 *
3809 * \return GL_TRUE if OK, GL_FALSE if error.
3810 */
3811static GLboolean
3812enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003813{
Brian Paul8c41a142005-11-19 15:36:28 +00003814#if 0
3815 /* These are not supported at this time */
3816 if ((ctx->Extensions.ARB_vertex_blend ||
3817 ctx->Extensions.EXT_vertex_weighting)
3818 && !enable_ext(ctx, id, "point_parameters"))
3819 return GL_FALSE;
3820 if (ctx->Extensions.ARB_matrix_palette
3821 && !enable_ext(ctx, id, "matrix_palette"))
3822 return GL_FALSE;
3823 if (ctx->Extensions.ARB_fragment_program_shadow
3824 && !enable_ext(ctx, id, "fragment_program_shadow"))
3825 return GL_FALSE;
3826#endif
3827 if (ctx->Extensions.EXT_point_parameters
3828 && !enable_ext(ctx, id, "point_parameters"))
3829 return GL_FALSE;
3830 if (ctx->Extensions.EXT_secondary_color
3831 && !enable_ext(ctx, id, "secondary_color"))
3832 return GL_FALSE;
3833 if (ctx->Extensions.EXT_fog_coord
3834 && !enable_ext(ctx, id, "fog_coord"))
3835 return GL_FALSE;
3836 if (ctx->Extensions.NV_texture_rectangle
3837 && !enable_ext(ctx, id, "texture_rectangle"))
3838 return GL_FALSE;
3839 if (ctx->Extensions.ARB_draw_buffers
3840 && !enable_ext(ctx, id, "draw_buffers"))
3841 return GL_FALSE;
3842
3843 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003844}
3845
Brian Paul8c41a142005-11-19 15:36:28 +00003846
Jouk Jansen40322e12004-04-05 08:50:36 +00003847/**
3848 * This kicks everything off.
3849 *
3850 * \param ctx - The GL Context
3851 * \param str - The program string
3852 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003853 * \param program - The arb_program struct to return all the parsed info in
3854 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003855 */
Brian Paul8c41a142005-11-19 15:36:28 +00003856static GLboolean
3857_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3858 const GLubyte *str, GLsizei len,
3859 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003860{
3861 GLint a, err, error_pos;
3862 char error_msg[300];
3863 GLuint parsed_len;
3864 struct var_cache *vc_head;
3865 grammar arbprogram_syn_id;
3866 GLubyte *parsed, *inst;
3867 GLubyte *strz = NULL;
3868 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3869
Brian Paul8c41a142005-11-19 15:36:28 +00003870 /* set the program target before parsing */
3871 program->Base.Target = target;
3872
Brian Paul7f76b8f2004-09-10 01:05:39 +00003873 /* Reset error state */
3874 _mesa_set_program_error(ctx, -1, NULL);
3875
Brian Paul8c41a142005-11-19 15:36:28 +00003876 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003877 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003878 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003879 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003880 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003881
3882 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3883 if (grammar_syn_id == 0) {
3884 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003885 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003886 _mesa_set_program_error (ctx, error_pos, error_msg);
3887 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003888 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003889 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003890 }
3891
Brian Paul8c41a142005-11-19 15:36:28 +00003892 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3893 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003894
Brian Paul49a80ca2006-04-28 15:40:11 +00003895 /* 'parsed' is unused here */
3896 _mesa_free (parsed);
3897 parsed = NULL;
3898
Brian Paul45703642005-10-29 15:52:31 +00003899 /* NOTE: we can't destroy grammar_syn_id right here because
3900 * grammar_destroy() can reset the last error
3901 */
Brian Paul8c41a142005-11-19 15:36:28 +00003902 if (err) {
3903 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003904 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3905 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003906 _mesa_error (ctx, GL_INVALID_OPERATION,
3907 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003908 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003909 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003910 }
3911
3912 grammar_destroy (grammar_syn_id);
3913
3914 arbprogram_syn_is_ok = 1;
3915 }
3916
3917 /* create the grammar object */
3918 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3919 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003920 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003921 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3922 _mesa_set_program_error (ctx, error_pos, error_msg);
3923 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003924 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003925 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003926 }
3927
3928 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003929 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003930 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
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
Brian Paul8c41a142005-11-19 15:36:28 +00003935 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3936 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003937 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003938 }
3939
3940 /* check for NULL character occurences */
3941 {
Brian Paul8c41a142005-11-19 15:36:28 +00003942 GLint i;
3943 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003944 if (str[i] == '\0') {
3945 _mesa_set_program_error (ctx, i, "invalid character");
Brian Paul8c41a142005-11-19 15:36:28 +00003946 _mesa_error (ctx, GL_INVALID_OPERATION,
3947 "glProgramStringARB(illegal character)");
Jouk Jansen40322e12004-04-05 08:50:36 +00003948 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003949 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003950 }
Brian Paul8c41a142005-11-19 15:36:28 +00003951 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003952 }
3953
3954 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003955 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003956 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003957 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3958 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003959 return GL_FALSE;
3960 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003961 _mesa_memcpy (strz, str, len);
3962 strz[len] = '\0';
3963
Michal Krolb80bc052004-10-21 14:09:54 +00003964 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003965 err = !grammar_fast_check(arbprogram_syn_id, strz,
3966 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003967
3968 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003969 if (err) {
3970 _mesa_free(strz);
Brian Paul49a80ca2006-04-28 15:40:11 +00003971 _mesa_free(parsed);
Jouk Jansen40322e12004-04-05 08:50:36 +00003972 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3973 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003974 _mesa_error (ctx, GL_INVALID_OPERATION,
3975 "glProgramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003976
3977 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003978#if DEBUG_PARSING
3979 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003980 int line, col;
3981 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003982 fprintf(stderr, "program: %s\n", (char *) strz);
3983 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
3984 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003985 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3986 } while (0)
3987#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003988
Jouk Jansen40322e12004-04-05 08:50:36 +00003989 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003990 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003991 }
3992
Jouk Jansen40322e12004-04-05 08:50:36 +00003993 grammar_destroy (arbprogram_syn_id);
3994
Brian Paul8c41a142005-11-19 15:36:28 +00003995 /*
3996 * Program string is syntactically correct at this point
3997 * Parse the tokenized version of the program now, generating
3998 * vertex/fragment program instructions.
3999 */
4000
Jouk Jansen40322e12004-04-05 08:50:36 +00004001 /* Initialize the arb_program struct */
4002 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00004003 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00004004 program->Base.NumInstructions =
4005 program->Base.NumTemporaries =
4006 program->Base.NumParameters =
4007 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00004008 program->Base.Parameters = _mesa_new_parameter_list ();
4009 program->Base.InputsRead = 0x0;
4010 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004011 program->Position = 0;
4012 program->MajorVersion = program->MinorVersion = 0;
4013 program->PrecisionOption = GL_DONT_CARE;
4014 program->FogOption = GL_NONE;
4015 program->HintPositionInvariant = GL_FALSE;
4016 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00004017 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004018 program->NumAluInstructions =
4019 program->NumTexInstructions =
4020 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00004021 program->UsesKill = 0;
4022
Jouk Jansen40322e12004-04-05 08:50:36 +00004023 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00004024 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004025
4026 /* Start examining the tokens in the array */
4027 inst = parsed;
4028
4029 /* Check the grammer rev */
4030 if (*inst++ != REVISION) {
4031 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00004032 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul45cd2f92005-11-03 02:25:10 +00004033 "glProgramStringARB(Grammar version mismatch)");
Brian Paul45703642005-10-29 15:52:31 +00004034 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004035 }
4036 else {
Michal Krolb80bc052004-10-21 14:09:54 +00004037 /* ignore program target */
4038 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00004039 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00004040 }
4041
4042 /*debug_variables(ctx, vc_head, program); */
4043
4044 /* We're done with the parsed binary array */
4045 var_cache_destroy (&vc_head);
4046
4047 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00004048
Brian Paul8c41a142005-11-19 15:36:28 +00004049 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4050 * to size [ap.Base.NumInstructions].
4051 */
Brian Paula7543902006-08-24 21:58:32 +00004052 program->Base.Instructions
4053 = _mesa_realloc_instructions(program->Base.Instructions,
4054 MAX_INSTRUCTIONS,
4055 program->Base.NumInstructions);
4056
Brian Paul45703642005-10-29 15:52:31 +00004057 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004058}
Brian Paul8c41a142005-11-19 15:36:28 +00004059
4060
4061
4062void
4063_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4064 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004065 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004066{
4067 struct arb_program ap;
4068 GLuint i;
4069
4070 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004071 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004072 /* Error in the program. Just return. */
4073 return;
4074 }
4075
4076 /* Copy the relevant contents of the arb_program struct into the
4077 * fragment_program struct.
4078 */
4079 program->Base.String = ap.Base.String;
4080 program->Base.NumInstructions = ap.Base.NumInstructions;
4081 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4082 program->Base.NumParameters = ap.Base.NumParameters;
4083 program->Base.NumAttributes = ap.Base.NumAttributes;
4084 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004085 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4086 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4087 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4088 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4089 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004090 program->NumAluInstructions = ap.NumAluInstructions;
4091 program->NumTexInstructions = ap.NumTexInstructions;
4092 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004093 program->NumNativeAluInstructions = ap.NumAluInstructions;
4094 program->NumNativeTexInstructions = ap.NumTexInstructions;
4095 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004096 program->Base.InputsRead = ap.Base.InputsRead;
4097 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4098 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4099 program->TexturesUsed[i] = ap.TexturesUsed[i];
4100 program->FogOption = ap.FogOption;
4101
4102 if (program->Base.Instructions)
4103 _mesa_free(program->Base.Instructions);
4104 program->Base.Instructions = ap.Base.Instructions;
4105
4106 if (program->Base.Parameters)
4107 _mesa_free_parameter_list(program->Base.Parameters);
4108 program->Base.Parameters = ap.Base.Parameters;
4109
4110#if DEBUG_FP
4111 _mesa_print_program(&program.Base);
4112#endif
4113}
4114
4115
4116
4117/**
4118 * Parse the vertex program string. If success, update the given
4119 * vertex_program object with the new program. Else, leave the vertex_program
4120 * object unchanged.
4121 */
4122void
4123_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4124 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004125 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004126{
4127 struct arb_program ap;
4128
4129 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4130
Brian Paul95801792005-12-06 15:41:43 +00004131 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004132 /* Error in the program. Just return. */
4133 return;
4134 }
4135
4136 /* Copy the relevant contents of the arb_program struct into the
4137 * vertex_program struct.
4138 */
4139 program->Base.String = ap.Base.String;
4140 program->Base.NumInstructions = ap.Base.NumInstructions;
4141 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4142 program->Base.NumParameters = ap.Base.NumParameters;
4143 program->Base.NumAttributes = ap.Base.NumAttributes;
4144 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004145 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4146 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4147 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4148 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4149 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004150 program->Base.InputsRead = ap.Base.InputsRead;
4151 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4152 program->IsPositionInvariant = ap.HintPositionInvariant;
4153
4154 if (program->Base.Instructions)
4155 _mesa_free(program->Base.Instructions);
4156 program->Base.Instructions = ap.Base.Instructions;
4157
4158 if (program->Base.Parameters)
4159 _mesa_free_parameter_list(program->Base.Parameters);
4160 program->Base.Parameters = ap.Base.Parameters;
4161
4162#if DEBUG_VP
4163 _mesa_print_program(&program->Base);
4164#endif
4165}