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