blob: 6d5567ec85b9292f11c9b2fb68d73a2deba17893 [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
Brian Paulb7fc1c32006-08-30 23:38:03 +00002646/**
2647 * Parse fragment program destination register.
2648 * \return 1 if error, 0 if no error.
2649 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002650static GLuint
2651parse_fp_dst_reg(GLcontext * ctx, GLubyte ** inst,
2652 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002653 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002654{
Brian Paul7aebaf32005-10-30 21:23:23 +00002655 GLint mask;
2656 GLuint idx;
2657 enum register_file file;
2658
Keith Whitwell7c26b612005-04-21 14:46:57 +00002659 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002660 return 1;
2661
Keith Whitwell7c26b612005-04-21 14:46:57 +00002662 reg->CondMask = 0; /* NV only */
2663 reg->CondSwizzle = 0; /* NV only */
2664 reg->File = file;
2665 reg->Index = idx;
2666 reg->WriteMask = mask;
2667 return 0;
2668}
2669
2670
Brian Paul7aebaf32005-10-30 21:23:23 +00002671/**
2672 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002673 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002674 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002675static GLuint
2676parse_fp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002677 struct var_cache **vc_head,
2678 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002679 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002680{
Brian Paul7aebaf32005-10-30 21:23:23 +00002681 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002682 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002683 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002684 GLubyte Swizzle[4];
2685 GLboolean IsRelOffset;
2686
2687 /* Grab the sign */
2688 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2689
2690 /* And the src reg */
2691 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2692 return 1;
2693
2694 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002695 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002696
Keith Whitwell7c26b612005-04-21 14:46:57 +00002697 reg->File = File;
2698 reg->Index = Index;
2699 reg->Abs = 0; /* NV only */
2700 reg->NegateAbs = 0; /* NV only */
2701 reg->NegateBase = Negate;
2702 reg->Swizzle = (Swizzle[0] << 0);
2703
Jouk Jansen40322e12004-04-05 08:50:36 +00002704 return 0;
2705}
2706
Keith Whitwell7c26b612005-04-21 14:46:57 +00002707
Jouk Jansen40322e12004-04-05 08:50:36 +00002708/**
2709 * This is a big mother that handles getting opcodes into the instruction
2710 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002711 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002712 */
2713static GLuint
2714parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2715 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002716 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002717{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002718 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002719 GLuint texcoord;
2720 GLubyte instClass, type, code;
2721 GLboolean rel;
2722
Brian Paul7e807512005-11-05 17:10:45 +00002723 _mesa_init_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002724
2725 /* Record the position in the program string for debugging */
2726 fp->StringPos = Program->Position;
2727
2728 /* OP_ALU_INST or OP_TEX_INST */
2729 instClass = *(*inst)++;
2730
2731 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2732 * OP_TEX_{SAMPLE, KIL}
2733 */
2734 type = *(*inst)++;
2735
2736 /* The actual opcode name */
2737 code = *(*inst)++;
2738
2739 /* Increment the correct count */
2740 switch (instClass) {
2741 case OP_ALU_INST:
2742 Program->NumAluInstructions++;
2743 break;
2744 case OP_TEX_INST:
2745 Program->NumTexInstructions++;
2746 break;
2747 }
2748
Jouk Jansen40322e12004-04-05 08:50:36 +00002749 switch (type) {
2750 case OP_ALU_VECTOR:
2751 switch (code) {
2752 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002753 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002755 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002756 break;
2757
2758 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002759 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002761 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002762 break;
2763
2764 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002765 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002766 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002767 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002768 break;
2769
2770 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002771 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002772 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002773 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002774 break;
2775
2776 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002777 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002778 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002779 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002780 break;
2781 }
2782
Keith Whitwell7c26b612005-04-21 14:46:57 +00002783 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002784 return 1;
2785
Keith Whitwell7c26b612005-04-21 14:46:57 +00002786 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002788 break;
2789
2790 case OP_ALU_SCALAR:
2791 switch (code) {
2792 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002793 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002794 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002795 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002796 break;
2797
2798 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002799 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002801 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002802 break;
2803
2804 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002805 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002806 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002807 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002808 break;
2809
2810 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002811 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002812 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002813 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002814 break;
2815
2816 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002817 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002818 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002819 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002820 break;
2821
2822 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002823 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002824 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002825 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002826 break;
2827
2828 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002829 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002830 case OP_SCS:
2831
Brian Paul7e807512005-11-05 17:10:45 +00002832 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002833 break;
2834 }
2835
Keith Whitwell7c26b612005-04-21 14:46:57 +00002836 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002837 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002838
2839 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002840 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 break;
2842
2843 case OP_ALU_BINSC:
2844 switch (code) {
2845 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002846 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002848 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 break;
2850 }
2851
Keith Whitwell7c26b612005-04-21 14:46:57 +00002852 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002854
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002856 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002857 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002858 }
2859 break;
2860
2861
2862 case OP_ALU_BIN:
2863 switch (code) {
2864 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002865 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002866 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002867 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002868 break;
2869
2870 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002871 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002872 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002873 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002874 break;
2875
2876 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002877 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002878 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002879 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002880 break;
2881
2882 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002883 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002884 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002885 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002886 break;
2887
2888 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002889 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002890 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002891 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002892 break;
2893
2894 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002895 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002896 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002897 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002898 break;
2899
2900 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002901 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002902 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002903 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002904 break;
2905
2906 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002907 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002909 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002910 break;
2911
2912 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002913 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002915 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002916 break;
2917
2918 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002919 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002920 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002921 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002922 break;
2923
2924 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002925 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002926 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002927 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002928 break;
2929
2930 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002931 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002932 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002933 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002934 break;
2935 }
2936
Keith Whitwell7c26b612005-04-21 14:46:57 +00002937 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002938 return 1;
2939 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002940 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2941 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002942 }
2943 break;
2944
2945 case OP_ALU_TRI:
2946 switch (code) {
2947 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002948 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002949 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002950 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002951 break;
2952
2953 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002954 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002955 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002956 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002957 break;
2958
2959 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002960 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002961 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002962 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002963 break;
2964 }
2965
Keith Whitwell7c26b612005-04-21 14:46:57 +00002966 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002967 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002968
Jouk Jansen40322e12004-04-05 08:50:36 +00002969 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002970 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2971 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002972 }
2973 break;
2974
2975 case OP_ALU_SWZ:
2976 switch (code) {
2977 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002978 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002979 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002980 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002981 break;
2982 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002983 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002984 return 1;
2985
Keith Whitwell7c26b612005-04-21 14:46:57 +00002986 {
Brian Paul32df89e2005-10-29 18:26:43 +00002987 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002988 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002989 enum register_file file;
2990 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002991
Brian Paul32df89e2005-10-29 18:26:43 +00002992 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002993 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002994 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2995 fp->SrcReg[0].File = file;
2996 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002997 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002998 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2999 swizzle[1],
3000 swizzle[2],
3001 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003002 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003003 break;
3004
3005 case OP_TEX_SAMPLE:
3006 switch (code) {
3007 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003008 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00003010 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003011 break;
3012
3013 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003014 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003015 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003016 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003017 break;
3018
3019 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003020 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003021 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003022 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003023 break;
3024 }
3025
Keith Whitwell7c26b612005-04-21 14:46:57 +00003026 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003027 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003028
3029 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003030 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003031
3032 /* texImageUnit */
3033 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3034 return 1;
3035 fp->TexSrcUnit = texcoord;
3036
3037 /* texTarget */
3038 switch (*(*inst)++) {
3039 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003040 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003041 break;
3042 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003043 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003044 break;
3045 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003046 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003047 break;
3048 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003049 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003050 break;
3051 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003052 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003053 break;
3054 case TEXTARGET_SHADOW1D:
3055 case TEXTARGET_SHADOW2D:
3056 case TEXTARGET_SHADOWRECT:
3057 /* TODO ARB_fragment_program_shadow code */
3058 break;
3059 }
Brian Paulb7fc1c32006-08-30 23:38:03 +00003060 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3061 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3062 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
3063 char *msg = "multiple targets used on one texture image unit";
3064 _mesa_set_program_error(ctx, Program->Position, msg);
3065 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3066 return 1;
3067 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003068 break;
3069
3070 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003071 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003072 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003073 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003074 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003075 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003076 default:
3077 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3078 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003079 }
3080
3081 return 0;
3082}
3083
Keith Whitwell7c26b612005-04-21 14:46:57 +00003084static GLuint
3085parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3086 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003087 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003088{
Brian Paul7aebaf32005-10-30 21:23:23 +00003089 GLint mask;
3090 GLuint idx;
3091 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003092
3093 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3094 return 1;
3095
3096 reg->File = file;
3097 reg->Index = idx;
3098 reg->WriteMask = mask;
3099 return 0;
3100}
3101
3102/**
3103 * Handle the parsing out of a masked address register
3104 *
3105 * \param Index - The register index we write to
3106 * \param WriteMask - The mask controlling which components we write (1->write)
3107 *
3108 * \return 0 on sucess, 1 on error
3109 */
3110static GLuint
3111parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3112 struct var_cache **vc_head,
3113 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003114 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003115{
3116 GLint idx;
3117
3118 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3119 return 1;
3120
3121 /* This should be 0x8 */
3122 (*inst)++;
3123
3124 reg->File = PROGRAM_ADDRESS;
3125 reg->Index = idx;
3126
3127 /* Writemask of .x is implied */
3128 reg->WriteMask = 0x1;
3129 return 0;
3130}
3131
3132/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003133 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003134 */
3135static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003136parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3137 struct var_cache **vc_head,
3138 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003139 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003140{
Brian Paul7aebaf32005-10-30 21:23:23 +00003141 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003142 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003143 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003144 GLubyte swizzle[4];
3145 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003146
3147 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003148 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003149
3150 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003151 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003152 return 1;
3153
3154 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003155 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003156
Brian Paul32df89e2005-10-29 18:26:43 +00003157 reg->File = file;
3158 reg->Index = index;
3159 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3160 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003161 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003162 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003163 return 0;
3164}
3165
3166
3167static GLuint
3168parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003169 struct var_cache **vc_head,
3170 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003171 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003172{
Brian Paul7aebaf32005-10-30 21:23:23 +00003173 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003174 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003175 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003176 GLubyte Swizzle[4];
3177 GLboolean IsRelOffset;
3178
3179 /* Grab the sign */
3180 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3181
3182 /* And the src reg */
3183 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3184 return 1;
3185
3186 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003187 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003188
3189 reg->File = File;
3190 reg->Index = Index;
3191 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003192 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003193 reg->RelAddr = IsRelOffset;
3194 return 0;
3195}
3196
3197
Jouk Jansen40322e12004-04-05 08:50:36 +00003198/**
3199 * This is a big mother that handles getting opcodes into the instruction
3200 * and handling the src & dst registers for vertex program instructions
3201 */
3202static GLuint
3203parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3204 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003205 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003206{
3207 GLint a;
3208 GLubyte type, code;
3209
3210 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3211 type = *(*inst)++;
3212
3213 /* The actual opcode name */
3214 code = *(*inst)++;
3215
Brian Paul7e807512005-11-05 17:10:45 +00003216 _mesa_init_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003217 /* Record the position in the program string for debugging */
3218 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003219
3220 switch (type) {
3221 /* XXX: */
3222 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003223 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003224
3225 /* Remember to set SrcReg.RelAddr; */
3226
3227 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003228 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003229 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003230
Jouk Jansen40322e12004-04-05 08:50:36 +00003231 vp->DstReg.File = PROGRAM_ADDRESS;
3232
3233 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003234 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 return 1;
3236
3237 break;
3238
3239 case OP_ALU_VECTOR:
3240 switch (code) {
3241 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003242 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003243 break;
3244 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003245 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003246 break;
3247 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003248 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003249 break;
3250 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003251 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003252 break;
3253 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003254 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003255 break;
3256 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003257
3258 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003259 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003260
3261 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003262 return 1;
3263 break;
3264
3265 case OP_ALU_SCALAR:
3266 switch (code) {
3267 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003268 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003269 break;
3270 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003271 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003272 break;
3273 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003274 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003275 break;
3276 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003277 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003278 break;
3279 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003280 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003281 break;
3282 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003283 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003284 break;
3285 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003286 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003287 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003288
3289 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003290 return 1;
3291 break;
3292
3293 case OP_ALU_BINSC:
3294 switch (code) {
3295 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003296 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 break;
3298 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003299 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003301
Jouk Jansen40322e12004-04-05 08:50:36 +00003302 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003303 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003304 return 1;
3305 }
3306 break;
3307
3308 case OP_ALU_BIN:
3309 switch (code) {
3310 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003311 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003312 break;
3313 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003314 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003315 break;
3316 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003317 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003318 break;
3319 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003320 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003321 break;
3322 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003323 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003324 break;
3325 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003326 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003327 break;
3328 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003329 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003330 break;
3331 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003332 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003333 break;
3334 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003335 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003336 break;
3337 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003338 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003339 break;
3340 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003341 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003342 break;
3343 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003344 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003345 break;
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 < 2; 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_TRI:
3357 switch (code) {
3358 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003359 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003360 break;
3361 }
3362
Keith Whitwell7c26b612005-04-21 14:46:57 +00003363 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003364 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003365
Jouk Jansen40322e12004-04-05 08:50:36 +00003366 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003367 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003368 return 1;
3369 }
3370 break;
3371
3372 case OP_ALU_SWZ:
3373 switch (code) {
3374 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003375 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003376 break;
3377 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003378 {
Brian Paul32df89e2005-10-29 18:26:43 +00003379 GLubyte swizzle[4];
3380 GLubyte negateMask;
3381 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003382 enum register_file file;
3383 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003384
Keith Whitwell7c26b612005-04-21 14:46:57 +00003385 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3386 return 1;
3387
Brian Paul32df89e2005-10-29 18:26:43 +00003388 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003389 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003390 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3391 vp->SrcReg[0].File = file;
3392 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003393 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003394 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3395 swizzle[1],
3396 swizzle[2],
3397 swizzle[3]);
3398 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003399 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003400 break;
3401 }
3402 return 0;
3403}
3404
3405#if DEBUG_PARSING
3406
3407static GLvoid
3408print_state_token (GLint token)
3409{
3410 switch (token) {
3411 case STATE_MATERIAL:
3412 fprintf (stderr, "STATE_MATERIAL ");
3413 break;
3414 case STATE_LIGHT:
3415 fprintf (stderr, "STATE_LIGHT ");
3416 break;
3417
3418 case STATE_LIGHTMODEL_AMBIENT:
3419 fprintf (stderr, "STATE_AMBIENT ");
3420 break;
3421
3422 case STATE_LIGHTMODEL_SCENECOLOR:
3423 fprintf (stderr, "STATE_SCENECOLOR ");
3424 break;
3425
3426 case STATE_LIGHTPROD:
3427 fprintf (stderr, "STATE_LIGHTPROD ");
3428 break;
3429
3430 case STATE_TEXGEN:
3431 fprintf (stderr, "STATE_TEXGEN ");
3432 break;
3433
3434 case STATE_FOG_COLOR:
3435 fprintf (stderr, "STATE_FOG_COLOR ");
3436 break;
3437
3438 case STATE_FOG_PARAMS:
3439 fprintf (stderr, "STATE_FOG_PARAMS ");
3440 break;
3441
3442 case STATE_CLIPPLANE:
3443 fprintf (stderr, "STATE_CLIPPLANE ");
3444 break;
3445
3446 case STATE_POINT_SIZE:
3447 fprintf (stderr, "STATE_POINT_SIZE ");
3448 break;
3449
3450 case STATE_POINT_ATTENUATION:
3451 fprintf (stderr, "STATE_ATTENUATION ");
3452 break;
3453
3454 case STATE_MATRIX:
3455 fprintf (stderr, "STATE_MATRIX ");
3456 break;
3457
3458 case STATE_MODELVIEW:
3459 fprintf (stderr, "STATE_MODELVIEW ");
3460 break;
3461
3462 case STATE_PROJECTION:
3463 fprintf (stderr, "STATE_PROJECTION ");
3464 break;
3465
3466 case STATE_MVP:
3467 fprintf (stderr, "STATE_MVP ");
3468 break;
3469
3470 case STATE_TEXTURE:
3471 fprintf (stderr, "STATE_TEXTURE ");
3472 break;
3473
3474 case STATE_PROGRAM:
3475 fprintf (stderr, "STATE_PROGRAM ");
3476 break;
3477
3478 case STATE_MATRIX_INVERSE:
3479 fprintf (stderr, "STATE_INVERSE ");
3480 break;
3481
3482 case STATE_MATRIX_TRANSPOSE:
3483 fprintf (stderr, "STATE_TRANSPOSE ");
3484 break;
3485
3486 case STATE_MATRIX_INVTRANS:
3487 fprintf (stderr, "STATE_INVTRANS ");
3488 break;
3489
3490 case STATE_AMBIENT:
3491 fprintf (stderr, "STATE_AMBIENT ");
3492 break;
3493
3494 case STATE_DIFFUSE:
3495 fprintf (stderr, "STATE_DIFFUSE ");
3496 break;
3497
3498 case STATE_SPECULAR:
3499 fprintf (stderr, "STATE_SPECULAR ");
3500 break;
3501
3502 case STATE_EMISSION:
3503 fprintf (stderr, "STATE_EMISSION ");
3504 break;
3505
3506 case STATE_SHININESS:
3507 fprintf (stderr, "STATE_SHININESS ");
3508 break;
3509
3510 case STATE_HALF:
3511 fprintf (stderr, "STATE_HALF ");
3512 break;
3513
3514 case STATE_POSITION:
3515 fprintf (stderr, "STATE_POSITION ");
3516 break;
3517
3518 case STATE_ATTENUATION:
3519 fprintf (stderr, "STATE_ATTENUATION ");
3520 break;
3521
3522 case STATE_SPOT_DIRECTION:
3523 fprintf (stderr, "STATE_DIRECTION ");
3524 break;
3525
3526 case STATE_TEXGEN_EYE_S:
3527 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3528 break;
3529
3530 case STATE_TEXGEN_EYE_T:
3531 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3532 break;
3533
3534 case STATE_TEXGEN_EYE_R:
3535 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3536 break;
3537
3538 case STATE_TEXGEN_EYE_Q:
3539 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3540 break;
3541
3542 case STATE_TEXGEN_OBJECT_S:
3543 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3544 break;
3545
3546 case STATE_TEXGEN_OBJECT_T:
3547 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3548 break;
3549
3550 case STATE_TEXGEN_OBJECT_R:
3551 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3552 break;
3553
3554 case STATE_TEXGEN_OBJECT_Q:
3555 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3556 break;
3557
3558 case STATE_TEXENV_COLOR:
3559 fprintf (stderr, "STATE_TEXENV_COLOR ");
3560 break;
3561
3562 case STATE_DEPTH_RANGE:
3563 fprintf (stderr, "STATE_DEPTH_RANGE ");
3564 break;
3565
3566 case STATE_VERTEX_PROGRAM:
3567 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3568 break;
3569
3570 case STATE_FRAGMENT_PROGRAM:
3571 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3572 break;
3573
3574 case STATE_ENV:
3575 fprintf (stderr, "STATE_ENV ");
3576 break;
3577
3578 case STATE_LOCAL:
3579 fprintf (stderr, "STATE_LOCAL ");
3580 break;
3581
3582 }
3583 fprintf (stderr, "[%d] ", token);
3584}
3585
3586
3587static GLvoid
3588debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3589 struct arb_program *Program)
3590{
3591 struct var_cache *vc;
3592 GLint a, b;
3593
3594 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3595
3596 /* First of all, print out the contents of the var_cache */
3597 vc = vc_head;
3598 while (vc) {
3599 fprintf (stderr, "[%x]\n", vc);
3600 switch (vc->type) {
3601 case vt_none:
3602 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3603 break;
3604 case vt_attrib:
3605 fprintf (stderr, "ATTRIB %s\n", vc->name);
3606 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3607 break;
3608 case vt_param:
3609 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3610 vc->param_binding_begin, vc->param_binding_length);
3611 b = vc->param_binding_begin;
3612 for (a = 0; a < vc->param_binding_length; a++) {
3613 fprintf (stderr, "%s\n",
3614 Program->Parameters->Parameters[a + b].Name);
3615 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3616 print_state_token (Program->Parameters->Parameters[a + b].
3617 StateIndexes[0]);
3618 print_state_token (Program->Parameters->Parameters[a + b].
3619 StateIndexes[1]);
3620 print_state_token (Program->Parameters->Parameters[a + b].
3621 StateIndexes[2]);
3622 print_state_token (Program->Parameters->Parameters[a + b].
3623 StateIndexes[3]);
3624 print_state_token (Program->Parameters->Parameters[a + b].
3625 StateIndexes[4]);
3626 print_state_token (Program->Parameters->Parameters[a + b].
3627 StateIndexes[5]);
3628 }
3629 else
3630 fprintf (stderr, "%f %f %f %f\n",
3631 Program->Parameters->Parameters[a + b].Values[0],
3632 Program->Parameters->Parameters[a + b].Values[1],
3633 Program->Parameters->Parameters[a + b].Values[2],
3634 Program->Parameters->Parameters[a + b].Values[3]);
3635 }
3636 break;
3637 case vt_temp:
3638 fprintf (stderr, "TEMP %s\n", vc->name);
3639 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3640 break;
3641 case vt_output:
3642 fprintf (stderr, "OUTPUT %s\n", vc->name);
3643 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3644 break;
3645 case vt_alias:
3646 fprintf (stderr, "ALIAS %s\n", vc->name);
3647 fprintf (stderr, " binding: 0x%x (%s)\n",
3648 vc->alias_binding, vc->alias_binding->name);
3649 break;
3650 }
3651 vc = vc->next;
3652 }
3653}
3654
Brian Paul72030e02005-11-03 03:30:34 +00003655#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003656
3657
3658/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003659 * The main loop for parsing a fragment or vertex program
3660 *
Brian Paul72030e02005-11-03 03:30:34 +00003661 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003662 */
Brian Paul72030e02005-11-03 03:30:34 +00003663static GLint
Brian Paul8c41a142005-11-19 15:36:28 +00003664parse_instructions(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
Brian Paul45703642005-10-29 15:52:31 +00003665 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003666{
Brian Paul72030e02005-11-03 03:30:34 +00003667 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3668 ? ctx->Const.FragmentProgram.MaxInstructions
3669 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003670 GLint err = 0;
3671
Brian Paul72030e02005-11-03 03:30:34 +00003672 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3673
Jouk Jansen40322e12004-04-05 08:50:36 +00003674 Program->MajorVersion = (GLuint) * inst++;
3675 Program->MinorVersion = (GLuint) * inst++;
3676
3677 while (*inst != END) {
3678 switch (*inst++) {
3679
3680 case OPTION:
3681 switch (*inst++) {
3682 case ARB_PRECISION_HINT_FASTEST:
3683 Program->PrecisionOption = GL_FASTEST;
3684 break;
3685
3686 case ARB_PRECISION_HINT_NICEST:
3687 Program->PrecisionOption = GL_NICEST;
3688 break;
3689
3690 case ARB_FOG_EXP:
3691 Program->FogOption = GL_EXP;
3692 break;
3693
3694 case ARB_FOG_EXP2:
3695 Program->FogOption = GL_EXP2;
3696 break;
3697
3698 case ARB_FOG_LINEAR:
3699 Program->FogOption = GL_LINEAR;
3700 break;
3701
3702 case ARB_POSITION_INVARIANT:
3703 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003704 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003705 break;
3706
3707 case ARB_FRAGMENT_PROGRAM_SHADOW:
3708 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3709 /* TODO ARB_fragment_program_shadow code */
3710 }
3711 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003712
3713 case ARB_DRAW_BUFFERS:
3714 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3715 /* do nothing for now */
3716 }
3717 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 }
3719 break;
3720
3721 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003722 /* check length */
3723 if (Program->Base.NumInstructions + 1 >= maxInst) {
3724 const char *msg = "Max instruction count exceeded";
3725 _mesa_set_program_error(ctx, Program->Position, msg);
3726 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3727 return 1;
3728 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003729 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003730 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003731 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003732 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003733 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003734 }
3735 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003736 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003737 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003738 }
3739
Brian Paul72030e02005-11-03 03:30:34 +00003740 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003741 Program->Base.NumInstructions++;
3742 break;
3743
3744 case DECLARATION:
3745 err = parse_declaration (ctx, &inst, vc_head, Program);
3746 break;
3747
3748 default:
3749 break;
3750 }
3751
3752 if (err)
3753 break;
3754 }
3755
3756 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003757 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003758 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul8c41a142005-11-19 15:36:28 +00003759 _mesa_init_instruction(Program->Base.Instructions + numInst);
3760 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003761 /* YYY Wrong Position in program, whatever, at least not random -> crash
3762 Program->Position = parse_position (&inst);
3763 */
Brian Paul8c41a142005-11-19 15:36:28 +00003764 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003765 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003766 Program->Base.NumInstructions++;
3767
Brian Paul05051032005-11-01 04:36:33 +00003768 /*
3769 * Initialize native counts to logical counts. The device driver may
3770 * change them if program is translated into a hardware program.
3771 */
3772 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3773 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3774 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3775 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3776 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003777
Jouk Jansen40322e12004-04-05 08:50:36 +00003778 return err;
3779}
3780
Brian Paul7aebaf32005-10-30 21:23:23 +00003781
Jouk Jansen40322e12004-04-05 08:50:36 +00003782/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003783__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003784#include "grammar_syn.h"
3785;
3786
Brian Paul7aebaf32005-10-30 21:23:23 +00003787
Brian Paul8c41a142005-11-19 15:36:28 +00003788/**
3789 * Set a grammar parameter.
3790 * \param name the grammar parameter
3791 * \param value the new parameter value
3792 * \return 0 if OK, 1 if error
3793 */
3794static int
3795set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003796{
3797 char error_msg[300];
3798 GLint error_pos;
3799
Brian Paul8c41a142005-11-19 15:36:28 +00003800 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003801 return 0;
3802
3803 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3804 _mesa_set_program_error (ctx, error_pos, error_msg);
3805 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3806 return 1;
3807}
3808
Brian Paul8c41a142005-11-19 15:36:28 +00003809
3810/**
3811 * Enable support for the given language option in the parser.
3812 * \return 1 if OK, 0 if error
3813 */
3814static int
3815enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003816{
Brian Paul8c41a142005-11-19 15:36:28 +00003817 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003818}
3819
Brian Paul8c41a142005-11-19 15:36:28 +00003820
3821/**
3822 * Enable parser extensions based on which OpenGL extensions are supported
3823 * by this rendering context.
3824 *
3825 * \return GL_TRUE if OK, GL_FALSE if error.
3826 */
3827static GLboolean
3828enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003829{
Brian Paul8c41a142005-11-19 15:36:28 +00003830#if 0
3831 /* These are not supported at this time */
3832 if ((ctx->Extensions.ARB_vertex_blend ||
3833 ctx->Extensions.EXT_vertex_weighting)
3834 && !enable_ext(ctx, id, "point_parameters"))
3835 return GL_FALSE;
3836 if (ctx->Extensions.ARB_matrix_palette
3837 && !enable_ext(ctx, id, "matrix_palette"))
3838 return GL_FALSE;
3839 if (ctx->Extensions.ARB_fragment_program_shadow
3840 && !enable_ext(ctx, id, "fragment_program_shadow"))
3841 return GL_FALSE;
3842#endif
3843 if (ctx->Extensions.EXT_point_parameters
3844 && !enable_ext(ctx, id, "point_parameters"))
3845 return GL_FALSE;
3846 if (ctx->Extensions.EXT_secondary_color
3847 && !enable_ext(ctx, id, "secondary_color"))
3848 return GL_FALSE;
3849 if (ctx->Extensions.EXT_fog_coord
3850 && !enable_ext(ctx, id, "fog_coord"))
3851 return GL_FALSE;
3852 if (ctx->Extensions.NV_texture_rectangle
3853 && !enable_ext(ctx, id, "texture_rectangle"))
3854 return GL_FALSE;
3855 if (ctx->Extensions.ARB_draw_buffers
3856 && !enable_ext(ctx, id, "draw_buffers"))
3857 return GL_FALSE;
3858
3859 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003860}
3861
Brian Paul8c41a142005-11-19 15:36:28 +00003862
Jouk Jansen40322e12004-04-05 08:50:36 +00003863/**
3864 * This kicks everything off.
3865 *
3866 * \param ctx - The GL Context
3867 * \param str - The program string
3868 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003869 * \param program - The arb_program struct to return all the parsed info in
3870 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003871 */
Brian Paul8c41a142005-11-19 15:36:28 +00003872static GLboolean
3873_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3874 const GLubyte *str, GLsizei len,
3875 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003876{
3877 GLint a, err, error_pos;
3878 char error_msg[300];
3879 GLuint parsed_len;
3880 struct var_cache *vc_head;
3881 grammar arbprogram_syn_id;
3882 GLubyte *parsed, *inst;
3883 GLubyte *strz = NULL;
3884 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3885
Brian Paul8c41a142005-11-19 15:36:28 +00003886 /* set the program target before parsing */
3887 program->Base.Target = target;
3888
Brian Paul7f76b8f2004-09-10 01:05:39 +00003889 /* Reset error state */
3890 _mesa_set_program_error(ctx, -1, NULL);
3891
Brian Paul8c41a142005-11-19 15:36:28 +00003892 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003893 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003894 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003895 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003896 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003897
3898 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3899 if (grammar_syn_id == 0) {
3900 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003901 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003902 _mesa_set_program_error (ctx, error_pos, error_msg);
3903 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003904 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003905 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003906 }
3907
Brian Paul8c41a142005-11-19 15:36:28 +00003908 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3909 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003910
Brian Paul49a80ca2006-04-28 15:40:11 +00003911 /* 'parsed' is unused here */
3912 _mesa_free (parsed);
3913 parsed = NULL;
3914
Brian Paul45703642005-10-29 15:52:31 +00003915 /* NOTE: we can't destroy grammar_syn_id right here because
3916 * grammar_destroy() can reset the last error
3917 */
Brian Paul8c41a142005-11-19 15:36:28 +00003918 if (err) {
3919 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003920 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3921 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003922 _mesa_error (ctx, GL_INVALID_OPERATION,
3923 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003924 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003925 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003926 }
3927
3928 grammar_destroy (grammar_syn_id);
3929
3930 arbprogram_syn_is_ok = 1;
3931 }
3932
3933 /* create the grammar object */
3934 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3935 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003936 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003937 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3938 _mesa_set_program_error (ctx, error_pos, error_msg);
3939 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003940 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003941 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003942 }
3943
3944 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003945 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003946 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3947 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003948 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003949 }
3950
Brian Paul8c41a142005-11-19 15:36:28 +00003951 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3952 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003953 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003954 }
3955
3956 /* check for NULL character occurences */
3957 {
Brian Paul8c41a142005-11-19 15:36:28 +00003958 GLint i;
3959 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003960 if (str[i] == '\0') {
3961 _mesa_set_program_error (ctx, i, "invalid character");
Brian Paul8c41a142005-11-19 15:36:28 +00003962 _mesa_error (ctx, GL_INVALID_OPERATION,
3963 "glProgramStringARB(illegal character)");
Jouk Jansen40322e12004-04-05 08:50:36 +00003964 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003965 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003966 }
Brian Paul8c41a142005-11-19 15:36:28 +00003967 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003968 }
3969
3970 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003971 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003972 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003973 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3974 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003975 return GL_FALSE;
3976 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003977 _mesa_memcpy (strz, str, len);
3978 strz[len] = '\0';
3979
Michal Krolb80bc052004-10-21 14:09:54 +00003980 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003981 err = !grammar_fast_check(arbprogram_syn_id, strz,
3982 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003983
3984 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003985 if (err) {
3986 _mesa_free(strz);
Brian Paul49a80ca2006-04-28 15:40:11 +00003987 _mesa_free(parsed);
Jouk Jansen40322e12004-04-05 08:50:36 +00003988 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3989 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003990 _mesa_error (ctx, GL_INVALID_OPERATION,
3991 "glProgramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003992
3993 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003994#if DEBUG_PARSING
3995 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003996 int line, col;
3997 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003998 fprintf(stderr, "program: %s\n", (char *) strz);
3999 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
4000 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00004001 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
4002 } while (0)
4003#endif
Brian Paul5fe90292004-07-20 21:15:13 +00004004
Jouk Jansen40322e12004-04-05 08:50:36 +00004005 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00004006 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004007 }
4008
Jouk Jansen40322e12004-04-05 08:50:36 +00004009 grammar_destroy (arbprogram_syn_id);
4010
Brian Paul8c41a142005-11-19 15:36:28 +00004011 /*
4012 * Program string is syntactically correct at this point
4013 * Parse the tokenized version of the program now, generating
4014 * vertex/fragment program instructions.
4015 */
4016
Jouk Jansen40322e12004-04-05 08:50:36 +00004017 /* Initialize the arb_program struct */
4018 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00004019 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00004020 program->Base.NumInstructions =
4021 program->Base.NumTemporaries =
4022 program->Base.NumParameters =
4023 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00004024 program->Base.Parameters = _mesa_new_parameter_list ();
4025 program->Base.InputsRead = 0x0;
4026 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004027 program->Position = 0;
4028 program->MajorVersion = program->MinorVersion = 0;
4029 program->PrecisionOption = GL_DONT_CARE;
4030 program->FogOption = GL_NONE;
4031 program->HintPositionInvariant = GL_FALSE;
4032 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00004033 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004034 program->NumAluInstructions =
4035 program->NumTexInstructions =
4036 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00004037 program->UsesKill = 0;
4038
Jouk Jansen40322e12004-04-05 08:50:36 +00004039 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00004040 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004041
4042 /* Start examining the tokens in the array */
4043 inst = parsed;
4044
4045 /* Check the grammer rev */
4046 if (*inst++ != REVISION) {
4047 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00004048 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul45cd2f92005-11-03 02:25:10 +00004049 "glProgramStringARB(Grammar version mismatch)");
Brian Paul45703642005-10-29 15:52:31 +00004050 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004051 }
4052 else {
Michal Krolb80bc052004-10-21 14:09:54 +00004053 /* ignore program target */
4054 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00004055 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00004056 }
4057
4058 /*debug_variables(ctx, vc_head, program); */
4059
4060 /* We're done with the parsed binary array */
4061 var_cache_destroy (&vc_head);
4062
4063 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00004064
Brian Paul8c41a142005-11-19 15:36:28 +00004065 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4066 * to size [ap.Base.NumInstructions].
4067 */
Brian Paula7543902006-08-24 21:58:32 +00004068 program->Base.Instructions
4069 = _mesa_realloc_instructions(program->Base.Instructions,
4070 MAX_INSTRUCTIONS,
4071 program->Base.NumInstructions);
4072
Brian Paul45703642005-10-29 15:52:31 +00004073 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004074}
Brian Paul8c41a142005-11-19 15:36:28 +00004075
4076
4077
4078void
4079_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4080 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004081 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004082{
4083 struct arb_program ap;
4084 GLuint i;
4085
4086 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004087 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004088 /* Error in the program. Just return. */
4089 return;
4090 }
4091
4092 /* Copy the relevant contents of the arb_program struct into the
4093 * fragment_program struct.
4094 */
4095 program->Base.String = ap.Base.String;
4096 program->Base.NumInstructions = ap.Base.NumInstructions;
4097 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4098 program->Base.NumParameters = ap.Base.NumParameters;
4099 program->Base.NumAttributes = ap.Base.NumAttributes;
4100 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004101 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4102 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4103 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4104 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4105 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004106 program->NumAluInstructions = ap.NumAluInstructions;
4107 program->NumTexInstructions = ap.NumTexInstructions;
4108 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004109 program->NumNativeAluInstructions = ap.NumAluInstructions;
4110 program->NumNativeTexInstructions = ap.NumTexInstructions;
4111 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004112 program->Base.InputsRead = ap.Base.InputsRead;
4113 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4114 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4115 program->TexturesUsed[i] = ap.TexturesUsed[i];
4116 program->FogOption = ap.FogOption;
4117
4118 if (program->Base.Instructions)
4119 _mesa_free(program->Base.Instructions);
4120 program->Base.Instructions = ap.Base.Instructions;
4121
4122 if (program->Base.Parameters)
4123 _mesa_free_parameter_list(program->Base.Parameters);
4124 program->Base.Parameters = ap.Base.Parameters;
4125
4126#if DEBUG_FP
4127 _mesa_print_program(&program.Base);
4128#endif
4129}
4130
4131
4132
4133/**
4134 * Parse the vertex program string. If success, update the given
4135 * vertex_program object with the new program. Else, leave the vertex_program
4136 * object unchanged.
4137 */
4138void
4139_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4140 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004141 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004142{
4143 struct arb_program ap;
4144
4145 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4146
Brian Paul95801792005-12-06 15:41:43 +00004147 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004148 /* Error in the program. Just return. */
4149 return;
4150 }
4151
4152 /* Copy the relevant contents of the arb_program struct into the
4153 * vertex_program struct.
4154 */
4155 program->Base.String = ap.Base.String;
4156 program->Base.NumInstructions = ap.Base.NumInstructions;
4157 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4158 program->Base.NumParameters = ap.Base.NumParameters;
4159 program->Base.NumAttributes = ap.Base.NumAttributes;
4160 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004161 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4162 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4163 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4164 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4165 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004166 program->Base.InputsRead = ap.Base.InputsRead;
4167 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4168 program->IsPositionInvariant = ap.HintPositionInvariant;
4169
4170 if (program->Base.Instructions)
4171 _mesa_free(program->Base.Instructions);
4172 program->Base.Instructions = ap.Base.Instructions;
4173
4174 if (program->Base.Parameters)
4175 _mesa_free_parameter_list(program->Base.Parameters);
4176 program->Base.Parameters = ap.Base.Parameters;
4177
4178#if DEBUG_VP
4179 _mesa_print_program(&program->Base);
4180#endif
4181}