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