blob: ae85a93ecf9f271e30769a10a6d9615a958d92cc [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
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"
44
Brian Paula6c423d2004-08-25 15:59:48 +000045#if !defined(__GNUC__) && !defined(__extension__)
46# define __extension__
47#endif
48
Jouk Jansen40322e12004-04-05 08:50:36 +000049/* TODO:
50 * Fragment Program Stuff:
51 * -----------------------------------------------------
52 *
53 * - things from Michal's email
54 * + overflow on atoi
55 * + not-overflowing floats (don't use parse_integer..)
56 * + can remove range checking in arbparse.c
57 *
58 * - check all limits of number of various variables
59 * + parameters
60 *
61 * - test! test! test!
62 *
63 * Vertex Program Stuff:
64 * -----------------------------------------------------
65 * - Optimize param array usage and count limits correctly, see spec,
66 * section 2.14.3.7
67 * + Record if an array is reference absolutly or relatively (or both)
68 * + For absolute arrays, store a bitmap of accesses
69 * + For single parameters, store an access flag
70 * + After parsing, make a parameter cleanup and merging pass, where
71 * relative arrays are layed out first, followed by abs arrays, and
72 * finally single state.
73 * + Remap offsets for param src and dst registers
74 * + Now we can properly count parameter usage
75 *
76 * - Multiple state binding errors in param arrays (see spec, just before
77 * section 2.14.3.3)
78 * - grep for XXX
79 *
80 * Mesa Stuff
81 * -----------------------------------------------------
82 * - User clipping planes vs. PositionInvariant
83 * - Is it sufficient to just multiply by the mvp to transform in the
84 * PositionInvariant case? Or do we need something more involved?
85 *
86 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
87 * - fetch state listed in program_parameters list
88 * + WTF should this go???
89 * + currently in nvvertexec.c and s_nvfragprog.c
90 *
91 * - allow for multiple address registers (and fetch address regs properly)
92 *
93 * Cosmetic Stuff
94 * -----------------------------------------------------
95 * - remove any leftover unused grammer.c stuff (dict_ ?)
96 * - fix grammer.c error handling so its not static
97 * - #ifdef around stuff pertaining to extentions
98 *
99 * Outstanding Questions:
100 * -----------------------------------------------------
101 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
102 * what gets hacked off because of this:
103 * + VERTEX_ATTRIB_MATRIXINDEX
104 * + VERTEX_ATTRIB_WEIGHT
105 * + MATRIX_MODELVIEW
106 * + MATRIX_PALETTE
107 *
108 * - When can we fetch env/local params from their own register files, and
109 * when to we have to fetch them into the main state register file?
110 * (think arrays)
111 *
112 * Grammar Changes:
113 * -----------------------------------------------------
114 */
115
116/* Changes since moving the file to shader directory
117
1182004-III-4 ------------------------------------------------------------
119- added #include "grammar_mesa.h"
120- removed grammar specific code part (it resides now in grammar.c)
121- added GL_ARB_fragment_program_shadow tokens
122- modified #include "arbparse_syn.h"
123- major changes inside _mesa_parse_arb_program()
124- check the program string for '\0' characters
125- copy the program string to a one-byte-longer location to have
126 it null-terminated
127- position invariance test (not writing to result.position) moved
128 to syntax part
129*/
130
131typedef GLubyte *production;
132
133/**
134 * This is the text describing the rules to parse the grammar
135 */
Brian Paula6c423d2004-08-25 15:59:48 +0000136__extension__ static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000137#include "arbprogram_syn.h"
138;
139
140/**
141 * These should match up with the values defined in arbprogram.syn
142 */
143
144/*
145 Changes:
146 - changed and merged V_* and F_* opcode values to OP_*.
147 - added GL_ARB_fragment_program_shadow specific tokens (michal)
148*/
Brian Paulbe76b7f2004-10-04 14:40:05 +0000149#define REVISION 0x08
Jouk Jansen40322e12004-04-05 08:50:36 +0000150
151/* program type */
152#define FRAGMENT_PROGRAM 0x01
153#define VERTEX_PROGRAM 0x02
154
155/* program section */
156#define OPTION 0x01
157#define INSTRUCTION 0x02
158#define DECLARATION 0x03
159#define END 0x04
160
161/* GL_ARB_fragment_program option flags */
162#define ARB_PRECISION_HINT_FASTEST 0x01
163#define ARB_PRECISION_HINT_NICEST 0x02
164#define ARB_FOG_EXP 0x04
165#define ARB_FOG_EXP2 0x08
166#define ARB_FOG_LINEAR 0x10
167
168/* GL_ARB_vertex_program option flags */
169#define ARB_POSITION_INVARIANT 0x20
170
171/* GL_ARB_fragment_program_shadow option flags */
172#define ARB_FRAGMENT_PROGRAM_SHADOW 0x40
173
174/* GL_ARB_fragment_program instruction class */
175#define OP_ALU_INST 0x00
176#define OP_TEX_INST 0x01
177
178/* GL_ARB_vertex_program instruction class */
179/* OP_ALU_INST */
180
181/* GL_ARB_fragment_program instruction type */
182#define OP_ALU_VECTOR 0x00
183#define OP_ALU_SCALAR 0x01
184#define OP_ALU_BINSC 0x02
185#define OP_ALU_BIN 0x03
186#define OP_ALU_TRI 0x04
187#define OP_ALU_SWZ 0x05
188#define OP_TEX_SAMPLE 0x06
189#define OP_TEX_KIL 0x07
190
191/* GL_ARB_vertex_program instruction type */
192#define OP_ALU_ARL 0x08
193/* OP_ALU_VECTOR */
194/* OP_ALU_SCALAR */
195/* OP_ALU_BINSC */
196/* OP_ALU_BIN */
197/* OP_ALU_TRI */
198/* OP_ALU_SWZ */
199
200/* GL_ARB_fragment_program instruction code */
201#define OP_ABS 0x00
202#define OP_ABS_SAT 0x1B
203#define OP_FLR 0x09
204#define OP_FLR_SAT 0x26
205#define OP_FRC 0x0A
206#define OP_FRC_SAT 0x27
207#define OP_LIT 0x0C
208#define OP_LIT_SAT 0x2A
209#define OP_MOV 0x11
210#define OP_MOV_SAT 0x30
211#define OP_COS 0x1F
212#define OP_COS_SAT 0x20
213#define OP_EX2 0x07
214#define OP_EX2_SAT 0x25
215#define OP_LG2 0x0B
216#define OP_LG2_SAT 0x29
217#define OP_RCP 0x14
218#define OP_RCP_SAT 0x33
219#define OP_RSQ 0x15
220#define OP_RSQ_SAT 0x34
221#define OP_SIN 0x38
222#define OP_SIN_SAT 0x39
223#define OP_SCS 0x35
224#define OP_SCS_SAT 0x36
225#define OP_POW 0x13
226#define OP_POW_SAT 0x32
227#define OP_ADD 0x01
228#define OP_ADD_SAT 0x1C
229#define OP_DP3 0x03
230#define OP_DP3_SAT 0x21
231#define OP_DP4 0x04
232#define OP_DP4_SAT 0x22
233#define OP_DPH 0x05
234#define OP_DPH_SAT 0x23
235#define OP_DST 0x06
236#define OP_DST_SAT 0x24
237#define OP_MAX 0x0F
238#define OP_MAX_SAT 0x2E
239#define OP_MIN 0x10
240#define OP_MIN_SAT 0x2F
241#define OP_MUL 0x12
242#define OP_MUL_SAT 0x31
243#define OP_SGE 0x16
244#define OP_SGE_SAT 0x37
245#define OP_SLT 0x17
246#define OP_SLT_SAT 0x3A
247#define OP_SUB 0x18
248#define OP_SUB_SAT 0x3B
249#define OP_XPD 0x1A
250#define OP_XPD_SAT 0x43
251#define OP_CMP 0x1D
252#define OP_CMP_SAT 0x1E
253#define OP_LRP 0x2B
254#define OP_LRP_SAT 0x2C
255#define OP_MAD 0x0E
256#define OP_MAD_SAT 0x2D
257#define OP_SWZ 0x19
258#define OP_SWZ_SAT 0x3C
259#define OP_TEX 0x3D
260#define OP_TEX_SAT 0x3E
261#define OP_TXB 0x3F
262#define OP_TXB_SAT 0x40
263#define OP_TXP 0x41
264#define OP_TXP_SAT 0x42
265#define OP_KIL 0x28
266
267/* GL_ARB_vertex_program instruction code */
268#define OP_ARL 0x02
269/* OP_ABS */
270/* OP_FLR */
271/* OP_FRC */
272/* OP_LIT */
273/* OP_MOV */
274/* OP_EX2 */
275#define OP_EXP 0x08
276/* OP_LG2 */
277#define OP_LOG 0x0D
278/* OP_RCP */
279/* OP_RSQ */
280/* OP_POW */
281/* OP_ADD */
282/* OP_DP3 */
283/* OP_DP4 */
284/* OP_DPH */
285/* OP_DST */
286/* OP_MAX */
287/* OP_MIN */
288/* OP_MUL */
289/* OP_SGE */
290/* OP_SLT */
291/* OP_SUB */
292/* OP_XPD */
293/* OP_MAD */
294/* OP_SWZ */
295
296/* fragment attribute binding */
297#define FRAGMENT_ATTRIB_COLOR 0x01
298#define FRAGMENT_ATTRIB_TEXCOORD 0x02
299#define FRAGMENT_ATTRIB_FOGCOORD 0x03
300#define FRAGMENT_ATTRIB_POSITION 0x04
301
302/* vertex attribute binding */
303#define VERTEX_ATTRIB_POSITION 0x01
304#define VERTEX_ATTRIB_WEIGHT 0x02
305#define VERTEX_ATTRIB_NORMAL 0x03
306#define VERTEX_ATTRIB_COLOR 0x04
307#define VERTEX_ATTRIB_FOGCOORD 0x05
308#define VERTEX_ATTRIB_TEXCOORD 0x06
309#define VERTEX_ATTRIB_MATRIXINDEX 0x07
310#define VERTEX_ATTRIB_GENERIC 0x08
311
312/* fragment result binding */
313#define FRAGMENT_RESULT_COLOR 0x01
314#define FRAGMENT_RESULT_DEPTH 0x02
315
316/* vertex result binding */
317#define VERTEX_RESULT_POSITION 0x01
318#define VERTEX_RESULT_COLOR 0x02
319#define VERTEX_RESULT_FOGCOORD 0x03
320#define VERTEX_RESULT_POINTSIZE 0x04
321#define VERTEX_RESULT_TEXCOORD 0x05
322
323/* texture target */
324#define TEXTARGET_1D 0x01
325#define TEXTARGET_2D 0x02
326#define TEXTARGET_3D 0x03
327#define TEXTARGET_RECT 0x04
328#define TEXTARGET_CUBE 0x05
329/* GL_ARB_fragment_program_shadow */
330#define TEXTARGET_SHADOW1D 0x06
331#define TEXTARGET_SHADOW2D 0x07
332#define TEXTARGET_SHADOWRECT 0x08
333
334/* face type */
335#define FACE_FRONT 0x00
336#define FACE_BACK 0x01
337
338/* color type */
339#define COLOR_PRIMARY 0x00
340#define COLOR_SECONDARY 0x01
341
342/* component */
343#define COMPONENT_X 0x00
344#define COMPONENT_Y 0x01
345#define COMPONENT_Z 0x02
346#define COMPONENT_W 0x03
347#define COMPONENT_0 0x04
348#define COMPONENT_1 0x05
349
350/* array index type */
351#define ARRAY_INDEX_ABSOLUTE 0x00
352#define ARRAY_INDEX_RELATIVE 0x01
353
354/* matrix name */
355#define MATRIX_MODELVIEW 0x01
356#define MATRIX_PROJECTION 0x02
357#define MATRIX_MVP 0x03
358#define MATRIX_TEXTURE 0x04
359#define MATRIX_PALETTE 0x05
360#define MATRIX_PROGRAM 0x06
361
362/* matrix modifier */
363#define MATRIX_MODIFIER_IDENTITY 0x00
364#define MATRIX_MODIFIER_INVERSE 0x01
365#define MATRIX_MODIFIER_TRANSPOSE 0x02
366#define MATRIX_MODIFIER_INVTRANS 0x03
367
368/* constant type */
369#define CONSTANT_SCALAR 0x01
370#define CONSTANT_VECTOR 0x02
371
372/* program param type */
373#define PROGRAM_PARAM_ENV 0x01
374#define PROGRAM_PARAM_LOCAL 0x02
375
376/* register type */
377#define REGISTER_ATTRIB 0x01
378#define REGISTER_PARAM 0x02
379#define REGISTER_RESULT 0x03
380#define REGISTER_ESTABLISHED_NAME 0x04
381
382/* param binding */
383#define PARAM_NULL 0x00
384#define PARAM_ARRAY_ELEMENT 0x01
385#define PARAM_STATE_ELEMENT 0x02
386#define PARAM_PROGRAM_ELEMENT 0x03
387#define PARAM_PROGRAM_ELEMENTS 0x04
388#define PARAM_CONSTANT 0x05
389
390/* param state property */
391#define STATE_MATERIAL_PARSER 0x01
392#define STATE_LIGHT_PARSER 0x02
393#define STATE_LIGHT_MODEL 0x03
394#define STATE_LIGHT_PROD 0x04
395#define STATE_FOG 0x05
396#define STATE_MATRIX_ROWS 0x06
397/* GL_ARB_fragment_program */
398#define STATE_TEX_ENV 0x07
399#define STATE_DEPTH 0x08
400/* GL_ARB_vertex_program */
401#define STATE_TEX_GEN 0x09
402#define STATE_CLIP_PLANE 0x0A
403#define STATE_POINT 0x0B
404
405/* state material property */
406#define MATERIAL_AMBIENT 0x01
407#define MATERIAL_DIFFUSE 0x02
408#define MATERIAL_SPECULAR 0x03
409#define MATERIAL_EMISSION 0x04
410#define MATERIAL_SHININESS 0x05
411
412/* state light property */
413#define LIGHT_AMBIENT 0x01
414#define LIGHT_DIFFUSE 0x02
415#define LIGHT_SPECULAR 0x03
416#define LIGHT_POSITION 0x04
417#define LIGHT_ATTENUATION 0x05
418#define LIGHT_HALF 0x06
419#define LIGHT_SPOT_DIRECTION 0x07
420
421/* state light model property */
422#define LIGHT_MODEL_AMBIENT 0x01
423#define LIGHT_MODEL_SCENECOLOR 0x02
424
425/* state light product property */
426#define LIGHT_PROD_AMBIENT 0x01
427#define LIGHT_PROD_DIFFUSE 0x02
428#define LIGHT_PROD_SPECULAR 0x03
429
430/* state texture environment property */
431#define TEX_ENV_COLOR 0x01
432
433/* state texture generation coord property */
434#define TEX_GEN_EYE 0x01
435#define TEX_GEN_OBJECT 0x02
436
437/* state fog property */
438#define FOG_COLOR 0x01
439#define FOG_PARAMS 0x02
440
441/* state depth property */
442#define DEPTH_RANGE 0x01
443
444/* state point parameters property */
445#define POINT_SIZE 0x01
446#define POINT_ATTENUATION 0x02
447
448/* declaration */
449#define ATTRIB 0x01
450#define PARAM 0x02
451#define TEMP 0x03
452#define OUTPUT 0x04
453#define ALIAS 0x05
454/* GL_ARB_vertex_program */
455#define ADDRESS 0x06
456
457/*-----------------------------------------------------------------------
458 * From here on down is the semantic checking portion
459 *
460 */
461
462/**
463 * Variable Table Handling functions
464 */
465typedef enum
466{
467 vt_none,
468 vt_address,
469 vt_attrib,
470 vt_param,
471 vt_temp,
472 vt_output,
473 vt_alias
474} var_type;
475
476
477/*
478 * Setting an explicit field for each of the binding properties is a bit wasteful
479 * of space, but it should be much more clear when reading later on..
480 */
481struct var_cache
482{
483 GLubyte *name;
484 var_type type;
485 GLuint address_binding; /* The index of the address register we should
486 * be using */
487 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
488 GLuint attrib_binding_idx; /* The index into the attrib register file corresponding
489 * to the state in attrib_binding */
490 GLuint attrib_is_generic; /* If the attrib was specified through a generic
491 * vertex attrib */
492 GLuint temp_binding; /* The index of the temp register we are to use */
493 GLuint output_binding; /* For type vt_output, see nvfragprog.h for values */
494 GLuint output_binding_idx; /* This is the index into the result register file
495 * corresponding to the bound result state */
496 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
497 * that this is aliased to */
498 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
499 * PROGRAM_ENV_PARAM} */
500 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
501 * the tokens representing our bound state (or constants)
502 * start */
503 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
504 * we take up with our state tokens or constants. Note that
505 * this is _not_ the same as the number of param registers
506 * we eventually use */
507 struct var_cache *next;
508};
509
510static GLvoid
511var_cache_create (struct var_cache **va)
512{
513 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
514 if (*va) {
515 (**va).name = NULL;
516 (**va).type = vt_none;
517 (**va).attrib_binding = ~0;
518 (**va).attrib_is_generic = 0;
519 (**va).temp_binding = ~0;
520 (**va).output_binding = ~0;
521 (**va).output_binding_idx = ~0;
522 (**va).param_binding_type = ~0;
523 (**va).param_binding_begin = ~0;
524 (**va).param_binding_length = ~0;
525 (**va).alias_binding = NULL;
526 (**va).next = NULL;
527 }
528}
529
530static GLvoid
531var_cache_destroy (struct var_cache **va)
532{
533 if (*va) {
534 var_cache_destroy (&(**va).next);
535 _mesa_free (*va);
536 *va = NULL;
537 }
538}
539
540static GLvoid
541var_cache_append (struct var_cache **va, struct var_cache *nv)
542{
543 if (*va)
544 var_cache_append (&(**va).next, nv);
545 else
546 *va = nv;
547}
548
549static struct var_cache *
550var_cache_find (struct var_cache *va, GLubyte * name)
551{
552 struct var_cache *first = va;
553
554 while (va) {
555 if (!strcmp ( (const char*) name, (const char*) va->name)) {
556 if (va->type == vt_alias)
557 return var_cache_find (first, va->name);
558 return va;
559 }
560
561 va = va->next;
562 }
563
564 return NULL;
565}
566
567/**
568 * constructs an integer from 4 GLubytes in LE format
569 */
570static GLuint
571parse_position (GLubyte ** inst)
572{
573 GLuint value;
574
575 value = (GLuint) (*(*inst)++);
576 value += (GLuint) (*(*inst)++) * 0x100;
577 value += (GLuint) (*(*inst)++) * 0x10000;
578 value += (GLuint) (*(*inst)++) * 0x1000000;
579
580 return value;
581}
582
583/**
584 * This will, given a string, lookup the string as a variable name in the
585 * var cache. If the name is found, the var cache node corresponding to the
586 * var name is returned. If it is not found, a new entry is allocated
587 *
588 * \param I Points into the binary array where the string identifier begins
589 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
590 * \return The location on the var_cache corresponding the the string starting at I
591 */
592static struct var_cache *
593parse_string (GLubyte ** inst, struct var_cache **vc_head,
594 struct arb_program *Program, GLuint * found)
595{
596 GLubyte *i = *inst;
597 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000598 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000599
600 *inst += _mesa_strlen ((char *) i) + 1;
601
602 va = var_cache_find (*vc_head, i);
603
604 if (va) {
605 *found = 1;
606 return va;
607 }
608
609 *found = 0;
610 var_cache_create (&va);
611 va->name = i;
612
613 var_cache_append (vc_head, va);
614
615 return va;
616}
617
618static char *
619parse_string_without_adding (GLubyte ** inst, struct arb_program *Program)
620{
621 GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000622 (void) Program;
623
Jouk Jansen40322e12004-04-05 08:50:36 +0000624 *inst += _mesa_strlen ((char *) i) + 1;
625
626 return (char *) i;
627}
628
629/**
Brian Paul05908952004-06-08 15:20:23 +0000630 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000631 */
Brian Paul05908952004-06-08 15:20:23 +0000632static GLint
Jouk Jansen40322e12004-04-05 08:50:36 +0000633parse_sign (GLubyte ** inst)
634{
635 /*return *(*inst)++ != '+'; */
636
637 if (**inst == '-') {
638 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000639 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000640 }
641 else if (**inst == '+') {
642 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000643 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000644 }
645
Brian Paul05908952004-06-08 15:20:23 +0000646 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000647}
648
649/**
650 * parses and returns signed integer
651 */
652static GLint
653parse_integer (GLubyte ** inst, struct arb_program *Program)
654{
655 GLint sign;
656 GLint value;
657
658 /* check if *inst points to '+' or '-'
659 * if yes, grab the sign and increment *inst
660 */
661 sign = parse_sign (inst);
662
663 /* now check if *inst points to 0
664 * if yes, increment the *inst and return the default value
665 */
666 if (**inst == 0) {
667 (*inst)++;
668 return 0;
669 }
670
671 /* parse the integer as you normally would do it */
672 value = _mesa_atoi (parse_string_without_adding (inst, Program));
673
674 /* now, after terminating 0 there is a position
675 * to parse it - parse_position()
676 */
677 Program->Position = parse_position (inst);
678
Brian Paul05908952004-06-08 15:20:23 +0000679 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000680}
681
682/**
683 */
684static GLfloat
685parse_float (GLubyte ** inst, struct arb_program *Program)
686{
687 GLint tmp[5], denom;
688 GLuint leading_zeros =0;
689 GLfloat value = 0;
690
Jouk Jansen40322e12004-04-05 08:50:36 +0000691 tmp[1] = parse_integer (inst, Program); /* This is the integer portion of the number */
692
693 /* Now we grab the fractional portion of the number (the digits after
694 * the .). We can have leading 0's here, which parse_integer will ignore,
695 * so we'll check for those first
696 */
697 while ((**inst == '0') && ( *(*inst+1) != 0))
698 {
699 leading_zeros++;
700 (*inst)++;
701 }
702 tmp[2] = parse_integer (inst, Program); /* This is the fractional portion of the number */
703 tmp[3] = parse_sign (inst); /* This is the sign of the exponent */
704 tmp[4] = parse_integer (inst, Program); /* This is the exponent */
705
706 value = (GLfloat) tmp[1];
707 denom = 1;
708 while (denom < tmp[2])
709 denom *= 10;
710 denom *= (GLint) _mesa_pow( 10, leading_zeros );
711 value += (GLfloat) tmp[2] / (GLfloat) denom;
Brian Paul05908952004-06-08 15:20:23 +0000712
Jouk Jansen40322e12004-04-05 08:50:36 +0000713 value *= (GLfloat) _mesa_pow (10, (GLfloat) tmp[3] * (GLfloat) tmp[4]);
714
715 return value;
716}
717
718
719/**
720 */
721static GLfloat
722parse_signed_float (GLubyte ** inst, struct arb_program *Program)
723{
Brian Paul05908952004-06-08 15:20:23 +0000724 GLint sign = parse_sign (inst);
725 GLfloat value = parse_float (inst, Program);
726 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000727}
728
729/**
730 * This picks out a constant value from the parsed array. The constant vector is r
731 * returned in the *values array, which should be of length 4.
732 *
733 * \param values - The 4 component vector with the constant value in it
734 */
735static GLvoid
736parse_constant (GLubyte ** inst, GLfloat *values, struct arb_program *Program,
737 GLboolean use)
738{
739 GLuint components, i;
740
741
742 switch (*(*inst)++) {
743 case CONSTANT_SCALAR:
744 if (use == GL_TRUE) {
745 values[0] =
746 values[1] =
747 values[2] = values[3] = parse_float (inst, Program);
748 }
749 else {
750 values[0] =
751 values[1] =
752 values[2] = values[3] = parse_signed_float (inst, Program);
753 }
754
755 break;
756 case CONSTANT_VECTOR:
757 values[0] = values[1] = values[2] = 0;
758 values[3] = 1;
759 components = *(*inst)++;
760 for (i = 0; i < components; i++) {
761 values[i] = parse_signed_float (inst, Program);
762 }
763 break;
764 }
765}
766
767/**
768 * \param offset The offset from the address register that we should
769 * address
770 *
771 * \return 0 on sucess, 1 on error
772 */
773static GLuint
774parse_relative_offset (GLcontext *ctx, GLubyte **inst, struct arb_program *Program,
775 GLint *offset)
776{
777 *offset = parse_integer(inst, Program);
778 if ((*offset > 63) || (*offset < -64)) {
779 _mesa_set_program_error (ctx, Program->Position,
780 "Relative offset out of range");
781 _mesa_error (ctx, GL_INVALID_OPERATION, "Relative offset %d out of range",
782 *offset);
783 return 1;
784 }
785
786 return 0;
787}
788
789/**
790 * \param color 0 if color type is primary, 1 if color type is secondary
791 * \return 0 on sucess, 1 on error
792 */
793static GLuint
794parse_color_type (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
795 GLint * color)
796{
Brian Paula6c423d2004-08-25 15:59:48 +0000797 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000798 *color = *(*inst)++ != COLOR_PRIMARY;
799 return 0;
800}
801
802/**
803 * Get an integer corresponding to a generic vertex attribute.
804 *
805 * \return 0 on sucess, 1 on error
806 */
807static GLuint
808parse_generic_attrib_num(GLcontext *ctx, GLubyte ** inst,
809 struct arb_program *Program, GLuint *attrib)
810{
Brian Paulbd997cd2004-07-20 21:12:56 +0000811 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000812
Brian Paulbd997cd2004-07-20 21:12:56 +0000813 if ((i < 0) || (i > MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000814 {
815 _mesa_set_program_error (ctx, Program->Position,
816 "Invalid generic vertex attribute index");
817 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid generic vertex attribute index");
818
819 return 1;
820 }
821
Brian Paulbd997cd2004-07-20 21:12:56 +0000822 *attrib = (GLuint) i;
823
Jouk Jansen40322e12004-04-05 08:50:36 +0000824 return 0;
825}
826
827
828/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000829 * \param color The index of the color buffer to write into
830 * \return 0 on sucess, 1 on error
831 */
832static GLuint
833parse_output_color_num (GLcontext * ctx, GLubyte ** inst,
834 struct arb_program *Program, GLuint * color)
835{
836 GLint i = parse_integer (inst, Program);
837
838 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
839 _mesa_set_program_error (ctx, Program->Position,
840 "Invalid draw buffer index");
841 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid draw buffer index");
842 return 1;
843 }
844
845 *color = (GLuint) i;
846 return 0;
847}
848
849
850/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000851 * \param coord The texture unit index
852 * \return 0 on sucess, 1 on error
853 */
854static GLuint
855parse_texcoord_num (GLcontext * ctx, GLubyte ** inst,
856 struct arb_program *Program, GLuint * coord)
857{
Brian Paulbd997cd2004-07-20 21:12:56 +0000858 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000859
Brian Paula6c423d2004-08-25 15:59:48 +0000860 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000861 _mesa_set_program_error (ctx, Program->Position,
862 "Invalid texture unit index");
863 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid texture unit index");
864 return 1;
865 }
866
Brian Paulbd997cd2004-07-20 21:12:56 +0000867 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000868 return 0;
869}
870
871/**
872 * \param coord The weight index
873 * \return 0 on sucess, 1 on error
874 */
875static GLuint
876parse_weight_num (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
877 GLint * coord)
878{
879 *coord = parse_integer (inst, Program);
880
881 if ((*coord < 0) || (*coord >= 1)) {
882 _mesa_set_program_error (ctx, Program->Position,
883 "Invalid weight index");
884 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid weight index");
885 return 1;
886 }
887
888 return 0;
889}
890
891/**
892 * \param coord The clip plane index
893 * \return 0 on sucess, 1 on error
894 */
895static GLuint
896parse_clipplane_num (GLcontext * ctx, GLubyte ** inst,
897 struct arb_program *Program, GLint * coord)
898{
899 *coord = parse_integer (inst, Program);
900
901 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
902 _mesa_set_program_error (ctx, Program->Position,
903 "Invalid clip plane index");
904 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid clip plane index");
905 return 1;
906 }
907
908 return 0;
909}
910
911
912/**
913 * \return 0 on front face, 1 on back face
914 */
915static GLuint
916parse_face_type (GLubyte ** inst)
917{
918 switch (*(*inst)++) {
919 case FACE_FRONT:
920 return 0;
921
922 case FACE_BACK:
923 return 1;
924 }
925 return 0;
926}
927
928
929/**
930 * Given a matrix and a modifier token on the binary array, return tokens
931 * that _mesa_fetch_state() [program.c] can understand.
932 *
933 * \param matrix - the matrix we are talking about
934 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
935 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
936 * \return 0 on sucess, 1 on failure
937 */
938static GLuint
939parse_matrix (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
940 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
941{
942 GLubyte mat = *(*inst)++;
943
944 *matrix_idx = 0;
945
946 switch (mat) {
947 case MATRIX_MODELVIEW:
948 *matrix = STATE_MODELVIEW;
949 *matrix_idx = parse_integer (inst, Program);
950 if (*matrix_idx > 0) {
951 _mesa_set_program_error (ctx, Program->Position,
952 "ARB_vertex_blend not supported\n");
953 _mesa_error (ctx, GL_INVALID_OPERATION,
954 "ARB_vertex_blend not supported\n");
955 return 1;
956 }
957 break;
958
959 case MATRIX_PROJECTION:
960 *matrix = STATE_PROJECTION;
961 break;
962
963 case MATRIX_MVP:
964 *matrix = STATE_MVP;
965 break;
966
967 case MATRIX_TEXTURE:
968 *matrix = STATE_TEXTURE;
969 *matrix_idx = parse_integer (inst, Program);
970 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
971 _mesa_set_program_error (ctx, Program->Position,
972 "Invalid Texture Unit");
973 _mesa_error (ctx, GL_INVALID_OPERATION,
974 "Invalid Texture Unit: %d", *matrix_idx);
975 return 1;
976 }
977 break;
978
979 /* This is not currently supported (ARB_matrix_palette) */
980 case MATRIX_PALETTE:
981 *matrix_idx = parse_integer (inst, Program);
982 _mesa_set_program_error (ctx, Program->Position,
983 "ARB_matrix_palette not supported\n");
984 _mesa_error (ctx, GL_INVALID_OPERATION,
985 "ARB_matrix_palette not supported\n");
986 return 1;
987 break;
988
989 case MATRIX_PROGRAM:
990 *matrix = STATE_PROGRAM;
991 *matrix_idx = parse_integer (inst, Program);
992 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
993 _mesa_set_program_error (ctx, Program->Position,
994 "Invalid Program Matrix");
995 _mesa_error (ctx, GL_INVALID_OPERATION,
996 "Invalid Program Matrix: %d", *matrix_idx);
997 return 1;
998 }
999 break;
1000 }
1001
1002 switch (*(*inst)++) {
1003 case MATRIX_MODIFIER_IDENTITY:
1004 *matrix_modifier = 0;
1005 break;
1006 case MATRIX_MODIFIER_INVERSE:
1007 *matrix_modifier = STATE_MATRIX_INVERSE;
1008 break;
1009 case MATRIX_MODIFIER_TRANSPOSE:
1010 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1011 break;
1012 case MATRIX_MODIFIER_INVTRANS:
1013 *matrix_modifier = STATE_MATRIX_INVTRANS;
1014 break;
1015 }
1016
1017 return 0;
1018}
1019
1020
1021/**
1022 * This parses a state string (rather, the binary version of it) into
1023 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1024 *
1025 * \param inst - the start in the binary arry to start working from
1026 * \param state_tokens - the storage for the 6-token state description
1027 * \return - 0 on sucess, 1 on error
1028 */
1029static GLuint
1030parse_state_single_item (GLcontext * ctx, GLubyte ** inst,
1031 struct arb_program *Program, GLint * state_tokens)
1032{
1033 switch (*(*inst)++) {
1034 case STATE_MATERIAL_PARSER:
1035 state_tokens[0] = STATE_MATERIAL;
1036 state_tokens[1] = parse_face_type (inst);
1037 switch (*(*inst)++) {
1038 case MATERIAL_AMBIENT:
1039 state_tokens[2] = STATE_AMBIENT;
1040 break;
1041 case MATERIAL_DIFFUSE:
1042 state_tokens[2] = STATE_DIFFUSE;
1043 break;
1044 case MATERIAL_SPECULAR:
1045 state_tokens[2] = STATE_SPECULAR;
1046 break;
1047 case MATERIAL_EMISSION:
1048 state_tokens[2] = STATE_EMISSION;
1049 break;
1050 case MATERIAL_SHININESS:
1051 state_tokens[2] = STATE_SHININESS;
1052 break;
1053 }
1054 break;
1055
1056 case STATE_LIGHT_PARSER:
1057 state_tokens[0] = STATE_LIGHT;
1058 state_tokens[1] = parse_integer (inst, Program);
1059
1060 /* Check the value of state_tokens[1] against the # of lights */
1061 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1062 _mesa_set_program_error (ctx, Program->Position,
1063 "Invalid Light Number");
1064 _mesa_error (ctx, GL_INVALID_OPERATION,
1065 "Invalid Light Number: %d", state_tokens[1]);
1066 return 1;
1067 }
1068
1069 switch (*(*inst)++) {
1070 case LIGHT_AMBIENT:
1071 state_tokens[2] = STATE_AMBIENT;
1072 break;
1073 case LIGHT_DIFFUSE:
1074 state_tokens[2] = STATE_DIFFUSE;
1075 break;
1076 case LIGHT_SPECULAR:
1077 state_tokens[2] = STATE_SPECULAR;
1078 break;
1079 case LIGHT_POSITION:
1080 state_tokens[2] = STATE_POSITION;
1081 break;
1082 case LIGHT_ATTENUATION:
1083 state_tokens[2] = STATE_ATTENUATION;
1084 break;
1085 case LIGHT_HALF:
1086 state_tokens[2] = STATE_HALF;
1087 break;
1088 case LIGHT_SPOT_DIRECTION:
1089 state_tokens[2] = STATE_SPOT_DIRECTION;
1090 break;
1091 }
1092 break;
1093
1094 case STATE_LIGHT_MODEL:
1095 switch (*(*inst)++) {
1096 case LIGHT_MODEL_AMBIENT:
1097 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1098 break;
1099 case LIGHT_MODEL_SCENECOLOR:
1100 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1101 state_tokens[1] = parse_face_type (inst);
1102 break;
1103 }
1104 break;
1105
1106 case STATE_LIGHT_PROD:
1107 state_tokens[0] = STATE_LIGHTPROD;
1108 state_tokens[1] = parse_integer (inst, Program);
1109
1110 /* Check the value of state_tokens[1] against the # of lights */
1111 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1112 _mesa_set_program_error (ctx, Program->Position,
1113 "Invalid Light Number");
1114 _mesa_error (ctx, GL_INVALID_OPERATION,
1115 "Invalid Light Number: %d", state_tokens[1]);
1116 return 1;
1117 }
1118
1119 state_tokens[2] = parse_face_type (inst);
1120 switch (*(*inst)++) {
1121 case LIGHT_PROD_AMBIENT:
1122 state_tokens[3] = STATE_AMBIENT;
1123 break;
1124 case LIGHT_PROD_DIFFUSE:
1125 state_tokens[3] = STATE_DIFFUSE;
1126 break;
1127 case LIGHT_PROD_SPECULAR:
1128 state_tokens[3] = STATE_SPECULAR;
1129 break;
1130 }
1131 break;
1132
1133
1134 case STATE_FOG:
1135 switch (*(*inst)++) {
1136 case FOG_COLOR:
1137 state_tokens[0] = STATE_FOG_COLOR;
1138 break;
1139 case FOG_PARAMS:
1140 state_tokens[0] = STATE_FOG_PARAMS;
1141 break;
1142 }
1143 break;
1144
1145 case STATE_TEX_ENV:
1146 state_tokens[1] = parse_integer (inst, Program);
1147 switch (*(*inst)++) {
1148 case TEX_ENV_COLOR:
1149 state_tokens[0] = STATE_TEXENV_COLOR;
1150 break;
1151 }
1152 break;
1153
1154 case STATE_TEX_GEN:
1155 {
1156 GLuint type, coord;
1157
1158 state_tokens[0] = STATE_TEXGEN;
1159 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1160
1161 if (parse_texcoord_num (ctx, inst, Program, &coord))
1162 return 1;
1163 state_tokens[1] = coord;
1164
1165 /* EYE or OBJECT */
1166 type = *(*inst++);
1167
1168 /* 0 - s, 1 - t, 2 - r, 3 - q */
1169 coord = *(*inst++);
1170
1171 if (type == TEX_GEN_EYE) {
1172 switch (coord) {
1173 case COMPONENT_X:
1174 state_tokens[2] = STATE_TEXGEN_EYE_S;
1175 break;
1176 case COMPONENT_Y:
1177 state_tokens[2] = STATE_TEXGEN_EYE_T;
1178 break;
1179 case COMPONENT_Z:
1180 state_tokens[2] = STATE_TEXGEN_EYE_R;
1181 break;
1182 case COMPONENT_W:
1183 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1184 break;
1185 }
1186 }
1187 else {
1188 switch (coord) {
1189 case COMPONENT_X:
1190 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1191 break;
1192 case COMPONENT_Y:
1193 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1194 break;
1195 case COMPONENT_Z:
1196 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1197 break;
1198 case COMPONENT_W:
1199 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1200 break;
1201 }
1202 }
1203 }
1204 break;
1205
1206 case STATE_DEPTH:
1207 switch (*(*inst)++) {
1208 case DEPTH_RANGE:
1209 state_tokens[0] = STATE_DEPTH_RANGE;
1210 break;
1211 }
1212 break;
1213
1214 case STATE_CLIP_PLANE:
1215 state_tokens[0] = STATE_CLIPPLANE;
1216 state_tokens[1] = parse_integer (inst, Program);
1217 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1218 return 1;
1219 break;
1220
1221 case STATE_POINT:
1222 switch (*(*inst++)) {
1223 case POINT_SIZE:
1224 state_tokens[0] = STATE_POINT_SIZE;
1225 break;
1226
1227 case POINT_ATTENUATION:
1228 state_tokens[0] = STATE_POINT_ATTENUATION;
1229 break;
1230 }
1231 break;
1232
1233 /* XXX: I think this is the correct format for a matrix row */
1234 case STATE_MATRIX_ROWS:
1235 state_tokens[0] = STATE_MATRIX;
1236 if (parse_matrix
1237 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1238 &state_tokens[5]))
1239 return 1;
1240
1241 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1242
1243 if ((**inst) != 0) { /* Either the last row, 0 */
1244 state_tokens[4] = parse_integer (inst, Program);
1245 if (state_tokens[4] < state_tokens[3]) {
1246 _mesa_set_program_error (ctx, Program->Position,
1247 "Second matrix index less than the first");
1248 _mesa_error (ctx, GL_INVALID_OPERATION,
1249 "Second matrix index (%d) less than the first (%d)",
1250 state_tokens[4], state_tokens[3]);
1251 return 1;
1252 }
1253 }
1254 else {
1255 state_tokens[4] = state_tokens[3];
1256 (*inst)++;
1257 }
1258 break;
1259 }
1260
1261 return 0;
1262}
1263
1264/**
1265 * This parses a state string (rather, the binary version of it) into
1266 * a 6-token similar for the state fetching code in program.c
1267 *
1268 * One might ask, why fetch these parameters into just like you fetch
1269 * state when they are already stored in other places?
1270 *
1271 * Because of array offsets -> We can stick env/local parameters in the
1272 * middle of a parameter array and then index someplace into the array
1273 * when we execute.
1274 *
1275 * One optimization might be to only do this for the cases where the
1276 * env/local parameters end up inside of an array, and leave the
1277 * single parameters (or arrays of pure env/local pareameters) in their
1278 * respective register files.
1279 *
1280 * For ENV parameters, the format is:
1281 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1282 * state_tokens[1] = STATE_ENV
1283 * state_tokens[2] = the parameter index
1284 *
1285 * for LOCAL parameters, the format is:
1286 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1287 * state_tokens[1] = STATE_LOCAL
1288 * state_tokens[2] = the parameter index
1289 *
1290 * \param inst - the start in the binary arry to start working from
1291 * \param state_tokens - the storage for the 6-token state description
1292 * \return - 0 on sucess, 1 on failure
1293 */
1294static GLuint
1295parse_program_single_item (GLcontext * ctx, GLubyte ** inst,
1296 struct arb_program *Program, GLint * state_tokens)
1297{
1298 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1299 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1300 else
1301 state_tokens[0] = STATE_VERTEX_PROGRAM;
1302
1303
1304 switch (*(*inst)++) {
1305 case PROGRAM_PARAM_ENV:
1306 state_tokens[1] = STATE_ENV;
1307 state_tokens[2] = parse_integer (inst, Program);
1308
1309 /* Check state_tokens[2] against the number of ENV parameters available */
1310 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1311 (state_tokens[2] >= (GLint) ctx->Const.MaxFragmentProgramEnvParams))
1312 ||
1313 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1314 (state_tokens[2] >= (GLint) ctx->Const.MaxVertexProgramEnvParams))) {
1315 _mesa_set_program_error (ctx, Program->Position,
1316 "Invalid Program Env Parameter");
1317 _mesa_error (ctx, GL_INVALID_OPERATION,
1318 "Invalid Program Env Parameter: %d",
1319 state_tokens[2]);
1320 return 1;
1321 }
1322
1323 break;
1324
1325 case PROGRAM_PARAM_LOCAL:
1326 state_tokens[1] = STATE_LOCAL;
1327 state_tokens[2] = parse_integer (inst, Program);
1328
1329 /* Check state_tokens[2] against the number of LOCAL parameters available */
1330 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1331 (state_tokens[2] >= (GLint) ctx->Const.MaxFragmentProgramLocalParams))
1332 ||
1333 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1334 (state_tokens[2] >= (GLint) ctx->Const.MaxVertexProgramLocalParams))) {
1335 _mesa_set_program_error (ctx, Program->Position,
1336 "Invalid Program Local Parameter");
1337 _mesa_error (ctx, GL_INVALID_OPERATION,
1338 "Invalid Program Local Parameter: %d",
1339 state_tokens[2]);
1340 return 1;
1341 }
1342 break;
1343 }
1344
1345 return 0;
1346}
1347
1348/**
1349 * For ARB_vertex_program, programs are not allowed to use both an explicit
1350 * vertex attribute and a generic vertex attribute corresponding to the same
1351 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1352 *
1353 * This will walk our var_cache and make sure that nobody does anything fishy.
1354 *
1355 * \return 0 on sucess, 1 on error
1356 */
1357static GLuint
1358generic_attrib_check(struct var_cache *vc_head)
1359{
1360 int a;
1361 struct var_cache *curr;
1362 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1363 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1364
1365 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1366 explicitAttrib[a] = GL_FALSE;
1367 genericAttrib[a] = GL_FALSE;
1368 }
1369
1370 curr = vc_head;
1371 while (curr) {
1372 if (curr->type == vt_attrib) {
1373 if (curr->attrib_is_generic)
1374 genericAttrib[ curr->attrib_binding_idx ] = GL_TRUE;
1375 else
1376 explicitAttrib[ curr->attrib_binding_idx ] = GL_TRUE;
1377 }
1378
1379 curr = curr->next;
1380 }
1381
1382 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1383 if ((explicitAttrib[a]) && (genericAttrib[a]))
1384 return 1;
1385 }
1386
1387 return 0;
1388}
1389
1390/**
1391 * This will handle the binding side of an ATTRIB var declaration
1392 *
1393 * \param binding - the fragment input register state, defined in nvfragprog.h
1394 * \param binding_idx - the index in the attrib register file that binding is associated with
1395 * \return returns 0 on sucess, 1 on error
1396 *
1397 * See nvfragparse.c for attrib register file layout
1398 */
1399static GLuint
1400parse_attrib_binding (GLcontext * ctx, GLubyte ** inst,
1401 struct arb_program *Program, GLuint * binding,
1402 GLuint * binding_idx, GLuint *is_generic)
1403{
1404 GLuint texcoord;
1405 GLint coord;
1406 GLint err = 0;
1407
1408 *is_generic = 0;
1409 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1410 switch (*(*inst)++) {
1411 case FRAGMENT_ATTRIB_COLOR:
1412 err = parse_color_type (ctx, inst, Program, &coord);
1413 *binding = FRAG_ATTRIB_COL0 + coord;
1414 *binding_idx = 1 + coord;
1415 break;
1416
1417 case FRAGMENT_ATTRIB_TEXCOORD:
1418 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1419 *binding = FRAG_ATTRIB_TEX0 + texcoord;
1420 *binding_idx = 4 + texcoord;
1421 break;
1422
1423 case FRAGMENT_ATTRIB_FOGCOORD:
1424 *binding = FRAG_ATTRIB_FOGC;
1425 *binding_idx = 3;
1426 break;
1427
1428 case FRAGMENT_ATTRIB_POSITION:
1429 *binding = FRAG_ATTRIB_WPOS;
1430 *binding_idx = 0;
1431 break;
1432
1433 default:
1434 err = 1;
1435 break;
1436 }
1437 }
1438 else {
1439 switch (*(*inst)++) {
1440 case VERTEX_ATTRIB_POSITION:
1441 *binding = VERT_ATTRIB_POS;
1442 *binding_idx = 0;
1443 break;
1444
1445 case VERTEX_ATTRIB_WEIGHT:
1446 {
1447 GLint weight;
1448
1449 err = parse_weight_num (ctx, inst, Program, &weight);
1450 *binding = VERT_ATTRIB_WEIGHT;
1451 *binding_idx = 1;
1452 }
1453 _mesa_set_program_error (ctx, Program->Position,
1454 "ARB_vertex_blend not supported\n");
1455 _mesa_error (ctx, GL_INVALID_OPERATION,
1456 "ARB_vertex_blend not supported\n");
1457 return 1;
1458 break;
1459
1460 case VERTEX_ATTRIB_NORMAL:
1461 *binding = VERT_ATTRIB_NORMAL;
1462 *binding_idx = 2;
1463 break;
1464
1465 case VERTEX_ATTRIB_COLOR:
1466 {
1467 GLint color;
1468
1469 err = parse_color_type (ctx, inst, Program, &color);
1470 if (color) {
1471 *binding = VERT_ATTRIB_COLOR1;
1472 *binding_idx = 4;
1473 }
1474 else {
1475 *binding = VERT_ATTRIB_COLOR0;
1476 *binding_idx = 3;
1477 }
1478 }
1479 break;
1480
1481 case VERTEX_ATTRIB_FOGCOORD:
1482 *binding = VERT_ATTRIB_FOG;
1483 *binding_idx = 5;
1484 break;
1485
1486 case VERTEX_ATTRIB_TEXCOORD:
1487 {
1488 GLuint unit;
1489
1490 err = parse_texcoord_num (ctx, inst, Program, &unit);
1491 *binding = VERT_ATTRIB_TEX0 + unit;
1492 *binding_idx = 8 + unit;
1493 }
1494 break;
1495
1496 /* It looks like we don't support this at all, atm */
1497 case VERTEX_ATTRIB_MATRIXINDEX:
1498 parse_integer (inst, Program);
1499 _mesa_set_program_error (ctx, Program->Position,
1500 "ARB_palette_matrix not supported");
1501 _mesa_error (ctx, GL_INVALID_OPERATION,
1502 "ARB_palette_matrix not supported");
1503 return 1;
1504 break;
1505
1506 case VERTEX_ATTRIB_GENERIC:
1507 {
1508 GLuint attrib;
1509
1510 if (!parse_generic_attrib_num(ctx, inst, Program, &attrib)) {
1511 *is_generic = 1;
1512 switch (attrib) {
1513 case 0:
1514 *binding = VERT_ATTRIB_POS;
1515 break;
1516 case 1:
1517 *binding = VERT_ATTRIB_WEIGHT;
1518 break;
1519 case 2:
1520 *binding = VERT_ATTRIB_NORMAL;
1521 break;
1522 case 3:
1523 *binding = VERT_ATTRIB_COLOR0;
1524 break;
1525 case 4:
1526 *binding = VERT_ATTRIB_COLOR1;
1527 break;
1528 case 5:
1529 *binding = VERT_ATTRIB_FOG;
1530 break;
1531 case 6:
1532 break;
1533 case 7:
1534 break;
1535 default:
1536 *binding = VERT_ATTRIB_TEX0 + (attrib-8);
1537 break;
1538 }
1539 *binding_idx = attrib;
1540 }
1541 }
1542 break;
1543
1544 default:
1545 err = 1;
1546 break;
1547 }
1548 }
1549
1550 /* Can this even happen? */
1551 if (err) {
1552 _mesa_set_program_error (ctx, Program->Position,
1553 "Bad attribute binding");
1554 _mesa_error (ctx, GL_INVALID_OPERATION, "Bad attribute binding");
1555 }
1556
1557 Program->InputsRead |= (1 << *binding_idx);
1558
1559 return err;
1560}
1561
1562/**
1563 * This translates between a binary token for an output variable type
1564 * and the mesa token for the same thing.
1565 *
1566 *
1567 * XXX: What is the 'name' for vertex program state? -> do we need it?
1568 * I don't think we do;
1569 *
1570 * See nvfragprog.h for definitions
1571 *
1572 * \param inst - The parsed tokens
1573 * \param binding - The name of the state we are binding too
1574 * \param binding_idx - The index into the result register file that this is bound too
1575 *
1576 * See nvfragparse.c for the register file layout for fragment programs
1577 * See nvvertparse.c for the register file layout for vertex programs
1578 */
1579static GLuint
1580parse_result_binding (GLcontext * ctx, GLubyte ** inst, GLuint * binding,
1581 GLuint * binding_idx, struct arb_program *Program)
1582{
Brian Paulbe76b7f2004-10-04 14:40:05 +00001583 GLuint b, out_color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001584
1585 switch (*(*inst)++) {
1586 case FRAGMENT_RESULT_COLOR:
1587 /* for frag programs, this is FRAGMENT_RESULT_COLOR */
1588 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paulbe76b7f2004-10-04 14:40:05 +00001589 /* This gets result of the color buffer we're supposed to
1590 * draw into
1591 */
1592 parse_output_color_num(ctx, inst, Program, &out_color);
1593
Jouk Jansen40322e12004-04-05 08:50:36 +00001594 *binding = FRAG_OUTPUT_COLR;
Brian Paulbe76b7f2004-10-04 14:40:05 +00001595
1596 /* XXX: We're ignoring the color buffer for now. */
Jouk Jansen40322e12004-04-05 08:50:36 +00001597 *binding_idx = 0;
1598 }
1599 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1600 else {
1601 *binding_idx = 0;
1602 }
1603 break;
1604
1605 case FRAGMENT_RESULT_DEPTH:
1606 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1607 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1608 *binding = FRAG_OUTPUT_DEPR;
1609 *binding_idx = 2;
1610 }
1611 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1612 else {
1613 GLint color_type;
1614 GLuint face_type = parse_face_type(inst);
1615 GLint color_type_ret = parse_color_type(ctx, inst, Program, &color_type);
1616
1617 /* back face */
1618 if (face_type) {
1619 if (color_type_ret) return 1;
1620
1621 /* secondary color */
1622 if (color_type) {
1623 *binding_idx = 4;
1624 }
1625 /* primary color */
1626 else {
1627 *binding_idx = 3;
1628 }
1629 }
1630 /* front face */
1631 else {
1632 /* secondary color */
1633 if (color_type) {
1634 *binding_idx = 2;
1635 }
1636 /* primary color */
1637 else {
1638 *binding_idx = 1;
1639 }
1640 }
1641 }
1642 break;
1643
1644 case VERTEX_RESULT_FOGCOORD:
1645 *binding_idx = 5;
1646 break;
1647
1648 case VERTEX_RESULT_POINTSIZE:
1649 *binding_idx = 6;
1650 break;
1651
1652 case VERTEX_RESULT_TEXCOORD:
1653 if (parse_texcoord_num (ctx, inst, Program, &b))
1654 return 1;
1655 *binding_idx = 7 + b;
1656 break;
1657 }
1658
1659 Program->OutputsWritten |= (1 << *binding_idx);
1660
1661 return 0;
1662}
1663
1664/**
1665 * This handles the declaration of ATTRIB variables
1666 *
1667 * XXX: Still needs
1668 * parse_vert_attrib_binding(), or something like that
1669 *
1670 * \return 0 on sucess, 1 on error
1671 */
1672static GLint
1673parse_attrib (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1674 struct arb_program *Program)
1675{
1676 GLuint found;
1677 char *error_msg;
1678 struct var_cache *attrib_var;
1679
1680 attrib_var = parse_string (inst, vc_head, Program, &found);
1681 Program->Position = parse_position (inst);
1682 if (found) {
1683 error_msg = (char *)
1684 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1685 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1686 attrib_var->name);
1687
1688 _mesa_set_program_error (ctx, Program->Position, error_msg);
1689 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1690
1691 _mesa_free (error_msg);
1692 return 1;
1693 }
1694
1695 attrib_var->type = vt_attrib;
1696
1697 /* I think this is ok now - karl */
1698 /* XXX: */
1699 /*if (Program->type == GL_FRAGMENT_PROGRAM_ARB) */
1700 {
1701 if (parse_attrib_binding
1702 (ctx, inst, Program, &attrib_var->attrib_binding,
1703 &attrib_var->attrib_binding_idx, &attrib_var->attrib_is_generic))
1704 return 1;
1705 if (generic_attrib_check(*vc_head)) {
1706 _mesa_set_program_error (ctx, Program->Position,
1707 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
1708 _mesa_error (ctx, GL_INVALID_OPERATION,
1709 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
1710 return 1;
1711 }
1712
1713 }
1714
1715 Program->Base.NumAttributes++;
1716 return 0;
1717}
1718
1719/**
1720 * \param use -- TRUE if we're called when declaring implicit parameters,
1721 * FALSE if we're declaraing variables. This has to do with
1722 * if we get a signed or unsigned float for scalar constants
1723 */
1724static GLuint
1725parse_param_elements (GLcontext * ctx, GLubyte ** inst,
1726 struct var_cache *param_var,
1727 struct arb_program *Program, GLboolean use)
1728{
1729 GLint idx;
1730 GLuint err;
1731 GLint state_tokens[6];
1732 GLfloat const_values[4];
1733
1734 err = 0;
1735
1736 switch (*(*inst)++) {
1737 case PARAM_STATE_ELEMENT:
1738
1739 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1740 return 1;
1741
1742 /* If we adding STATE_MATRIX that has multiple rows, we need to
1743 * unroll it and call _mesa_add_state_reference() for each row
1744 */
1745 if ((state_tokens[0] == STATE_MATRIX)
1746 && (state_tokens[3] != state_tokens[4])) {
1747 GLint row;
1748 GLint first_row = state_tokens[3];
1749 GLint last_row = state_tokens[4];
1750
1751 for (row = first_row; row <= last_row; row++) {
1752 state_tokens[3] = state_tokens[4] = row;
1753
1754 idx =
1755 _mesa_add_state_reference (Program->Parameters,
1756 state_tokens);
1757 if (param_var->param_binding_begin == ~0U)
1758 param_var->param_binding_begin = idx;
1759 param_var->param_binding_length++;
1760 Program->Base.NumParameters++;
1761 }
1762 }
1763 else {
1764 idx =
1765 _mesa_add_state_reference (Program->Parameters, state_tokens);
1766 if (param_var->param_binding_begin == ~0U)
1767 param_var->param_binding_begin = idx;
1768 param_var->param_binding_length++;
1769 Program->Base.NumParameters++;
1770 }
1771 break;
1772
1773 case PARAM_PROGRAM_ELEMENT:
1774
1775 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1776 return 1;
1777 idx = _mesa_add_state_reference (Program->Parameters, state_tokens);
1778 if (param_var->param_binding_begin == ~0U)
1779 param_var->param_binding_begin = idx;
1780 param_var->param_binding_length++;
1781 Program->Base.NumParameters++;
1782
1783 /* Check if there is more: 0 -> we're done, else its an integer */
1784 if (**inst) {
1785 GLuint out_of_range, new_idx;
1786 GLuint start_idx = state_tokens[2] + 1;
1787 GLuint end_idx = parse_integer (inst, Program);
1788
1789 out_of_range = 0;
1790 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1791 if (((state_tokens[1] == STATE_ENV)
1792 && (end_idx >= ctx->Const.MaxFragmentProgramEnvParams))
1793 || ((state_tokens[1] == STATE_LOCAL)
1794 && (end_idx >=
1795 ctx->Const.MaxFragmentProgramLocalParams)))
1796 out_of_range = 1;
1797 }
1798 else {
1799 if (((state_tokens[1] == STATE_ENV)
1800 && (end_idx >= ctx->Const.MaxVertexProgramEnvParams))
1801 || ((state_tokens[1] == STATE_LOCAL)
1802 && (end_idx >=
1803 ctx->Const.MaxVertexProgramLocalParams)))
1804 out_of_range = 1;
1805 }
1806 if (out_of_range) {
1807 _mesa_set_program_error (ctx, Program->Position,
1808 "Invalid Program Parameter");
1809 _mesa_error (ctx, GL_INVALID_OPERATION,
1810 "Invalid Program Parameter: %d", end_idx);
1811 return 1;
1812 }
1813
1814 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1815 state_tokens[2] = new_idx;
1816 idx =
1817 _mesa_add_state_reference (Program->Parameters,
1818 state_tokens);
1819 param_var->param_binding_length++;
1820 Program->Base.NumParameters++;
1821 }
1822 }
1823 else
1824 {
1825 (*inst)++;
1826 }
1827 break;
1828
1829 case PARAM_CONSTANT:
1830 parse_constant (inst, const_values, Program, use);
1831 idx =
1832 _mesa_add_named_constant (Program->Parameters,
1833 (char *) param_var->name, const_values);
1834 if (param_var->param_binding_begin == ~0U)
1835 param_var->param_binding_begin = idx;
1836 param_var->param_binding_length++;
1837 Program->Base.NumParameters++;
1838 break;
1839
1840 default:
1841 _mesa_set_program_error (ctx, Program->Position,
1842 "Unexpected token in parse_param_elements()");
1843 _mesa_error (ctx, GL_INVALID_OPERATION,
1844 "Unexpected token in parse_param_elements()");
1845 return 1;
1846 }
1847
1848 /* Make sure we haven't blown past our parameter limits */
1849 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1850 (Program->Base.NumParameters >=
1851 ctx->Const.MaxVertexProgramLocalParams))
1852 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1853 && (Program->Base.NumParameters >=
1854 ctx->Const.MaxFragmentProgramLocalParams))) {
1855 _mesa_set_program_error (ctx, Program->Position,
1856 "Too many parameter variables");
1857 _mesa_error (ctx, GL_INVALID_OPERATION, "Too many parameter variables");
1858 return 1;
1859 }
1860
1861 return err;
1862}
1863
1864/**
1865 * This picks out PARAM program parameter bindings.
1866 *
1867 * XXX: This needs to be stressed & tested
1868 *
1869 * \return 0 on sucess, 1 on error
1870 */
1871static GLuint
1872parse_param (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1873 struct arb_program *Program)
1874{
Brian Paulbd997cd2004-07-20 21:12:56 +00001875 GLuint found, err;
1876 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001877 char *error_msg;
1878 struct var_cache *param_var;
1879
1880 err = 0;
1881 param_var = parse_string (inst, vc_head, Program, &found);
1882 Program->Position = parse_position (inst);
1883
1884 if (found) {
1885 error_msg = (char *) _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
1886 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1887 param_var->name);
1888
1889 _mesa_set_program_error (ctx, Program->Position, error_msg);
1890 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1891
1892 _mesa_free (error_msg);
1893 return 1;
1894 }
1895
1896 specified_length = parse_integer (inst, Program);
1897
1898 if (specified_length < 0) {
1899 _mesa_set_program_error (ctx, Program->Position,
1900 "Negative parameter array length");
1901 _mesa_error (ctx, GL_INVALID_OPERATION,
1902 "Negative parameter array length: %d", specified_length);
1903 return 1;
1904 }
1905
1906 param_var->type = vt_param;
1907 param_var->param_binding_length = 0;
1908
1909 /* Right now, everything is shoved into the main state register file.
1910 *
1911 * In the future, it would be nice to leave things ENV/LOCAL params
1912 * in their respective register files, if possible
1913 */
1914 param_var->param_binding_type = PROGRAM_STATE_VAR;
1915
1916 /* Remember to:
1917 * * - add each guy to the parameter list
1918 * * - increment the param_var->param_binding_len
1919 * * - store the param_var->param_binding_begin for the first one
1920 * * - compare the actual len to the specified len at the end
1921 */
1922 while (**inst != PARAM_NULL) {
1923 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1924 return 1;
1925 }
1926
1927 /* Test array length here! */
1928 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001929 if (specified_length != (int)param_var->param_binding_length) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001930 _mesa_set_program_error (ctx, Program->Position,
1931 "Declared parameter array lenght does not match parameter list");
1932 _mesa_error (ctx, GL_INVALID_OPERATION,
1933 "Declared parameter array lenght does not match parameter list");
1934 }
1935 }
1936
1937 (*inst)++;
1938
1939 return 0;
1940}
1941
1942/**
1943 *
1944 */
1945static GLuint
1946parse_param_use (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1947 struct arb_program *Program, struct var_cache **new_var)
1948{
1949 struct var_cache *param_var;
1950
1951 /* First, insert a dummy entry into the var_cache */
1952 var_cache_create (&param_var);
1953 param_var->name = (GLubyte *) _mesa_strdup (" ");
1954 param_var->type = vt_param;
1955
1956 param_var->param_binding_length = 0;
1957 /* Don't fill in binding_begin; We use the default value of -1
1958 * to tell if its already initialized, elsewhere.
1959 *
1960 * param_var->param_binding_begin = 0;
1961 */
1962 param_var->param_binding_type = PROGRAM_STATE_VAR;
1963
1964 var_cache_append (vc_head, param_var);
1965
1966 /* Then fill it with juicy parameter goodness */
1967 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1968 return 1;
1969
1970 *new_var = param_var;
1971
1972 return 0;
1973}
1974
1975
1976/**
1977 * This handles the declaration of TEMP variables
1978 *
1979 * \return 0 on sucess, 1 on error
1980 */
1981static GLuint
1982parse_temp (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1983 struct arb_program *Program)
1984{
1985 GLuint found;
1986 struct var_cache *temp_var;
1987 char *error_msg;
1988
1989 while (**inst != 0) {
1990 temp_var = parse_string (inst, vc_head, Program, &found);
1991 Program->Position = parse_position (inst);
1992 if (found) {
1993 error_msg = (char *)
1994 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1995 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1996 temp_var->name);
1997
1998 _mesa_set_program_error (ctx, Program->Position, error_msg);
1999 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2000
2001 _mesa_free (error_msg);
2002 return 1;
2003 }
2004
2005 temp_var->type = vt_temp;
2006
2007 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2008 (Program->Base.NumTemporaries >=
2009 ctx->Const.MaxFragmentProgramTemps))
2010 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2011 && (Program->Base.NumTemporaries >=
2012 ctx->Const.MaxVertexProgramTemps))) {
2013 _mesa_set_program_error (ctx, Program->Position,
2014 "Too many TEMP variables declared");
2015 _mesa_error (ctx, GL_INVALID_OPERATION,
2016 "Too many TEMP variables declared");
2017 return 1;
2018 }
2019
2020 temp_var->temp_binding = Program->Base.NumTemporaries;
2021 Program->Base.NumTemporaries++;
2022 }
2023 (*inst)++;
2024
2025 return 0;
2026}
2027
2028/**
2029 * This handles variables of the OUTPUT variety
2030 *
2031 * \return 0 on sucess, 1 on error
2032 */
2033static GLuint
2034parse_output (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2035 struct arb_program *Program)
2036{
2037 GLuint found;
2038 struct var_cache *output_var;
2039
2040 output_var = parse_string (inst, vc_head, Program, &found);
2041 Program->Position = parse_position (inst);
2042 if (found) {
2043 char *error_msg;
2044 error_msg = (char *)
2045 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2046 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2047 output_var->name);
2048
2049 _mesa_set_program_error (ctx, Program->Position, error_msg);
2050 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2051
2052 _mesa_free (error_msg);
2053 return 1;
2054 }
2055
2056 output_var->type = vt_output;
2057 return parse_result_binding (ctx, inst, &output_var->output_binding,
2058 &output_var->output_binding_idx, Program);
2059}
2060
2061/**
2062 * This handles variables of the ALIAS kind
2063 *
2064 * \return 0 on sucess, 1 on error
2065 */
2066static GLuint
2067parse_alias (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2068 struct arb_program *Program)
2069{
2070 GLuint found;
2071 struct var_cache *temp_var;
2072 char *error_msg;
2073
2074
2075 temp_var = parse_string (inst, vc_head, Program, &found);
2076 Program->Position = parse_position (inst);
2077
2078 if (found) {
2079 error_msg = (char *)
2080 _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_alias;
2092 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2093 Program->Position = parse_position (inst);
2094
2095 if (!found)
2096 {
2097 error_msg = (char *)
2098 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2099 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2100 temp_var->alias_binding->name);
2101
2102 _mesa_set_program_error (ctx, Program->Position, error_msg);
2103 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2104
2105 _mesa_free (error_msg);
2106 return 1;
2107 }
2108
2109 return 0;
2110}
2111
2112/**
2113 * This handles variables of the ADDRESS kind
2114 *
2115 * \return 0 on sucess, 1 on error
2116 */
2117static GLuint
2118parse_address (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2119 struct arb_program *Program)
2120{
2121 GLuint found;
2122 struct var_cache *temp_var;
2123 char *error_msg;
2124
2125 while (**inst != 0) {
2126 temp_var = parse_string (inst, vc_head, Program, &found);
2127 Program->Position = parse_position (inst);
2128 if (found) {
2129 error_msg = (char *)
2130 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2131 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2132 temp_var->name);
2133
2134 _mesa_set_program_error (ctx, Program->Position, error_msg);
2135 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2136
2137 _mesa_free (error_msg);
2138 return 1;
2139 }
2140
2141 temp_var->type = vt_address;
2142
2143 if (Program->Base.NumAddressRegs >=
2144 ctx->Const.MaxVertexProgramAddressRegs) {
2145 _mesa_set_program_error (ctx, Program->Position,
2146 "Too many ADDRESS variables declared");
2147 _mesa_error (ctx, GL_INVALID_OPERATION,
2148 "Too many ADDRESS variables declared");
2149 return 1;
2150 }
2151
2152 temp_var->address_binding = Program->Base.NumAddressRegs;
2153 Program->Base.NumAddressRegs++;
2154 }
2155 (*inst)++;
2156
2157 return 0;
2158}
2159
2160/**
2161 * Parse a program declaration
2162 *
2163 * \return 0 on sucess, 1 on error
2164 */
2165static GLint
2166parse_declaration (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2167 struct arb_program *Program)
2168{
2169 GLint err = 0;
2170
2171 switch (*(*inst)++) {
2172 case ADDRESS:
2173 err = parse_address (ctx, inst, vc_head, Program);
2174 break;
2175
2176 case ALIAS:
2177 err = parse_alias (ctx, inst, vc_head, Program);
2178 break;
2179
2180 case ATTRIB:
2181 err = parse_attrib (ctx, inst, vc_head, Program);
2182 break;
2183
2184 case OUTPUT:
2185 err = parse_output (ctx, inst, vc_head, Program);
2186 break;
2187
2188 case PARAM:
2189 err = parse_param (ctx, inst, vc_head, Program);
2190 break;
2191
2192 case TEMP:
2193 err = parse_temp (ctx, inst, vc_head, Program);
2194 break;
2195 }
2196
2197 return err;
2198}
2199
2200/**
2201 * Handle the parsing out of a masked destination register
2202 *
2203 * If we are a vertex program, make sure we don't write to
2204 * result.position of we have specified that the program is
2205 * position invariant
2206 *
2207 * \param File - The register file we write to
2208 * \param Index - The register index we write to
2209 * \param WriteMask - The mask controlling which components we write (1->write)
2210 *
2211 * \return 0 on sucess, 1 on error
2212 */
2213static GLuint
2214parse_masked_dst_reg (GLcontext * ctx, GLubyte ** inst,
2215 struct var_cache **vc_head, struct arb_program *Program,
2216 GLint * File, GLint * Index, GLboolean * WriteMask)
2217{
2218 GLuint result;
2219 GLubyte mask;
2220 struct var_cache *dst;
2221
2222 /* We either have a result register specified, or a
2223 * variable that may or may not be writable
2224 */
2225 switch (*(*inst)++) {
2226 case REGISTER_RESULT:
2227 if (parse_result_binding
2228 (ctx, inst, &result, (GLuint *) Index, Program))
2229 return 1;
2230 *File = PROGRAM_OUTPUT;
2231 break;
2232
2233 case REGISTER_ESTABLISHED_NAME:
2234 dst = parse_string (inst, vc_head, Program, &result);
2235 Program->Position = parse_position (inst);
2236
2237 /* If the name has never been added to our symbol table, we're hosed */
2238 if (!result) {
2239 _mesa_set_program_error (ctx, Program->Position,
2240 "0: Undefined variable");
2241 _mesa_error (ctx, GL_INVALID_OPERATION, "0: Undefined variable: %s",
2242 dst->name);
2243 return 1;
2244 }
2245
2246 switch (dst->type) {
2247 case vt_output:
2248 *File = PROGRAM_OUTPUT;
2249 *Index = dst->output_binding_idx;
2250 break;
2251
2252 case vt_temp:
2253 *File = PROGRAM_TEMPORARY;
2254 *Index = dst->temp_binding;
2255 break;
2256
2257 /* If the var type is not vt_output or vt_temp, no go */
2258 default:
2259 _mesa_set_program_error (ctx, Program->Position,
2260 "Destination register is read only");
2261 _mesa_error (ctx, GL_INVALID_OPERATION,
2262 "Destination register is read only: %s",
2263 dst->name);
2264 return 1;
2265 }
2266 break;
2267
2268 default:
2269 _mesa_set_program_error (ctx, Program->Position,
2270 "Unexpected opcode in parse_masked_dst_reg()");
2271 _mesa_error (ctx, GL_INVALID_OPERATION,
2272 "Unexpected opcode in parse_masked_dst_reg()");
2273 return 1;
2274 }
2275
2276
2277 /* Position invariance test */
2278 /* This test is done now in syntax portion - when position invariance OPTION
2279 is specified, "result.position" rule is disabled so there is no way
2280 to write the position
2281 */
2282 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2283 (*Index == 0)) {
2284 _mesa_set_program_error (ctx, Program->Position,
2285 "Vertex program specified position invariance and wrote vertex position");
2286 _mesa_error (ctx, GL_INVALID_OPERATION,
2287 "Vertex program specified position invariance and wrote vertex position");
2288 }*/
2289
2290 /* And then the mask.
2291 * w,a -> bit 0
2292 * z,b -> bit 1
2293 * y,g -> bit 2
2294 * x,r -> bit 3
2295 */
2296 mask = *(*inst)++;
2297
2298 WriteMask[0] = (GLboolean) (mask & (1 << 3)) >> 3;
2299 WriteMask[1] = (GLboolean) (mask & (1 << 2)) >> 2;
2300 WriteMask[2] = (GLboolean) (mask & (1 << 1)) >> 1;
2301 WriteMask[3] = (GLboolean) (mask & (1));
2302
2303 return 0;
2304}
2305
2306
2307/**
2308 * Handle the parsing of a address register
2309 *
2310 * \param Index - The register index we write to
2311 *
2312 * \return 0 on sucess, 1 on error
2313 */
2314static GLuint
2315parse_address_reg (GLcontext * ctx, GLubyte ** inst,
2316 struct var_cache **vc_head,
2317 struct arb_program *Program, GLint * Index)
2318{
2319 struct var_cache *dst;
2320 GLuint result;
Brian Paula6c423d2004-08-25 15:59:48 +00002321 (void) Index;
Jouk Jansen40322e12004-04-05 08:50:36 +00002322
2323 dst = parse_string (inst, vc_head, Program, &result);
2324 Program->Position = parse_position (inst);
2325
2326 /* If the name has never been added to our symbol table, we're hosed */
2327 if (!result) {
2328 _mesa_set_program_error (ctx, Program->Position, "Undefined variable");
2329 _mesa_error (ctx, GL_INVALID_OPERATION, "Undefined variable: %s",
2330 dst->name);
2331 return 1;
2332 }
2333
2334 if (dst->type != vt_address) {
2335 _mesa_set_program_error (ctx, Program->Position,
2336 "Variable is not of type ADDRESS");
2337 _mesa_error (ctx, GL_INVALID_OPERATION,
2338 "Variable: %s is not of type ADDRESS", dst->name);
2339 return 1;
2340 }
2341
2342 return 0;
2343}
2344
2345/**
2346 * Handle the parsing out of a masked address register
2347 *
2348 * \param Index - The register index we write to
2349 * \param WriteMask - The mask controlling which components we write (1->write)
2350 *
2351 * \return 0 on sucess, 1 on error
2352 */
2353static GLuint
2354parse_masked_address_reg (GLcontext * ctx, GLubyte ** inst,
2355 struct var_cache **vc_head,
2356 struct arb_program *Program, GLint * Index,
2357 GLboolean * WriteMask)
2358{
2359 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2360 return 1;
2361
2362 /* This should be 0x8 */
2363 (*inst)++;
2364
2365 /* Writemask of .x is implied */
2366 WriteMask[0] = 1;
2367 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2368
2369 return 0;
2370}
2371
2372
2373/**
2374 * Parse out a swizzle mask.
2375 *
2376 * The values in the input stream are:
2377 * COMPONENT_X -> x/r
2378 * COMPONENT_Y -> y/g
2379 * COMPONENT_Z-> z/b
2380 * COMPONENT_W-> w/a
2381 *
2382 * The values in the output mask are:
2383 * 0 -> x/r
2384 * 1 -> y/g
2385 * 2 -> z/b
2386 * 3 -> w/a
2387 *
2388 * The len parameter allows us to grab 4 components for a vector
2389 * swizzle, or just 1 component for a scalar src register selection
2390 */
2391static GLuint
2392parse_swizzle_mask (GLubyte ** inst, GLubyte * mask, GLint len)
2393{
2394 GLint a;
2395
2396 for (a = 0; a < 4; a++)
2397 mask[a] = a;
2398
2399 for (a = 0; a < len; a++) {
2400 switch (*(*inst)++) {
2401 case COMPONENT_X:
2402 mask[a] = 0;
2403 break;
2404
2405 case COMPONENT_Y:
2406 mask[a] = 1;
2407 break;
2408
2409 case COMPONENT_Z:
2410 mask[a] = 2;
2411 break;
2412
2413 case COMPONENT_W:
2414 mask[a] = 3;
2415 break;
2416 }
2417 }
2418
2419 return 0;
2420}
2421
2422/**
2423 */
2424static GLuint
2425parse_extended_swizzle_mask (GLubyte ** inst, GLubyte * mask, GLboolean * Negate)
2426{
2427 GLint a;
2428 GLubyte swz;
2429
2430 *Negate = GL_FALSE;
2431 for (a = 0; a < 4; a++) {
Brian Paul05908952004-06-08 15:20:23 +00002432 if (parse_sign (inst) == -1)
Jouk Jansen40322e12004-04-05 08:50:36 +00002433 *Negate = GL_TRUE;
2434
2435 swz = *(*inst)++;
2436
2437 switch (swz) {
2438 case COMPONENT_0:
2439 mask[a] = SWIZZLE_ZERO;
2440 break;
2441 case COMPONENT_1:
2442 mask[a] = SWIZZLE_ONE;
2443 break;
2444 case COMPONENT_X:
2445 mask[a] = SWIZZLE_X;
2446 break;
2447 case COMPONENT_Y:
2448 mask[a] = SWIZZLE_Y;
2449 break;
2450 case COMPONENT_Z:
2451 mask[a] = SWIZZLE_Z;
2452 break;
2453 case COMPONENT_W:
2454 mask[a] = SWIZZLE_W;
2455 break;
2456
2457 }
2458#if 0
2459 if (swz == 0)
2460 mask[a] = SWIZZLE_ZERO;
2461 else if (swz == 1)
2462 mask[a] = SWIZZLE_ONE;
2463 else
2464 mask[a] = swz - 2;
2465#endif
2466
2467 }
2468
2469 return 0;
2470}
2471
2472
2473static GLuint
2474parse_src_reg (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2475 struct arb_program *Program, GLint * File, GLint * Index,
2476 GLboolean *IsRelOffset )
2477{
2478 struct var_cache *src;
Brian Paulbd997cd2004-07-20 21:12:56 +00002479 GLuint binding_state, binding_idx, is_generic, found;
2480 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002481
2482 /* And the binding for the src */
2483 switch (*(*inst)++) {
2484 case REGISTER_ATTRIB:
2485 if (parse_attrib_binding
2486 (ctx, inst, Program, &binding_state, &binding_idx, &is_generic))
2487 return 1;
2488 *File = PROGRAM_INPUT;
2489 *Index = binding_idx;
2490
2491 /* We need to insert a dummy variable into the var_cache so we can
2492 * catch generic vertex attrib aliasing errors
2493 */
2494 var_cache_create(&src);
2495 src->type = vt_attrib;
2496 src->name = (GLubyte *)_mesa_strdup("Dummy Attrib Variable");
2497 src->attrib_binding = binding_state;
2498 src->attrib_binding_idx = binding_idx;
2499 src->attrib_is_generic = is_generic;
2500 var_cache_append(vc_head, src);
2501 if (generic_attrib_check(*vc_head)) {
2502 _mesa_set_program_error (ctx, Program->Position,
2503 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2504 _mesa_error (ctx, GL_INVALID_OPERATION,
2505 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2506 return 1;
2507 }
2508 break;
2509
2510 case REGISTER_PARAM:
2511 switch (**inst) {
2512 case PARAM_ARRAY_ELEMENT:
2513 (*inst)++;
2514 src = parse_string (inst, vc_head, Program, &found);
2515 Program->Position = parse_position (inst);
2516
2517 if (!found) {
2518 _mesa_set_program_error (ctx, Program->Position,
2519 "2: Undefined variable");
2520 _mesa_error (ctx, GL_INVALID_OPERATION,
2521 "2: Undefined variable: %s", src->name);
2522 return 1;
2523 }
2524
2525 *File = src->param_binding_type;
2526
2527 switch (*(*inst)++) {
2528 case ARRAY_INDEX_ABSOLUTE:
2529 offset = parse_integer (inst, Program);
2530
2531 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002532 || (offset >= (int)src->param_binding_length)) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002533 _mesa_set_program_error (ctx, Program->Position,
2534 "Index out of range");
2535 _mesa_error (ctx, GL_INVALID_OPERATION,
2536 "Index %d out of range for %s", offset,
2537 src->name);
2538 return 1;
2539 }
2540
2541 *Index = src->param_binding_begin + offset;
2542 break;
2543
2544 case ARRAY_INDEX_RELATIVE:
2545 {
2546 GLint addr_reg_idx, rel_off;
2547
2548 /* First, grab the address regiseter */
2549 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2550 return 1;
2551
2552 /* And the .x */
2553 ((*inst)++);
2554 ((*inst)++);
2555 ((*inst)++);
2556 ((*inst)++);
2557
2558 /* Then the relative offset */
2559 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2560
2561 /* And store it properly */
2562 *Index = src->param_binding_begin + rel_off;
2563 *IsRelOffset = 1;
2564 }
2565 break;
2566 }
2567 break;
2568
2569 default:
2570
2571 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2572 return 1;
2573
2574 *File = src->param_binding_type;
2575 *Index = src->param_binding_begin;
2576 break;
2577 }
2578 break;
2579
2580 case REGISTER_ESTABLISHED_NAME:
2581
2582 src = parse_string (inst, vc_head, Program, &found);
2583 Program->Position = parse_position (inst);
2584
2585 /* If the name has never been added to our symbol table, we're hosed */
2586 if (!found) {
2587 _mesa_set_program_error (ctx, Program->Position,
2588 "3: Undefined variable");
2589 _mesa_error (ctx, GL_INVALID_OPERATION, "3: Undefined variable: %s",
2590 src->name);
2591 return 1;
2592 }
2593
2594 switch (src->type) {
2595 case vt_attrib:
2596 *File = PROGRAM_INPUT;
2597 *Index = src->attrib_binding_idx;
2598 break;
2599
2600 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2601 case vt_param:
2602 *File = src->param_binding_type;
2603 *Index = src->param_binding_begin;
2604 break;
2605
2606 case vt_temp:
2607 *File = PROGRAM_TEMPORARY;
2608 *Index = src->temp_binding;
2609 break;
2610
2611 /* If the var type is vt_output no go */
2612 default:
2613 _mesa_set_program_error (ctx, Program->Position,
2614 "destination register is read only");
2615 _mesa_error (ctx, GL_INVALID_OPERATION,
2616 "destination register is read only: %s",
2617 src->name);
2618 return 1;
2619 }
2620 break;
2621
2622 default:
2623 _mesa_set_program_error (ctx, Program->Position,
2624 "Unknown token in parse_src_reg");
2625 _mesa_error (ctx, GL_INVALID_OPERATION,
2626 "Unknown token in parse_src_reg");
2627 return 1;
2628 }
2629
2630 return 0;
2631}
2632
2633/**
2634 */
2635static GLuint
2636parse_vector_src_reg (GLcontext * ctx, GLubyte ** inst,
2637 struct var_cache **vc_head, struct arb_program *Program,
2638 GLint * File, GLint * Index, GLboolean * Negate,
2639 GLubyte * Swizzle, GLboolean *IsRelOffset)
2640{
2641 /* Grab the sign */
Brian Paul05908952004-06-08 15:20:23 +00002642 *Negate = (parse_sign (inst) == -1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002643
2644 /* And the src reg */
2645 if (parse_src_reg (ctx, inst, vc_head, Program, File, Index, IsRelOffset))
2646 return 1;
2647
2648 /* finally, the swizzle */
2649 parse_swizzle_mask (inst, Swizzle, 4);
2650
2651 return 0;
2652}
2653
2654/**
2655 */
2656static GLuint
2657parse_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
2658 struct var_cache **vc_head, struct arb_program *Program,
2659 GLint * File, GLint * Index, GLboolean * Negate,
2660 GLubyte * Swizzle, GLboolean *IsRelOffset)
2661{
2662 /* Grab the sign */
Brian Paul05908952004-06-08 15:20:23 +00002663 *Negate = (parse_sign (inst) == -1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002664
2665 /* And the src reg */
2666 if (parse_src_reg (ctx, inst, vc_head, Program, File, Index, IsRelOffset))
2667 return 1;
2668
2669 /* Now, get the component and shove it into all the swizzle slots */
2670 parse_swizzle_mask (inst, Swizzle, 1);
2671
2672 return 0;
2673}
2674
2675/**
2676 * This is a big mother that handles getting opcodes into the instruction
2677 * and handling the src & dst registers for fragment program instructions
2678 */
2679static GLuint
2680parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2681 struct var_cache **vc_head, struct arb_program *Program,
2682 struct fp_instruction *fp)
2683{
2684 GLint a, b;
2685 GLubyte swz[4]; /* FP's swizzle mask is a GLubyte, while VP's is GLuint */
2686 GLuint texcoord;
2687 GLubyte instClass, type, code;
2688 GLboolean rel;
2689
2690 /* No condition codes in ARB_fp */
2691 fp->UpdateCondRegister = 0;
2692
2693 /* Record the position in the program string for debugging */
2694 fp->StringPos = Program->Position;
2695
2696 /* OP_ALU_INST or OP_TEX_INST */
2697 instClass = *(*inst)++;
2698
2699 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2700 * OP_TEX_{SAMPLE, KIL}
2701 */
2702 type = *(*inst)++;
2703
2704 /* The actual opcode name */
2705 code = *(*inst)++;
2706
2707 /* Increment the correct count */
2708 switch (instClass) {
2709 case OP_ALU_INST:
2710 Program->NumAluInstructions++;
2711 break;
2712 case OP_TEX_INST:
2713 Program->NumTexInstructions++;
2714 break;
2715 }
2716
2717 fp->Saturate = 0;
2718 fp->Precision = FLOAT32;
2719
2720 fp->DstReg.CondMask = COND_TR;
2721
2722 switch (type) {
2723 case OP_ALU_VECTOR:
2724 switch (code) {
2725 case OP_ABS_SAT:
2726 fp->Saturate = 1;
2727 case OP_ABS:
2728 fp->Opcode = FP_OPCODE_ABS;
2729 break;
2730
2731 case OP_FLR_SAT:
2732 fp->Saturate = 1;
2733 case OP_FLR:
2734 fp->Opcode = FP_OPCODE_FLR;
2735 break;
2736
2737 case OP_FRC_SAT:
2738 fp->Saturate = 1;
2739 case OP_FRC:
2740 fp->Opcode = FP_OPCODE_FRC;
2741 break;
2742
2743 case OP_LIT_SAT:
2744 fp->Saturate = 1;
2745 case OP_LIT:
2746 fp->Opcode = FP_OPCODE_LIT;
2747 break;
2748
2749 case OP_MOV_SAT:
2750 fp->Saturate = 1;
2751 case OP_MOV:
2752 fp->Opcode = FP_OPCODE_MOV;
2753 break;
2754 }
2755
2756 if (parse_masked_dst_reg
2757 (ctx, inst, vc_head, Program, (GLint *) & fp->DstReg.File,
2758 &fp->DstReg.Index, fp->DstReg.WriteMask))
2759 return 1;
2760
2761 fp->SrcReg[0].Abs = GL_FALSE;
2762 fp->SrcReg[0].NegateAbs = GL_FALSE;
2763 if (parse_vector_src_reg
2764 (ctx, inst, vc_head, Program, (GLint *) & fp->SrcReg[0].File,
2765 &fp->SrcReg[0].Index, &fp->SrcReg[0].NegateBase,
2766 swz, &rel))
2767 return 1;
2768 for (b=0; b<4; b++)
2769 fp->SrcReg[0].Swizzle[b] = swz[b];
2770 break;
2771
2772 case OP_ALU_SCALAR:
2773 switch (code) {
2774 case OP_COS_SAT:
2775 fp->Saturate = 1;
2776 case OP_COS:
2777 fp->Opcode = FP_OPCODE_COS;
2778 break;
2779
2780 case OP_EX2_SAT:
2781 fp->Saturate = 1;
2782 case OP_EX2:
2783 fp->Opcode = FP_OPCODE_EX2;
2784 break;
2785
2786 case OP_LG2_SAT:
2787 fp->Saturate = 1;
2788 case OP_LG2:
2789 fp->Opcode = FP_OPCODE_LG2;
2790 break;
2791
2792 case OP_RCP_SAT:
2793 fp->Saturate = 1;
2794 case OP_RCP:
2795 fp->Opcode = FP_OPCODE_RCP;
2796 break;
2797
2798 case OP_RSQ_SAT:
2799 fp->Saturate = 1;
2800 case OP_RSQ:
2801 fp->Opcode = FP_OPCODE_RSQ;
2802 break;
2803
2804 case OP_SIN_SAT:
2805 fp->Saturate = 1;
2806 case OP_SIN:
2807 fp->Opcode = FP_OPCODE_SIN;
2808 break;
2809
2810 case OP_SCS_SAT:
2811 fp->Saturate = 1;
2812 case OP_SCS:
2813
2814 fp->Opcode = FP_OPCODE_SCS;
2815 break;
2816 }
2817
2818 if (parse_masked_dst_reg
2819 (ctx, inst, vc_head, Program, (GLint *) & fp->DstReg.File,
2820 &fp->DstReg.Index, fp->DstReg.WriteMask))
2821 return 1;
2822 fp->SrcReg[0].Abs = GL_FALSE;
2823 fp->SrcReg[0].NegateAbs = GL_FALSE;
2824 if (parse_scalar_src_reg
2825 (ctx, inst, vc_head, Program, (GLint *) & fp->SrcReg[0].File,
2826 &fp->SrcReg[0].Index, &fp->SrcReg[0].NegateBase,
2827 swz, &rel))
2828 return 1;
2829 for (b=0; b<4; b++)
2830 fp->SrcReg[0].Swizzle[b] = swz[b];
2831 break;
2832
2833 case OP_ALU_BINSC:
2834 switch (code) {
2835 case OP_POW_SAT:
2836 fp->Saturate = 1;
2837 case OP_POW:
2838 fp->Opcode = FP_OPCODE_POW;
2839 break;
2840 }
2841
2842 if (parse_masked_dst_reg
2843 (ctx, inst, vc_head, Program, (GLint *) & fp->DstReg.File,
2844 &fp->DstReg.Index, fp->DstReg.WriteMask))
2845 return 1;
2846 for (a = 0; a < 2; a++) {
2847 fp->SrcReg[a].Abs = GL_FALSE;
2848 fp->SrcReg[a].NegateAbs = GL_FALSE;
2849 if (parse_scalar_src_reg
2850 (ctx, inst, vc_head, Program, (GLint *) & fp->SrcReg[a].File,
2851 &fp->SrcReg[a].Index, &fp->SrcReg[a].NegateBase,
2852 swz, &rel))
2853 return 1;
2854 for (b=0; b<4; b++)
2855 fp->SrcReg[a].Swizzle[b] = swz[b];
2856 }
2857 break;
2858
2859
2860 case OP_ALU_BIN:
2861 switch (code) {
2862 case OP_ADD_SAT:
2863 fp->Saturate = 1;
2864 case OP_ADD:
2865 fp->Opcode = FP_OPCODE_ADD;
2866 break;
2867
2868 case OP_DP3_SAT:
2869 fp->Saturate = 1;
2870 case OP_DP3:
2871 fp->Opcode = FP_OPCODE_DP3;
2872 break;
2873
2874 case OP_DP4_SAT:
2875 fp->Saturate = 1;
2876 case OP_DP4:
2877 fp->Opcode = FP_OPCODE_DP4;
2878 break;
2879
2880 case OP_DPH_SAT:
2881 fp->Saturate = 1;
2882 case OP_DPH:
2883 fp->Opcode = FP_OPCODE_DPH;
2884 break;
2885
2886 case OP_DST_SAT:
2887 fp->Saturate = 1;
2888 case OP_DST:
2889 fp->Opcode = FP_OPCODE_DST;
2890 break;
2891
2892 case OP_MAX_SAT:
2893 fp->Saturate = 1;
2894 case OP_MAX:
2895 fp->Opcode = FP_OPCODE_MAX;
2896 break;
2897
2898 case OP_MIN_SAT:
2899 fp->Saturate = 1;
2900 case OP_MIN:
2901 fp->Opcode = FP_OPCODE_MIN;
2902 break;
2903
2904 case OP_MUL_SAT:
2905 fp->Saturate = 1;
2906 case OP_MUL:
2907 fp->Opcode = FP_OPCODE_MUL;
2908 break;
2909
2910 case OP_SGE_SAT:
2911 fp->Saturate = 1;
2912 case OP_SGE:
2913 fp->Opcode = FP_OPCODE_SGE;
2914 break;
2915
2916 case OP_SLT_SAT:
2917 fp->Saturate = 1;
2918 case OP_SLT:
2919 fp->Opcode = FP_OPCODE_SLT;
2920 break;
2921
2922 case OP_SUB_SAT:
2923 fp->Saturate = 1;
2924 case OP_SUB:
2925 fp->Opcode = FP_OPCODE_SUB;
2926 break;
2927
2928 case OP_XPD_SAT:
2929 fp->Saturate = 1;
2930 case OP_XPD:
2931 fp->Opcode = FP_OPCODE_XPD;
2932 break;
2933 }
2934
2935 if (parse_masked_dst_reg
2936 (ctx, inst, vc_head, Program, (GLint *) & fp->DstReg.File,
2937 &fp->DstReg.Index, fp->DstReg.WriteMask))
2938 return 1;
2939 for (a = 0; a < 2; a++) {
2940 fp->SrcReg[a].Abs = GL_FALSE;
2941 fp->SrcReg[a].NegateAbs = GL_FALSE;
2942 if (parse_vector_src_reg
2943 (ctx, inst, vc_head, Program, (GLint *) & fp->SrcReg[a].File,
2944 &fp->SrcReg[a].Index, &fp->SrcReg[a].NegateBase,
2945 swz, &rel))
2946 return 1;
2947 for (b=0; b<4; b++)
2948 fp->SrcReg[a].Swizzle[b] = swz[b];
2949 }
2950 break;
2951
2952 case OP_ALU_TRI:
2953 switch (code) {
2954 case OP_CMP_SAT:
2955 fp->Saturate = 1;
2956 case OP_CMP:
2957 fp->Opcode = FP_OPCODE_CMP;
2958 break;
2959
2960 case OP_LRP_SAT:
2961 fp->Saturate = 1;
2962 case OP_LRP:
2963 fp->Opcode = FP_OPCODE_LRP;
2964 break;
2965
2966 case OP_MAD_SAT:
2967 fp->Saturate = 1;
2968 case OP_MAD:
2969 fp->Opcode = FP_OPCODE_MAD;
2970 break;
2971 }
2972
2973 if (parse_masked_dst_reg
2974 (ctx, inst, vc_head, Program, (GLint *) & fp->DstReg.File,
2975 &fp->DstReg.Index, fp->DstReg.WriteMask))
2976 return 1;
2977 for (a = 0; a < 3; a++) {
2978 fp->SrcReg[a].Abs = GL_FALSE;
2979 fp->SrcReg[a].NegateAbs = GL_FALSE;
2980 if (parse_vector_src_reg
2981 (ctx, inst, vc_head, Program, (GLint *) & fp->SrcReg[a].File,
2982 &fp->SrcReg[a].Index, &fp->SrcReg[a].NegateBase,
2983 swz, &rel))
2984 return 1;
2985 for (b=0; b<4; b++)
2986 fp->SrcReg[a].Swizzle[b] = swz[b];
2987 }
2988 break;
2989
2990 case OP_ALU_SWZ:
2991 switch (code) {
2992 case OP_SWZ_SAT:
2993 fp->Saturate = 1;
2994 case OP_SWZ:
2995 fp->Opcode = FP_OPCODE_SWZ;
2996 break;
2997 }
2998 if (parse_masked_dst_reg
2999 (ctx, inst, vc_head, Program, (GLint *) & fp->DstReg.File,
3000 &fp->DstReg.Index, fp->DstReg.WriteMask))
3001 return 1;
3002
3003 if (parse_src_reg
3004 (ctx, inst, vc_head, Program, (GLint *) & fp->SrcReg[0].File,
3005 &fp->SrcReg[0].Index, &rel))
3006 return 1;
3007 parse_extended_swizzle_mask (inst, swz,
3008 &fp->SrcReg[0].NegateBase);
3009 for (b=0; b<4; b++)
3010 fp->SrcReg[0].Swizzle[b] = swz[b];
3011 break;
3012
3013 case OP_TEX_SAMPLE:
3014 switch (code) {
3015 case OP_TEX_SAT:
3016 fp->Saturate = 1;
3017 case OP_TEX:
3018 fp->Opcode = FP_OPCODE_TEX;
3019 break;
3020
3021 case OP_TXP_SAT:
3022 fp->Saturate = 1;
3023 case OP_TXP:
3024 fp->Opcode = FP_OPCODE_TXP;
3025 break;
3026
3027 case OP_TXB_SAT:
3028
3029 fp->Saturate = 1;
3030 case OP_TXB:
3031 fp->Opcode = FP_OPCODE_TXB;
3032 break;
3033 }
3034
3035 if (parse_masked_dst_reg
3036 (ctx, inst, vc_head, Program, (GLint *) & fp->DstReg.File,
3037 &fp->DstReg.Index, fp->DstReg.WriteMask))
3038 return 1;
3039 fp->SrcReg[0].Abs = GL_FALSE;
3040 fp->SrcReg[0].NegateAbs = GL_FALSE;
3041 if (parse_vector_src_reg
3042 (ctx, inst, vc_head, Program, (GLint *) & fp->SrcReg[0].File,
3043 &fp->SrcReg[0].Index, &fp->SrcReg[0].NegateBase,
3044 swz, &rel))
3045 return 1;
3046 for (b=0; b<4; b++)
3047 fp->SrcReg[0].Swizzle[b] = swz[b];
3048
3049 /* texImageUnit */
3050 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3051 return 1;
3052 fp->TexSrcUnit = texcoord;
3053
3054 /* texTarget */
3055 switch (*(*inst)++) {
3056 case TEXTARGET_1D:
3057 fp->TexSrcBit = TEXTURE_1D_BIT;
3058 break;
3059 case TEXTARGET_2D:
3060 fp->TexSrcBit = TEXTURE_2D_BIT;
3061 break;
3062 case TEXTARGET_3D:
3063 fp->TexSrcBit = TEXTURE_3D_BIT;
3064 break;
3065 case TEXTARGET_RECT:
3066 fp->TexSrcBit = TEXTURE_RECT_BIT;
3067 break;
3068 case TEXTARGET_CUBE:
3069 fp->TexSrcBit = TEXTURE_CUBE_BIT;
3070 break;
3071 case TEXTARGET_SHADOW1D:
3072 case TEXTARGET_SHADOW2D:
3073 case TEXTARGET_SHADOWRECT:
3074 /* TODO ARB_fragment_program_shadow code */
3075 break;
3076 }
3077 Program->TexturesUsed[texcoord] |= fp->TexSrcBit;
3078 break;
3079
3080 case OP_TEX_KIL:
3081 fp->Opcode = FP_OPCODE_KIL;
3082 fp->SrcReg[0].Abs = GL_FALSE;
3083 fp->SrcReg[0].NegateAbs = GL_FALSE;
3084 if (parse_vector_src_reg
3085 (ctx, inst, vc_head, Program, (GLint *) & fp->SrcReg[0].File,
3086 &fp->SrcReg[0].Index, &fp->SrcReg[0].NegateBase,
3087 swz, &rel))
3088 return 1;
3089 for (b=0; b<4; b++)
3090 fp->SrcReg[0].Swizzle[b] = swz[b];
3091 break;
3092 }
3093
3094 return 0;
3095}
3096
3097/**
3098 * This is a big mother that handles getting opcodes into the instruction
3099 * and handling the src & dst registers for vertex program instructions
3100 */
3101static GLuint
3102parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3103 struct var_cache **vc_head, struct arb_program *Program,
3104 struct vp_instruction *vp)
3105{
3106 GLint a;
3107 GLubyte type, code;
3108
3109 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3110 type = *(*inst)++;
3111
3112 /* The actual opcode name */
3113 code = *(*inst)++;
3114
3115 /* Record the position in the program string for debugging */
3116 vp->StringPos = Program->Position;
3117
3118 vp->SrcReg[0].RelAddr = vp->SrcReg[1].RelAddr = vp->SrcReg[2].RelAddr = 0;
3119
3120 for (a = 0; a < 4; a++) {
3121 vp->SrcReg[0].Swizzle[a] = a;
3122 vp->SrcReg[1].Swizzle[a] = a;
3123 vp->SrcReg[2].Swizzle[a] = a;
3124 vp->DstReg.WriteMask[a] = 1;
3125 }
3126
3127 switch (type) {
3128 /* XXX: */
3129 case OP_ALU_ARL:
3130 vp->Opcode = VP_OPCODE_ARL;
3131
3132 /* Remember to set SrcReg.RelAddr; */
3133
3134 /* Get the masked address register [dst] */
3135 if (parse_masked_address_reg
3136 (ctx, inst, vc_head, Program, &vp->DstReg.Index,
3137 vp->DstReg.WriteMask))
3138 return 1;
3139 vp->DstReg.File = PROGRAM_ADDRESS;
3140
3141 /* Get a scalar src register */
3142 if (parse_scalar_src_reg
3143 (ctx, inst, vc_head, Program, (GLint *) & vp->SrcReg[0].File,
3144 &vp->SrcReg[0].Index, &vp->SrcReg[0].Negate,
3145 vp->SrcReg[0].Swizzle, &vp->SrcReg[0].RelAddr))
3146 return 1;
3147
3148 break;
3149
3150 case OP_ALU_VECTOR:
3151 switch (code) {
3152 case OP_ABS:
3153 vp->Opcode = VP_OPCODE_ABS;
3154 break;
3155 case OP_FLR:
3156 vp->Opcode = VP_OPCODE_FLR;
3157 break;
3158 case OP_FRC:
3159 vp->Opcode = VP_OPCODE_FRC;
3160 break;
3161 case OP_LIT:
3162 vp->Opcode = VP_OPCODE_LIT;
3163 break;
3164 case OP_MOV:
3165 vp->Opcode = VP_OPCODE_MOV;
3166 break;
3167 }
3168 if (parse_masked_dst_reg
3169 (ctx, inst, vc_head, Program, (GLint *) & vp->DstReg.File,
3170 &vp->DstReg.Index, vp->DstReg.WriteMask))
3171 return 1;
3172 if (parse_vector_src_reg
3173 (ctx, inst, vc_head, Program, (GLint *) & vp->SrcReg[0].File,
3174 &vp->SrcReg[0].Index, &vp->SrcReg[0].Negate,
3175 vp->SrcReg[0].Swizzle, &vp->SrcReg[0].RelAddr))
3176 return 1;
3177 break;
3178
3179 case OP_ALU_SCALAR:
3180 switch (code) {
3181 case OP_EX2:
3182 vp->Opcode = VP_OPCODE_EX2;
3183 break;
3184 case OP_EXP:
3185 vp->Opcode = VP_OPCODE_EXP;
3186 break;
3187 case OP_LG2:
3188 vp->Opcode = VP_OPCODE_LG2;
3189 break;
3190 case OP_LOG:
3191 vp->Opcode = VP_OPCODE_LOG;
3192 break;
3193 case OP_RCP:
3194 vp->Opcode = VP_OPCODE_RCP;
3195 break;
3196 case OP_RSQ:
3197 vp->Opcode = VP_OPCODE_RSQ;
3198 break;
3199 }
3200 if (parse_masked_dst_reg
3201 (ctx, inst, vc_head, Program, (GLint *) & vp->DstReg.File,
3202 &vp->DstReg.Index, vp->DstReg.WriteMask))
3203 return 1;
3204 if (parse_scalar_src_reg
3205 (ctx, inst, vc_head, Program, (GLint *) & vp->SrcReg[0].File,
3206 &vp->SrcReg[0].Index, &vp->SrcReg[0].Negate,
3207 vp->SrcReg[0].Swizzle, &vp->SrcReg[0].RelAddr))
3208 return 1;
3209 break;
3210
3211 case OP_ALU_BINSC:
3212 switch (code) {
3213 case OP_POW:
3214 vp->Opcode = VP_OPCODE_POW;
3215 break;
3216 }
3217 if (parse_masked_dst_reg
3218 (ctx, inst, vc_head, Program, (GLint *) & vp->DstReg.File,
3219 &vp->DstReg.Index, vp->DstReg.WriteMask))
3220 return 1;
3221 for (a = 0; a < 2; a++) {
3222 if (parse_scalar_src_reg
3223 (ctx, inst, vc_head, Program, (GLint *) & vp->SrcReg[a].File,
3224 &vp->SrcReg[a].Index, &vp->SrcReg[a].Negate,
3225 vp->SrcReg[a].Swizzle, &vp->SrcReg[a].RelAddr))
3226 return 1;
3227 }
3228 break;
3229
3230 case OP_ALU_BIN:
3231 switch (code) {
3232 case OP_ADD:
3233 vp->Opcode = VP_OPCODE_ADD;
3234 break;
3235 case OP_DP3:
3236 vp->Opcode = VP_OPCODE_DP3;
3237 break;
3238 case OP_DP4:
3239 vp->Opcode = VP_OPCODE_DP4;
3240 break;
3241 case OP_DPH:
3242 vp->Opcode = VP_OPCODE_DPH;
3243 break;
3244 case OP_DST:
3245 vp->Opcode = VP_OPCODE_DST;
3246 break;
3247 case OP_MAX:
3248 vp->Opcode = VP_OPCODE_MAX;
3249 break;
3250 case OP_MIN:
3251 vp->Opcode = VP_OPCODE_MIN;
3252 break;
3253 case OP_MUL:
3254 vp->Opcode = VP_OPCODE_MUL;
3255 break;
3256 case OP_SGE:
3257 vp->Opcode = VP_OPCODE_SGE;
3258 break;
3259 case OP_SLT:
3260 vp->Opcode = VP_OPCODE_SLT;
3261 break;
3262 case OP_SUB:
3263 vp->Opcode = VP_OPCODE_SUB;
3264 break;
3265 case OP_XPD:
3266 vp->Opcode = VP_OPCODE_XPD;
3267 break;
3268 }
3269 if (parse_masked_dst_reg
3270 (ctx, inst, vc_head, Program, (GLint *) & vp->DstReg.File,
3271 &vp->DstReg.Index, vp->DstReg.WriteMask))
3272 return 1;
3273 for (a = 0; a < 2; a++) {
3274 if (parse_vector_src_reg
3275 (ctx, inst, vc_head, Program, (GLint *) & vp->SrcReg[a].File,
3276 &vp->SrcReg[a].Index, &vp->SrcReg[a].Negate,
3277 vp->SrcReg[a].Swizzle, &vp->SrcReg[a].RelAddr))
3278 return 1;
3279 }
3280 break;
3281
3282 case OP_ALU_TRI:
3283 switch (code) {
3284 case OP_MAD:
3285 vp->Opcode = VP_OPCODE_MAD;
3286 break;
3287 }
3288
3289 if (parse_masked_dst_reg
3290 (ctx, inst, vc_head, Program, (GLint *) & vp->DstReg.File,
3291 &vp->DstReg.Index, vp->DstReg.WriteMask))
3292 return 1;
3293 for (a = 0; a < 3; a++) {
3294 if (parse_vector_src_reg
3295 (ctx, inst, vc_head, Program, (GLint *) & vp->SrcReg[a].File,
3296 &vp->SrcReg[a].Index, &vp->SrcReg[a].Negate,
3297 vp->SrcReg[a].Swizzle, &vp->SrcReg[a].RelAddr))
3298 return 1;
3299 }
3300 break;
3301
3302 case OP_ALU_SWZ:
3303 switch (code) {
3304 case OP_SWZ:
3305 vp->Opcode = VP_OPCODE_SWZ;
3306 break;
3307 }
3308 if (parse_masked_dst_reg
3309 (ctx, inst, vc_head, Program, (GLint *) & vp->DstReg.File,
3310 &vp->DstReg.Index, vp->DstReg.WriteMask))
3311 return 1;
3312
3313 if (parse_src_reg
3314 (ctx, inst, vc_head, Program, (GLint *) & vp->SrcReg[0].File,
3315 &vp->SrcReg[0].Index, &vp->SrcReg[0].RelAddr))
3316 return 1;
3317 parse_extended_swizzle_mask (inst, vp->SrcReg[0].Swizzle,
3318 &vp->SrcReg[0].Negate);
3319 break;
3320 }
3321 return 0;
3322}
3323
3324#if DEBUG_PARSING
3325
3326static GLvoid
3327print_state_token (GLint token)
3328{
3329 switch (token) {
3330 case STATE_MATERIAL:
3331 fprintf (stderr, "STATE_MATERIAL ");
3332 break;
3333 case STATE_LIGHT:
3334 fprintf (stderr, "STATE_LIGHT ");
3335 break;
3336
3337 case STATE_LIGHTMODEL_AMBIENT:
3338 fprintf (stderr, "STATE_AMBIENT ");
3339 break;
3340
3341 case STATE_LIGHTMODEL_SCENECOLOR:
3342 fprintf (stderr, "STATE_SCENECOLOR ");
3343 break;
3344
3345 case STATE_LIGHTPROD:
3346 fprintf (stderr, "STATE_LIGHTPROD ");
3347 break;
3348
3349 case STATE_TEXGEN:
3350 fprintf (stderr, "STATE_TEXGEN ");
3351 break;
3352
3353 case STATE_FOG_COLOR:
3354 fprintf (stderr, "STATE_FOG_COLOR ");
3355 break;
3356
3357 case STATE_FOG_PARAMS:
3358 fprintf (stderr, "STATE_FOG_PARAMS ");
3359 break;
3360
3361 case STATE_CLIPPLANE:
3362 fprintf (stderr, "STATE_CLIPPLANE ");
3363 break;
3364
3365 case STATE_POINT_SIZE:
3366 fprintf (stderr, "STATE_POINT_SIZE ");
3367 break;
3368
3369 case STATE_POINT_ATTENUATION:
3370 fprintf (stderr, "STATE_ATTENUATION ");
3371 break;
3372
3373 case STATE_MATRIX:
3374 fprintf (stderr, "STATE_MATRIX ");
3375 break;
3376
3377 case STATE_MODELVIEW:
3378 fprintf (stderr, "STATE_MODELVIEW ");
3379 break;
3380
3381 case STATE_PROJECTION:
3382 fprintf (stderr, "STATE_PROJECTION ");
3383 break;
3384
3385 case STATE_MVP:
3386 fprintf (stderr, "STATE_MVP ");
3387 break;
3388
3389 case STATE_TEXTURE:
3390 fprintf (stderr, "STATE_TEXTURE ");
3391 break;
3392
3393 case STATE_PROGRAM:
3394 fprintf (stderr, "STATE_PROGRAM ");
3395 break;
3396
3397 case STATE_MATRIX_INVERSE:
3398 fprintf (stderr, "STATE_INVERSE ");
3399 break;
3400
3401 case STATE_MATRIX_TRANSPOSE:
3402 fprintf (stderr, "STATE_TRANSPOSE ");
3403 break;
3404
3405 case STATE_MATRIX_INVTRANS:
3406 fprintf (stderr, "STATE_INVTRANS ");
3407 break;
3408
3409 case STATE_AMBIENT:
3410 fprintf (stderr, "STATE_AMBIENT ");
3411 break;
3412
3413 case STATE_DIFFUSE:
3414 fprintf (stderr, "STATE_DIFFUSE ");
3415 break;
3416
3417 case STATE_SPECULAR:
3418 fprintf (stderr, "STATE_SPECULAR ");
3419 break;
3420
3421 case STATE_EMISSION:
3422 fprintf (stderr, "STATE_EMISSION ");
3423 break;
3424
3425 case STATE_SHININESS:
3426 fprintf (stderr, "STATE_SHININESS ");
3427 break;
3428
3429 case STATE_HALF:
3430 fprintf (stderr, "STATE_HALF ");
3431 break;
3432
3433 case STATE_POSITION:
3434 fprintf (stderr, "STATE_POSITION ");
3435 break;
3436
3437 case STATE_ATTENUATION:
3438 fprintf (stderr, "STATE_ATTENUATION ");
3439 break;
3440
3441 case STATE_SPOT_DIRECTION:
3442 fprintf (stderr, "STATE_DIRECTION ");
3443 break;
3444
3445 case STATE_TEXGEN_EYE_S:
3446 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3447 break;
3448
3449 case STATE_TEXGEN_EYE_T:
3450 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3451 break;
3452
3453 case STATE_TEXGEN_EYE_R:
3454 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3455 break;
3456
3457 case STATE_TEXGEN_EYE_Q:
3458 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3459 break;
3460
3461 case STATE_TEXGEN_OBJECT_S:
3462 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3463 break;
3464
3465 case STATE_TEXGEN_OBJECT_T:
3466 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3467 break;
3468
3469 case STATE_TEXGEN_OBJECT_R:
3470 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3471 break;
3472
3473 case STATE_TEXGEN_OBJECT_Q:
3474 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3475 break;
3476
3477 case STATE_TEXENV_COLOR:
3478 fprintf (stderr, "STATE_TEXENV_COLOR ");
3479 break;
3480
3481 case STATE_DEPTH_RANGE:
3482 fprintf (stderr, "STATE_DEPTH_RANGE ");
3483 break;
3484
3485 case STATE_VERTEX_PROGRAM:
3486 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3487 break;
3488
3489 case STATE_FRAGMENT_PROGRAM:
3490 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3491 break;
3492
3493 case STATE_ENV:
3494 fprintf (stderr, "STATE_ENV ");
3495 break;
3496
3497 case STATE_LOCAL:
3498 fprintf (stderr, "STATE_LOCAL ");
3499 break;
3500
3501 }
3502 fprintf (stderr, "[%d] ", token);
3503}
3504
3505
3506static GLvoid
3507debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3508 struct arb_program *Program)
3509{
3510 struct var_cache *vc;
3511 GLint a, b;
3512
3513 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3514
3515 /* First of all, print out the contents of the var_cache */
3516 vc = vc_head;
3517 while (vc) {
3518 fprintf (stderr, "[%x]\n", vc);
3519 switch (vc->type) {
3520 case vt_none:
3521 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3522 break;
3523 case vt_attrib:
3524 fprintf (stderr, "ATTRIB %s\n", vc->name);
3525 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3526 break;
3527 case vt_param:
3528 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3529 vc->param_binding_begin, vc->param_binding_length);
3530 b = vc->param_binding_begin;
3531 for (a = 0; a < vc->param_binding_length; a++) {
3532 fprintf (stderr, "%s\n",
3533 Program->Parameters->Parameters[a + b].Name);
3534 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3535 print_state_token (Program->Parameters->Parameters[a + b].
3536 StateIndexes[0]);
3537 print_state_token (Program->Parameters->Parameters[a + b].
3538 StateIndexes[1]);
3539 print_state_token (Program->Parameters->Parameters[a + b].
3540 StateIndexes[2]);
3541 print_state_token (Program->Parameters->Parameters[a + b].
3542 StateIndexes[3]);
3543 print_state_token (Program->Parameters->Parameters[a + b].
3544 StateIndexes[4]);
3545 print_state_token (Program->Parameters->Parameters[a + b].
3546 StateIndexes[5]);
3547 }
3548 else
3549 fprintf (stderr, "%f %f %f %f\n",
3550 Program->Parameters->Parameters[a + b].Values[0],
3551 Program->Parameters->Parameters[a + b].Values[1],
3552 Program->Parameters->Parameters[a + b].Values[2],
3553 Program->Parameters->Parameters[a + b].Values[3]);
3554 }
3555 break;
3556 case vt_temp:
3557 fprintf (stderr, "TEMP %s\n", vc->name);
3558 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3559 break;
3560 case vt_output:
3561 fprintf (stderr, "OUTPUT %s\n", vc->name);
3562 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3563 break;
3564 case vt_alias:
3565 fprintf (stderr, "ALIAS %s\n", vc->name);
3566 fprintf (stderr, " binding: 0x%x (%s)\n",
3567 vc->alias_binding, vc->alias_binding->name);
3568 break;
3569 }
3570 vc = vc->next;
3571 }
3572}
3573
3574#endif
3575
3576
3577/**
3578 * The main loop for parsing a fragment or vertex program
3579 *
3580 * \return 0 on sucess, 1 on error
3581 */
3582static GLint
3583parse_arb_program (GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
3584 struct arb_program *Program)
3585{
3586 GLint err = 0;
3587
3588 Program->MajorVersion = (GLuint) * inst++;
3589 Program->MinorVersion = (GLuint) * inst++;
3590
3591 while (*inst != END) {
3592 switch (*inst++) {
3593
3594 case OPTION:
3595 switch (*inst++) {
3596 case ARB_PRECISION_HINT_FASTEST:
3597 Program->PrecisionOption = GL_FASTEST;
3598 break;
3599
3600 case ARB_PRECISION_HINT_NICEST:
3601 Program->PrecisionOption = GL_NICEST;
3602 break;
3603
3604 case ARB_FOG_EXP:
3605 Program->FogOption = GL_EXP;
3606 break;
3607
3608 case ARB_FOG_EXP2:
3609 Program->FogOption = GL_EXP2;
3610 break;
3611
3612 case ARB_FOG_LINEAR:
3613 Program->FogOption = GL_LINEAR;
3614 break;
3615
3616 case ARB_POSITION_INVARIANT:
3617 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
3618 Program->HintPositionInvariant = 1;
3619 break;
3620
3621 case ARB_FRAGMENT_PROGRAM_SHADOW:
3622 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3623 /* TODO ARB_fragment_program_shadow code */
3624 }
3625 break;
3626 }
3627 break;
3628
3629 case INSTRUCTION:
3630 Program->Position = parse_position (&inst);
3631
3632 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3633
3634 /* Check the instruction count
3635 * XXX: Does END count as an instruction?
3636 */
3637 if (Program->Base.NumInstructions+1 == MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS) {
3638 _mesa_set_program_error (ctx, Program->Position,
3639 "Max instruction count exceeded!");
3640 _mesa_error (ctx, GL_INVALID_OPERATION,
3641 "Max instruction count exceeded!");
3642 }
3643
3644 /* Realloc Program->FPInstructions */
3645 Program->FPInstructions =
3646 (struct fp_instruction *) _mesa_realloc (Program->FPInstructions,
3647 Program->Base.NumInstructions*sizeof(struct fp_instruction),
3648 (Program->Base.NumInstructions+1)*sizeof (struct fp_instruction));
3649
3650 /* parse the current instruction */
3651 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
3652 &Program->FPInstructions[Program->Base.NumInstructions]);
3653
3654 }
3655 else {
3656 /* Check the instruction count
3657 * XXX: Does END count as an instruction?
3658 */
3659 if (Program->Base.NumInstructions+1 == MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS) {
3660 _mesa_set_program_error (ctx, Program->Position,
3661 "Max instruction count exceeded!");
3662 _mesa_error (ctx, GL_INVALID_OPERATION,
3663 "Max instruction count exceeded!");
3664 }
3665
3666 /* Realloc Program->VPInstructions */
3667 Program->VPInstructions =
3668 (struct vp_instruction *) _mesa_realloc (Program->VPInstructions,
3669 Program->Base.NumInstructions*sizeof(struct vp_instruction),
3670 (Program->Base.NumInstructions +1)*sizeof(struct vp_instruction));
3671
3672 /* parse the current instruction */
3673 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
3674 &Program->VPInstructions[Program->Base.NumInstructions]);
3675 }
3676
3677 /* increment Program->Base.NumInstructions */
3678 Program->Base.NumInstructions++;
3679 break;
3680
3681 case DECLARATION:
3682 err = parse_declaration (ctx, &inst, vc_head, Program);
3683 break;
3684
3685 default:
3686 break;
3687 }
3688
3689 if (err)
3690 break;
3691 }
3692
3693 /* Finally, tag on an OPCODE_END instruction */
3694 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3695 Program->FPInstructions =
3696 (struct fp_instruction *) _mesa_realloc (Program->FPInstructions,
3697 Program->Base.NumInstructions*sizeof(struct fp_instruction),
3698 (Program->Base.NumInstructions+1)*sizeof(struct fp_instruction));
3699
3700 Program->FPInstructions[Program->Base.NumInstructions].Opcode = FP_OPCODE_END;
3701 /* YYY Wrong Position in program, whatever, at least not random -> crash
3702 Program->Position = parse_position (&inst);
3703 */
3704 Program->FPInstructions[Program->Base.NumInstructions].StringPos = Program->Position;
3705 }
3706 else {
3707 Program->VPInstructions =
3708 (struct vp_instruction *) _mesa_realloc (Program->VPInstructions,
3709 Program->Base.NumInstructions*sizeof(struct vp_instruction),
3710 (Program->Base.NumInstructions+1)*sizeof(struct vp_instruction));
3711
3712 Program->VPInstructions[Program->Base.NumInstructions].Opcode = VP_OPCODE_END;
3713 /* YYY Wrong Position in program, whatever, at least not random -> crash
3714 Program->Position = parse_position (&inst);
3715 */
3716 Program->VPInstructions[Program->Base.NumInstructions].StringPos = Program->Position;
3717 }
3718
3719 /* increment Program->Base.NumInstructions */
3720 Program->Base.NumInstructions++;
3721
3722 return err;
3723}
3724
3725/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003726__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003727#include "grammar_syn.h"
3728;
3729
3730static int set_reg8 (GLcontext *ctx, grammar id, const byte *name, byte value)
3731{
3732 char error_msg[300];
3733 GLint error_pos;
3734
3735 if (grammar_set_reg8 (id, name, value))
3736 return 0;
3737
3738 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3739 _mesa_set_program_error (ctx, error_pos, error_msg);
3740 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3741 return 1;
3742}
3743
3744static int extension_is_supported (const GLubyte *ext)
3745{
3746 const GLubyte *extensions = glGetString (GL_EXTENSIONS);
3747 const GLubyte *end = extensions + _mesa_strlen ((const char *) extensions);
3748 const GLint ext_len = _mesa_strlen ((const char *) ext);
3749
3750 while (extensions < end)
3751 {
3752 const GLubyte *name_end = (const GLubyte *) strchr ((const char *) extensions, ' ');
3753 if (name_end == NULL)
3754 name_end = end;
3755 if (name_end - extensions == ext_len && _mesa_strncmp ((const char *) ext,
3756 (const char *) extensions, ext_len) == 0)
3757 return 1;
3758 extensions = name_end + 1;
3759 }
3760
3761 return 0;
3762}
3763
3764static int enable_ext (GLcontext *ctx, grammar id, const byte *name, const byte *extname)
3765{
3766 if (extension_is_supported (extname))
3767 if (set_reg8 (ctx, id, name, 0x01))
3768 return 1;
3769 return 0;
3770}
3771
3772/**
3773 * This kicks everything off.
3774 *
3775 * \param ctx - The GL Context
3776 * \param str - The program string
3777 * \param len - The program string length
3778 * \param Program - The arb_program struct to return all the parsed info in
3779 * \return 0 on sucess, 1 on error
3780 */
3781GLuint
3782_mesa_parse_arb_program (GLcontext * ctx, const GLubyte * str, GLsizei len,
3783 struct arb_program * program)
3784{
3785 GLint a, err, error_pos;
3786 char error_msg[300];
3787 GLuint parsed_len;
3788 struct var_cache *vc_head;
3789 grammar arbprogram_syn_id;
3790 GLubyte *parsed, *inst;
3791 GLubyte *strz = NULL;
3792 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3793
Brian Paul7f76b8f2004-09-10 01:05:39 +00003794 /* Reset error state */
3795 _mesa_set_program_error(ctx, -1, NULL);
3796
Jouk Jansen40322e12004-04-05 08:50:36 +00003797#if DEBUG_PARSING
3798 fprintf (stderr, "Loading grammar text!\n");
3799#endif
3800
3801 /* check if the arb_grammar_text (arbprogram.syn) is syntactically correct */
3802 if (!arbprogram_syn_is_ok) {
3803 grammar grammar_syn_id;
3804 GLint err;
3805 GLuint parsed_len;
3806 byte *parsed;
3807
3808 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3809 if (grammar_syn_id == 0) {
3810 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3811 _mesa_set_program_error (ctx, error_pos, error_msg);
3812 _mesa_error (ctx, GL_INVALID_OPERATION,
3813 "Error loading grammar rule set");
3814 return 1;
3815 }
3816
3817 err = grammar_check (grammar_syn_id, (byte *) arb_grammar_text, &parsed, &parsed_len);
3818
3819 /* NOTE: we cant destroy grammar_syn_id right here because grammar_destroy() can
3820 reset the last error
3821 */
3822
3823 if (err == 0) {
3824 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3825 _mesa_set_program_error (ctx, error_pos, error_msg);
3826 _mesa_error (ctx, GL_INVALID_OPERATION, "Error loading grammar rule set");
3827
3828 grammar_destroy (grammar_syn_id);
3829 return 1;
3830 }
3831
3832 grammar_destroy (grammar_syn_id);
3833
3834 arbprogram_syn_is_ok = 1;
3835 }
3836
3837 /* create the grammar object */
3838 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3839 if (arbprogram_syn_id == 0) {
3840 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3841 _mesa_set_program_error (ctx, error_pos, error_msg);
3842 _mesa_error (ctx, GL_INVALID_OPERATION,
3843 "Error loading grammer rule set");
3844 return 1;
3845 }
3846
3847 /* Set program_target register value */
3848 if (set_reg8 (ctx, arbprogram_syn_id, (byte *) "program_target",
3849 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3850 grammar_destroy (arbprogram_syn_id);
3851 return 1;
3852 }
3853
3854 /* Enable all active extensions */
3855 if (enable_ext (ctx, arbprogram_syn_id,
3856 (byte *) "vertex_blend", (byte *) "GL_ARB_vertex_blend") ||
3857 enable_ext (ctx, arbprogram_syn_id,
3858 (byte *) "vertex_blend", (byte *) "GL_EXT_vertex_weighting") ||
3859 enable_ext (ctx, arbprogram_syn_id,
3860 (byte *) "matrix_palette", (byte *) "GL_ARB_matrix_palette") ||
3861 enable_ext (ctx, arbprogram_syn_id,
3862 (byte *) "point_parameters", (byte *) "GL_ARB_point_parameters") ||
3863 enable_ext (ctx, arbprogram_syn_id,
3864 (byte *) "point_parameters", (byte *) "GL_EXT_point_parameters") ||
3865 enable_ext (ctx, arbprogram_syn_id,
3866 (byte *) "secondary_color", (byte *) "GL_EXT_secondary_color") ||
3867 enable_ext (ctx, arbprogram_syn_id,
3868 (byte *) "fog_coord", (byte *) "GL_EXT_fog_coord") ||
3869 enable_ext (ctx, arbprogram_syn_id,
Brian Pauledfe0fe2004-08-20 14:21:20 +00003870 (byte *) "texture_rectangle", (byte *) "GL_ARB_texture_rectangle") ||
3871 enable_ext (ctx, arbprogram_syn_id,
Jouk Jansen40322e12004-04-05 08:50:36 +00003872 (byte *) "texture_rectangle", (byte *) "GL_EXT_texture_rectangle") ||
3873 enable_ext (ctx, arbprogram_syn_id,
3874 (byte *) "texture_rectangle", (byte *) "GL_NV_texture_rectangle") ||
3875 enable_ext (ctx, arbprogram_syn_id,
3876 (byte *) "fragment_program_shadow", (byte *) "GL_ARB_fragment_program_shadow")) {
3877 grammar_destroy (arbprogram_syn_id);
3878 return 1;
3879 }
3880
3881 /* check for NULL character occurences */
3882 {
3883 int i;
3884 for (i = 0; i < len; i++)
3885 if (str[i] == '\0') {
3886 _mesa_set_program_error (ctx, i, "invalid character");
3887 _mesa_error (ctx, GL_INVALID_OPERATION, "Lexical Error");
3888
3889 grammar_destroy (arbprogram_syn_id);
3890 return 1;
3891 }
3892 }
3893
3894 /* copy the program string to a null-terminated string */
3895 /* XXX should I check for NULL from malloc()? */
Brian Paulbdd15b52004-05-04 15:11:06 +00003896 strz = (GLubyte *) _mesa_malloc (len + 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003897 _mesa_memcpy (strz, str, len);
3898 strz[len] = '\0';
3899
3900#if DEBUG_PARSING
3901 printf ("Checking Grammar!\n");
3902#endif
3903 err = grammar_check (arbprogram_syn_id, strz, &parsed, &parsed_len);
3904
3905 /* Syntax parse error */
3906 if (err == 0) {
3907 _mesa_free (strz);
3908 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3909 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003910 _mesa_error (ctx, GL_INVALID_OPERATION, "glProgramStringARB(syntax error)");
3911
3912 /* useful for debugging */
3913 if (0) {
3914 int line, col;
3915 char *s;
Brian Paul17386de2004-08-22 17:24:27 +00003916 printf("Program: %s\n", (char *) strz);
Brian Paul5fe90292004-07-20 21:15:13 +00003917 printf("Error Pos: %d\n", ctx->Program.ErrorPos);
3918 s = (char *) _mesa_find_line_column(strz, strz+ctx->Program.ErrorPos, &line, &col);
3919 printf("line %d col %d: %s\n", line, col, s);
3920 }
3921
Jouk Jansen40322e12004-04-05 08:50:36 +00003922 grammar_destroy (arbprogram_syn_id);
3923 return 1;
3924 }
3925
3926#if DEBUG_PARSING
3927 printf ("Destroying grammer dict [parse retval: %d]\n", err);
3928#endif
3929 grammar_destroy (arbprogram_syn_id);
3930
3931 /* Initialize the arb_program struct */
3932 program->Base.String = strz;
3933 program->Base.NumInstructions =
3934 program->Base.NumTemporaries =
3935 program->Base.NumParameters =
3936 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
3937 program->Parameters = _mesa_new_parameter_list ();
3938 program->InputsRead = 0;
3939 program->OutputsWritten = 0;
3940 program->Position = 0;
3941 program->MajorVersion = program->MinorVersion = 0;
3942 program->PrecisionOption = GL_DONT_CARE;
3943 program->FogOption = GL_NONE;
3944 program->HintPositionInvariant = GL_FALSE;
3945 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
3946 program->TexturesUsed[a] = 0;
3947 program->NumAluInstructions =
3948 program->NumTexInstructions =
3949 program->NumTexIndirections = 0;
3950
3951 program->FPInstructions = NULL;
3952 program->VPInstructions = NULL;
3953
3954 vc_head = NULL;
3955 err = 0;
3956
3957 /* Start examining the tokens in the array */
3958 inst = parsed;
3959
3960 /* Check the grammer rev */
3961 if (*inst++ != REVISION) {
3962 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul5fe90292004-07-20 21:15:13 +00003963 _mesa_error (ctx, GL_INVALID_OPERATION, "glProgramStringARB(Grammar verison mismatch)");
Jouk Jansen40322e12004-04-05 08:50:36 +00003964 err = 1;
3965 }
3966 else {
3967 switch (*inst++) {
3968 case FRAGMENT_PROGRAM:
3969 program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
3970 break;
3971
3972 case VERTEX_PROGRAM:
3973 program->Base.Target = GL_VERTEX_PROGRAM_ARB;
3974 break;
3975 }
3976
3977 err = parse_arb_program (ctx, inst, &vc_head, program);
3978#if DEBUG_PARSING
3979 fprintf (stderr, "Symantic analysis returns %d [1 is bad!]\n", err);
3980#endif
3981 }
3982
3983 /*debug_variables(ctx, vc_head, program); */
3984
3985 /* We're done with the parsed binary array */
3986 var_cache_destroy (&vc_head);
3987
3988 _mesa_free (parsed);
3989#if DEBUG_PARSING
3990 printf ("_mesa_parse_arb_program() done\n");
3991#endif
3992 return err;
3993}