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