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