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