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