blob: 7b56b078a11062a38553410a96e355ad65d76f44 [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) &&
Brian Paul05051032005-11-01 04:36:33 +00001327 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001328 ||
1329 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001330 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001331 _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) &&
Brian Paul05051032005-11-01 04:36:33 +00001347 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001348 ||
1349 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001350 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001351 _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,
Brian Paul90ebb582005-11-02 18:06:12 +00001540 * one of the VERT_RESULT_* or FRAG_RESULT_* 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);
Brian Paul90ebb582005-11-02 18:06:12 +00001558 *outputReg = FRAG_RESULT_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 */
Brian Paul90ebb582005-11-02 18:06:12 +00001569 *outputReg = FRAG_RESULT_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)
Brian Paul05051032005-11-01 04:36:33 +00001745 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001746 || ((state_tokens[1] == STATE_LOCAL)
1747 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001748 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001749 out_of_range = 1;
1750 }
1751 else {
1752 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001753 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001754 || ((state_tokens[1] == STATE_LOCAL)
1755 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001756 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001757 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 >=
Brian Paul05051032005-11-01 04:36:33 +00001803 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001804 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1805 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001806 ctx->Const.FragmentProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001807 _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 >=
Brian Paul05051032005-11-01 04:36:33 +00001961 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001962 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1963 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001964 ctx->Const.VertexProgram.MaxTemps))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001965 _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 >=
Brian Paul05051032005-11-01 04:36:33 +00002094 ctx->Const.VertexProgram.MaxAddressRegs) {
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 Whitwellc3626a92005-11-01 17:25:49 +00003016 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003017 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003018 return 1;
Keith Whitwell219f3c42005-07-01 17:12:29 +00003019 fp->Opcode = FP_OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003020 break;
3021 }
3022
3023 return 0;
3024}
3025
Keith Whitwell7c26b612005-04-21 14:46:57 +00003026static GLuint
3027parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3028 struct var_cache **vc_head, struct arb_program *Program,
3029 struct vp_dst_register *reg )
3030{
Brian Paul7aebaf32005-10-30 21:23:23 +00003031 GLint mask;
3032 GLuint idx;
3033 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003034
3035 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3036 return 1;
3037
3038 reg->File = file;
3039 reg->Index = idx;
3040 reg->WriteMask = mask;
3041 return 0;
3042}
3043
3044/**
3045 * Handle the parsing out of a masked address register
3046 *
3047 * \param Index - The register index we write to
3048 * \param WriteMask - The mask controlling which components we write (1->write)
3049 *
3050 * \return 0 on sucess, 1 on error
3051 */
3052static GLuint
3053parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3054 struct var_cache **vc_head,
3055 struct arb_program *Program,
3056 struct vp_dst_register *reg)
3057{
3058 GLint idx;
3059
3060 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3061 return 1;
3062
3063 /* This should be 0x8 */
3064 (*inst)++;
3065
3066 reg->File = PROGRAM_ADDRESS;
3067 reg->Index = idx;
3068
3069 /* Writemask of .x is implied */
3070 reg->WriteMask = 0x1;
3071 return 0;
3072}
3073
3074/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003075 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003076 */
3077static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003078parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3079 struct var_cache **vc_head,
3080 struct arb_program *program,
3081 struct vp_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003082{
Brian Paul7aebaf32005-10-30 21:23:23 +00003083 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003084 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003085 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003086 GLubyte swizzle[4];
3087 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003088
3089 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003090 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003091
3092 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003093 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003094 return 1;
3095
3096 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003097 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003098
Brian Paul32df89e2005-10-29 18:26:43 +00003099 reg->File = file;
3100 reg->Index = index;
3101 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3102 swizzle[2], swizzle[3]);
Brian Paul7aebaf32005-10-30 21:23:23 +00003103 reg->Negate = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003104 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003105 return 0;
3106}
3107
3108
3109static GLuint
3110parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003111 struct var_cache **vc_head,
3112 struct arb_program *Program,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003113 struct vp_src_register *reg )
3114{
Brian Paul7aebaf32005-10-30 21:23:23 +00003115 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003116 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003117 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003118 GLubyte Swizzle[4];
3119 GLboolean IsRelOffset;
3120
3121 /* Grab the sign */
3122 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3123
3124 /* And the src reg */
3125 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3126 return 1;
3127
3128 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003129 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003130
3131 reg->File = File;
3132 reg->Index = Index;
3133 reg->Swizzle = (Swizzle[0] << 0);
3134 reg->Negate = Negate;
3135 reg->RelAddr = IsRelOffset;
3136 return 0;
3137}
3138
3139
Jouk Jansen40322e12004-04-05 08:50:36 +00003140/**
3141 * This is a big mother that handles getting opcodes into the instruction
3142 * and handling the src & dst registers for vertex program instructions
3143 */
3144static GLuint
3145parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3146 struct var_cache **vc_head, struct arb_program *Program,
3147 struct vp_instruction *vp)
3148{
3149 GLint a;
3150 GLubyte type, code;
3151
3152 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3153 type = *(*inst)++;
3154
3155 /* The actual opcode name */
3156 code = *(*inst)++;
3157
Brian Paul7aebaf32005-10-30 21:23:23 +00003158 _mesa_init_vp_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003159 /* Record the position in the program string for debugging */
3160 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003161
3162 switch (type) {
3163 /* XXX: */
3164 case OP_ALU_ARL:
3165 vp->Opcode = VP_OPCODE_ARL;
3166
3167 /* Remember to set SrcReg.RelAddr; */
3168
3169 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003170 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003171 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003172
Jouk Jansen40322e12004-04-05 08:50:36 +00003173 vp->DstReg.File = PROGRAM_ADDRESS;
3174
3175 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003176 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003177 return 1;
3178
3179 break;
3180
3181 case OP_ALU_VECTOR:
3182 switch (code) {
3183 case OP_ABS:
3184 vp->Opcode = VP_OPCODE_ABS;
3185 break;
3186 case OP_FLR:
3187 vp->Opcode = VP_OPCODE_FLR;
3188 break;
3189 case OP_FRC:
3190 vp->Opcode = VP_OPCODE_FRC;
3191 break;
3192 case OP_LIT:
3193 vp->Opcode = VP_OPCODE_LIT;
3194 break;
3195 case OP_MOV:
3196 vp->Opcode = VP_OPCODE_MOV;
3197 break;
3198 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003199
3200 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003201 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003202
3203 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003204 return 1;
3205 break;
3206
3207 case OP_ALU_SCALAR:
3208 switch (code) {
3209 case OP_EX2:
3210 vp->Opcode = VP_OPCODE_EX2;
3211 break;
3212 case OP_EXP:
3213 vp->Opcode = VP_OPCODE_EXP;
3214 break;
3215 case OP_LG2:
3216 vp->Opcode = VP_OPCODE_LG2;
3217 break;
3218 case OP_LOG:
3219 vp->Opcode = VP_OPCODE_LOG;
3220 break;
3221 case OP_RCP:
3222 vp->Opcode = VP_OPCODE_RCP;
3223 break;
3224 case OP_RSQ:
3225 vp->Opcode = VP_OPCODE_RSQ;
3226 break;
3227 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003228 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003229 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003230
3231 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003232 return 1;
3233 break;
3234
3235 case OP_ALU_BINSC:
3236 switch (code) {
3237 case OP_POW:
3238 vp->Opcode = VP_OPCODE_POW;
3239 break;
3240 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003241 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003242 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003243
Jouk Jansen40322e12004-04-05 08:50:36 +00003244 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003245 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003246 return 1;
3247 }
3248 break;
3249
3250 case OP_ALU_BIN:
3251 switch (code) {
3252 case OP_ADD:
3253 vp->Opcode = VP_OPCODE_ADD;
3254 break;
3255 case OP_DP3:
3256 vp->Opcode = VP_OPCODE_DP3;
3257 break;
3258 case OP_DP4:
3259 vp->Opcode = VP_OPCODE_DP4;
3260 break;
3261 case OP_DPH:
3262 vp->Opcode = VP_OPCODE_DPH;
3263 break;
3264 case OP_DST:
3265 vp->Opcode = VP_OPCODE_DST;
3266 break;
3267 case OP_MAX:
3268 vp->Opcode = VP_OPCODE_MAX;
3269 break;
3270 case OP_MIN:
3271 vp->Opcode = VP_OPCODE_MIN;
3272 break;
3273 case OP_MUL:
3274 vp->Opcode = VP_OPCODE_MUL;
3275 break;
3276 case OP_SGE:
3277 vp->Opcode = VP_OPCODE_SGE;
3278 break;
3279 case OP_SLT:
3280 vp->Opcode = VP_OPCODE_SLT;
3281 break;
3282 case OP_SUB:
3283 vp->Opcode = VP_OPCODE_SUB;
3284 break;
3285 case OP_XPD:
3286 vp->Opcode = VP_OPCODE_XPD;
3287 break;
3288 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003289 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003290 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003291
Jouk Jansen40322e12004-04-05 08:50:36 +00003292 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003293 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 return 1;
3295 }
3296 break;
3297
3298 case OP_ALU_TRI:
3299 switch (code) {
3300 case OP_MAD:
3301 vp->Opcode = VP_OPCODE_MAD;
3302 break;
3303 }
3304
Keith Whitwell7c26b612005-04-21 14:46:57 +00003305 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003306 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003307
Jouk Jansen40322e12004-04-05 08:50:36 +00003308 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003309 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003310 return 1;
3311 }
3312 break;
3313
3314 case OP_ALU_SWZ:
3315 switch (code) {
3316 case OP_SWZ:
3317 vp->Opcode = VP_OPCODE_SWZ;
3318 break;
3319 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003320 {
Brian Paul32df89e2005-10-29 18:26:43 +00003321 GLubyte swizzle[4];
3322 GLubyte negateMask;
3323 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003324 enum register_file file;
3325 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003326
Keith Whitwell7c26b612005-04-21 14:46:57 +00003327 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3328 return 1;
3329
Brian Paul32df89e2005-10-29 18:26:43 +00003330 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003331 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003332 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3333 vp->SrcReg[0].File = file;
3334 vp->SrcReg[0].Index = index;
3335 vp->SrcReg[0].Negate = negateMask;
3336 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3337 swizzle[1],
3338 swizzle[2],
3339 swizzle[3]);
3340 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003341 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003342 break;
3343 }
3344 return 0;
3345}
3346
3347#if DEBUG_PARSING
3348
3349static GLvoid
3350print_state_token (GLint token)
3351{
3352 switch (token) {
3353 case STATE_MATERIAL:
3354 fprintf (stderr, "STATE_MATERIAL ");
3355 break;
3356 case STATE_LIGHT:
3357 fprintf (stderr, "STATE_LIGHT ");
3358 break;
3359
3360 case STATE_LIGHTMODEL_AMBIENT:
3361 fprintf (stderr, "STATE_AMBIENT ");
3362 break;
3363
3364 case STATE_LIGHTMODEL_SCENECOLOR:
3365 fprintf (stderr, "STATE_SCENECOLOR ");
3366 break;
3367
3368 case STATE_LIGHTPROD:
3369 fprintf (stderr, "STATE_LIGHTPROD ");
3370 break;
3371
3372 case STATE_TEXGEN:
3373 fprintf (stderr, "STATE_TEXGEN ");
3374 break;
3375
3376 case STATE_FOG_COLOR:
3377 fprintf (stderr, "STATE_FOG_COLOR ");
3378 break;
3379
3380 case STATE_FOG_PARAMS:
3381 fprintf (stderr, "STATE_FOG_PARAMS ");
3382 break;
3383
3384 case STATE_CLIPPLANE:
3385 fprintf (stderr, "STATE_CLIPPLANE ");
3386 break;
3387
3388 case STATE_POINT_SIZE:
3389 fprintf (stderr, "STATE_POINT_SIZE ");
3390 break;
3391
3392 case STATE_POINT_ATTENUATION:
3393 fprintf (stderr, "STATE_ATTENUATION ");
3394 break;
3395
3396 case STATE_MATRIX:
3397 fprintf (stderr, "STATE_MATRIX ");
3398 break;
3399
3400 case STATE_MODELVIEW:
3401 fprintf (stderr, "STATE_MODELVIEW ");
3402 break;
3403
3404 case STATE_PROJECTION:
3405 fprintf (stderr, "STATE_PROJECTION ");
3406 break;
3407
3408 case STATE_MVP:
3409 fprintf (stderr, "STATE_MVP ");
3410 break;
3411
3412 case STATE_TEXTURE:
3413 fprintf (stderr, "STATE_TEXTURE ");
3414 break;
3415
3416 case STATE_PROGRAM:
3417 fprintf (stderr, "STATE_PROGRAM ");
3418 break;
3419
3420 case STATE_MATRIX_INVERSE:
3421 fprintf (stderr, "STATE_INVERSE ");
3422 break;
3423
3424 case STATE_MATRIX_TRANSPOSE:
3425 fprintf (stderr, "STATE_TRANSPOSE ");
3426 break;
3427
3428 case STATE_MATRIX_INVTRANS:
3429 fprintf (stderr, "STATE_INVTRANS ");
3430 break;
3431
3432 case STATE_AMBIENT:
3433 fprintf (stderr, "STATE_AMBIENT ");
3434 break;
3435
3436 case STATE_DIFFUSE:
3437 fprintf (stderr, "STATE_DIFFUSE ");
3438 break;
3439
3440 case STATE_SPECULAR:
3441 fprintf (stderr, "STATE_SPECULAR ");
3442 break;
3443
3444 case STATE_EMISSION:
3445 fprintf (stderr, "STATE_EMISSION ");
3446 break;
3447
3448 case STATE_SHININESS:
3449 fprintf (stderr, "STATE_SHININESS ");
3450 break;
3451
3452 case STATE_HALF:
3453 fprintf (stderr, "STATE_HALF ");
3454 break;
3455
3456 case STATE_POSITION:
3457 fprintf (stderr, "STATE_POSITION ");
3458 break;
3459
3460 case STATE_ATTENUATION:
3461 fprintf (stderr, "STATE_ATTENUATION ");
3462 break;
3463
3464 case STATE_SPOT_DIRECTION:
3465 fprintf (stderr, "STATE_DIRECTION ");
3466 break;
3467
3468 case STATE_TEXGEN_EYE_S:
3469 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3470 break;
3471
3472 case STATE_TEXGEN_EYE_T:
3473 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3474 break;
3475
3476 case STATE_TEXGEN_EYE_R:
3477 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3478 break;
3479
3480 case STATE_TEXGEN_EYE_Q:
3481 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3482 break;
3483
3484 case STATE_TEXGEN_OBJECT_S:
3485 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3486 break;
3487
3488 case STATE_TEXGEN_OBJECT_T:
3489 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3490 break;
3491
3492 case STATE_TEXGEN_OBJECT_R:
3493 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3494 break;
3495
3496 case STATE_TEXGEN_OBJECT_Q:
3497 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3498 break;
3499
3500 case STATE_TEXENV_COLOR:
3501 fprintf (stderr, "STATE_TEXENV_COLOR ");
3502 break;
3503
3504 case STATE_DEPTH_RANGE:
3505 fprintf (stderr, "STATE_DEPTH_RANGE ");
3506 break;
3507
3508 case STATE_VERTEX_PROGRAM:
3509 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3510 break;
3511
3512 case STATE_FRAGMENT_PROGRAM:
3513 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3514 break;
3515
3516 case STATE_ENV:
3517 fprintf (stderr, "STATE_ENV ");
3518 break;
3519
3520 case STATE_LOCAL:
3521 fprintf (stderr, "STATE_LOCAL ");
3522 break;
3523
3524 }
3525 fprintf (stderr, "[%d] ", token);
3526}
3527
3528
3529static GLvoid
3530debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3531 struct arb_program *Program)
3532{
3533 struct var_cache *vc;
3534 GLint a, b;
3535
3536 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3537
3538 /* First of all, print out the contents of the var_cache */
3539 vc = vc_head;
3540 while (vc) {
3541 fprintf (stderr, "[%x]\n", vc);
3542 switch (vc->type) {
3543 case vt_none:
3544 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3545 break;
3546 case vt_attrib:
3547 fprintf (stderr, "ATTRIB %s\n", vc->name);
3548 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3549 break;
3550 case vt_param:
3551 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3552 vc->param_binding_begin, vc->param_binding_length);
3553 b = vc->param_binding_begin;
3554 for (a = 0; a < vc->param_binding_length; a++) {
3555 fprintf (stderr, "%s\n",
3556 Program->Parameters->Parameters[a + b].Name);
3557 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3558 print_state_token (Program->Parameters->Parameters[a + b].
3559 StateIndexes[0]);
3560 print_state_token (Program->Parameters->Parameters[a + b].
3561 StateIndexes[1]);
3562 print_state_token (Program->Parameters->Parameters[a + b].
3563 StateIndexes[2]);
3564 print_state_token (Program->Parameters->Parameters[a + b].
3565 StateIndexes[3]);
3566 print_state_token (Program->Parameters->Parameters[a + b].
3567 StateIndexes[4]);
3568 print_state_token (Program->Parameters->Parameters[a + b].
3569 StateIndexes[5]);
3570 }
3571 else
3572 fprintf (stderr, "%f %f %f %f\n",
3573 Program->Parameters->Parameters[a + b].Values[0],
3574 Program->Parameters->Parameters[a + b].Values[1],
3575 Program->Parameters->Parameters[a + b].Values[2],
3576 Program->Parameters->Parameters[a + b].Values[3]);
3577 }
3578 break;
3579 case vt_temp:
3580 fprintf (stderr, "TEMP %s\n", vc->name);
3581 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3582 break;
3583 case vt_output:
3584 fprintf (stderr, "OUTPUT %s\n", vc->name);
3585 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3586 break;
3587 case vt_alias:
3588 fprintf (stderr, "ALIAS %s\n", vc->name);
3589 fprintf (stderr, " binding: 0x%x (%s)\n",
3590 vc->alias_binding, vc->alias_binding->name);
3591 break;
3592 }
3593 vc = vc->next;
3594 }
3595}
3596
Brian Paul72030e02005-11-03 03:30:34 +00003597#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003598
3599
3600/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003601 * The main loop for parsing a fragment or vertex program
3602 *
Brian Paul72030e02005-11-03 03:30:34 +00003603 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003604 */
Brian Paul72030e02005-11-03 03:30:34 +00003605static GLint
Brian Paul45703642005-10-29 15:52:31 +00003606parse_arb_program(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
3607 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003608{
Brian Paul72030e02005-11-03 03:30:34 +00003609 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3610 ? ctx->Const.FragmentProgram.MaxInstructions
3611 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003612 GLint err = 0;
3613
Brian Paul72030e02005-11-03 03:30:34 +00003614 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3615
Jouk Jansen40322e12004-04-05 08:50:36 +00003616 Program->MajorVersion = (GLuint) * inst++;
3617 Program->MinorVersion = (GLuint) * inst++;
3618
3619 while (*inst != END) {
3620 switch (*inst++) {
3621
3622 case OPTION:
3623 switch (*inst++) {
3624 case ARB_PRECISION_HINT_FASTEST:
3625 Program->PrecisionOption = GL_FASTEST;
3626 break;
3627
3628 case ARB_PRECISION_HINT_NICEST:
3629 Program->PrecisionOption = GL_NICEST;
3630 break;
3631
3632 case ARB_FOG_EXP:
3633 Program->FogOption = GL_EXP;
3634 break;
3635
3636 case ARB_FOG_EXP2:
3637 Program->FogOption = GL_EXP2;
3638 break;
3639
3640 case ARB_FOG_LINEAR:
3641 Program->FogOption = GL_LINEAR;
3642 break;
3643
3644 case ARB_POSITION_INVARIANT:
3645 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003646 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003647 break;
3648
3649 case ARB_FRAGMENT_PROGRAM_SHADOW:
3650 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3651 /* TODO ARB_fragment_program_shadow code */
3652 }
3653 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003654
3655 case ARB_DRAW_BUFFERS:
3656 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3657 /* do nothing for now */
3658 }
3659 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003660 }
3661 break;
3662
3663 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003664 /* check length */
3665 if (Program->Base.NumInstructions + 1 >= maxInst) {
3666 const char *msg = "Max instruction count exceeded";
3667 _mesa_set_program_error(ctx, Program->Position, msg);
3668 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3669 return 1;
3670 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003671 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003672 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003673 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003674 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00003675 &Program->FPInstructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003676 }
3677 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003678 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00003679 &Program->VPInstructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003680 }
3681
Brian Paul72030e02005-11-03 03:30:34 +00003682 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003683 Program->Base.NumInstructions++;
3684 break;
3685
3686 case DECLARATION:
3687 err = parse_declaration (ctx, &inst, vc_head, Program);
3688 break;
3689
3690 default:
3691 break;
3692 }
3693
3694 if (err)
3695 break;
3696 }
3697
3698 /* Finally, tag on an OPCODE_END instruction */
3699 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00003700 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul7aebaf32005-10-30 21:23:23 +00003701 _mesa_init_fp_instruction(Program->FPInstructions + numInst);
3702 Program->FPInstructions[numInst].Opcode = FP_OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003703 /* YYY Wrong Position in program, whatever, at least not random -> crash
3704 Program->Position = parse_position (&inst);
3705 */
Brian Paul7aebaf32005-10-30 21:23:23 +00003706 Program->FPInstructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003707 }
3708 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00003709 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul7aebaf32005-10-30 21:23:23 +00003710 _mesa_init_vp_instruction(Program->VPInstructions + numInst);
3711 Program->VPInstructions[numInst].Opcode = VP_OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003712 /* YYY Wrong Position in program, whatever, at least not random -> crash
3713 Program->Position = parse_position (&inst);
3714 */
Brian Paul7aebaf32005-10-30 21:23:23 +00003715 Program->VPInstructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003716 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 Program->Base.NumInstructions++;
3718
Brian Paul05051032005-11-01 04:36:33 +00003719 /*
3720 * Initialize native counts to logical counts. The device driver may
3721 * change them if program is translated into a hardware program.
3722 */
3723 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3724 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3725 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3726 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3727 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
3728 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3729 struct fragment_program *fp = (struct fragment_program *) Program;
3730 fp->NumNativeAluInstructions = fp->NumAluInstructions;
3731 fp->NumNativeTexInstructions = fp->NumTexInstructions;
3732 fp->NumNativeTexIndirections = fp->NumTexIndirections;
3733 }
3734
Jouk Jansen40322e12004-04-05 08:50:36 +00003735 return err;
3736}
3737
Brian Paul7aebaf32005-10-30 21:23:23 +00003738
Jouk Jansen40322e12004-04-05 08:50:36 +00003739/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003740__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003741#include "grammar_syn.h"
3742;
3743
Brian Paul7aebaf32005-10-30 21:23:23 +00003744
Jouk Jansen40322e12004-04-05 08:50:36 +00003745static int set_reg8 (GLcontext *ctx, grammar id, const byte *name, byte value)
3746{
3747 char error_msg[300];
3748 GLint error_pos;
3749
3750 if (grammar_set_reg8 (id, name, value))
3751 return 0;
3752
3753 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3754 _mesa_set_program_error (ctx, error_pos, error_msg);
3755 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3756 return 1;
3757}
3758
3759static int extension_is_supported (const GLubyte *ext)
3760{
Ian Romanick9bdfee32005-07-18 12:31:24 +00003761 const GLubyte *extensions = CALL_GetString(GET_DISPATCH(), (GL_EXTENSIONS));
Jouk Jansen40322e12004-04-05 08:50:36 +00003762 const GLubyte *end = extensions + _mesa_strlen ((const char *) extensions);
Karl Schultz6258b762005-05-05 21:08:07 +00003763 const GLint ext_len = (GLint)_mesa_strlen ((const char *) ext);
Jouk Jansen40322e12004-04-05 08:50:36 +00003764
3765 while (extensions < end)
3766 {
Brian Paulb3aefd12005-09-19 20:12:32 +00003767 const GLubyte *name_end = (const GLubyte *) _mesa_strstr ((const char *) extensions, " ");
Jouk Jansen40322e12004-04-05 08:50:36 +00003768 if (name_end == NULL)
3769 name_end = end;
3770 if (name_end - extensions == ext_len && _mesa_strncmp ((const char *) ext,
3771 (const char *) extensions, ext_len) == 0)
3772 return 1;
3773 extensions = name_end + 1;
3774 }
3775
3776 return 0;
3777}
3778
3779static int enable_ext (GLcontext *ctx, grammar id, const byte *name, const byte *extname)
3780{
3781 if (extension_is_supported (extname))
3782 if (set_reg8 (ctx, id, name, 0x01))
3783 return 1;
3784 return 0;
3785}
3786
3787/**
3788 * This kicks everything off.
3789 *
3790 * \param ctx - The GL Context
3791 * \param str - The program string
3792 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003793 * \param program - The arb_program struct to return all the parsed info in
3794 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003795 */
Brian Paul45703642005-10-29 15:52:31 +00003796GLboolean
Jouk Jansen40322e12004-04-05 08:50:36 +00003797_mesa_parse_arb_program (GLcontext * ctx, const GLubyte * str, GLsizei len,
3798 struct arb_program * program)
3799{
3800 GLint a, err, error_pos;
3801 char error_msg[300];
3802 GLuint parsed_len;
3803 struct var_cache *vc_head;
3804 grammar arbprogram_syn_id;
3805 GLubyte *parsed, *inst;
3806 GLubyte *strz = NULL;
3807 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3808
Brian Paul7f76b8f2004-09-10 01:05:39 +00003809 /* Reset error state */
3810 _mesa_set_program_error(ctx, -1, NULL);
3811
Jouk Jansen40322e12004-04-05 08:50:36 +00003812#if DEBUG_PARSING
3813 fprintf (stderr, "Loading grammar text!\n");
3814#endif
3815
3816 /* check if the arb_grammar_text (arbprogram.syn) is syntactically correct */
3817 if (!arbprogram_syn_is_ok) {
3818 grammar grammar_syn_id;
3819 GLint err;
3820 GLuint parsed_len;
3821 byte *parsed;
3822
3823 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3824 if (grammar_syn_id == 0) {
3825 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3826 _mesa_set_program_error (ctx, error_pos, error_msg);
3827 _mesa_error (ctx, GL_INVALID_OPERATION,
3828 "Error loading grammar rule set");
Brian Paul45703642005-10-29 15:52:31 +00003829 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003830 }
3831
3832 err = grammar_check (grammar_syn_id, (byte *) arb_grammar_text, &parsed, &parsed_len);
3833
Brian Paul45703642005-10-29 15:52:31 +00003834 /* NOTE: we can't destroy grammar_syn_id right here because
3835 * grammar_destroy() can reset the last error
3836 */
Jouk Jansen40322e12004-04-05 08:50:36 +00003837 if (err == 0) {
3838 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3839 _mesa_set_program_error (ctx, error_pos, error_msg);
3840 _mesa_error (ctx, GL_INVALID_OPERATION, "Error loading grammar rule set");
3841
3842 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003843 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003844 }
3845
3846 grammar_destroy (grammar_syn_id);
3847
3848 arbprogram_syn_is_ok = 1;
3849 }
3850
3851 /* create the grammar object */
3852 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3853 if (arbprogram_syn_id == 0) {
3854 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3855 _mesa_set_program_error (ctx, error_pos, error_msg);
3856 _mesa_error (ctx, GL_INVALID_OPERATION,
3857 "Error loading grammer rule set");
Brian Paul45703642005-10-29 15:52:31 +00003858 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003859 }
3860
3861 /* Set program_target register value */
3862 if (set_reg8 (ctx, arbprogram_syn_id, (byte *) "program_target",
3863 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3864 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003865 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003866 }
3867
3868 /* Enable all active extensions */
3869 if (enable_ext (ctx, arbprogram_syn_id,
3870 (byte *) "vertex_blend", (byte *) "GL_ARB_vertex_blend") ||
3871 enable_ext (ctx, arbprogram_syn_id,
3872 (byte *) "vertex_blend", (byte *) "GL_EXT_vertex_weighting") ||
3873 enable_ext (ctx, arbprogram_syn_id,
3874 (byte *) "matrix_palette", (byte *) "GL_ARB_matrix_palette") ||
3875 enable_ext (ctx, arbprogram_syn_id,
3876 (byte *) "point_parameters", (byte *) "GL_ARB_point_parameters") ||
3877 enable_ext (ctx, arbprogram_syn_id,
3878 (byte *) "point_parameters", (byte *) "GL_EXT_point_parameters") ||
3879 enable_ext (ctx, arbprogram_syn_id,
3880 (byte *) "secondary_color", (byte *) "GL_EXT_secondary_color") ||
3881 enable_ext (ctx, arbprogram_syn_id,
3882 (byte *) "fog_coord", (byte *) "GL_EXT_fog_coord") ||
3883 enable_ext (ctx, arbprogram_syn_id,
Brian Pauledfe0fe2004-08-20 14:21:20 +00003884 (byte *) "texture_rectangle", (byte *) "GL_ARB_texture_rectangle") ||
3885 enable_ext (ctx, arbprogram_syn_id,
Jouk Jansen40322e12004-04-05 08:50:36 +00003886 (byte *) "texture_rectangle", (byte *) "GL_EXT_texture_rectangle") ||
3887 enable_ext (ctx, arbprogram_syn_id,
3888 (byte *) "texture_rectangle", (byte *) "GL_NV_texture_rectangle") ||
3889 enable_ext (ctx, arbprogram_syn_id,
Michal Krolad22ce82004-10-11 08:13:25 +00003890 (byte *) "fragment_program_shadow", (byte *) "GL_ARB_fragment_program_shadow") ||
3891 enable_ext (ctx, arbprogram_syn_id,
3892 (byte *) "draw_buffers", (byte *) "GL_ARB_draw_buffers")) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003893 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003894 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003895 }
3896
3897 /* check for NULL character occurences */
3898 {
3899 int i;
3900 for (i = 0; i < len; i++)
3901 if (str[i] == '\0') {
3902 _mesa_set_program_error (ctx, i, "invalid character");
3903 _mesa_error (ctx, GL_INVALID_OPERATION, "Lexical Error");
3904
3905 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003906 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003907 }
3908 }
3909
3910 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003911 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003912 if (!strz) {
3913 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glprogramStringARB");
3914 return GL_FALSE;
3915 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003916 _mesa_memcpy (strz, str, len);
3917 strz[len] = '\0';
3918
3919#if DEBUG_PARSING
Brian Paulb3aefd12005-09-19 20:12:32 +00003920 fprintf (stderr, "Checking Grammar!\n");
Jouk Jansen40322e12004-04-05 08:50:36 +00003921#endif
Michal Krolb80bc052004-10-21 14:09:54 +00003922 /* do a fast check on program string - initial production buffer is 4K */
3923 err = grammar_fast_check (arbprogram_syn_id, strz, &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003924
3925 /* Syntax parse error */
3926 if (err == 0) {
3927 _mesa_free (strz);
3928 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3929 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul45703642005-10-29 15:52:31 +00003930 _mesa_error (ctx, GL_INVALID_OPERATION, "glprogramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003931
3932 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003933#if DEBUG_PARSING
3934 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003935 int line, col;
3936 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003937 fprintf(stderr, "program: %s\n", (char *) strz);
3938 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
3939 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003940 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3941 } while (0)
3942#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003943
Jouk Jansen40322e12004-04-05 08:50:36 +00003944 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003945 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003946 }
3947
3948#if DEBUG_PARSING
Brian Paulb3aefd12005-09-19 20:12:32 +00003949 fprintf (stderr, "Destroying grammer dict [parse retval: %d]\n", err);
Jouk Jansen40322e12004-04-05 08:50:36 +00003950#endif
3951 grammar_destroy (arbprogram_syn_id);
3952
3953 /* Initialize the arb_program struct */
3954 program->Base.String = strz;
3955 program->Base.NumInstructions =
3956 program->Base.NumTemporaries =
3957 program->Base.NumParameters =
3958 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
3959 program->Parameters = _mesa_new_parameter_list ();
Brian Paul45cd2f92005-11-03 02:25:10 +00003960 program->InputsRead = 0x0;
3961 program->OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003962 program->Position = 0;
3963 program->MajorVersion = program->MinorVersion = 0;
3964 program->PrecisionOption = GL_DONT_CARE;
3965 program->FogOption = GL_NONE;
3966 program->HintPositionInvariant = GL_FALSE;
3967 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003968 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003969 program->NumAluInstructions =
3970 program->NumTexInstructions =
3971 program->NumTexIndirections = 0;
3972
Keith Whitwellc3626a92005-11-01 17:25:49 +00003973 program->UsesKill = 0;
3974
Jouk Jansen40322e12004-04-05 08:50:36 +00003975 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003976 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003977
3978 /* Start examining the tokens in the array */
3979 inst = parsed;
3980
3981 /* Check the grammer rev */
3982 if (*inst++ != REVISION) {
3983 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003984 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul45cd2f92005-11-03 02:25:10 +00003985 "glProgramStringARB(Grammar version mismatch)");
Brian Paul45703642005-10-29 15:52:31 +00003986 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003987 }
3988 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003989 /* ignore program target */
3990 inst++;
Jouk Jansen40322e12004-04-05 08:50:36 +00003991
3992 err = parse_arb_program (ctx, inst, &vc_head, program);
3993#if DEBUG_PARSING
3994 fprintf (stderr, "Symantic analysis returns %d [1 is bad!]\n", err);
3995#endif
3996 }
3997
3998 /*debug_variables(ctx, vc_head, program); */
3999
4000 /* We're done with the parsed binary array */
4001 var_cache_destroy (&vc_head);
4002
4003 _mesa_free (parsed);
4004#if DEBUG_PARSING
Brian Paulb3aefd12005-09-19 20:12:32 +00004005 fprintf (stderr, "_mesa_parse_arb_program() done\n");
Jouk Jansen40322e12004-04-05 08:50:36 +00004006#endif
Brian Paul45703642005-10-29 15:52:31 +00004007
4008 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004009}