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