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