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