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