blob: 8370203eaa4455f159fcb8ddbbf6f1243c935419 [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian Paul54cfe692005-10-21 15:22:36 +00003 * Version: 6.5
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Brian Paul54cfe692005-10-21 15:22:36 +00005 * Copyright (C) 1999-2005 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
33#include "mtypes.h"
34#include "glheader.h"
35#include "context.h"
36#include "hash.h"
37#include "imports.h"
38#include "macros.h"
39#include "program.h"
40#include "nvvertprog.h"
41#include "nvfragprog.h"
42#include "arbprogparse.h"
43#include "grammar_mesa.h"
Brian Paul32df89e2005-10-29 18:26:43 +000044#include "program.h"
Ian Romanick9bdfee32005-07-18 12:31:24 +000045#include "dispatch.h"
46
Alan Hourihane38b317d2004-12-14 09:11:52 +000047#ifndef __extension__
48#if !defined(__GNUC__) || (__GNUC__ < 2) || \
49 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
Brian Paula6c423d2004-08-25 15:59:48 +000050# define __extension__
51#endif
Alan Hourihane38b317d2004-12-14 09:11:52 +000052#endif
Brian Paula6c423d2004-08-25 15:59:48 +000053
Jouk Jansen40322e12004-04-05 08:50:36 +000054/* TODO:
55 * Fragment Program Stuff:
56 * -----------------------------------------------------
57 *
58 * - things from Michal's email
59 * + overflow on atoi
60 * + not-overflowing floats (don't use parse_integer..)
61 * + can remove range checking in arbparse.c
62 *
63 * - check all limits of number of various variables
64 * + parameters
65 *
66 * - test! test! test!
67 *
68 * Vertex Program Stuff:
69 * -----------------------------------------------------
70 * - Optimize param array usage and count limits correctly, see spec,
71 * section 2.14.3.7
72 * + Record if an array is reference absolutly or relatively (or both)
73 * + For absolute arrays, store a bitmap of accesses
74 * + For single parameters, store an access flag
75 * + After parsing, make a parameter cleanup and merging pass, where
76 * relative arrays are layed out first, followed by abs arrays, and
77 * finally single state.
78 * + Remap offsets for param src and dst registers
79 * + Now we can properly count parameter usage
80 *
81 * - Multiple state binding errors in param arrays (see spec, just before
82 * section 2.14.3.3)
83 * - grep for XXX
84 *
85 * Mesa Stuff
86 * -----------------------------------------------------
87 * - User clipping planes vs. PositionInvariant
88 * - Is it sufficient to just multiply by the mvp to transform in the
89 * PositionInvariant case? Or do we need something more involved?
90 *
91 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
92 * - fetch state listed in program_parameters list
93 * + WTF should this go???
94 * + currently in nvvertexec.c and s_nvfragprog.c
95 *
96 * - allow for multiple address registers (and fetch address regs properly)
97 *
98 * Cosmetic Stuff
99 * -----------------------------------------------------
100 * - remove any leftover unused grammer.c stuff (dict_ ?)
101 * - fix grammer.c error handling so its not static
102 * - #ifdef around stuff pertaining to extentions
103 *
104 * Outstanding Questions:
105 * -----------------------------------------------------
106 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
107 * what gets hacked off because of this:
108 * + VERTEX_ATTRIB_MATRIXINDEX
109 * + VERTEX_ATTRIB_WEIGHT
110 * + MATRIX_MODELVIEW
111 * + MATRIX_PALETTE
112 *
113 * - When can we fetch env/local params from their own register files, and
114 * when to we have to fetch them into the main state register file?
115 * (think arrays)
116 *
117 * Grammar Changes:
118 * -----------------------------------------------------
119 */
120
121/* Changes since moving the file to shader directory
122
1232004-III-4 ------------------------------------------------------------
124- added #include "grammar_mesa.h"
125- removed grammar specific code part (it resides now in grammar.c)
126- added GL_ARB_fragment_program_shadow tokens
127- modified #include "arbparse_syn.h"
128- major changes inside _mesa_parse_arb_program()
129- check the program string for '\0' characters
130- copy the program string to a one-byte-longer location to have
131 it null-terminated
132- position invariance test (not writing to result.position) moved
133 to syntax part
134*/
135
136typedef GLubyte *production;
137
138/**
139 * This is the text describing the rules to parse the grammar
140 */
Brian Paula6c423d2004-08-25 15:59:48 +0000141__extension__ static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000142#include "arbprogram_syn.h"
143;
144
145/**
146 * These should match up with the values defined in arbprogram.syn
147 */
148
149/*
150 Changes:
151 - changed and merged V_* and F_* opcode values to OP_*.
152 - added GL_ARB_fragment_program_shadow specific tokens (michal)
153*/
Michal Krolb80bc052004-10-21 14:09:54 +0000154#define REVISION 0x09
Jouk Jansen40322e12004-04-05 08:50:36 +0000155
156/* program type */
157#define FRAGMENT_PROGRAM 0x01
158#define VERTEX_PROGRAM 0x02
159
160/* program section */
161#define OPTION 0x01
162#define INSTRUCTION 0x02
163#define DECLARATION 0x03
164#define END 0x04
165
Michal Krolb80bc052004-10-21 14:09:54 +0000166/* GL_ARB_fragment_program option */
167#define ARB_PRECISION_HINT_FASTEST 0x00
168#define ARB_PRECISION_HINT_NICEST 0x01
169#define ARB_FOG_EXP 0x02
170#define ARB_FOG_EXP2 0x03
171#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000172
Michal Krolb80bc052004-10-21 14:09:54 +0000173/* GL_ARB_vertex_program option */
174#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000175
Michal Krolb80bc052004-10-21 14:09:54 +0000176/* GL_ARB_fragment_program_shadow option */
177#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000178
Michal Krolb80bc052004-10-21 14:09:54 +0000179/* GL_ARB_draw_buffers option */
180#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000181
Jouk Jansen40322e12004-04-05 08:50:36 +0000182/* GL_ARB_fragment_program instruction class */
183#define OP_ALU_INST 0x00
184#define OP_TEX_INST 0x01
185
186/* GL_ARB_vertex_program instruction class */
187/* OP_ALU_INST */
188
189/* GL_ARB_fragment_program instruction type */
190#define OP_ALU_VECTOR 0x00
191#define OP_ALU_SCALAR 0x01
192#define OP_ALU_BINSC 0x02
193#define OP_ALU_BIN 0x03
194#define OP_ALU_TRI 0x04
195#define OP_ALU_SWZ 0x05
196#define OP_TEX_SAMPLE 0x06
197#define OP_TEX_KIL 0x07
198
199/* GL_ARB_vertex_program instruction type */
200#define OP_ALU_ARL 0x08
201/* OP_ALU_VECTOR */
202/* OP_ALU_SCALAR */
203/* OP_ALU_BINSC */
204/* OP_ALU_BIN */
205/* OP_ALU_TRI */
206/* OP_ALU_SWZ */
207
208/* GL_ARB_fragment_program instruction code */
209#define OP_ABS 0x00
210#define OP_ABS_SAT 0x1B
211#define OP_FLR 0x09
212#define OP_FLR_SAT 0x26
213#define OP_FRC 0x0A
214#define OP_FRC_SAT 0x27
215#define OP_LIT 0x0C
216#define OP_LIT_SAT 0x2A
217#define OP_MOV 0x11
218#define OP_MOV_SAT 0x30
219#define OP_COS 0x1F
220#define OP_COS_SAT 0x20
221#define OP_EX2 0x07
222#define OP_EX2_SAT 0x25
223#define OP_LG2 0x0B
224#define OP_LG2_SAT 0x29
225#define OP_RCP 0x14
226#define OP_RCP_SAT 0x33
227#define OP_RSQ 0x15
228#define OP_RSQ_SAT 0x34
229#define OP_SIN 0x38
230#define OP_SIN_SAT 0x39
231#define OP_SCS 0x35
232#define OP_SCS_SAT 0x36
233#define OP_POW 0x13
234#define OP_POW_SAT 0x32
235#define OP_ADD 0x01
236#define OP_ADD_SAT 0x1C
237#define OP_DP3 0x03
238#define OP_DP3_SAT 0x21
239#define OP_DP4 0x04
240#define OP_DP4_SAT 0x22
241#define OP_DPH 0x05
242#define OP_DPH_SAT 0x23
243#define OP_DST 0x06
244#define OP_DST_SAT 0x24
245#define OP_MAX 0x0F
246#define OP_MAX_SAT 0x2E
247#define OP_MIN 0x10
248#define OP_MIN_SAT 0x2F
249#define OP_MUL 0x12
250#define OP_MUL_SAT 0x31
251#define OP_SGE 0x16
252#define OP_SGE_SAT 0x37
253#define OP_SLT 0x17
254#define OP_SLT_SAT 0x3A
255#define OP_SUB 0x18
256#define OP_SUB_SAT 0x3B
257#define OP_XPD 0x1A
258#define OP_XPD_SAT 0x43
259#define OP_CMP 0x1D
260#define OP_CMP_SAT 0x1E
261#define OP_LRP 0x2B
262#define OP_LRP_SAT 0x2C
263#define OP_MAD 0x0E
264#define OP_MAD_SAT 0x2D
265#define OP_SWZ 0x19
266#define OP_SWZ_SAT 0x3C
267#define OP_TEX 0x3D
268#define OP_TEX_SAT 0x3E
269#define OP_TXB 0x3F
270#define OP_TXB_SAT 0x40
271#define OP_TXP 0x41
272#define OP_TXP_SAT 0x42
273#define OP_KIL 0x28
274
275/* GL_ARB_vertex_program instruction code */
276#define OP_ARL 0x02
277/* OP_ABS */
278/* OP_FLR */
279/* OP_FRC */
280/* OP_LIT */
281/* OP_MOV */
282/* OP_EX2 */
283#define OP_EXP 0x08
284/* OP_LG2 */
285#define OP_LOG 0x0D
286/* OP_RCP */
287/* OP_RSQ */
288/* OP_POW */
289/* OP_ADD */
290/* OP_DP3 */
291/* OP_DP4 */
292/* OP_DPH */
293/* OP_DST */
294/* OP_MAX */
295/* OP_MIN */
296/* OP_MUL */
297/* OP_SGE */
298/* OP_SLT */
299/* OP_SUB */
300/* OP_XPD */
301/* OP_MAD */
302/* OP_SWZ */
303
304/* fragment attribute binding */
305#define FRAGMENT_ATTRIB_COLOR 0x01
306#define FRAGMENT_ATTRIB_TEXCOORD 0x02
307#define FRAGMENT_ATTRIB_FOGCOORD 0x03
308#define FRAGMENT_ATTRIB_POSITION 0x04
309
310/* vertex attribute binding */
311#define VERTEX_ATTRIB_POSITION 0x01
312#define VERTEX_ATTRIB_WEIGHT 0x02
313#define VERTEX_ATTRIB_NORMAL 0x03
314#define VERTEX_ATTRIB_COLOR 0x04
315#define VERTEX_ATTRIB_FOGCOORD 0x05
316#define VERTEX_ATTRIB_TEXCOORD 0x06
317#define VERTEX_ATTRIB_MATRIXINDEX 0x07
318#define VERTEX_ATTRIB_GENERIC 0x08
319
320/* fragment result binding */
321#define FRAGMENT_RESULT_COLOR 0x01
322#define FRAGMENT_RESULT_DEPTH 0x02
323
324/* vertex result binding */
325#define VERTEX_RESULT_POSITION 0x01
326#define VERTEX_RESULT_COLOR 0x02
327#define VERTEX_RESULT_FOGCOORD 0x03
328#define VERTEX_RESULT_POINTSIZE 0x04
329#define VERTEX_RESULT_TEXCOORD 0x05
330
331/* texture target */
332#define TEXTARGET_1D 0x01
333#define TEXTARGET_2D 0x02
334#define TEXTARGET_3D 0x03
335#define TEXTARGET_RECT 0x04
336#define TEXTARGET_CUBE 0x05
337/* GL_ARB_fragment_program_shadow */
338#define TEXTARGET_SHADOW1D 0x06
339#define TEXTARGET_SHADOW2D 0x07
340#define TEXTARGET_SHADOWRECT 0x08
341
342/* face type */
343#define FACE_FRONT 0x00
344#define FACE_BACK 0x01
345
346/* color type */
347#define COLOR_PRIMARY 0x00
348#define COLOR_SECONDARY 0x01
349
350/* component */
351#define COMPONENT_X 0x00
352#define COMPONENT_Y 0x01
353#define COMPONENT_Z 0x02
354#define COMPONENT_W 0x03
355#define COMPONENT_0 0x04
356#define COMPONENT_1 0x05
357
358/* array index type */
359#define ARRAY_INDEX_ABSOLUTE 0x00
360#define ARRAY_INDEX_RELATIVE 0x01
361
362/* matrix name */
363#define MATRIX_MODELVIEW 0x01
364#define MATRIX_PROJECTION 0x02
365#define MATRIX_MVP 0x03
366#define MATRIX_TEXTURE 0x04
367#define MATRIX_PALETTE 0x05
368#define MATRIX_PROGRAM 0x06
369
370/* matrix modifier */
371#define MATRIX_MODIFIER_IDENTITY 0x00
372#define MATRIX_MODIFIER_INVERSE 0x01
373#define MATRIX_MODIFIER_TRANSPOSE 0x02
374#define MATRIX_MODIFIER_INVTRANS 0x03
375
376/* constant type */
377#define CONSTANT_SCALAR 0x01
378#define CONSTANT_VECTOR 0x02
379
380/* program param type */
381#define PROGRAM_PARAM_ENV 0x01
382#define PROGRAM_PARAM_LOCAL 0x02
383
384/* register type */
385#define REGISTER_ATTRIB 0x01
386#define REGISTER_PARAM 0x02
387#define REGISTER_RESULT 0x03
388#define REGISTER_ESTABLISHED_NAME 0x04
389
390/* param binding */
391#define PARAM_NULL 0x00
392#define PARAM_ARRAY_ELEMENT 0x01
393#define PARAM_STATE_ELEMENT 0x02
394#define PARAM_PROGRAM_ELEMENT 0x03
395#define PARAM_PROGRAM_ELEMENTS 0x04
396#define PARAM_CONSTANT 0x05
397
398/* param state property */
399#define STATE_MATERIAL_PARSER 0x01
400#define STATE_LIGHT_PARSER 0x02
401#define STATE_LIGHT_MODEL 0x03
402#define STATE_LIGHT_PROD 0x04
403#define STATE_FOG 0x05
404#define STATE_MATRIX_ROWS 0x06
405/* GL_ARB_fragment_program */
406#define STATE_TEX_ENV 0x07
407#define STATE_DEPTH 0x08
408/* GL_ARB_vertex_program */
409#define STATE_TEX_GEN 0x09
410#define STATE_CLIP_PLANE 0x0A
411#define STATE_POINT 0x0B
412
413/* state material property */
414#define MATERIAL_AMBIENT 0x01
415#define MATERIAL_DIFFUSE 0x02
416#define MATERIAL_SPECULAR 0x03
417#define MATERIAL_EMISSION 0x04
418#define MATERIAL_SHININESS 0x05
419
420/* state light property */
421#define LIGHT_AMBIENT 0x01
422#define LIGHT_DIFFUSE 0x02
423#define LIGHT_SPECULAR 0x03
424#define LIGHT_POSITION 0x04
425#define LIGHT_ATTENUATION 0x05
426#define LIGHT_HALF 0x06
427#define LIGHT_SPOT_DIRECTION 0x07
428
429/* state light model property */
430#define LIGHT_MODEL_AMBIENT 0x01
431#define LIGHT_MODEL_SCENECOLOR 0x02
432
433/* state light product property */
434#define LIGHT_PROD_AMBIENT 0x01
435#define LIGHT_PROD_DIFFUSE 0x02
436#define LIGHT_PROD_SPECULAR 0x03
437
438/* state texture environment property */
439#define TEX_ENV_COLOR 0x01
440
441/* state texture generation coord property */
442#define TEX_GEN_EYE 0x01
443#define TEX_GEN_OBJECT 0x02
444
445/* state fog property */
446#define FOG_COLOR 0x01
447#define FOG_PARAMS 0x02
448
449/* state depth property */
450#define DEPTH_RANGE 0x01
451
452/* state point parameters property */
453#define POINT_SIZE 0x01
454#define POINT_ATTENUATION 0x02
455
456/* declaration */
457#define ATTRIB 0x01
458#define PARAM 0x02
459#define TEMP 0x03
460#define OUTPUT 0x04
461#define ALIAS 0x05
462/* GL_ARB_vertex_program */
463#define ADDRESS 0x06
464
465/*-----------------------------------------------------------------------
466 * From here on down is the semantic checking portion
467 *
468 */
469
470/**
471 * Variable Table Handling functions
472 */
473typedef enum
474{
475 vt_none,
476 vt_address,
477 vt_attrib,
478 vt_param,
479 vt_temp,
480 vt_output,
481 vt_alias
482} var_type;
483
484
Brian Paul7aebaf32005-10-30 21:23:23 +0000485/**
486 * Setting an explicit field for each of the binding properties is a bit
487 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000488 */
489struct var_cache
490{
491 GLubyte *name;
492 var_type type;
493 GLuint address_binding; /* The index of the address register we should
494 * be using */
495 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000496 GLuint attrib_is_generic; /* If the attrib was specified through a generic
497 * vertex attrib */
498 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000499 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000500 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
501 * that this is aliased to */
502 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
503 * PROGRAM_ENV_PARAM} */
504 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
505 * the tokens representing our bound state (or constants)
506 * start */
507 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
508 * we take up with our state tokens or constants. Note that
509 * this is _not_ the same as the number of param registers
510 * we eventually use */
511 struct var_cache *next;
512};
513
514static GLvoid
515var_cache_create (struct var_cache **va)
516{
517 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
518 if (*va) {
519 (**va).name = NULL;
520 (**va).type = vt_none;
521 (**va).attrib_binding = ~0;
522 (**va).attrib_is_generic = 0;
523 (**va).temp_binding = ~0;
524 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000525 (**va).param_binding_type = ~0;
526 (**va).param_binding_begin = ~0;
527 (**va).param_binding_length = ~0;
528 (**va).alias_binding = NULL;
529 (**va).next = NULL;
530 }
531}
532
533static GLvoid
534var_cache_destroy (struct var_cache **va)
535{
536 if (*va) {
537 var_cache_destroy (&(**va).next);
538 _mesa_free (*va);
539 *va = NULL;
540 }
541}
542
543static GLvoid
544var_cache_append (struct var_cache **va, struct var_cache *nv)
545{
546 if (*va)
547 var_cache_append (&(**va).next, nv);
548 else
549 *va = nv;
550}
551
552static struct var_cache *
553var_cache_find (struct var_cache *va, GLubyte * name)
554{
Brian Paul0a360cf2005-01-17 01:21:03 +0000555 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000556
557 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000558 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000559 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000560 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000561 return va;
562 }
563
564 va = va->next;
565 }
566
567 return NULL;
568}
569
570/**
571 * constructs an integer from 4 GLubytes in LE format
572 */
573static GLuint
574parse_position (GLubyte ** inst)
575{
576 GLuint value;
577
578 value = (GLuint) (*(*inst)++);
579 value += (GLuint) (*(*inst)++) * 0x100;
580 value += (GLuint) (*(*inst)++) * 0x10000;
581 value += (GLuint) (*(*inst)++) * 0x1000000;
582
583 return value;
584}
585
586/**
587 * This will, given a string, lookup the string as a variable name in the
588 * var cache. If the name is found, the var cache node corresponding to the
589 * var name is returned. If it is not found, a new entry is allocated
590 *
591 * \param I Points into the binary array where the string identifier begins
592 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
593 * \return The location on the var_cache corresponding the the string starting at I
594 */
595static struct var_cache *
596parse_string (GLubyte ** inst, struct var_cache **vc_head,
597 struct arb_program *Program, GLuint * found)
598{
599 GLubyte *i = *inst;
600 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000601 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000602
603 *inst += _mesa_strlen ((char *) i) + 1;
604
605 va = var_cache_find (*vc_head, i);
606
607 if (va) {
608 *found = 1;
609 return va;
610 }
611
612 *found = 0;
613 var_cache_create (&va);
614 va->name = i;
615
616 var_cache_append (vc_head, va);
617
618 return va;
619}
620
621static char *
622parse_string_without_adding (GLubyte ** inst, struct arb_program *Program)
623{
624 GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000625 (void) Program;
626
Jouk Jansen40322e12004-04-05 08:50:36 +0000627 *inst += _mesa_strlen ((char *) i) + 1;
628
629 return (char *) i;
630}
631
632/**
Brian Paul05908952004-06-08 15:20:23 +0000633 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000634 */
Brian Paul05908952004-06-08 15:20:23 +0000635static GLint
Jouk Jansen40322e12004-04-05 08:50:36 +0000636parse_sign (GLubyte ** inst)
637{
638 /*return *(*inst)++ != '+'; */
639
640 if (**inst == '-') {
641 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000642 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000643 }
644 else if (**inst == '+') {
645 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000646 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000647 }
648
Brian Paul05908952004-06-08 15:20:23 +0000649 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000650}
651
652/**
653 * parses and returns signed integer
654 */
655static GLint
656parse_integer (GLubyte ** inst, struct arb_program *Program)
657{
658 GLint sign;
659 GLint value;
660
661 /* check if *inst points to '+' or '-'
662 * if yes, grab the sign and increment *inst
663 */
664 sign = parse_sign (inst);
665
666 /* now check if *inst points to 0
667 * if yes, increment the *inst and return the default value
668 */
669 if (**inst == 0) {
670 (*inst)++;
671 return 0;
672 }
673
674 /* parse the integer as you normally would do it */
675 value = _mesa_atoi (parse_string_without_adding (inst, Program));
676
677 /* now, after terminating 0 there is a position
678 * to parse it - parse_position()
679 */
680 Program->Position = parse_position (inst);
681
Brian Paul05908952004-06-08 15:20:23 +0000682 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000683}
684
685/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000686 Accumulate this string of digits, and return them as
687 a large integer represented in floating point (for range).
688 If scale is not NULL, also accumulates a power-of-ten
689 integer scale factor that represents the number of digits
690 in the string.
691*/
692static GLdouble
693parse_float_string(GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
694{
695 GLdouble value = 0.0;
696 GLdouble oscale = 1.0;
697
698 if (**inst == 0) { /* this string of digits is empty-- do nothing */
699 (*inst)++;
700 }
701 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000702 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000703 GLubyte digit = *((*inst)++);
704 value = value * 10.0 + (GLint) (digit - '0');
705 oscale *= 10.0;
706 }
707 assert(**inst == 0); /* integer string should end with 0 */
708 (*inst)++; /* skip over terminating 0 */
709 Program->Position = parse_position(inst); /* skip position (from integer) */
710 }
711 if (scale)
712 *scale = oscale;
713 return value;
714}
715
716/**
717 Parse an unsigned floating-point number from this stream of tokenized
718 characters. Example floating-point formats supported:
719 12.34
720 12
721 0.34
722 .34
723 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000724 */
725static GLfloat
726parse_float (GLubyte ** inst, struct arb_program *Program)
727{
Brian Paul1ff8f502005-02-16 15:08:29 +0000728 GLint exponent;
729 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000730
Brian Paul1ff8f502005-02-16 15:08:29 +0000731 whole = parse_float_string(inst, Program, 0);
732 fraction = parse_float_string(inst, Program, &fracScale);
733
734 /* Parse signed exponent */
735 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000736
Brian Paul1ff8f502005-02-16 15:08:29 +0000737 /* Assemble parts of floating-point number: */
738 return (GLfloat) ((whole + fraction / fracScale) *
739 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000740}
741
742
743/**
744 */
745static GLfloat
746parse_signed_float (GLubyte ** inst, struct arb_program *Program)
747{
Brian Paul05908952004-06-08 15:20:23 +0000748 GLint sign = parse_sign (inst);
749 GLfloat value = parse_float (inst, Program);
750 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000751}
752
753/**
754 * This picks out a constant value from the parsed array. The constant vector is r
755 * returned in the *values array, which should be of length 4.
756 *
757 * \param values - The 4 component vector with the constant value in it
758 */
759static GLvoid
760parse_constant (GLubyte ** inst, GLfloat *values, struct arb_program *Program,
761 GLboolean use)
762{
763 GLuint components, i;
764
765
766 switch (*(*inst)++) {
767 case CONSTANT_SCALAR:
768 if (use == GL_TRUE) {
769 values[0] =
770 values[1] =
771 values[2] = values[3] = parse_float (inst, Program);
772 }
773 else {
774 values[0] =
775 values[1] =
776 values[2] = values[3] = parse_signed_float (inst, Program);
777 }
778
779 break;
780 case CONSTANT_VECTOR:
781 values[0] = values[1] = values[2] = 0;
782 values[3] = 1;
783 components = *(*inst)++;
784 for (i = 0; i < components; i++) {
785 values[i] = parse_signed_float (inst, Program);
786 }
787 break;
788 }
789}
790
791/**
792 * \param offset The offset from the address register that we should
793 * address
794 *
795 * \return 0 on sucess, 1 on error
796 */
797static GLuint
798parse_relative_offset (GLcontext *ctx, GLubyte **inst, struct arb_program *Program,
799 GLint *offset)
800{
801 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000802 return 0;
803}
804
805/**
806 * \param color 0 if color type is primary, 1 if color type is secondary
807 * \return 0 on sucess, 1 on error
808 */
809static GLuint
810parse_color_type (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
811 GLint * color)
812{
Brian Paula6c423d2004-08-25 15:59:48 +0000813 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000814 *color = *(*inst)++ != COLOR_PRIMARY;
815 return 0;
816}
817
818/**
819 * Get an integer corresponding to a generic vertex attribute.
820 *
821 * \return 0 on sucess, 1 on error
822 */
823static GLuint
824parse_generic_attrib_num(GLcontext *ctx, GLubyte ** inst,
825 struct arb_program *Program, GLuint *attrib)
826{
Brian Paulbd997cd2004-07-20 21:12:56 +0000827 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000828
Brian Paulbd997cd2004-07-20 21:12:56 +0000829 if ((i < 0) || (i > MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000830 {
831 _mesa_set_program_error (ctx, Program->Position,
832 "Invalid generic vertex attribute index");
833 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid generic vertex attribute index");
834
835 return 1;
836 }
837
Brian Paulbd997cd2004-07-20 21:12:56 +0000838 *attrib = (GLuint) i;
839
Jouk Jansen40322e12004-04-05 08:50:36 +0000840 return 0;
841}
842
843
844/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000845 * \param color The index of the color buffer to write into
846 * \return 0 on sucess, 1 on error
847 */
848static GLuint
849parse_output_color_num (GLcontext * ctx, GLubyte ** inst,
850 struct arb_program *Program, GLuint * color)
851{
852 GLint i = parse_integer (inst, Program);
853
854 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
855 _mesa_set_program_error (ctx, Program->Position,
856 "Invalid draw buffer index");
857 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid draw buffer index");
858 return 1;
859 }
860
861 *color = (GLuint) i;
862 return 0;
863}
864
865
866/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000867 * \param coord The texture unit index
868 * \return 0 on sucess, 1 on error
869 */
870static GLuint
871parse_texcoord_num (GLcontext * ctx, GLubyte ** inst,
872 struct arb_program *Program, GLuint * coord)
873{
Brian Paulbd997cd2004-07-20 21:12:56 +0000874 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000875
Brian Paula6c423d2004-08-25 15:59:48 +0000876 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000877 _mesa_set_program_error (ctx, Program->Position,
878 "Invalid texture unit index");
879 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid texture unit index");
880 return 1;
881 }
882
Brian Paulbd997cd2004-07-20 21:12:56 +0000883 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000884 return 0;
885}
886
887/**
888 * \param coord The weight index
889 * \return 0 on sucess, 1 on error
890 */
891static GLuint
892parse_weight_num (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
893 GLint * coord)
894{
895 *coord = parse_integer (inst, Program);
896
897 if ((*coord < 0) || (*coord >= 1)) {
898 _mesa_set_program_error (ctx, Program->Position,
899 "Invalid weight index");
900 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid weight index");
901 return 1;
902 }
903
904 return 0;
905}
906
907/**
908 * \param coord The clip plane index
909 * \return 0 on sucess, 1 on error
910 */
911static GLuint
912parse_clipplane_num (GLcontext * ctx, GLubyte ** inst,
913 struct arb_program *Program, GLint * coord)
914{
915 *coord = parse_integer (inst, Program);
916
917 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
918 _mesa_set_program_error (ctx, Program->Position,
919 "Invalid clip plane index");
920 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid clip plane index");
921 return 1;
922 }
923
924 return 0;
925}
926
927
928/**
929 * \return 0 on front face, 1 on back face
930 */
931static GLuint
932parse_face_type (GLubyte ** inst)
933{
934 switch (*(*inst)++) {
935 case FACE_FRONT:
936 return 0;
937
938 case FACE_BACK:
939 return 1;
940 }
941 return 0;
942}
943
944
945/**
946 * Given a matrix and a modifier token on the binary array, return tokens
947 * that _mesa_fetch_state() [program.c] can understand.
948 *
949 * \param matrix - the matrix we are talking about
950 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
951 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
952 * \return 0 on sucess, 1 on failure
953 */
954static GLuint
955parse_matrix (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
956 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
957{
958 GLubyte mat = *(*inst)++;
959
960 *matrix_idx = 0;
961
962 switch (mat) {
963 case MATRIX_MODELVIEW:
964 *matrix = STATE_MODELVIEW;
965 *matrix_idx = parse_integer (inst, Program);
966 if (*matrix_idx > 0) {
967 _mesa_set_program_error (ctx, Program->Position,
968 "ARB_vertex_blend not supported\n");
969 _mesa_error (ctx, GL_INVALID_OPERATION,
970 "ARB_vertex_blend not supported\n");
971 return 1;
972 }
973 break;
974
975 case MATRIX_PROJECTION:
976 *matrix = STATE_PROJECTION;
977 break;
978
979 case MATRIX_MVP:
980 *matrix = STATE_MVP;
981 break;
982
983 case MATRIX_TEXTURE:
984 *matrix = STATE_TEXTURE;
985 *matrix_idx = parse_integer (inst, Program);
986 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
987 _mesa_set_program_error (ctx, Program->Position,
988 "Invalid Texture Unit");
989 _mesa_error (ctx, GL_INVALID_OPERATION,
990 "Invalid Texture Unit: %d", *matrix_idx);
991 return 1;
992 }
993 break;
994
995 /* This is not currently supported (ARB_matrix_palette) */
996 case MATRIX_PALETTE:
997 *matrix_idx = parse_integer (inst, Program);
998 _mesa_set_program_error (ctx, Program->Position,
999 "ARB_matrix_palette not supported\n");
1000 _mesa_error (ctx, GL_INVALID_OPERATION,
1001 "ARB_matrix_palette not supported\n");
1002 return 1;
1003 break;
1004
1005 case MATRIX_PROGRAM:
1006 *matrix = STATE_PROGRAM;
1007 *matrix_idx = parse_integer (inst, Program);
1008 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
1009 _mesa_set_program_error (ctx, Program->Position,
1010 "Invalid Program Matrix");
1011 _mesa_error (ctx, GL_INVALID_OPERATION,
1012 "Invalid Program Matrix: %d", *matrix_idx);
1013 return 1;
1014 }
1015 break;
1016 }
1017
1018 switch (*(*inst)++) {
1019 case MATRIX_MODIFIER_IDENTITY:
1020 *matrix_modifier = 0;
1021 break;
1022 case MATRIX_MODIFIER_INVERSE:
1023 *matrix_modifier = STATE_MATRIX_INVERSE;
1024 break;
1025 case MATRIX_MODIFIER_TRANSPOSE:
1026 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1027 break;
1028 case MATRIX_MODIFIER_INVTRANS:
1029 *matrix_modifier = STATE_MATRIX_INVTRANS;
1030 break;
1031 }
1032
1033 return 0;
1034}
1035
1036
1037/**
1038 * This parses a state string (rather, the binary version of it) into
1039 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1040 *
1041 * \param inst - the start in the binary arry to start working from
1042 * \param state_tokens - the storage for the 6-token state description
1043 * \return - 0 on sucess, 1 on error
1044 */
1045static GLuint
1046parse_state_single_item (GLcontext * ctx, GLubyte ** inst,
1047 struct arb_program *Program, GLint * state_tokens)
1048{
1049 switch (*(*inst)++) {
1050 case STATE_MATERIAL_PARSER:
1051 state_tokens[0] = STATE_MATERIAL;
1052 state_tokens[1] = parse_face_type (inst);
1053 switch (*(*inst)++) {
1054 case MATERIAL_AMBIENT:
1055 state_tokens[2] = STATE_AMBIENT;
1056 break;
1057 case MATERIAL_DIFFUSE:
1058 state_tokens[2] = STATE_DIFFUSE;
1059 break;
1060 case MATERIAL_SPECULAR:
1061 state_tokens[2] = STATE_SPECULAR;
1062 break;
1063 case MATERIAL_EMISSION:
1064 state_tokens[2] = STATE_EMISSION;
1065 break;
1066 case MATERIAL_SHININESS:
1067 state_tokens[2] = STATE_SHININESS;
1068 break;
1069 }
1070 break;
1071
1072 case STATE_LIGHT_PARSER:
1073 state_tokens[0] = STATE_LIGHT;
1074 state_tokens[1] = parse_integer (inst, Program);
1075
1076 /* Check the value of state_tokens[1] against the # of lights */
1077 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1078 _mesa_set_program_error (ctx, Program->Position,
1079 "Invalid Light Number");
1080 _mesa_error (ctx, GL_INVALID_OPERATION,
1081 "Invalid Light Number: %d", state_tokens[1]);
1082 return 1;
1083 }
1084
1085 switch (*(*inst)++) {
1086 case LIGHT_AMBIENT:
1087 state_tokens[2] = STATE_AMBIENT;
1088 break;
1089 case LIGHT_DIFFUSE:
1090 state_tokens[2] = STATE_DIFFUSE;
1091 break;
1092 case LIGHT_SPECULAR:
1093 state_tokens[2] = STATE_SPECULAR;
1094 break;
1095 case LIGHT_POSITION:
1096 state_tokens[2] = STATE_POSITION;
1097 break;
1098 case LIGHT_ATTENUATION:
1099 state_tokens[2] = STATE_ATTENUATION;
1100 break;
1101 case LIGHT_HALF:
1102 state_tokens[2] = STATE_HALF;
1103 break;
1104 case LIGHT_SPOT_DIRECTION:
1105 state_tokens[2] = STATE_SPOT_DIRECTION;
1106 break;
1107 }
1108 break;
1109
1110 case STATE_LIGHT_MODEL:
1111 switch (*(*inst)++) {
1112 case LIGHT_MODEL_AMBIENT:
1113 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1114 break;
1115 case LIGHT_MODEL_SCENECOLOR:
1116 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1117 state_tokens[1] = parse_face_type (inst);
1118 break;
1119 }
1120 break;
1121
1122 case STATE_LIGHT_PROD:
1123 state_tokens[0] = STATE_LIGHTPROD;
1124 state_tokens[1] = parse_integer (inst, Program);
1125
1126 /* Check the value of state_tokens[1] against the # of lights */
1127 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1128 _mesa_set_program_error (ctx, Program->Position,
1129 "Invalid Light Number");
1130 _mesa_error (ctx, GL_INVALID_OPERATION,
1131 "Invalid Light Number: %d", state_tokens[1]);
1132 return 1;
1133 }
1134
1135 state_tokens[2] = parse_face_type (inst);
1136 switch (*(*inst)++) {
1137 case LIGHT_PROD_AMBIENT:
1138 state_tokens[3] = STATE_AMBIENT;
1139 break;
1140 case LIGHT_PROD_DIFFUSE:
1141 state_tokens[3] = STATE_DIFFUSE;
1142 break;
1143 case LIGHT_PROD_SPECULAR:
1144 state_tokens[3] = STATE_SPECULAR;
1145 break;
1146 }
1147 break;
1148
1149
1150 case STATE_FOG:
1151 switch (*(*inst)++) {
1152 case FOG_COLOR:
1153 state_tokens[0] = STATE_FOG_COLOR;
1154 break;
1155 case FOG_PARAMS:
1156 state_tokens[0] = STATE_FOG_PARAMS;
1157 break;
1158 }
1159 break;
1160
1161 case STATE_TEX_ENV:
1162 state_tokens[1] = parse_integer (inst, Program);
1163 switch (*(*inst)++) {
1164 case TEX_ENV_COLOR:
1165 state_tokens[0] = STATE_TEXENV_COLOR;
1166 break;
1167 }
1168 break;
1169
1170 case STATE_TEX_GEN:
1171 {
1172 GLuint type, coord;
1173
1174 state_tokens[0] = STATE_TEXGEN;
1175 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1176
1177 if (parse_texcoord_num (ctx, inst, Program, &coord))
1178 return 1;
1179 state_tokens[1] = coord;
1180
1181 /* EYE or OBJECT */
1182 type = *(*inst++);
1183
1184 /* 0 - s, 1 - t, 2 - r, 3 - q */
1185 coord = *(*inst++);
1186
1187 if (type == TEX_GEN_EYE) {
1188 switch (coord) {
1189 case COMPONENT_X:
1190 state_tokens[2] = STATE_TEXGEN_EYE_S;
1191 break;
1192 case COMPONENT_Y:
1193 state_tokens[2] = STATE_TEXGEN_EYE_T;
1194 break;
1195 case COMPONENT_Z:
1196 state_tokens[2] = STATE_TEXGEN_EYE_R;
1197 break;
1198 case COMPONENT_W:
1199 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1200 break;
1201 }
1202 }
1203 else {
1204 switch (coord) {
1205 case COMPONENT_X:
1206 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1207 break;
1208 case COMPONENT_Y:
1209 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1210 break;
1211 case COMPONENT_Z:
1212 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1213 break;
1214 case COMPONENT_W:
1215 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1216 break;
1217 }
1218 }
1219 }
1220 break;
1221
1222 case STATE_DEPTH:
1223 switch (*(*inst)++) {
1224 case DEPTH_RANGE:
1225 state_tokens[0] = STATE_DEPTH_RANGE;
1226 break;
1227 }
1228 break;
1229
1230 case STATE_CLIP_PLANE:
1231 state_tokens[0] = STATE_CLIPPLANE;
1232 state_tokens[1] = parse_integer (inst, Program);
1233 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1234 return 1;
1235 break;
1236
1237 case STATE_POINT:
1238 switch (*(*inst++)) {
1239 case POINT_SIZE:
1240 state_tokens[0] = STATE_POINT_SIZE;
1241 break;
1242
1243 case POINT_ATTENUATION:
1244 state_tokens[0] = STATE_POINT_ATTENUATION;
1245 break;
1246 }
1247 break;
1248
1249 /* XXX: I think this is the correct format for a matrix row */
1250 case STATE_MATRIX_ROWS:
1251 state_tokens[0] = STATE_MATRIX;
1252 if (parse_matrix
1253 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1254 &state_tokens[5]))
1255 return 1;
1256
1257 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1258
1259 if ((**inst) != 0) { /* Either the last row, 0 */
1260 state_tokens[4] = parse_integer (inst, Program);
1261 if (state_tokens[4] < state_tokens[3]) {
1262 _mesa_set_program_error (ctx, Program->Position,
1263 "Second matrix index less than the first");
1264 _mesa_error (ctx, GL_INVALID_OPERATION,
1265 "Second matrix index (%d) less than the first (%d)",
1266 state_tokens[4], state_tokens[3]);
1267 return 1;
1268 }
1269 }
1270 else {
1271 state_tokens[4] = state_tokens[3];
1272 (*inst)++;
1273 }
1274 break;
1275 }
1276
1277 return 0;
1278}
1279
1280/**
1281 * This parses a state string (rather, the binary version of it) into
1282 * a 6-token similar for the state fetching code in program.c
1283 *
1284 * One might ask, why fetch these parameters into just like you fetch
1285 * state when they are already stored in other places?
1286 *
1287 * Because of array offsets -> We can stick env/local parameters in the
1288 * middle of a parameter array and then index someplace into the array
1289 * when we execute.
1290 *
1291 * One optimization might be to only do this for the cases where the
1292 * env/local parameters end up inside of an array, and leave the
1293 * single parameters (or arrays of pure env/local pareameters) in their
1294 * respective register files.
1295 *
1296 * For ENV parameters, the format is:
1297 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1298 * state_tokens[1] = STATE_ENV
1299 * state_tokens[2] = the parameter index
1300 *
1301 * for LOCAL parameters, the format is:
1302 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1303 * state_tokens[1] = STATE_LOCAL
1304 * state_tokens[2] = the parameter index
1305 *
1306 * \param inst - the start in the binary arry to start working from
1307 * \param state_tokens - the storage for the 6-token state description
1308 * \return - 0 on sucess, 1 on failure
1309 */
1310static GLuint
1311parse_program_single_item (GLcontext * ctx, GLubyte ** inst,
1312 struct arb_program *Program, GLint * state_tokens)
1313{
1314 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1315 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1316 else
1317 state_tokens[0] = STATE_VERTEX_PROGRAM;
1318
1319
1320 switch (*(*inst)++) {
1321 case PROGRAM_PARAM_ENV:
1322 state_tokens[1] = STATE_ENV;
1323 state_tokens[2] = parse_integer (inst, Program);
1324
1325 /* Check state_tokens[2] against the number of ENV parameters available */
1326 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1327 (state_tokens[2] >= (GLint) ctx->Const.MaxFragmentProgramEnvParams))
1328 ||
1329 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1330 (state_tokens[2] >= (GLint) ctx->Const.MaxVertexProgramEnvParams))) {
1331 _mesa_set_program_error (ctx, Program->Position,
1332 "Invalid Program Env Parameter");
1333 _mesa_error (ctx, GL_INVALID_OPERATION,
1334 "Invalid Program Env Parameter: %d",
1335 state_tokens[2]);
1336 return 1;
1337 }
1338
1339 break;
1340
1341 case PROGRAM_PARAM_LOCAL:
1342 state_tokens[1] = STATE_LOCAL;
1343 state_tokens[2] = parse_integer (inst, Program);
1344
1345 /* Check state_tokens[2] against the number of LOCAL parameters available */
1346 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1347 (state_tokens[2] >= (GLint) ctx->Const.MaxFragmentProgramLocalParams))
1348 ||
1349 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1350 (state_tokens[2] >= (GLint) ctx->Const.MaxVertexProgramLocalParams))) {
1351 _mesa_set_program_error (ctx, Program->Position,
1352 "Invalid Program Local Parameter");
1353 _mesa_error (ctx, GL_INVALID_OPERATION,
1354 "Invalid Program Local Parameter: %d",
1355 state_tokens[2]);
1356 return 1;
1357 }
1358 break;
1359 }
1360
1361 return 0;
1362}
1363
1364/**
1365 * For ARB_vertex_program, programs are not allowed to use both an explicit
1366 * vertex attribute and a generic vertex attribute corresponding to the same
1367 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1368 *
1369 * This will walk our var_cache and make sure that nobody does anything fishy.
1370 *
1371 * \return 0 on sucess, 1 on error
1372 */
1373static GLuint
1374generic_attrib_check(struct var_cache *vc_head)
1375{
1376 int a;
1377 struct var_cache *curr;
1378 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1379 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1380
1381 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1382 explicitAttrib[a] = GL_FALSE;
1383 genericAttrib[a] = GL_FALSE;
1384 }
1385
1386 curr = vc_head;
1387 while (curr) {
1388 if (curr->type == vt_attrib) {
1389 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001390 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001391 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001392 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001393 }
1394
1395 curr = curr->next;
1396 }
1397
1398 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1399 if ((explicitAttrib[a]) && (genericAttrib[a]))
1400 return 1;
1401 }
1402
1403 return 0;
1404}
1405
1406/**
1407 * This will handle the binding side of an ATTRIB var declaration
1408 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001409 * \param inputReg returns the input register index, one of the
1410 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001411 * \return returns 0 on sucess, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001412 */
1413static GLuint
Brian Paul18e7c5c2005-10-30 21:46:00 +00001414parse_attrib_binding(GLcontext * ctx, GLubyte ** inst,
1415 struct arb_program *Program,
1416 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001417{
Jouk Jansen40322e12004-04-05 08:50:36 +00001418 GLint err = 0;
1419
1420 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001421
Jouk Jansen40322e12004-04-05 08:50:36 +00001422 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1423 switch (*(*inst)++) {
1424 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001425 {
1426 GLint coord;
1427 err = parse_color_type (ctx, inst, Program, &coord);
1428 *inputReg = FRAG_ATTRIB_COL0 + coord;
1429 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001430 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001431 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001432 {
1433 GLuint texcoord;
1434 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1435 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1436 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001437 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001438 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001439 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001440 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001441 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001442 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001443 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001444 default:
1445 err = 1;
1446 break;
1447 }
1448 }
1449 else {
1450 switch (*(*inst)++) {
1451 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001452 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001453 break;
1454
1455 case VERTEX_ATTRIB_WEIGHT:
1456 {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001457 const char *msg = "ARB_vertex_blend not supported";
Jouk Jansen40322e12004-04-05 08:50:36 +00001458 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001459 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001460 *inputReg = VERT_ATTRIB_WEIGHT;
1461 _mesa_set_program_error(ctx, Program->Position, msg);
1462 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001463 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001464 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001465
1466 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001467 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001468 break;
1469
1470 case VERTEX_ATTRIB_COLOR:
1471 {
1472 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001473 err = parse_color_type (ctx, inst, Program, &color);
1474 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001475 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001476 }
1477 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001478 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001479 }
1480 }
1481 break;
1482
1483 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001484 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001485 break;
1486
1487 case VERTEX_ATTRIB_TEXCOORD:
1488 {
1489 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001490 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001491 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001492 }
1493 break;
1494
Jouk Jansen40322e12004-04-05 08:50:36 +00001495 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001496 /* Not supported at this time */
1497 {
1498 const char *msg = "ARB_palette_matrix not supported";
1499 parse_integer (inst, Program);
1500 _mesa_set_program_error (ctx, Program->Position, msg);
1501 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
1502 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001503 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001504
1505 case VERTEX_ATTRIB_GENERIC:
1506 {
1507 GLuint attrib;
Jouk Jansen40322e12004-04-05 08:50:36 +00001508 if (!parse_generic_attrib_num(ctx, inst, Program, &attrib)) {
1509 *is_generic = 1;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001510 *inputReg = attrib;
Jouk Jansen40322e12004-04-05 08:50:36 +00001511 }
1512 }
1513 break;
1514
1515 default:
1516 err = 1;
1517 break;
1518 }
1519 }
1520
1521 /* Can this even happen? */
1522 if (err) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001523 const char *msg = "Bad attribute binding";
1524 _mesa_set_program_error(ctx, Program->Position, msg);
1525 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 }
1527
Brian Paul18e7c5c2005-10-30 21:46:00 +00001528 Program->InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001529
1530 return err;
1531}
1532
Brian Paul7aebaf32005-10-30 21:23:23 +00001533
Jouk Jansen40322e12004-04-05 08:50:36 +00001534/**
1535 * This translates between a binary token for an output variable type
1536 * and the mesa token for the same thing.
1537 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001538 * \param inst The parsed tokens
1539 * \param outputReg Returned index/number of the output register,
1540 * one of the VERT_RESULT_* or FRAG_OUTPUT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001541 */
1542static GLuint
Brian Paul7aebaf32005-10-30 21:23:23 +00001543parse_result_binding(GLcontext *ctx, GLubyte **inst,
1544 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001545{
Brian Paul7aebaf32005-10-30 21:23:23 +00001546 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001547
Brian Paul7aebaf32005-10-30 21:23:23 +00001548 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001549 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001550 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001551 GLuint out_color;
1552
Brian Paulbe76b7f2004-10-04 14:40:05 +00001553 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001554 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001555 */
1556 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001557 ASSERT(out_color < MAX_DRAW_BUFFERS);
1558 *outputReg = FRAG_OUTPUT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001559 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001560 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001561 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1562 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001563 }
1564 break;
1565
1566 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001567 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001568 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1569 *outputReg = FRAG_OUTPUT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001570 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001571 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001572 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001573 GLint color_type;
1574 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001575 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1576 if (err)
1577 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001578
Jouk Jansen40322e12004-04-05 08:50:36 +00001579 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001580 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001581 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001582 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001583 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001584 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001585 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001586 }
1587 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001588 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001589 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001590 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001591 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001592 }
1593 /* primary color */
1594 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001595 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001596 }
1597 }
1598 }
1599 break;
1600
1601 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001602 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001603 break;
1604
1605 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001606 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 break;
1608
1609 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001610 {
1611 GLuint unit;
1612 if (parse_texcoord_num (ctx, inst, Program, &unit))
1613 return 1;
1614 *outputReg = VERT_RESULT_TEX0 + unit;
1615 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001616 break;
1617 }
1618
Brian Paul7aebaf32005-10-30 21:23:23 +00001619 Program->OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001620
1621 return 0;
1622}
1623
Brian Paul7aebaf32005-10-30 21:23:23 +00001624
Jouk Jansen40322e12004-04-05 08:50:36 +00001625/**
1626 * This handles the declaration of ATTRIB variables
1627 *
1628 * XXX: Still needs
1629 * parse_vert_attrib_binding(), or something like that
1630 *
1631 * \return 0 on sucess, 1 on error
1632 */
1633static GLint
1634parse_attrib (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1635 struct arb_program *Program)
1636{
1637 GLuint found;
1638 char *error_msg;
1639 struct var_cache *attrib_var;
1640
1641 attrib_var = parse_string (inst, vc_head, Program, &found);
1642 Program->Position = parse_position (inst);
1643 if (found) {
1644 error_msg = (char *)
1645 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1646 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1647 attrib_var->name);
1648
1649 _mesa_set_program_error (ctx, Program->Position, error_msg);
1650 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1651
1652 _mesa_free (error_msg);
1653 return 1;
1654 }
1655
1656 attrib_var->type = vt_attrib;
1657
Brian Paul7aebaf32005-10-30 21:23:23 +00001658 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001659 &attrib_var->attrib_is_generic))
1660 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001661
Brian Paul7aebaf32005-10-30 21:23:23 +00001662 if (generic_attrib_check(*vc_head)) {
1663 _mesa_set_program_error(ctx, Program->Position,
1664 "Cannot use both a generic vertex attribute "
1665 "and a specific attribute of the same type");
1666 _mesa_error(ctx, GL_INVALID_OPERATION,
1667 "Cannot use both a generic vertex attribute and a specific "
1668 "attribute of the same type");
1669 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001670 }
1671
1672 Program->Base.NumAttributes++;
1673 return 0;
1674}
1675
1676/**
1677 * \param use -- TRUE if we're called when declaring implicit parameters,
1678 * FALSE if we're declaraing variables. This has to do with
1679 * if we get a signed or unsigned float for scalar constants
1680 */
1681static GLuint
1682parse_param_elements (GLcontext * ctx, GLubyte ** inst,
1683 struct var_cache *param_var,
1684 struct arb_program *Program, GLboolean use)
1685{
1686 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001687 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001688 GLint state_tokens[6];
1689 GLfloat const_values[4];
1690
Jouk Jansen40322e12004-04-05 08:50:36 +00001691 switch (*(*inst)++) {
1692 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001693 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1694 return 1;
1695
1696 /* If we adding STATE_MATRIX that has multiple rows, we need to
1697 * unroll it and call _mesa_add_state_reference() for each row
1698 */
1699 if ((state_tokens[0] == STATE_MATRIX)
1700 && (state_tokens[3] != state_tokens[4])) {
1701 GLint row;
1702 GLint first_row = state_tokens[3];
1703 GLint last_row = state_tokens[4];
1704
1705 for (row = first_row; row <= last_row; row++) {
1706 state_tokens[3] = state_tokens[4] = row;
1707
1708 idx =
1709 _mesa_add_state_reference (Program->Parameters,
1710 state_tokens);
1711 if (param_var->param_binding_begin == ~0U)
1712 param_var->param_binding_begin = idx;
1713 param_var->param_binding_length++;
1714 Program->Base.NumParameters++;
1715 }
1716 }
1717 else {
1718 idx =
1719 _mesa_add_state_reference (Program->Parameters, state_tokens);
1720 if (param_var->param_binding_begin == ~0U)
1721 param_var->param_binding_begin = idx;
1722 param_var->param_binding_length++;
1723 Program->Base.NumParameters++;
1724 }
1725 break;
1726
1727 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001728 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1729 return 1;
1730 idx = _mesa_add_state_reference (Program->Parameters, state_tokens);
1731 if (param_var->param_binding_begin == ~0U)
1732 param_var->param_binding_begin = idx;
1733 param_var->param_binding_length++;
1734 Program->Base.NumParameters++;
1735
1736 /* Check if there is more: 0 -> we're done, else its an integer */
1737 if (**inst) {
1738 GLuint out_of_range, new_idx;
1739 GLuint start_idx = state_tokens[2] + 1;
1740 GLuint end_idx = parse_integer (inst, Program);
1741
1742 out_of_range = 0;
1743 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1744 if (((state_tokens[1] == STATE_ENV)
1745 && (end_idx >= ctx->Const.MaxFragmentProgramEnvParams))
1746 || ((state_tokens[1] == STATE_LOCAL)
1747 && (end_idx >=
1748 ctx->Const.MaxFragmentProgramLocalParams)))
1749 out_of_range = 1;
1750 }
1751 else {
1752 if (((state_tokens[1] == STATE_ENV)
1753 && (end_idx >= ctx->Const.MaxVertexProgramEnvParams))
1754 || ((state_tokens[1] == STATE_LOCAL)
1755 && (end_idx >=
1756 ctx->Const.MaxVertexProgramLocalParams)))
1757 out_of_range = 1;
1758 }
1759 if (out_of_range) {
1760 _mesa_set_program_error (ctx, Program->Position,
1761 "Invalid Program Parameter");
1762 _mesa_error (ctx, GL_INVALID_OPERATION,
1763 "Invalid Program Parameter: %d", end_idx);
1764 return 1;
1765 }
1766
1767 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1768 state_tokens[2] = new_idx;
1769 idx =
1770 _mesa_add_state_reference (Program->Parameters,
1771 state_tokens);
1772 param_var->param_binding_length++;
1773 Program->Base.NumParameters++;
1774 }
1775 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001776 else {
1777 (*inst)++;
1778 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001779 break;
1780
1781 case PARAM_CONSTANT:
1782 parse_constant (inst, const_values, Program, use);
1783 idx =
1784 _mesa_add_named_constant (Program->Parameters,
1785 (char *) param_var->name, const_values);
1786 if (param_var->param_binding_begin == ~0U)
1787 param_var->param_binding_begin = idx;
1788 param_var->param_binding_length++;
1789 Program->Base.NumParameters++;
1790 break;
1791
1792 default:
Brian Paul7aebaf32005-10-30 21:23:23 +00001793 _mesa_set_program_error(ctx, Program->Position,
1794 "Unexpected token in parse_param_elements()");
1795 _mesa_error(ctx, GL_INVALID_OPERATION,
1796 "Unexpected token in parse_param_elements()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001797 return 1;
1798 }
1799
1800 /* Make sure we haven't blown past our parameter limits */
1801 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1802 (Program->Base.NumParameters >=
1803 ctx->Const.MaxVertexProgramLocalParams))
1804 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1805 && (Program->Base.NumParameters >=
1806 ctx->Const.MaxFragmentProgramLocalParams))) {
1807 _mesa_set_program_error (ctx, Program->Position,
1808 "Too many parameter variables");
1809 _mesa_error (ctx, GL_INVALID_OPERATION, "Too many parameter variables");
1810 return 1;
1811 }
1812
1813 return err;
1814}
1815
Brian Paul7aebaf32005-10-30 21:23:23 +00001816
Jouk Jansen40322e12004-04-05 08:50:36 +00001817/**
1818 * This picks out PARAM program parameter bindings.
1819 *
1820 * XXX: This needs to be stressed & tested
1821 *
1822 * \return 0 on sucess, 1 on error
1823 */
1824static GLuint
1825parse_param (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1826 struct arb_program *Program)
1827{
Brian Paulbd997cd2004-07-20 21:12:56 +00001828 GLuint found, err;
1829 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001830 struct var_cache *param_var;
1831
1832 err = 0;
1833 param_var = parse_string (inst, vc_head, Program, &found);
1834 Program->Position = parse_position (inst);
1835
1836 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001837 char *error_msg = (char *)
1838 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001839 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1840 param_var->name);
1841
1842 _mesa_set_program_error (ctx, Program->Position, error_msg);
1843 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1844
1845 _mesa_free (error_msg);
1846 return 1;
1847 }
1848
1849 specified_length = parse_integer (inst, Program);
1850
1851 if (specified_length < 0) {
1852 _mesa_set_program_error (ctx, Program->Position,
1853 "Negative parameter array length");
1854 _mesa_error (ctx, GL_INVALID_OPERATION,
1855 "Negative parameter array length: %d", specified_length);
1856 return 1;
1857 }
1858
1859 param_var->type = vt_param;
1860 param_var->param_binding_length = 0;
1861
1862 /* Right now, everything is shoved into the main state register file.
1863 *
1864 * In the future, it would be nice to leave things ENV/LOCAL params
1865 * in their respective register files, if possible
1866 */
1867 param_var->param_binding_type = PROGRAM_STATE_VAR;
1868
1869 /* Remember to:
1870 * * - add each guy to the parameter list
1871 * * - increment the param_var->param_binding_len
1872 * * - store the param_var->param_binding_begin for the first one
1873 * * - compare the actual len to the specified len at the end
1874 */
1875 while (**inst != PARAM_NULL) {
1876 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1877 return 1;
1878 }
1879
1880 /* Test array length here! */
1881 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001882 if (specified_length != (int)param_var->param_binding_length) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001883 const char *msg
1884 = "Declared parameter array length does not match parameter list";
1885 _mesa_set_program_error(ctx, Program->Position, msg);
1886 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001887 }
1888 }
1889
1890 (*inst)++;
1891
1892 return 0;
1893}
1894
1895/**
1896 *
1897 */
1898static GLuint
1899parse_param_use (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1900 struct arb_program *Program, struct var_cache **new_var)
1901{
1902 struct var_cache *param_var;
1903
1904 /* First, insert a dummy entry into the var_cache */
1905 var_cache_create (&param_var);
1906 param_var->name = (GLubyte *) _mesa_strdup (" ");
1907 param_var->type = vt_param;
1908
1909 param_var->param_binding_length = 0;
1910 /* Don't fill in binding_begin; We use the default value of -1
1911 * to tell if its already initialized, elsewhere.
1912 *
1913 * param_var->param_binding_begin = 0;
1914 */
1915 param_var->param_binding_type = PROGRAM_STATE_VAR;
1916
1917 var_cache_append (vc_head, param_var);
1918
1919 /* Then fill it with juicy parameter goodness */
1920 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1921 return 1;
1922
1923 *new_var = param_var;
1924
1925 return 0;
1926}
1927
1928
1929/**
1930 * This handles the declaration of TEMP variables
1931 *
1932 * \return 0 on sucess, 1 on error
1933 */
1934static GLuint
1935parse_temp (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1936 struct arb_program *Program)
1937{
1938 GLuint found;
1939 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001940
1941 while (**inst != 0) {
1942 temp_var = parse_string (inst, vc_head, Program, &found);
1943 Program->Position = parse_position (inst);
1944 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001945 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001946 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1947 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1948 temp_var->name);
1949
1950 _mesa_set_program_error (ctx, Program->Position, error_msg);
1951 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1952
1953 _mesa_free (error_msg);
1954 return 1;
1955 }
1956
1957 temp_var->type = vt_temp;
1958
1959 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1960 (Program->Base.NumTemporaries >=
1961 ctx->Const.MaxFragmentProgramTemps))
1962 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1963 && (Program->Base.NumTemporaries >=
1964 ctx->Const.MaxVertexProgramTemps))) {
1965 _mesa_set_program_error (ctx, Program->Position,
1966 "Too many TEMP variables declared");
1967 _mesa_error (ctx, GL_INVALID_OPERATION,
1968 "Too many TEMP variables declared");
1969 return 1;
1970 }
1971
1972 temp_var->temp_binding = Program->Base.NumTemporaries;
1973 Program->Base.NumTemporaries++;
1974 }
1975 (*inst)++;
1976
1977 return 0;
1978}
1979
1980/**
1981 * This handles variables of the OUTPUT variety
1982 *
1983 * \return 0 on sucess, 1 on error
1984 */
1985static GLuint
1986parse_output (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1987 struct arb_program *Program)
1988{
1989 GLuint found;
1990 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00001991 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00001992
1993 output_var = parse_string (inst, vc_head, Program, &found);
1994 Program->Position = parse_position (inst);
1995 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001996 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001997 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
1998 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1999 output_var->name);
2000
2001 _mesa_set_program_error (ctx, Program->Position, error_msg);
2002 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2003
2004 _mesa_free (error_msg);
2005 return 1;
2006 }
2007
2008 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002009
2010 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2011 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002012}
2013
2014/**
2015 * This handles variables of the ALIAS kind
2016 *
2017 * \return 0 on sucess, 1 on error
2018 */
2019static GLuint
2020parse_alias (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2021 struct arb_program *Program)
2022{
2023 GLuint found;
2024 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002025
2026 temp_var = parse_string (inst, vc_head, Program, &found);
2027 Program->Position = parse_position (inst);
2028
2029 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002030 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002031 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2032 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2033 temp_var->name);
2034
2035 _mesa_set_program_error (ctx, Program->Position, error_msg);
2036 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2037
2038 _mesa_free (error_msg);
2039 return 1;
2040 }
2041
2042 temp_var->type = vt_alias;
2043 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2044 Program->Position = parse_position (inst);
2045
2046 if (!found)
2047 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002048 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002049 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2050 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2051 temp_var->alias_binding->name);
2052
2053 _mesa_set_program_error (ctx, Program->Position, error_msg);
2054 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2055
2056 _mesa_free (error_msg);
2057 return 1;
2058 }
2059
2060 return 0;
2061}
2062
2063/**
2064 * This handles variables of the ADDRESS kind
2065 *
2066 * \return 0 on sucess, 1 on error
2067 */
2068static GLuint
2069parse_address (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2070 struct arb_program *Program)
2071{
2072 GLuint found;
2073 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002074
2075 while (**inst != 0) {
2076 temp_var = parse_string (inst, vc_head, Program, &found);
2077 Program->Position = parse_position (inst);
2078 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002079 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002080 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2081 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2082 temp_var->name);
2083
2084 _mesa_set_program_error (ctx, Program->Position, error_msg);
2085 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2086
2087 _mesa_free (error_msg);
2088 return 1;
2089 }
2090
2091 temp_var->type = vt_address;
2092
2093 if (Program->Base.NumAddressRegs >=
2094 ctx->Const.MaxVertexProgramAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002095 const char *msg = "Too many ADDRESS variables declared";
2096 _mesa_set_program_error(ctx, Program->Position, msg);
2097
2098 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002099 return 1;
2100 }
2101
2102 temp_var->address_binding = Program->Base.NumAddressRegs;
2103 Program->Base.NumAddressRegs++;
2104 }
2105 (*inst)++;
2106
2107 return 0;
2108}
2109
2110/**
2111 * Parse a program declaration
2112 *
2113 * \return 0 on sucess, 1 on error
2114 */
2115static GLint
2116parse_declaration (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2117 struct arb_program *Program)
2118{
2119 GLint err = 0;
2120
2121 switch (*(*inst)++) {
2122 case ADDRESS:
2123 err = parse_address (ctx, inst, vc_head, Program);
2124 break;
2125
2126 case ALIAS:
2127 err = parse_alias (ctx, inst, vc_head, Program);
2128 break;
2129
2130 case ATTRIB:
2131 err = parse_attrib (ctx, inst, vc_head, Program);
2132 break;
2133
2134 case OUTPUT:
2135 err = parse_output (ctx, inst, vc_head, Program);
2136 break;
2137
2138 case PARAM:
2139 err = parse_param (ctx, inst, vc_head, Program);
2140 break;
2141
2142 case TEMP:
2143 err = parse_temp (ctx, inst, vc_head, Program);
2144 break;
2145 }
2146
2147 return err;
2148}
2149
2150/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002151 * Handle the parsing out of a masked destination register, either for a
2152 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002153 *
2154 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002155 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002156 * position invariant
2157 *
2158 * \param File - The register file we write to
2159 * \param Index - The register index we write to
2160 * \param WriteMask - The mask controlling which components we write (1->write)
2161 *
2162 * \return 0 on sucess, 1 on error
2163 */
2164static GLuint
2165parse_masked_dst_reg (GLcontext * ctx, GLubyte ** inst,
2166 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002167 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002168{
Brian Paul7aebaf32005-10-30 21:23:23 +00002169 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002170 struct var_cache *dst;
2171
2172 /* We either have a result register specified, or a
2173 * variable that may or may not be writable
2174 */
2175 switch (*(*inst)++) {
2176 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002177 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002178 return 1;
2179 *File = PROGRAM_OUTPUT;
2180 break;
2181
2182 case REGISTER_ESTABLISHED_NAME:
2183 dst = parse_string (inst, vc_head, Program, &result);
2184 Program->Position = parse_position (inst);
2185
2186 /* If the name has never been added to our symbol table, we're hosed */
2187 if (!result) {
2188 _mesa_set_program_error (ctx, Program->Position,
2189 "0: Undefined variable");
2190 _mesa_error (ctx, GL_INVALID_OPERATION, "0: Undefined variable: %s",
2191 dst->name);
2192 return 1;
2193 }
2194
2195 switch (dst->type) {
2196 case vt_output:
2197 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002198 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002199 break;
2200
2201 case vt_temp:
2202 *File = PROGRAM_TEMPORARY;
2203 *Index = dst->temp_binding;
2204 break;
2205
2206 /* If the var type is not vt_output or vt_temp, no go */
2207 default:
2208 _mesa_set_program_error (ctx, Program->Position,
2209 "Destination register is read only");
2210 _mesa_error (ctx, GL_INVALID_OPERATION,
2211 "Destination register is read only: %s",
2212 dst->name);
2213 return 1;
2214 }
2215 break;
2216
2217 default:
2218 _mesa_set_program_error (ctx, Program->Position,
2219 "Unexpected opcode in parse_masked_dst_reg()");
2220 _mesa_error (ctx, GL_INVALID_OPERATION,
2221 "Unexpected opcode in parse_masked_dst_reg()");
2222 return 1;
2223 }
2224
2225
2226 /* Position invariance test */
2227 /* This test is done now in syntax portion - when position invariance OPTION
2228 is specified, "result.position" rule is disabled so there is no way
2229 to write the position
2230 */
2231 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2232 (*Index == 0)) {
2233 _mesa_set_program_error (ctx, Program->Position,
2234 "Vertex program specified position invariance and wrote vertex position");
2235 _mesa_error (ctx, GL_INVALID_OPERATION,
2236 "Vertex program specified position invariance and wrote vertex position");
2237 }*/
2238
2239 /* And then the mask.
2240 * w,a -> bit 0
2241 * z,b -> bit 1
2242 * y,g -> bit 2
2243 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002244 *
2245 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002246 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002247 tmp = (GLint) *(*inst)++;
2248 *WriteMask = (((tmp>>3) & 0x1) |
2249 ((tmp>>1) & 0x2) |
2250 ((tmp<<1) & 0x4) |
2251 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002252
2253 return 0;
2254}
2255
2256
2257/**
2258 * Handle the parsing of a address register
2259 *
2260 * \param Index - The register index we write to
2261 *
2262 * \return 0 on sucess, 1 on error
2263 */
2264static GLuint
2265parse_address_reg (GLcontext * ctx, GLubyte ** inst,
2266 struct var_cache **vc_head,
2267 struct arb_program *Program, GLint * Index)
2268{
2269 struct var_cache *dst;
2270 GLuint result;
Brian Paula6c423d2004-08-25 15:59:48 +00002271 (void) Index;
Jouk Jansen40322e12004-04-05 08:50:36 +00002272
2273 dst = parse_string (inst, vc_head, Program, &result);
2274 Program->Position = parse_position (inst);
2275
2276 /* If the name has never been added to our symbol table, we're hosed */
2277 if (!result) {
2278 _mesa_set_program_error (ctx, Program->Position, "Undefined variable");
2279 _mesa_error (ctx, GL_INVALID_OPERATION, "Undefined variable: %s",
2280 dst->name);
2281 return 1;
2282 }
2283
2284 if (dst->type != vt_address) {
2285 _mesa_set_program_error (ctx, Program->Position,
2286 "Variable is not of type ADDRESS");
2287 _mesa_error (ctx, GL_INVALID_OPERATION,
2288 "Variable: %s is not of type ADDRESS", dst->name);
2289 return 1;
2290 }
2291
2292 return 0;
2293}
2294
Brian Paul8e8fa632005-07-01 02:03:33 +00002295#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002296/**
2297 * Handle the parsing out of a masked address register
2298 *
2299 * \param Index - The register index we write to
2300 * \param WriteMask - The mask controlling which components we write (1->write)
2301 *
2302 * \return 0 on sucess, 1 on error
2303 */
2304static GLuint
2305parse_masked_address_reg (GLcontext * ctx, GLubyte ** inst,
2306 struct var_cache **vc_head,
2307 struct arb_program *Program, GLint * Index,
2308 GLboolean * WriteMask)
2309{
2310 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2311 return 1;
2312
2313 /* This should be 0x8 */
2314 (*inst)++;
2315
2316 /* Writemask of .x is implied */
2317 WriteMask[0] = 1;
2318 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2319
2320 return 0;
2321}
Brian Paul8e8fa632005-07-01 02:03:33 +00002322#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002323
2324/**
2325 * Parse out a swizzle mask.
2326 *
Brian Paul32df89e2005-10-29 18:26:43 +00002327 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002328 *
2329 * The len parameter allows us to grab 4 components for a vector
2330 * swizzle, or just 1 component for a scalar src register selection
2331 */
Brian Paul32df89e2005-10-29 18:26:43 +00002332static void
2333parse_swizzle_mask(GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002334{
Brian Paul32df89e2005-10-29 18:26:43 +00002335 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002336
Brian Paul32df89e2005-10-29 18:26:43 +00002337 for (i = 0; i < 4; i++)
2338 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002339
Brian Paul32df89e2005-10-29 18:26:43 +00002340 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002341 switch (*(*inst)++) {
2342 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002343 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002344 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002345 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002346 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002347 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002348 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002349 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002350 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002351 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002352 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002353 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002354 default:
2355 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2356 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002357 }
2358 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002359}
2360
Jouk Jansen40322e12004-04-05 08:50:36 +00002361
Brian Paul32df89e2005-10-29 18:26:43 +00002362/**
2363 * Parse an extended swizzle mask which is a sequence of
2364 * four x/y/z/w/0/1 tokens.
2365 * \return swizzle four swizzle values
2366 * \return negateMask four element bitfield
2367 */
2368static void
2369parse_extended_swizzle_mask(GLubyte **inst, GLubyte swizzle[4],
2370 GLubyte *negateMask)
2371{
2372 GLint i;
2373
2374 *negateMask = 0x0;
2375 for (i = 0; i < 4; i++) {
2376 GLubyte swz;
2377 if (parse_sign(inst) == -1)
2378 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002379
2380 swz = *(*inst)++;
2381
2382 switch (swz) {
2383 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002384 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002385 break;
2386 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002387 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002388 break;
2389 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002390 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002391 break;
2392 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002393 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002394 break;
2395 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002396 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002397 break;
2398 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002399 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002400 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002401 default:
2402 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2403 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002404 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002405 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002406}
2407
2408
2409static GLuint
2410parse_src_reg (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002411 struct arb_program *Program,
2412 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002413 GLboolean *IsRelOffset )
2414{
2415 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002416 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002417 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002418
Keith Whitwell7c26b612005-04-21 14:46:57 +00002419 *IsRelOffset = 0;
2420
Jouk Jansen40322e12004-04-05 08:50:36 +00002421 /* And the binding for the src */
2422 switch (*(*inst)++) {
2423 case REGISTER_ATTRIB:
2424 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002425 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002426 return 1;
2427 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002428 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002429
2430 /* We need to insert a dummy variable into the var_cache so we can
2431 * catch generic vertex attrib aliasing errors
2432 */
2433 var_cache_create(&src);
2434 src->type = vt_attrib;
2435 src->name = (GLubyte *)_mesa_strdup("Dummy Attrib Variable");
Brian Paul18e7c5c2005-10-30 21:46:00 +00002436 src->attrib_binding = binding;
2437 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002438 var_cache_append(vc_head, src);
2439 if (generic_attrib_check(*vc_head)) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002440 const char *msg = "Cannot use both a generic vertex attribute "
2441 "and a specific attribute of the same type";
2442 _mesa_set_program_error (ctx, Program->Position, msg);
2443 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002444 return 1;
2445 }
2446 break;
2447
2448 case REGISTER_PARAM:
2449 switch (**inst) {
2450 case PARAM_ARRAY_ELEMENT:
2451 (*inst)++;
2452 src = parse_string (inst, vc_head, Program, &found);
2453 Program->Position = parse_position (inst);
2454
2455 if (!found) {
2456 _mesa_set_program_error (ctx, Program->Position,
2457 "2: Undefined variable");
2458 _mesa_error (ctx, GL_INVALID_OPERATION,
2459 "2: Undefined variable: %s", src->name);
2460 return 1;
2461 }
2462
2463 *File = src->param_binding_type;
2464
2465 switch (*(*inst)++) {
2466 case ARRAY_INDEX_ABSOLUTE:
2467 offset = parse_integer (inst, Program);
2468
2469 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002470 || (offset >= (int)src->param_binding_length)) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002471 _mesa_set_program_error (ctx, Program->Position,
2472 "Index out of range");
2473 _mesa_error (ctx, GL_INVALID_OPERATION,
2474 "Index %d out of range for %s", offset,
2475 src->name);
2476 return 1;
2477 }
2478
2479 *Index = src->param_binding_begin + offset;
2480 break;
2481
2482 case ARRAY_INDEX_RELATIVE:
2483 {
2484 GLint addr_reg_idx, rel_off;
2485
2486 /* First, grab the address regiseter */
2487 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2488 return 1;
2489
2490 /* And the .x */
2491 ((*inst)++);
2492 ((*inst)++);
2493 ((*inst)++);
2494 ((*inst)++);
2495
2496 /* Then the relative offset */
2497 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2498
2499 /* And store it properly */
2500 *Index = src->param_binding_begin + rel_off;
2501 *IsRelOffset = 1;
2502 }
2503 break;
2504 }
2505 break;
2506
2507 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002508 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2509 return 1;
2510
2511 *File = src->param_binding_type;
2512 *Index = src->param_binding_begin;
2513 break;
2514 }
2515 break;
2516
2517 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002518 src = parse_string (inst, vc_head, Program, &found);
2519 Program->Position = parse_position (inst);
2520
2521 /* If the name has never been added to our symbol table, we're hosed */
2522 if (!found) {
2523 _mesa_set_program_error (ctx, Program->Position,
2524 "3: Undefined variable");
2525 _mesa_error (ctx, GL_INVALID_OPERATION, "3: Undefined variable: %s",
2526 src->name);
2527 return 1;
2528 }
2529
2530 switch (src->type) {
2531 case vt_attrib:
2532 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002533 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002534 break;
2535
2536 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2537 case vt_param:
2538 *File = src->param_binding_type;
2539 *Index = src->param_binding_begin;
2540 break;
2541
2542 case vt_temp:
2543 *File = PROGRAM_TEMPORARY;
2544 *Index = src->temp_binding;
2545 break;
2546
2547 /* If the var type is vt_output no go */
2548 default:
2549 _mesa_set_program_error (ctx, Program->Position,
2550 "destination register is read only");
2551 _mesa_error (ctx, GL_INVALID_OPERATION,
2552 "destination register is read only: %s",
2553 src->name);
2554 return 1;
2555 }
2556 break;
2557
2558 default:
2559 _mesa_set_program_error (ctx, Program->Position,
2560 "Unknown token in parse_src_reg");
2561 _mesa_error (ctx, GL_INVALID_OPERATION,
2562 "Unknown token in parse_src_reg");
2563 return 1;
2564 }
2565
2566 return 0;
2567}
2568
2569/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002570 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002571 */
2572static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00002573parse_fp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
2574 struct var_cache **vc_head,
2575 struct arb_program *program,
2576 struct fp_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002577{
Brian Paul7aebaf32005-10-30 21:23:23 +00002578 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002579 GLint index;
2580 GLboolean negate;
2581 GLubyte swizzle[4];
2582 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002583
Jouk Jansen40322e12004-04-05 08:50:36 +00002584 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002585 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002586
2587 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002588 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002589 return 1;
2590
2591 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002592 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002593
Brian Paul32df89e2005-10-29 18:26:43 +00002594 reg->File = file;
2595 reg->Index = index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002596 reg->Abs = 0; /* NV only */
2597 reg->NegateAbs = 0; /* NV only */
Brian Paul32df89e2005-10-29 18:26:43 +00002598 reg->NegateBase = negate;
2599 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002600 return 0;
2601}
2602
Jouk Jansen40322e12004-04-05 08:50:36 +00002603
Keith Whitwell7c26b612005-04-21 14:46:57 +00002604static GLuint
2605parse_fp_dst_reg(GLcontext * ctx, GLubyte ** inst,
2606 struct var_cache **vc_head, struct arb_program *Program,
2607 struct fp_dst_register *reg )
2608{
Brian Paul7aebaf32005-10-30 21:23:23 +00002609 GLint mask;
2610 GLuint idx;
2611 enum register_file file;
2612
Keith Whitwell7c26b612005-04-21 14:46:57 +00002613 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002614 return 1;
2615
Keith Whitwell7c26b612005-04-21 14:46:57 +00002616 reg->CondMask = 0; /* NV only */
2617 reg->CondSwizzle = 0; /* NV only */
2618 reg->File = file;
2619 reg->Index = idx;
2620 reg->WriteMask = mask;
2621 return 0;
2622}
2623
2624
Brian Paul7aebaf32005-10-30 21:23:23 +00002625/**
2626 * Parse fragment program scalar src register.
2627 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002628static GLuint
2629parse_fp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002630 struct var_cache **vc_head,
2631 struct arb_program *Program,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002632 struct fp_src_register *reg )
2633{
Brian Paul7aebaf32005-10-30 21:23:23 +00002634 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002635 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002636 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002637 GLubyte Swizzle[4];
2638 GLboolean IsRelOffset;
2639
2640 /* Grab the sign */
2641 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2642
2643 /* And the src reg */
2644 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2645 return 1;
2646
2647 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002648 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002649
Keith Whitwell7c26b612005-04-21 14:46:57 +00002650 reg->File = File;
2651 reg->Index = Index;
2652 reg->Abs = 0; /* NV only */
2653 reg->NegateAbs = 0; /* NV only */
2654 reg->NegateBase = Negate;
2655 reg->Swizzle = (Swizzle[0] << 0);
2656
Jouk Jansen40322e12004-04-05 08:50:36 +00002657 return 0;
2658}
2659
Keith Whitwell7c26b612005-04-21 14:46:57 +00002660
Jouk Jansen40322e12004-04-05 08:50:36 +00002661/**
2662 * This is a big mother that handles getting opcodes into the instruction
2663 * and handling the src & dst registers for fragment program instructions
2664 */
2665static GLuint
2666parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2667 struct var_cache **vc_head, struct arb_program *Program,
2668 struct fp_instruction *fp)
2669{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002670 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002671 GLuint texcoord;
2672 GLubyte instClass, type, code;
2673 GLboolean rel;
2674
Brian Paul7aebaf32005-10-30 21:23:23 +00002675 _mesa_init_fp_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002676
2677 /* Record the position in the program string for debugging */
2678 fp->StringPos = Program->Position;
2679
2680 /* OP_ALU_INST or OP_TEX_INST */
2681 instClass = *(*inst)++;
2682
2683 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2684 * OP_TEX_{SAMPLE, KIL}
2685 */
2686 type = *(*inst)++;
2687
2688 /* The actual opcode name */
2689 code = *(*inst)++;
2690
2691 /* Increment the correct count */
2692 switch (instClass) {
2693 case OP_ALU_INST:
2694 Program->NumAluInstructions++;
2695 break;
2696 case OP_TEX_INST:
2697 Program->NumTexInstructions++;
2698 break;
2699 }
2700
Jouk Jansen40322e12004-04-05 08:50:36 +00002701 switch (type) {
2702 case OP_ALU_VECTOR:
2703 switch (code) {
2704 case OP_ABS_SAT:
2705 fp->Saturate = 1;
2706 case OP_ABS:
2707 fp->Opcode = FP_OPCODE_ABS;
2708 break;
2709
2710 case OP_FLR_SAT:
2711 fp->Saturate = 1;
2712 case OP_FLR:
2713 fp->Opcode = FP_OPCODE_FLR;
2714 break;
2715
2716 case OP_FRC_SAT:
2717 fp->Saturate = 1;
2718 case OP_FRC:
2719 fp->Opcode = FP_OPCODE_FRC;
2720 break;
2721
2722 case OP_LIT_SAT:
2723 fp->Saturate = 1;
2724 case OP_LIT:
2725 fp->Opcode = FP_OPCODE_LIT;
2726 break;
2727
2728 case OP_MOV_SAT:
2729 fp->Saturate = 1;
2730 case OP_MOV:
2731 fp->Opcode = FP_OPCODE_MOV;
2732 break;
2733 }
2734
Keith Whitwell7c26b612005-04-21 14:46:57 +00002735 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002736 return 1;
2737
Keith Whitwell7c26b612005-04-21 14:46:57 +00002738 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002739 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002740 break;
2741
2742 case OP_ALU_SCALAR:
2743 switch (code) {
2744 case OP_COS_SAT:
2745 fp->Saturate = 1;
2746 case OP_COS:
2747 fp->Opcode = FP_OPCODE_COS;
2748 break;
2749
2750 case OP_EX2_SAT:
2751 fp->Saturate = 1;
2752 case OP_EX2:
2753 fp->Opcode = FP_OPCODE_EX2;
2754 break;
2755
2756 case OP_LG2_SAT:
2757 fp->Saturate = 1;
2758 case OP_LG2:
2759 fp->Opcode = FP_OPCODE_LG2;
2760 break;
2761
2762 case OP_RCP_SAT:
2763 fp->Saturate = 1;
2764 case OP_RCP:
2765 fp->Opcode = FP_OPCODE_RCP;
2766 break;
2767
2768 case OP_RSQ_SAT:
2769 fp->Saturate = 1;
2770 case OP_RSQ:
2771 fp->Opcode = FP_OPCODE_RSQ;
2772 break;
2773
2774 case OP_SIN_SAT:
2775 fp->Saturate = 1;
2776 case OP_SIN:
2777 fp->Opcode = FP_OPCODE_SIN;
2778 break;
2779
2780 case OP_SCS_SAT:
2781 fp->Saturate = 1;
2782 case OP_SCS:
2783
2784 fp->Opcode = FP_OPCODE_SCS;
2785 break;
2786 }
2787
Keith Whitwell7c26b612005-04-21 14:46:57 +00002788 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002789 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002790
2791 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002792 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 break;
2794
2795 case OP_ALU_BINSC:
2796 switch (code) {
2797 case OP_POW_SAT:
2798 fp->Saturate = 1;
2799 case OP_POW:
2800 fp->Opcode = FP_OPCODE_POW;
2801 break;
2802 }
2803
Keith Whitwell7c26b612005-04-21 14:46:57 +00002804 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002805 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002806
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002808 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002809 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002810 }
2811 break;
2812
2813
2814 case OP_ALU_BIN:
2815 switch (code) {
2816 case OP_ADD_SAT:
2817 fp->Saturate = 1;
2818 case OP_ADD:
2819 fp->Opcode = FP_OPCODE_ADD;
2820 break;
2821
2822 case OP_DP3_SAT:
2823 fp->Saturate = 1;
2824 case OP_DP3:
2825 fp->Opcode = FP_OPCODE_DP3;
2826 break;
2827
2828 case OP_DP4_SAT:
2829 fp->Saturate = 1;
2830 case OP_DP4:
2831 fp->Opcode = FP_OPCODE_DP4;
2832 break;
2833
2834 case OP_DPH_SAT:
2835 fp->Saturate = 1;
2836 case OP_DPH:
2837 fp->Opcode = FP_OPCODE_DPH;
2838 break;
2839
2840 case OP_DST_SAT:
2841 fp->Saturate = 1;
2842 case OP_DST:
2843 fp->Opcode = FP_OPCODE_DST;
2844 break;
2845
2846 case OP_MAX_SAT:
2847 fp->Saturate = 1;
2848 case OP_MAX:
2849 fp->Opcode = FP_OPCODE_MAX;
2850 break;
2851
2852 case OP_MIN_SAT:
2853 fp->Saturate = 1;
2854 case OP_MIN:
2855 fp->Opcode = FP_OPCODE_MIN;
2856 break;
2857
2858 case OP_MUL_SAT:
2859 fp->Saturate = 1;
2860 case OP_MUL:
2861 fp->Opcode = FP_OPCODE_MUL;
2862 break;
2863
2864 case OP_SGE_SAT:
2865 fp->Saturate = 1;
2866 case OP_SGE:
2867 fp->Opcode = FP_OPCODE_SGE;
2868 break;
2869
2870 case OP_SLT_SAT:
2871 fp->Saturate = 1;
2872 case OP_SLT:
2873 fp->Opcode = FP_OPCODE_SLT;
2874 break;
2875
2876 case OP_SUB_SAT:
2877 fp->Saturate = 1;
2878 case OP_SUB:
2879 fp->Opcode = FP_OPCODE_SUB;
2880 break;
2881
2882 case OP_XPD_SAT:
2883 fp->Saturate = 1;
2884 case OP_XPD:
2885 fp->Opcode = FP_OPCODE_XPD;
2886 break;
2887 }
2888
Keith Whitwell7c26b612005-04-21 14:46:57 +00002889 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002890 return 1;
2891 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002892 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2893 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002894 }
2895 break;
2896
2897 case OP_ALU_TRI:
2898 switch (code) {
2899 case OP_CMP_SAT:
2900 fp->Saturate = 1;
2901 case OP_CMP:
2902 fp->Opcode = FP_OPCODE_CMP;
2903 break;
2904
2905 case OP_LRP_SAT:
2906 fp->Saturate = 1;
2907 case OP_LRP:
2908 fp->Opcode = FP_OPCODE_LRP;
2909 break;
2910
2911 case OP_MAD_SAT:
2912 fp->Saturate = 1;
2913 case OP_MAD:
2914 fp->Opcode = FP_OPCODE_MAD;
2915 break;
2916 }
2917
Keith Whitwell7c26b612005-04-21 14:46:57 +00002918 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002919 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002920
Jouk Jansen40322e12004-04-05 08:50:36 +00002921 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002922 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2923 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002924 }
2925 break;
2926
2927 case OP_ALU_SWZ:
2928 switch (code) {
2929 case OP_SWZ_SAT:
2930 fp->Saturate = 1;
2931 case OP_SWZ:
2932 fp->Opcode = FP_OPCODE_SWZ;
2933 break;
2934 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002935 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 return 1;
2937
Keith Whitwell7c26b612005-04-21 14:46:57 +00002938 {
Brian Paul32df89e2005-10-29 18:26:43 +00002939 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002940 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002941 enum register_file file;
2942 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002943
Brian Paul32df89e2005-10-29 18:26:43 +00002944 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002945 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002946 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2947 fp->SrcReg[0].File = file;
2948 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002949 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002950 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2951 swizzle[1],
2952 swizzle[2],
2953 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002954 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002955 break;
2956
2957 case OP_TEX_SAMPLE:
2958 switch (code) {
2959 case OP_TEX_SAT:
2960 fp->Saturate = 1;
2961 case OP_TEX:
2962 fp->Opcode = FP_OPCODE_TEX;
2963 break;
2964
2965 case OP_TXP_SAT:
2966 fp->Saturate = 1;
2967 case OP_TXP:
2968 fp->Opcode = FP_OPCODE_TXP;
2969 break;
2970
2971 case OP_TXB_SAT:
Jouk Jansen40322e12004-04-05 08:50:36 +00002972 fp->Saturate = 1;
2973 case OP_TXB:
2974 fp->Opcode = FP_OPCODE_TXB;
2975 break;
2976 }
2977
Keith Whitwell7c26b612005-04-21 14:46:57 +00002978 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002979 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002980
2981 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002982 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002983
2984 /* texImageUnit */
2985 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2986 return 1;
2987 fp->TexSrcUnit = texcoord;
2988
2989 /* texTarget */
2990 switch (*(*inst)++) {
2991 case TEXTARGET_1D:
Keith Whitwell7c26b612005-04-21 14:46:57 +00002992 fp->TexSrcIdx = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002993 break;
2994 case TEXTARGET_2D:
Keith Whitwell7c26b612005-04-21 14:46:57 +00002995 fp->TexSrcIdx = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002996 break;
2997 case TEXTARGET_3D:
Keith Whitwell7c26b612005-04-21 14:46:57 +00002998 fp->TexSrcIdx = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002999 break;
3000 case TEXTARGET_RECT:
Keith Whitwell7c26b612005-04-21 14:46:57 +00003001 fp->TexSrcIdx = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003002 break;
3003 case TEXTARGET_CUBE:
Keith Whitwell7c26b612005-04-21 14:46:57 +00003004 fp->TexSrcIdx = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003005 break;
3006 case TEXTARGET_SHADOW1D:
3007 case TEXTARGET_SHADOW2D:
3008 case TEXTARGET_SHADOWRECT:
3009 /* TODO ARB_fragment_program_shadow code */
3010 break;
3011 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003012 Program->TexturesUsed[texcoord] |= (1<<fp->TexSrcIdx);
Jouk Jansen40322e12004-04-05 08:50:36 +00003013 break;
3014
3015 case OP_TEX_KIL:
Keith Whitwell7c26b612005-04-21 14:46:57 +00003016 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003017 return 1;
Keith Whitwell219f3c42005-07-01 17:12:29 +00003018 fp->Opcode = FP_OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003019 break;
3020 }
3021
3022 return 0;
3023}
3024
Keith Whitwell7c26b612005-04-21 14:46:57 +00003025static GLuint
3026parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3027 struct var_cache **vc_head, struct arb_program *Program,
3028 struct vp_dst_register *reg )
3029{
Brian Paul7aebaf32005-10-30 21:23:23 +00003030 GLint mask;
3031 GLuint idx;
3032 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003033
3034 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3035 return 1;
3036
3037 reg->File = file;
3038 reg->Index = idx;
3039 reg->WriteMask = mask;
3040 return 0;
3041}
3042
3043/**
3044 * Handle the parsing out of a masked address register
3045 *
3046 * \param Index - The register index we write to
3047 * \param WriteMask - The mask controlling which components we write (1->write)
3048 *
3049 * \return 0 on sucess, 1 on error
3050 */
3051static GLuint
3052parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3053 struct var_cache **vc_head,
3054 struct arb_program *Program,
3055 struct vp_dst_register *reg)
3056{
3057 GLint idx;
3058
3059 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3060 return 1;
3061
3062 /* This should be 0x8 */
3063 (*inst)++;
3064
3065 reg->File = PROGRAM_ADDRESS;
3066 reg->Index = idx;
3067
3068 /* Writemask of .x is implied */
3069 reg->WriteMask = 0x1;
3070 return 0;
3071}
3072
3073/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003074 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003075 */
3076static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003077parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3078 struct var_cache **vc_head,
3079 struct arb_program *program,
3080 struct vp_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003081{
Brian Paul7aebaf32005-10-30 21:23:23 +00003082 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003083 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003084 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003085 GLubyte swizzle[4];
3086 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003087
3088 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003089 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003090
3091 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003092 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003093 return 1;
3094
3095 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003096 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003097
Brian Paul32df89e2005-10-29 18:26:43 +00003098 reg->File = file;
3099 reg->Index = index;
3100 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3101 swizzle[2], swizzle[3]);
Brian Paul7aebaf32005-10-30 21:23:23 +00003102 reg->Negate = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003103 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003104 return 0;
3105}
3106
3107
3108static GLuint
3109parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003110 struct var_cache **vc_head,
3111 struct arb_program *Program,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003112 struct vp_src_register *reg )
3113{
Brian Paul7aebaf32005-10-30 21:23:23 +00003114 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003115 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003116 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003117 GLubyte Swizzle[4];
3118 GLboolean IsRelOffset;
3119
3120 /* Grab the sign */
3121 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3122
3123 /* And the src reg */
3124 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3125 return 1;
3126
3127 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003128 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003129
3130 reg->File = File;
3131 reg->Index = Index;
3132 reg->Swizzle = (Swizzle[0] << 0);
3133 reg->Negate = Negate;
3134 reg->RelAddr = IsRelOffset;
3135 return 0;
3136}
3137
3138
Jouk Jansen40322e12004-04-05 08:50:36 +00003139/**
3140 * This is a big mother that handles getting opcodes into the instruction
3141 * and handling the src & dst registers for vertex program instructions
3142 */
3143static GLuint
3144parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3145 struct var_cache **vc_head, struct arb_program *Program,
3146 struct vp_instruction *vp)
3147{
3148 GLint a;
3149 GLubyte type, code;
3150
3151 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3152 type = *(*inst)++;
3153
3154 /* The actual opcode name */
3155 code = *(*inst)++;
3156
Brian Paul7aebaf32005-10-30 21:23:23 +00003157 _mesa_init_vp_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003158 /* Record the position in the program string for debugging */
3159 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003160
3161 switch (type) {
3162 /* XXX: */
3163 case OP_ALU_ARL:
3164 vp->Opcode = VP_OPCODE_ARL;
3165
3166 /* Remember to set SrcReg.RelAddr; */
3167
3168 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003169 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003170 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003171
Jouk Jansen40322e12004-04-05 08:50:36 +00003172 vp->DstReg.File = PROGRAM_ADDRESS;
3173
3174 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003175 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003176 return 1;
3177
3178 break;
3179
3180 case OP_ALU_VECTOR:
3181 switch (code) {
3182 case OP_ABS:
3183 vp->Opcode = VP_OPCODE_ABS;
3184 break;
3185 case OP_FLR:
3186 vp->Opcode = VP_OPCODE_FLR;
3187 break;
3188 case OP_FRC:
3189 vp->Opcode = VP_OPCODE_FRC;
3190 break;
3191 case OP_LIT:
3192 vp->Opcode = VP_OPCODE_LIT;
3193 break;
3194 case OP_MOV:
3195 vp->Opcode = VP_OPCODE_MOV;
3196 break;
3197 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003198
3199 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003200 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003201
3202 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003203 return 1;
3204 break;
3205
3206 case OP_ALU_SCALAR:
3207 switch (code) {
3208 case OP_EX2:
3209 vp->Opcode = VP_OPCODE_EX2;
3210 break;
3211 case OP_EXP:
3212 vp->Opcode = VP_OPCODE_EXP;
3213 break;
3214 case OP_LG2:
3215 vp->Opcode = VP_OPCODE_LG2;
3216 break;
3217 case OP_LOG:
3218 vp->Opcode = VP_OPCODE_LOG;
3219 break;
3220 case OP_RCP:
3221 vp->Opcode = VP_OPCODE_RCP;
3222 break;
3223 case OP_RSQ:
3224 vp->Opcode = VP_OPCODE_RSQ;
3225 break;
3226 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003227 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003229
3230 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003231 return 1;
3232 break;
3233
3234 case OP_ALU_BINSC:
3235 switch (code) {
3236 case OP_POW:
3237 vp->Opcode = VP_OPCODE_POW;
3238 break;
3239 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003240 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003241 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003242
Jouk Jansen40322e12004-04-05 08:50:36 +00003243 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003244 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 return 1;
3246 }
3247 break;
3248
3249 case OP_ALU_BIN:
3250 switch (code) {
3251 case OP_ADD:
3252 vp->Opcode = VP_OPCODE_ADD;
3253 break;
3254 case OP_DP3:
3255 vp->Opcode = VP_OPCODE_DP3;
3256 break;
3257 case OP_DP4:
3258 vp->Opcode = VP_OPCODE_DP4;
3259 break;
3260 case OP_DPH:
3261 vp->Opcode = VP_OPCODE_DPH;
3262 break;
3263 case OP_DST:
3264 vp->Opcode = VP_OPCODE_DST;
3265 break;
3266 case OP_MAX:
3267 vp->Opcode = VP_OPCODE_MAX;
3268 break;
3269 case OP_MIN:
3270 vp->Opcode = VP_OPCODE_MIN;
3271 break;
3272 case OP_MUL:
3273 vp->Opcode = VP_OPCODE_MUL;
3274 break;
3275 case OP_SGE:
3276 vp->Opcode = VP_OPCODE_SGE;
3277 break;
3278 case OP_SLT:
3279 vp->Opcode = VP_OPCODE_SLT;
3280 break;
3281 case OP_SUB:
3282 vp->Opcode = VP_OPCODE_SUB;
3283 break;
3284 case OP_XPD:
3285 vp->Opcode = VP_OPCODE_XPD;
3286 break;
3287 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003288 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003289 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003290
Jouk Jansen40322e12004-04-05 08:50:36 +00003291 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003292 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003293 return 1;
3294 }
3295 break;
3296
3297 case OP_ALU_TRI:
3298 switch (code) {
3299 case OP_MAD:
3300 vp->Opcode = VP_OPCODE_MAD;
3301 break;
3302 }
3303
Keith Whitwell7c26b612005-04-21 14:46:57 +00003304 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003305 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003306
Jouk Jansen40322e12004-04-05 08:50:36 +00003307 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003308 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 return 1;
3310 }
3311 break;
3312
3313 case OP_ALU_SWZ:
3314 switch (code) {
3315 case OP_SWZ:
3316 vp->Opcode = VP_OPCODE_SWZ;
3317 break;
3318 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003319 {
Brian Paul32df89e2005-10-29 18:26:43 +00003320 GLubyte swizzle[4];
3321 GLubyte negateMask;
3322 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003323 enum register_file file;
3324 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003325
Keith Whitwell7c26b612005-04-21 14:46:57 +00003326 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3327 return 1;
3328
Brian Paul32df89e2005-10-29 18:26:43 +00003329 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003330 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003331 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3332 vp->SrcReg[0].File = file;
3333 vp->SrcReg[0].Index = index;
3334 vp->SrcReg[0].Negate = negateMask;
3335 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3336 swizzle[1],
3337 swizzle[2],
3338 swizzle[3]);
3339 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003340 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003341 break;
3342 }
3343 return 0;
3344}
3345
3346#if DEBUG_PARSING
3347
3348static GLvoid
3349print_state_token (GLint token)
3350{
3351 switch (token) {
3352 case STATE_MATERIAL:
3353 fprintf (stderr, "STATE_MATERIAL ");
3354 break;
3355 case STATE_LIGHT:
3356 fprintf (stderr, "STATE_LIGHT ");
3357 break;
3358
3359 case STATE_LIGHTMODEL_AMBIENT:
3360 fprintf (stderr, "STATE_AMBIENT ");
3361 break;
3362
3363 case STATE_LIGHTMODEL_SCENECOLOR:
3364 fprintf (stderr, "STATE_SCENECOLOR ");
3365 break;
3366
3367 case STATE_LIGHTPROD:
3368 fprintf (stderr, "STATE_LIGHTPROD ");
3369 break;
3370
3371 case STATE_TEXGEN:
3372 fprintf (stderr, "STATE_TEXGEN ");
3373 break;
3374
3375 case STATE_FOG_COLOR:
3376 fprintf (stderr, "STATE_FOG_COLOR ");
3377 break;
3378
3379 case STATE_FOG_PARAMS:
3380 fprintf (stderr, "STATE_FOG_PARAMS ");
3381 break;
3382
3383 case STATE_CLIPPLANE:
3384 fprintf (stderr, "STATE_CLIPPLANE ");
3385 break;
3386
3387 case STATE_POINT_SIZE:
3388 fprintf (stderr, "STATE_POINT_SIZE ");
3389 break;
3390
3391 case STATE_POINT_ATTENUATION:
3392 fprintf (stderr, "STATE_ATTENUATION ");
3393 break;
3394
3395 case STATE_MATRIX:
3396 fprintf (stderr, "STATE_MATRIX ");
3397 break;
3398
3399 case STATE_MODELVIEW:
3400 fprintf (stderr, "STATE_MODELVIEW ");
3401 break;
3402
3403 case STATE_PROJECTION:
3404 fprintf (stderr, "STATE_PROJECTION ");
3405 break;
3406
3407 case STATE_MVP:
3408 fprintf (stderr, "STATE_MVP ");
3409 break;
3410
3411 case STATE_TEXTURE:
3412 fprintf (stderr, "STATE_TEXTURE ");
3413 break;
3414
3415 case STATE_PROGRAM:
3416 fprintf (stderr, "STATE_PROGRAM ");
3417 break;
3418
3419 case STATE_MATRIX_INVERSE:
3420 fprintf (stderr, "STATE_INVERSE ");
3421 break;
3422
3423 case STATE_MATRIX_TRANSPOSE:
3424 fprintf (stderr, "STATE_TRANSPOSE ");
3425 break;
3426
3427 case STATE_MATRIX_INVTRANS:
3428 fprintf (stderr, "STATE_INVTRANS ");
3429 break;
3430
3431 case STATE_AMBIENT:
3432 fprintf (stderr, "STATE_AMBIENT ");
3433 break;
3434
3435 case STATE_DIFFUSE:
3436 fprintf (stderr, "STATE_DIFFUSE ");
3437 break;
3438
3439 case STATE_SPECULAR:
3440 fprintf (stderr, "STATE_SPECULAR ");
3441 break;
3442
3443 case STATE_EMISSION:
3444 fprintf (stderr, "STATE_EMISSION ");
3445 break;
3446
3447 case STATE_SHININESS:
3448 fprintf (stderr, "STATE_SHININESS ");
3449 break;
3450
3451 case STATE_HALF:
3452 fprintf (stderr, "STATE_HALF ");
3453 break;
3454
3455 case STATE_POSITION:
3456 fprintf (stderr, "STATE_POSITION ");
3457 break;
3458
3459 case STATE_ATTENUATION:
3460 fprintf (stderr, "STATE_ATTENUATION ");
3461 break;
3462
3463 case STATE_SPOT_DIRECTION:
3464 fprintf (stderr, "STATE_DIRECTION ");
3465 break;
3466
3467 case STATE_TEXGEN_EYE_S:
3468 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3469 break;
3470
3471 case STATE_TEXGEN_EYE_T:
3472 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3473 break;
3474
3475 case STATE_TEXGEN_EYE_R:
3476 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3477 break;
3478
3479 case STATE_TEXGEN_EYE_Q:
3480 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3481 break;
3482
3483 case STATE_TEXGEN_OBJECT_S:
3484 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3485 break;
3486
3487 case STATE_TEXGEN_OBJECT_T:
3488 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3489 break;
3490
3491 case STATE_TEXGEN_OBJECT_R:
3492 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3493 break;
3494
3495 case STATE_TEXGEN_OBJECT_Q:
3496 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3497 break;
3498
3499 case STATE_TEXENV_COLOR:
3500 fprintf (stderr, "STATE_TEXENV_COLOR ");
3501 break;
3502
3503 case STATE_DEPTH_RANGE:
3504 fprintf (stderr, "STATE_DEPTH_RANGE ");
3505 break;
3506
3507 case STATE_VERTEX_PROGRAM:
3508 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3509 break;
3510
3511 case STATE_FRAGMENT_PROGRAM:
3512 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3513 break;
3514
3515 case STATE_ENV:
3516 fprintf (stderr, "STATE_ENV ");
3517 break;
3518
3519 case STATE_LOCAL:
3520 fprintf (stderr, "STATE_LOCAL ");
3521 break;
3522
3523 }
3524 fprintf (stderr, "[%d] ", token);
3525}
3526
3527
3528static GLvoid
3529debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3530 struct arb_program *Program)
3531{
3532 struct var_cache *vc;
3533 GLint a, b;
3534
3535 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3536
3537 /* First of all, print out the contents of the var_cache */
3538 vc = vc_head;
3539 while (vc) {
3540 fprintf (stderr, "[%x]\n", vc);
3541 switch (vc->type) {
3542 case vt_none:
3543 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3544 break;
3545 case vt_attrib:
3546 fprintf (stderr, "ATTRIB %s\n", vc->name);
3547 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3548 break;
3549 case vt_param:
3550 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3551 vc->param_binding_begin, vc->param_binding_length);
3552 b = vc->param_binding_begin;
3553 for (a = 0; a < vc->param_binding_length; a++) {
3554 fprintf (stderr, "%s\n",
3555 Program->Parameters->Parameters[a + b].Name);
3556 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3557 print_state_token (Program->Parameters->Parameters[a + b].
3558 StateIndexes[0]);
3559 print_state_token (Program->Parameters->Parameters[a + b].
3560 StateIndexes[1]);
3561 print_state_token (Program->Parameters->Parameters[a + b].
3562 StateIndexes[2]);
3563 print_state_token (Program->Parameters->Parameters[a + b].
3564 StateIndexes[3]);
3565 print_state_token (Program->Parameters->Parameters[a + b].
3566 StateIndexes[4]);
3567 print_state_token (Program->Parameters->Parameters[a + b].
3568 StateIndexes[5]);
3569 }
3570 else
3571 fprintf (stderr, "%f %f %f %f\n",
3572 Program->Parameters->Parameters[a + b].Values[0],
3573 Program->Parameters->Parameters[a + b].Values[1],
3574 Program->Parameters->Parameters[a + b].Values[2],
3575 Program->Parameters->Parameters[a + b].Values[3]);
3576 }
3577 break;
3578 case vt_temp:
3579 fprintf (stderr, "TEMP %s\n", vc->name);
3580 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3581 break;
3582 case vt_output:
3583 fprintf (stderr, "OUTPUT %s\n", vc->name);
3584 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3585 break;
3586 case vt_alias:
3587 fprintf (stderr, "ALIAS %s\n", vc->name);
3588 fprintf (stderr, " binding: 0x%x (%s)\n",
3589 vc->alias_binding, vc->alias_binding->name);
3590 break;
3591 }
3592 vc = vc->next;
3593 }
3594}
3595
3596#endif
3597
3598
3599/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003600 * Grow an array of fragment program instructions.
3601 */
3602static struct fp_instruction *
3603realloc_fp_instructions(struct fp_instruction *oldArray, GLuint oldSize)
3604{
3605 struct fp_instruction *array = (struct fp_instruction *)
3606 _mesa_realloc(oldArray,
3607 oldSize * sizeof(struct fp_instruction),
3608 (oldSize + 1) * sizeof(struct fp_instruction));
3609 return array;
3610}
3611
3612/**
3613 * Grow an array of vertex program instructions.
3614 */
3615static struct vp_instruction *
3616realloc_vp_instructions(struct vp_instruction *oldArray, GLuint oldSize)
3617{
3618 struct vp_instruction *array = (struct vp_instruction *)
3619 _mesa_realloc(oldArray,
3620 oldSize * sizeof(struct vp_instruction),
3621 (oldSize + 1) * sizeof(struct vp_instruction));
3622 return array;
3623}
3624
3625
3626/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003627 * The main loop for parsing a fragment or vertex program
3628 *
Brian Paul45703642005-10-29 15:52:31 +00003629 * \return GL_TRUE on success, GL_FALSE on error.
Jouk Jansen40322e12004-04-05 08:50:36 +00003630 */
Brian Paul45703642005-10-29 15:52:31 +00003631static GLboolean
3632parse_arb_program(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
3633 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003634{
3635 GLint err = 0;
3636
3637 Program->MajorVersion = (GLuint) * inst++;
3638 Program->MinorVersion = (GLuint) * inst++;
3639
3640 while (*inst != END) {
3641 switch (*inst++) {
3642
3643 case OPTION:
3644 switch (*inst++) {
3645 case ARB_PRECISION_HINT_FASTEST:
3646 Program->PrecisionOption = GL_FASTEST;
3647 break;
3648
3649 case ARB_PRECISION_HINT_NICEST:
3650 Program->PrecisionOption = GL_NICEST;
3651 break;
3652
3653 case ARB_FOG_EXP:
3654 Program->FogOption = GL_EXP;
3655 break;
3656
3657 case ARB_FOG_EXP2:
3658 Program->FogOption = GL_EXP2;
3659 break;
3660
3661 case ARB_FOG_LINEAR:
3662 Program->FogOption = GL_LINEAR;
3663 break;
3664
3665 case ARB_POSITION_INVARIANT:
3666 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
3667 Program->HintPositionInvariant = 1;
3668 break;
3669
3670 case ARB_FRAGMENT_PROGRAM_SHADOW:
3671 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3672 /* TODO ARB_fragment_program_shadow code */
3673 }
3674 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003675
3676 case ARB_DRAW_BUFFERS:
3677 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3678 /* do nothing for now */
3679 }
3680 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003681 }
3682 break;
3683
3684 case INSTRUCTION:
3685 Program->Position = parse_position (&inst);
3686
3687 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00003688 /* Check instruction count. END counts as an instruction. */
3689 if (Program->Base.NumInstructions + 1
3690 == MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS) {
3691 const char *msg = "Max instruction count exceeded";
3692 _mesa_set_program_error(ctx, Program->Position, msg);
3693 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00003694 }
3695
Brian Paul7aebaf32005-10-30 21:23:23 +00003696 /* grow instruction list */
3697 Program->FPInstructions
3698 = realloc_fp_instructions(Program->FPInstructions,
3699 Program->Base.NumInstructions);
3700 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003701 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00003702 &Program->FPInstructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003703 }
3704 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00003705 /* Check instruction count. END counts as an instruction. */
3706 if (Program->Base.NumInstructions + 1
3707 == MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS) {
3708 const char *msg = "Max instruction count exceeded";
3709 _mesa_set_program_error(ctx, Program->Position, msg);
3710 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00003711 }
3712
Brian Paul7aebaf32005-10-30 21:23:23 +00003713 /* grow instruction list */
3714 Program->VPInstructions
3715 = realloc_vp_instructions(Program->VPInstructions,
3716 Program->Base.NumInstructions);
3717 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00003719 &Program->VPInstructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003720 }
3721
3722 /* increment Program->Base.NumInstructions */
3723 Program->Base.NumInstructions++;
3724 break;
3725
3726 case DECLARATION:
3727 err = parse_declaration (ctx, &inst, vc_head, Program);
3728 break;
3729
3730 default:
3731 break;
3732 }
3733
3734 if (err)
3735 break;
3736 }
3737
3738 /* Finally, tag on an OPCODE_END instruction */
3739 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00003740 const GLuint numInst = Program->Base.NumInstructions;
3741 Program->FPInstructions
3742 = realloc_fp_instructions(Program->FPInstructions, numInst);
3743 _mesa_init_fp_instruction(Program->FPInstructions + numInst);
3744 Program->FPInstructions[numInst].Opcode = FP_OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003745 /* YYY Wrong Position in program, whatever, at least not random -> crash
3746 Program->Position = parse_position (&inst);
3747 */
Brian Paul7aebaf32005-10-30 21:23:23 +00003748 Program->FPInstructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003749 }
3750 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00003751 const GLuint numInst = Program->Base.NumInstructions;
3752 Program->VPInstructions
3753 = realloc_vp_instructions(Program->VPInstructions, numInst);
3754 _mesa_init_vp_instruction(Program->VPInstructions + numInst);
3755 Program->VPInstructions[numInst].Opcode = VP_OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003756 /* YYY Wrong Position in program, whatever, at least not random -> crash
3757 Program->Position = parse_position (&inst);
3758 */
Brian Paul7aebaf32005-10-30 21:23:23 +00003759 Program->VPInstructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003760 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003761 Program->Base.NumInstructions++;
3762
3763 return err;
3764}
3765
Brian Paul7aebaf32005-10-30 21:23:23 +00003766
Jouk Jansen40322e12004-04-05 08:50:36 +00003767/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003768__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003769#include "grammar_syn.h"
3770;
3771
Brian Paul7aebaf32005-10-30 21:23:23 +00003772
Jouk Jansen40322e12004-04-05 08:50:36 +00003773static int set_reg8 (GLcontext *ctx, grammar id, const byte *name, byte value)
3774{
3775 char error_msg[300];
3776 GLint error_pos;
3777
3778 if (grammar_set_reg8 (id, name, value))
3779 return 0;
3780
3781 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3782 _mesa_set_program_error (ctx, error_pos, error_msg);
3783 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3784 return 1;
3785}
3786
3787static int extension_is_supported (const GLubyte *ext)
3788{
Ian Romanick9bdfee32005-07-18 12:31:24 +00003789 const GLubyte *extensions = CALL_GetString(GET_DISPATCH(), (GL_EXTENSIONS));
Jouk Jansen40322e12004-04-05 08:50:36 +00003790 const GLubyte *end = extensions + _mesa_strlen ((const char *) extensions);
Karl Schultz6258b762005-05-05 21:08:07 +00003791 const GLint ext_len = (GLint)_mesa_strlen ((const char *) ext);
Jouk Jansen40322e12004-04-05 08:50:36 +00003792
3793 while (extensions < end)
3794 {
Brian Paulb3aefd12005-09-19 20:12:32 +00003795 const GLubyte *name_end = (const GLubyte *) _mesa_strstr ((const char *) extensions, " ");
Jouk Jansen40322e12004-04-05 08:50:36 +00003796 if (name_end == NULL)
3797 name_end = end;
3798 if (name_end - extensions == ext_len && _mesa_strncmp ((const char *) ext,
3799 (const char *) extensions, ext_len) == 0)
3800 return 1;
3801 extensions = name_end + 1;
3802 }
3803
3804 return 0;
3805}
3806
3807static int enable_ext (GLcontext *ctx, grammar id, const byte *name, const byte *extname)
3808{
3809 if (extension_is_supported (extname))
3810 if (set_reg8 (ctx, id, name, 0x01))
3811 return 1;
3812 return 0;
3813}
3814
3815/**
3816 * This kicks everything off.
3817 *
3818 * \param ctx - The GL Context
3819 * \param str - The program string
3820 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003821 * \param program - The arb_program struct to return all the parsed info in
3822 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003823 */
Brian Paul45703642005-10-29 15:52:31 +00003824GLboolean
Jouk Jansen40322e12004-04-05 08:50:36 +00003825_mesa_parse_arb_program (GLcontext * ctx, const GLubyte * str, GLsizei len,
3826 struct arb_program * program)
3827{
3828 GLint a, err, error_pos;
3829 char error_msg[300];
3830 GLuint parsed_len;
3831 struct var_cache *vc_head;
3832 grammar arbprogram_syn_id;
3833 GLubyte *parsed, *inst;
3834 GLubyte *strz = NULL;
3835 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3836
Brian Paul7f76b8f2004-09-10 01:05:39 +00003837 /* Reset error state */
3838 _mesa_set_program_error(ctx, -1, NULL);
3839
Jouk Jansen40322e12004-04-05 08:50:36 +00003840#if DEBUG_PARSING
3841 fprintf (stderr, "Loading grammar text!\n");
3842#endif
3843
3844 /* check if the arb_grammar_text (arbprogram.syn) is syntactically correct */
3845 if (!arbprogram_syn_is_ok) {
3846 grammar grammar_syn_id;
3847 GLint err;
3848 GLuint parsed_len;
3849 byte *parsed;
3850
3851 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3852 if (grammar_syn_id == 0) {
3853 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3854 _mesa_set_program_error (ctx, error_pos, error_msg);
3855 _mesa_error (ctx, GL_INVALID_OPERATION,
3856 "Error loading grammar rule set");
Brian Paul45703642005-10-29 15:52:31 +00003857 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003858 }
3859
3860 err = grammar_check (grammar_syn_id, (byte *) arb_grammar_text, &parsed, &parsed_len);
3861
Brian Paul45703642005-10-29 15:52:31 +00003862 /* NOTE: we can't destroy grammar_syn_id right here because
3863 * grammar_destroy() can reset the last error
3864 */
Jouk Jansen40322e12004-04-05 08:50:36 +00003865 if (err == 0) {
3866 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3867 _mesa_set_program_error (ctx, error_pos, error_msg);
3868 _mesa_error (ctx, GL_INVALID_OPERATION, "Error loading grammar rule set");
3869
3870 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003871 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003872 }
3873
3874 grammar_destroy (grammar_syn_id);
3875
3876 arbprogram_syn_is_ok = 1;
3877 }
3878
3879 /* create the grammar object */
3880 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3881 if (arbprogram_syn_id == 0) {
3882 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3883 _mesa_set_program_error (ctx, error_pos, error_msg);
3884 _mesa_error (ctx, GL_INVALID_OPERATION,
3885 "Error loading grammer rule set");
Brian Paul45703642005-10-29 15:52:31 +00003886 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003887 }
3888
3889 /* Set program_target register value */
3890 if (set_reg8 (ctx, arbprogram_syn_id, (byte *) "program_target",
3891 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3892 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003893 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003894 }
3895
3896 /* Enable all active extensions */
3897 if (enable_ext (ctx, arbprogram_syn_id,
3898 (byte *) "vertex_blend", (byte *) "GL_ARB_vertex_blend") ||
3899 enable_ext (ctx, arbprogram_syn_id,
3900 (byte *) "vertex_blend", (byte *) "GL_EXT_vertex_weighting") ||
3901 enable_ext (ctx, arbprogram_syn_id,
3902 (byte *) "matrix_palette", (byte *) "GL_ARB_matrix_palette") ||
3903 enable_ext (ctx, arbprogram_syn_id,
3904 (byte *) "point_parameters", (byte *) "GL_ARB_point_parameters") ||
3905 enable_ext (ctx, arbprogram_syn_id,
3906 (byte *) "point_parameters", (byte *) "GL_EXT_point_parameters") ||
3907 enable_ext (ctx, arbprogram_syn_id,
3908 (byte *) "secondary_color", (byte *) "GL_EXT_secondary_color") ||
3909 enable_ext (ctx, arbprogram_syn_id,
3910 (byte *) "fog_coord", (byte *) "GL_EXT_fog_coord") ||
3911 enable_ext (ctx, arbprogram_syn_id,
Brian Pauledfe0fe2004-08-20 14:21:20 +00003912 (byte *) "texture_rectangle", (byte *) "GL_ARB_texture_rectangle") ||
3913 enable_ext (ctx, arbprogram_syn_id,
Jouk Jansen40322e12004-04-05 08:50:36 +00003914 (byte *) "texture_rectangle", (byte *) "GL_EXT_texture_rectangle") ||
3915 enable_ext (ctx, arbprogram_syn_id,
3916 (byte *) "texture_rectangle", (byte *) "GL_NV_texture_rectangle") ||
3917 enable_ext (ctx, arbprogram_syn_id,
Michal Krolad22ce82004-10-11 08:13:25 +00003918 (byte *) "fragment_program_shadow", (byte *) "GL_ARB_fragment_program_shadow") ||
3919 enable_ext (ctx, arbprogram_syn_id,
3920 (byte *) "draw_buffers", (byte *) "GL_ARB_draw_buffers")) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003921 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003922 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003923 }
3924
3925 /* check for NULL character occurences */
3926 {
3927 int i;
3928 for (i = 0; i < len; i++)
3929 if (str[i] == '\0') {
3930 _mesa_set_program_error (ctx, i, "invalid character");
3931 _mesa_error (ctx, GL_INVALID_OPERATION, "Lexical Error");
3932
3933 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003934 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003935 }
3936 }
3937
3938 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003939 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003940 if (!strz) {
3941 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glprogramStringARB");
3942 return GL_FALSE;
3943 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003944 _mesa_memcpy (strz, str, len);
3945 strz[len] = '\0';
3946
3947#if DEBUG_PARSING
Brian Paulb3aefd12005-09-19 20:12:32 +00003948 fprintf (stderr, "Checking Grammar!\n");
Jouk Jansen40322e12004-04-05 08:50:36 +00003949#endif
Michal Krolb80bc052004-10-21 14:09:54 +00003950 /* do a fast check on program string - initial production buffer is 4K */
3951 err = grammar_fast_check (arbprogram_syn_id, strz, &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003952
3953 /* Syntax parse error */
3954 if (err == 0) {
3955 _mesa_free (strz);
3956 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3957 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul45703642005-10-29 15:52:31 +00003958 _mesa_error (ctx, GL_INVALID_OPERATION, "glprogramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003959
3960 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003961#if DEBUG_PARSING
3962 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003963 int line, col;
3964 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003965 fprintf(stderr, "program: %s\n", (char *) strz);
3966 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
3967 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003968 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3969 } while (0)
3970#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003971
Jouk Jansen40322e12004-04-05 08:50:36 +00003972 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003973 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003974 }
3975
3976#if DEBUG_PARSING
Brian Paulb3aefd12005-09-19 20:12:32 +00003977 fprintf (stderr, "Destroying grammer dict [parse retval: %d]\n", err);
Jouk Jansen40322e12004-04-05 08:50:36 +00003978#endif
3979 grammar_destroy (arbprogram_syn_id);
3980
3981 /* Initialize the arb_program struct */
3982 program->Base.String = strz;
3983 program->Base.NumInstructions =
3984 program->Base.NumTemporaries =
3985 program->Base.NumParameters =
3986 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
3987 program->Parameters = _mesa_new_parameter_list ();
3988 program->InputsRead = 0;
3989 program->OutputsWritten = 0;
3990 program->Position = 0;
3991 program->MajorVersion = program->MinorVersion = 0;
3992 program->PrecisionOption = GL_DONT_CARE;
3993 program->FogOption = GL_NONE;
3994 program->HintPositionInvariant = GL_FALSE;
3995 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
3996 program->TexturesUsed[a] = 0;
3997 program->NumAluInstructions =
3998 program->NumTexInstructions =
3999 program->NumTexIndirections = 0;
4000
4001 program->FPInstructions = NULL;
4002 program->VPInstructions = NULL;
4003
4004 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00004005 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004006
4007 /* Start examining the tokens in the array */
4008 inst = parsed;
4009
4010 /* Check the grammer rev */
4011 if (*inst++ != REVISION) {
4012 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00004013 _mesa_error(ctx, GL_INVALID_OPERATION,
4014 "glProgramStringARB(Grammar verison mismatch)");
4015 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004016 }
4017 else {
Michal Krolb80bc052004-10-21 14:09:54 +00004018 /* ignore program target */
4019 inst++;
Jouk Jansen40322e12004-04-05 08:50:36 +00004020
4021 err = parse_arb_program (ctx, inst, &vc_head, program);
4022#if DEBUG_PARSING
4023 fprintf (stderr, "Symantic analysis returns %d [1 is bad!]\n", err);
4024#endif
4025 }
4026
4027 /*debug_variables(ctx, vc_head, program); */
4028
4029 /* We're done with the parsed binary array */
4030 var_cache_destroy (&vc_head);
4031
4032 _mesa_free (parsed);
4033#if DEBUG_PARSING
Brian Paulb3aefd12005-09-19 20:12:32 +00004034 fprintf (stderr, "_mesa_parse_arb_program() done\n");
Jouk Jansen40322e12004-04-05 08:50:36 +00004035#endif
Brian Paul45703642005-10-29 15:52:31 +00004036
4037 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004038}