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