blob: 6c6ec0736c5f762cad25c49a53c7674192de730a [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian Paula7543902006-08-24 21:58:32 +00003 * Version: 6.5.1
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Michal Krolbb38cad2006-04-11 11:41:11 +00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
Jouk Jansen40322e12004-04-05 08:50:36 +00006 *
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
Jouk Jansen40322e12004-04-05 08:50:36 +000033#include "glheader.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000034#include "imports.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000035#include "arbprogparse.h"
36#include "grammar_mesa.h"
Brian Paul32df89e2005-10-29 18:26:43 +000037#include "program.h"
Brian Paul8c41a142005-11-19 15:36:28 +000038#include "context.h"
Brian Paul6211a142006-08-24 23:37:13 +000039#include "macros.h"
Brian Paul8c41a142005-11-19 15:36:28 +000040#include "mtypes.h"
41#include "program_instruction.h"
42
43
Brian Paul6211a142006-08-24 23:37:13 +000044/* For ARB programs, use the NV instruction limits */
45#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
46 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000047
48
49/**
50 * This is basically a union of the vertex_program and fragment_program
51 * structs that we can use to parse the program into
52 *
53 * XXX we can probably get rid of this entirely someday.
54 */
55struct arb_program
56{
Brian Paul122629f2006-07-20 16:49:57 +000057 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000058
59 GLuint Position; /* Just used for error reporting while parsing */
60 GLuint MajorVersion;
61 GLuint MinorVersion;
62
63 /* ARB_vertex_progmra options */
64 GLboolean HintPositionInvariant;
65
66 /* ARB_fragment_progmra options */
67 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
68 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
69
70 /* ARB_fragment_program specifics */
71 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
72 GLuint NumAluInstructions;
73 GLuint NumTexInstructions;
74 GLuint NumTexIndirections;
75
76 GLboolean UsesKill;
77};
78
Ian Romanick9bdfee32005-07-18 12:31:24 +000079
Alan Hourihane38b317d2004-12-14 09:11:52 +000080#ifndef __extension__
81#if !defined(__GNUC__) || (__GNUC__ < 2) || \
82 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
Brian Paula6c423d2004-08-25 15:59:48 +000083# define __extension__
84#endif
Alan Hourihane38b317d2004-12-14 09:11:52 +000085#endif
Brian Paula6c423d2004-08-25 15:59:48 +000086
Jouk Jansen40322e12004-04-05 08:50:36 +000087/* TODO:
88 * Fragment Program Stuff:
89 * -----------------------------------------------------
90 *
91 * - things from Michal's email
92 * + overflow on atoi
93 * + not-overflowing floats (don't use parse_integer..)
94 * + can remove range checking in arbparse.c
95 *
96 * - check all limits of number of various variables
97 * + parameters
98 *
99 * - test! test! test!
100 *
101 * Vertex Program Stuff:
102 * -----------------------------------------------------
103 * - Optimize param array usage and count limits correctly, see spec,
104 * section 2.14.3.7
105 * + Record if an array is reference absolutly or relatively (or both)
106 * + For absolute arrays, store a bitmap of accesses
107 * + For single parameters, store an access flag
108 * + After parsing, make a parameter cleanup and merging pass, where
109 * relative arrays are layed out first, followed by abs arrays, and
110 * finally single state.
111 * + Remap offsets for param src and dst registers
112 * + Now we can properly count parameter usage
113 *
114 * - Multiple state binding errors in param arrays (see spec, just before
115 * section 2.14.3.3)
116 * - grep for XXX
117 *
118 * Mesa Stuff
119 * -----------------------------------------------------
120 * - User clipping planes vs. PositionInvariant
121 * - Is it sufficient to just multiply by the mvp to transform in the
122 * PositionInvariant case? Or do we need something more involved?
123 *
124 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
125 * - fetch state listed in program_parameters list
126 * + WTF should this go???
127 * + currently in nvvertexec.c and s_nvfragprog.c
128 *
129 * - allow for multiple address registers (and fetch address regs properly)
130 *
131 * Cosmetic Stuff
132 * -----------------------------------------------------
133 * - remove any leftover unused grammer.c stuff (dict_ ?)
134 * - fix grammer.c error handling so its not static
135 * - #ifdef around stuff pertaining to extentions
136 *
137 * Outstanding Questions:
138 * -----------------------------------------------------
139 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
140 * what gets hacked off because of this:
141 * + VERTEX_ATTRIB_MATRIXINDEX
142 * + VERTEX_ATTRIB_WEIGHT
143 * + MATRIX_MODELVIEW
144 * + MATRIX_PALETTE
145 *
146 * - When can we fetch env/local params from their own register files, and
147 * when to we have to fetch them into the main state register file?
148 * (think arrays)
149 *
150 * Grammar Changes:
151 * -----------------------------------------------------
152 */
153
154/* Changes since moving the file to shader directory
155
1562004-III-4 ------------------------------------------------------------
157- added #include "grammar_mesa.h"
158- removed grammar specific code part (it resides now in grammar.c)
159- added GL_ARB_fragment_program_shadow tokens
160- modified #include "arbparse_syn.h"
161- major changes inside _mesa_parse_arb_program()
162- check the program string for '\0' characters
163- copy the program string to a one-byte-longer location to have
164 it null-terminated
165- position invariance test (not writing to result.position) moved
166 to syntax part
167*/
168
169typedef GLubyte *production;
170
171/**
172 * This is the text describing the rules to parse the grammar
173 */
Brian Paula6c423d2004-08-25 15:59:48 +0000174__extension__ static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000175#include "arbprogram_syn.h"
176;
177
178/**
179 * These should match up with the values defined in arbprogram.syn
180 */
181
182/*
183 Changes:
184 - changed and merged V_* and F_* opcode values to OP_*.
185 - added GL_ARB_fragment_program_shadow specific tokens (michal)
186*/
Michal Krolb80bc052004-10-21 14:09:54 +0000187#define REVISION 0x09
Jouk Jansen40322e12004-04-05 08:50:36 +0000188
189/* program type */
190#define FRAGMENT_PROGRAM 0x01
191#define VERTEX_PROGRAM 0x02
192
193/* program section */
194#define OPTION 0x01
195#define INSTRUCTION 0x02
196#define DECLARATION 0x03
197#define END 0x04
198
Michal Krolb80bc052004-10-21 14:09:54 +0000199/* GL_ARB_fragment_program option */
200#define ARB_PRECISION_HINT_FASTEST 0x00
201#define ARB_PRECISION_HINT_NICEST 0x01
202#define ARB_FOG_EXP 0x02
203#define ARB_FOG_EXP2 0x03
204#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000205
Michal Krolb80bc052004-10-21 14:09:54 +0000206/* GL_ARB_vertex_program option */
207#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000208
Michal Krolb80bc052004-10-21 14:09:54 +0000209/* GL_ARB_fragment_program_shadow option */
210#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000211
Michal Krolb80bc052004-10-21 14:09:54 +0000212/* GL_ARB_draw_buffers option */
213#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000214
Jouk Jansen40322e12004-04-05 08:50:36 +0000215/* GL_ARB_fragment_program instruction class */
216#define OP_ALU_INST 0x00
217#define OP_TEX_INST 0x01
218
219/* GL_ARB_vertex_program instruction class */
220/* OP_ALU_INST */
221
222/* GL_ARB_fragment_program instruction type */
223#define OP_ALU_VECTOR 0x00
224#define OP_ALU_SCALAR 0x01
225#define OP_ALU_BINSC 0x02
226#define OP_ALU_BIN 0x03
227#define OP_ALU_TRI 0x04
228#define OP_ALU_SWZ 0x05
229#define OP_TEX_SAMPLE 0x06
230#define OP_TEX_KIL 0x07
231
232/* GL_ARB_vertex_program instruction type */
233#define OP_ALU_ARL 0x08
234/* OP_ALU_VECTOR */
235/* OP_ALU_SCALAR */
236/* OP_ALU_BINSC */
237/* OP_ALU_BIN */
238/* OP_ALU_TRI */
239/* OP_ALU_SWZ */
240
241/* GL_ARB_fragment_program instruction code */
242#define OP_ABS 0x00
243#define OP_ABS_SAT 0x1B
244#define OP_FLR 0x09
245#define OP_FLR_SAT 0x26
246#define OP_FRC 0x0A
247#define OP_FRC_SAT 0x27
248#define OP_LIT 0x0C
249#define OP_LIT_SAT 0x2A
250#define OP_MOV 0x11
251#define OP_MOV_SAT 0x30
252#define OP_COS 0x1F
253#define OP_COS_SAT 0x20
254#define OP_EX2 0x07
255#define OP_EX2_SAT 0x25
256#define OP_LG2 0x0B
257#define OP_LG2_SAT 0x29
258#define OP_RCP 0x14
259#define OP_RCP_SAT 0x33
260#define OP_RSQ 0x15
261#define OP_RSQ_SAT 0x34
262#define OP_SIN 0x38
263#define OP_SIN_SAT 0x39
264#define OP_SCS 0x35
265#define OP_SCS_SAT 0x36
266#define OP_POW 0x13
267#define OP_POW_SAT 0x32
268#define OP_ADD 0x01
269#define OP_ADD_SAT 0x1C
270#define OP_DP3 0x03
271#define OP_DP3_SAT 0x21
272#define OP_DP4 0x04
273#define OP_DP4_SAT 0x22
274#define OP_DPH 0x05
275#define OP_DPH_SAT 0x23
276#define OP_DST 0x06
277#define OP_DST_SAT 0x24
278#define OP_MAX 0x0F
279#define OP_MAX_SAT 0x2E
280#define OP_MIN 0x10
281#define OP_MIN_SAT 0x2F
282#define OP_MUL 0x12
283#define OP_MUL_SAT 0x31
284#define OP_SGE 0x16
285#define OP_SGE_SAT 0x37
286#define OP_SLT 0x17
287#define OP_SLT_SAT 0x3A
288#define OP_SUB 0x18
289#define OP_SUB_SAT 0x3B
290#define OP_XPD 0x1A
291#define OP_XPD_SAT 0x43
292#define OP_CMP 0x1D
293#define OP_CMP_SAT 0x1E
294#define OP_LRP 0x2B
295#define OP_LRP_SAT 0x2C
296#define OP_MAD 0x0E
297#define OP_MAD_SAT 0x2D
298#define OP_SWZ 0x19
299#define OP_SWZ_SAT 0x3C
300#define OP_TEX 0x3D
301#define OP_TEX_SAT 0x3E
302#define OP_TXB 0x3F
303#define OP_TXB_SAT 0x40
304#define OP_TXP 0x41
305#define OP_TXP_SAT 0x42
306#define OP_KIL 0x28
307
308/* GL_ARB_vertex_program instruction code */
309#define OP_ARL 0x02
310/* OP_ABS */
311/* OP_FLR */
312/* OP_FRC */
313/* OP_LIT */
314/* OP_MOV */
315/* OP_EX2 */
316#define OP_EXP 0x08
317/* OP_LG2 */
318#define OP_LOG 0x0D
319/* OP_RCP */
320/* OP_RSQ */
321/* OP_POW */
322/* OP_ADD */
323/* OP_DP3 */
324/* OP_DP4 */
325/* OP_DPH */
326/* OP_DST */
327/* OP_MAX */
328/* OP_MIN */
329/* OP_MUL */
330/* OP_SGE */
331/* OP_SLT */
332/* OP_SUB */
333/* OP_XPD */
334/* OP_MAD */
335/* OP_SWZ */
336
337/* fragment attribute binding */
338#define FRAGMENT_ATTRIB_COLOR 0x01
339#define FRAGMENT_ATTRIB_TEXCOORD 0x02
340#define FRAGMENT_ATTRIB_FOGCOORD 0x03
341#define FRAGMENT_ATTRIB_POSITION 0x04
342
343/* vertex attribute binding */
344#define VERTEX_ATTRIB_POSITION 0x01
345#define VERTEX_ATTRIB_WEIGHT 0x02
346#define VERTEX_ATTRIB_NORMAL 0x03
347#define VERTEX_ATTRIB_COLOR 0x04
348#define VERTEX_ATTRIB_FOGCOORD 0x05
349#define VERTEX_ATTRIB_TEXCOORD 0x06
350#define VERTEX_ATTRIB_MATRIXINDEX 0x07
351#define VERTEX_ATTRIB_GENERIC 0x08
352
353/* fragment result binding */
354#define FRAGMENT_RESULT_COLOR 0x01
355#define FRAGMENT_RESULT_DEPTH 0x02
356
357/* vertex result binding */
358#define VERTEX_RESULT_POSITION 0x01
359#define VERTEX_RESULT_COLOR 0x02
360#define VERTEX_RESULT_FOGCOORD 0x03
361#define VERTEX_RESULT_POINTSIZE 0x04
362#define VERTEX_RESULT_TEXCOORD 0x05
363
364/* texture target */
365#define TEXTARGET_1D 0x01
366#define TEXTARGET_2D 0x02
367#define TEXTARGET_3D 0x03
368#define TEXTARGET_RECT 0x04
369#define TEXTARGET_CUBE 0x05
370/* GL_ARB_fragment_program_shadow */
371#define TEXTARGET_SHADOW1D 0x06
372#define TEXTARGET_SHADOW2D 0x07
373#define TEXTARGET_SHADOWRECT 0x08
374
375/* face type */
376#define FACE_FRONT 0x00
377#define FACE_BACK 0x01
378
379/* color type */
380#define COLOR_PRIMARY 0x00
381#define COLOR_SECONDARY 0x01
382
383/* component */
384#define COMPONENT_X 0x00
385#define COMPONENT_Y 0x01
386#define COMPONENT_Z 0x02
387#define COMPONENT_W 0x03
388#define COMPONENT_0 0x04
389#define COMPONENT_1 0x05
390
391/* array index type */
392#define ARRAY_INDEX_ABSOLUTE 0x00
393#define ARRAY_INDEX_RELATIVE 0x01
394
395/* matrix name */
396#define MATRIX_MODELVIEW 0x01
397#define MATRIX_PROJECTION 0x02
398#define MATRIX_MVP 0x03
399#define MATRIX_TEXTURE 0x04
400#define MATRIX_PALETTE 0x05
401#define MATRIX_PROGRAM 0x06
402
403/* matrix modifier */
404#define MATRIX_MODIFIER_IDENTITY 0x00
405#define MATRIX_MODIFIER_INVERSE 0x01
406#define MATRIX_MODIFIER_TRANSPOSE 0x02
407#define MATRIX_MODIFIER_INVTRANS 0x03
408
409/* constant type */
410#define CONSTANT_SCALAR 0x01
411#define CONSTANT_VECTOR 0x02
412
413/* program param type */
414#define PROGRAM_PARAM_ENV 0x01
415#define PROGRAM_PARAM_LOCAL 0x02
416
417/* register type */
418#define REGISTER_ATTRIB 0x01
419#define REGISTER_PARAM 0x02
420#define REGISTER_RESULT 0x03
421#define REGISTER_ESTABLISHED_NAME 0x04
422
423/* param binding */
424#define PARAM_NULL 0x00
425#define PARAM_ARRAY_ELEMENT 0x01
426#define PARAM_STATE_ELEMENT 0x02
427#define PARAM_PROGRAM_ELEMENT 0x03
428#define PARAM_PROGRAM_ELEMENTS 0x04
429#define PARAM_CONSTANT 0x05
430
431/* param state property */
432#define STATE_MATERIAL_PARSER 0x01
433#define STATE_LIGHT_PARSER 0x02
434#define STATE_LIGHT_MODEL 0x03
435#define STATE_LIGHT_PROD 0x04
436#define STATE_FOG 0x05
437#define STATE_MATRIX_ROWS 0x06
438/* GL_ARB_fragment_program */
439#define STATE_TEX_ENV 0x07
440#define STATE_DEPTH 0x08
441/* GL_ARB_vertex_program */
442#define STATE_TEX_GEN 0x09
443#define STATE_CLIP_PLANE 0x0A
444#define STATE_POINT 0x0B
445
446/* state material property */
447#define MATERIAL_AMBIENT 0x01
448#define MATERIAL_DIFFUSE 0x02
449#define MATERIAL_SPECULAR 0x03
450#define MATERIAL_EMISSION 0x04
451#define MATERIAL_SHININESS 0x05
452
453/* state light property */
454#define LIGHT_AMBIENT 0x01
455#define LIGHT_DIFFUSE 0x02
456#define LIGHT_SPECULAR 0x03
457#define LIGHT_POSITION 0x04
458#define LIGHT_ATTENUATION 0x05
459#define LIGHT_HALF 0x06
460#define LIGHT_SPOT_DIRECTION 0x07
461
462/* state light model property */
463#define LIGHT_MODEL_AMBIENT 0x01
464#define LIGHT_MODEL_SCENECOLOR 0x02
465
466/* state light product property */
467#define LIGHT_PROD_AMBIENT 0x01
468#define LIGHT_PROD_DIFFUSE 0x02
469#define LIGHT_PROD_SPECULAR 0x03
470
471/* state texture environment property */
472#define TEX_ENV_COLOR 0x01
473
474/* state texture generation coord property */
475#define TEX_GEN_EYE 0x01
476#define TEX_GEN_OBJECT 0x02
477
478/* state fog property */
479#define FOG_COLOR 0x01
480#define FOG_PARAMS 0x02
481
482/* state depth property */
483#define DEPTH_RANGE 0x01
484
485/* state point parameters property */
486#define POINT_SIZE 0x01
487#define POINT_ATTENUATION 0x02
488
489/* declaration */
490#define ATTRIB 0x01
491#define PARAM 0x02
492#define TEMP 0x03
493#define OUTPUT 0x04
494#define ALIAS 0x05
495/* GL_ARB_vertex_program */
496#define ADDRESS 0x06
497
498/*-----------------------------------------------------------------------
499 * From here on down is the semantic checking portion
500 *
501 */
502
503/**
504 * Variable Table Handling functions
505 */
506typedef enum
507{
508 vt_none,
509 vt_address,
510 vt_attrib,
511 vt_param,
512 vt_temp,
513 vt_output,
514 vt_alias
515} var_type;
516
517
Brian Paul7aebaf32005-10-30 21:23:23 +0000518/**
519 * Setting an explicit field for each of the binding properties is a bit
520 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000521 */
522struct var_cache
523{
Brian Paul6a769d92006-04-28 15:42:15 +0000524 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000525 var_type type;
526 GLuint address_binding; /* The index of the address register we should
527 * be using */
528 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000529 GLuint attrib_is_generic; /* If the attrib was specified through a generic
530 * vertex attrib */
531 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000532 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000533 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
534 * that this is aliased to */
535 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
536 * PROGRAM_ENV_PARAM} */
537 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
538 * the tokens representing our bound state (or constants)
539 * start */
540 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
541 * we take up with our state tokens or constants. Note that
542 * this is _not_ the same as the number of param registers
543 * we eventually use */
544 struct var_cache *next;
545};
546
547static GLvoid
548var_cache_create (struct var_cache **va)
549{
550 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
551 if (*va) {
552 (**va).name = NULL;
553 (**va).type = vt_none;
554 (**va).attrib_binding = ~0;
555 (**va).attrib_is_generic = 0;
556 (**va).temp_binding = ~0;
557 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000558 (**va).param_binding_type = ~0;
559 (**va).param_binding_begin = ~0;
560 (**va).param_binding_length = ~0;
561 (**va).alias_binding = NULL;
562 (**va).next = NULL;
563 }
564}
565
566static GLvoid
567var_cache_destroy (struct var_cache **va)
568{
569 if (*va) {
570 var_cache_destroy (&(**va).next);
571 _mesa_free (*va);
572 *va = NULL;
573 }
574}
575
576static GLvoid
577var_cache_append (struct var_cache **va, struct var_cache *nv)
578{
579 if (*va)
580 var_cache_append (&(**va).next, nv);
581 else
582 *va = nv;
583}
584
585static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000586var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000587{
Brian Paul0a360cf2005-01-17 01:21:03 +0000588 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000589
590 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000591 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000592 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000593 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000594 return va;
595 }
596
597 va = va->next;
598 }
599
600 return NULL;
601}
602
Brian Paula088f162006-09-05 23:08:51 +0000603
604
605/**
606 * Called when an error is detected while parsing/compiling a program.
607 * Sets the ctx->Program.ErrorString field to descript and records a
608 * GL_INVALID_OPERATION error.
609 * \param position position of error in program string
610 * \param descrip verbose error description
611 */
612static void
613program_error(GLcontext *ctx, GLint position, const char *descrip)
614{
615 if (descrip) {
616 const char *prefix = "glProgramString(", *suffix = ")";
617 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
618 _mesa_strlen(prefix) +
619 _mesa_strlen(suffix) + 1);
620 if (str) {
621 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
622 _mesa_error(ctx, GL_INVALID_OPERATION, str);
623 _mesa_free(str);
624 }
625 }
626 _mesa_set_program_error(ctx, position, descrip);
627}
628
629
630
Jouk Jansen40322e12004-04-05 08:50:36 +0000631/**
632 * constructs an integer from 4 GLubytes in LE format
633 */
634static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000635parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000636{
637 GLuint value;
638
639 value = (GLuint) (*(*inst)++);
640 value += (GLuint) (*(*inst)++) * 0x100;
641 value += (GLuint) (*(*inst)++) * 0x10000;
642 value += (GLuint) (*(*inst)++) * 0x1000000;
643
644 return value;
645}
646
647/**
648 * This will, given a string, lookup the string as a variable name in the
649 * var cache. If the name is found, the var cache node corresponding to the
650 * var name is returned. If it is not found, a new entry is allocated
651 *
652 * \param I Points into the binary array where the string identifier begins
653 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
654 * \return The location on the var_cache corresponding the the string starting at I
655 */
656static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000657parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000658 struct arb_program *Program, GLuint * found)
659{
Brian Paula088f162006-09-05 23:08:51 +0000660 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000661 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000662 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000663
664 *inst += _mesa_strlen ((char *) i) + 1;
665
666 va = var_cache_find (*vc_head, i);
667
668 if (va) {
669 *found = 1;
670 return va;
671 }
672
673 *found = 0;
674 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000675 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000676
677 var_cache_append (vc_head, va);
678
679 return va;
680}
681
682static char *
Brian Paula088f162006-09-05 23:08:51 +0000683parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000684{
Brian Paula088f162006-09-05 23:08:51 +0000685 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000686 (void) Program;
687
Jouk Jansen40322e12004-04-05 08:50:36 +0000688 *inst += _mesa_strlen ((char *) i) + 1;
689
690 return (char *) i;
691}
692
693/**
Brian Paul05908952004-06-08 15:20:23 +0000694 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000695 */
Brian Paul05908952004-06-08 15:20:23 +0000696static GLint
Brian Paula088f162006-09-05 23:08:51 +0000697parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000698{
699 /*return *(*inst)++ != '+'; */
700
701 if (**inst == '-') {
702 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000703 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000704 }
705 else if (**inst == '+') {
706 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000707 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000708 }
709
Brian Paul05908952004-06-08 15:20:23 +0000710 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000711}
712
713/**
714 * parses and returns signed integer
715 */
716static GLint
Brian Paula088f162006-09-05 23:08:51 +0000717parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000718{
719 GLint sign;
720 GLint value;
721
722 /* check if *inst points to '+' or '-'
723 * if yes, grab the sign and increment *inst
724 */
725 sign = parse_sign (inst);
726
727 /* now check if *inst points to 0
728 * if yes, increment the *inst and return the default value
729 */
730 if (**inst == 0) {
731 (*inst)++;
732 return 0;
733 }
734
735 /* parse the integer as you normally would do it */
736 value = _mesa_atoi (parse_string_without_adding (inst, Program));
737
738 /* now, after terminating 0 there is a position
739 * to parse it - parse_position()
740 */
741 Program->Position = parse_position (inst);
742
Brian Paul05908952004-06-08 15:20:23 +0000743 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000744}
745
746/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000747 Accumulate this string of digits, and return them as
748 a large integer represented in floating point (for range).
749 If scale is not NULL, also accumulates a power-of-ten
750 integer scale factor that represents the number of digits
751 in the string.
752*/
753static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000754parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000755{
756 GLdouble value = 0.0;
757 GLdouble oscale = 1.0;
758
759 if (**inst == 0) { /* this string of digits is empty-- do nothing */
760 (*inst)++;
761 }
762 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000763 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000764 GLubyte digit = *((*inst)++);
765 value = value * 10.0 + (GLint) (digit - '0');
766 oscale *= 10.0;
767 }
768 assert(**inst == 0); /* integer string should end with 0 */
769 (*inst)++; /* skip over terminating 0 */
770 Program->Position = parse_position(inst); /* skip position (from integer) */
771 }
772 if (scale)
773 *scale = oscale;
774 return value;
775}
776
777/**
778 Parse an unsigned floating-point number from this stream of tokenized
779 characters. Example floating-point formats supported:
780 12.34
781 12
782 0.34
783 .34
784 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000785 */
786static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000787parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000788{
Brian Paul1ff8f502005-02-16 15:08:29 +0000789 GLint exponent;
790 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000791
Brian Paul1ff8f502005-02-16 15:08:29 +0000792 whole = parse_float_string(inst, Program, 0);
793 fraction = parse_float_string(inst, Program, &fracScale);
794
795 /* Parse signed exponent */
796 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000797
Brian Paul1ff8f502005-02-16 15:08:29 +0000798 /* Assemble parts of floating-point number: */
799 return (GLfloat) ((whole + fraction / fracScale) *
800 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000801}
802
803
804/**
805 */
806static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000807parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000808{
Brian Paul05908952004-06-08 15:20:23 +0000809 GLint sign = parse_sign (inst);
810 GLfloat value = parse_float (inst, Program);
811 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000812}
813
814/**
815 * This picks out a constant value from the parsed array. The constant vector is r
816 * returned in the *values array, which should be of length 4.
817 *
818 * \param values - The 4 component vector with the constant value in it
819 */
820static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000821parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000822 GLboolean use)
823{
824 GLuint components, i;
825
826
827 switch (*(*inst)++) {
828 case CONSTANT_SCALAR:
829 if (use == GL_TRUE) {
830 values[0] =
831 values[1] =
832 values[2] = values[3] = parse_float (inst, Program);
833 }
834 else {
835 values[0] =
836 values[1] =
837 values[2] = values[3] = parse_signed_float (inst, Program);
838 }
839
840 break;
841 case CONSTANT_VECTOR:
842 values[0] = values[1] = values[2] = 0;
843 values[3] = 1;
844 components = *(*inst)++;
845 for (i = 0; i < components; i++) {
846 values[i] = parse_signed_float (inst, Program);
847 }
848 break;
849 }
850}
851
852/**
853 * \param offset The offset from the address register that we should
854 * address
855 *
856 * \return 0 on sucess, 1 on error
857 */
858static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000859parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
860 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000861{
Brian Paul6a769d92006-04-28 15:42:15 +0000862 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000863 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000864 return 0;
865}
866
867/**
868 * \param color 0 if color type is primary, 1 if color type is secondary
869 * \return 0 on sucess, 1 on error
870 */
871static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000872parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000873 GLint * color)
874{
Brian Paula6c423d2004-08-25 15:59:48 +0000875 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000876 *color = *(*inst)++ != COLOR_PRIMARY;
877 return 0;
878}
879
880/**
881 * Get an integer corresponding to a generic vertex attribute.
882 *
883 * \return 0 on sucess, 1 on error
884 */
885static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000886parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000887 struct arb_program *Program, GLuint *attrib)
888{
Brian Paulbd997cd2004-07-20 21:12:56 +0000889 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000890
Michal Krolbb38cad2006-04-11 11:41:11 +0000891 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000892 {
Brian Paula088f162006-09-05 23:08:51 +0000893 program_error(ctx, Program->Position,
894 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000895 return 1;
896 }
897
Brian Paulbd997cd2004-07-20 21:12:56 +0000898 *attrib = (GLuint) i;
899
Jouk Jansen40322e12004-04-05 08:50:36 +0000900 return 0;
901}
902
903
904/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000905 * \param color The index of the color buffer to write into
906 * \return 0 on sucess, 1 on error
907 */
908static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000909parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000910 struct arb_program *Program, GLuint * color)
911{
912 GLint i = parse_integer (inst, Program);
913
914 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000915 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000916 return 1;
917 }
918
919 *color = (GLuint) i;
920 return 0;
921}
922
923
924/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000925 * \param coord The texture unit index
926 * \return 0 on sucess, 1 on error
927 */
928static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000929parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000930 struct arb_program *Program, GLuint * coord)
931{
Brian Paulbd997cd2004-07-20 21:12:56 +0000932 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000933
Brian Paula6c423d2004-08-25 15:59:48 +0000934 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000935 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000936 return 1;
937 }
938
Brian Paulbd997cd2004-07-20 21:12:56 +0000939 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000940 return 0;
941}
942
943/**
944 * \param coord The weight index
945 * \return 0 on sucess, 1 on error
946 */
947static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000948parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000949 GLint * coord)
950{
951 *coord = parse_integer (inst, Program);
952
953 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000954 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000955 return 1;
956 }
957
958 return 0;
959}
960
961/**
962 * \param coord The clip plane index
963 * \return 0 on sucess, 1 on error
964 */
965static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000966parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000967 struct arb_program *Program, GLint * coord)
968{
969 *coord = parse_integer (inst, Program);
970
971 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +0000972 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000973 return 1;
974 }
975
976 return 0;
977}
978
979
980/**
981 * \return 0 on front face, 1 on back face
982 */
983static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000984parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000985{
986 switch (*(*inst)++) {
987 case FACE_FRONT:
988 return 0;
989
990 case FACE_BACK:
991 return 1;
992 }
993 return 0;
994}
995
996
997/**
998 * Given a matrix and a modifier token on the binary array, return tokens
999 * that _mesa_fetch_state() [program.c] can understand.
1000 *
1001 * \param matrix - the matrix we are talking about
1002 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1003 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1004 * \return 0 on sucess, 1 on failure
1005 */
1006static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001007parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001008 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1009{
1010 GLubyte mat = *(*inst)++;
1011
1012 *matrix_idx = 0;
1013
1014 switch (mat) {
1015 case MATRIX_MODELVIEW:
1016 *matrix = STATE_MODELVIEW;
1017 *matrix_idx = parse_integer (inst, Program);
1018 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001019 program_error(ctx, Program->Position,
1020 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001021 return 1;
1022 }
1023 break;
1024
1025 case MATRIX_PROJECTION:
1026 *matrix = STATE_PROJECTION;
1027 break;
1028
1029 case MATRIX_MVP:
1030 *matrix = STATE_MVP;
1031 break;
1032
1033 case MATRIX_TEXTURE:
1034 *matrix = STATE_TEXTURE;
1035 *matrix_idx = parse_integer (inst, Program);
1036 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001037 program_error(ctx, Program->Position, "Invalid Texture Unit");
1038 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001039 return 1;
1040 }
1041 break;
1042
1043 /* This is not currently supported (ARB_matrix_palette) */
1044 case MATRIX_PALETTE:
1045 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001046 program_error(ctx, Program->Position,
1047 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001048 return 1;
1049 break;
1050
1051 case MATRIX_PROGRAM:
1052 *matrix = STATE_PROGRAM;
1053 *matrix_idx = parse_integer (inst, Program);
1054 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001055 program_error(ctx, Program->Position, "Invalid Program Matrix");
1056 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001057 return 1;
1058 }
1059 break;
1060 }
1061
1062 switch (*(*inst)++) {
1063 case MATRIX_MODIFIER_IDENTITY:
1064 *matrix_modifier = 0;
1065 break;
1066 case MATRIX_MODIFIER_INVERSE:
1067 *matrix_modifier = STATE_MATRIX_INVERSE;
1068 break;
1069 case MATRIX_MODIFIER_TRANSPOSE:
1070 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1071 break;
1072 case MATRIX_MODIFIER_INVTRANS:
1073 *matrix_modifier = STATE_MATRIX_INVTRANS;
1074 break;
1075 }
1076
1077 return 0;
1078}
1079
1080
1081/**
1082 * This parses a state string (rather, the binary version of it) into
1083 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1084 *
1085 * \param inst - the start in the binary arry to start working from
1086 * \param state_tokens - the storage for the 6-token state description
1087 * \return - 0 on sucess, 1 on error
1088 */
1089static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001090parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001091 struct arb_program *Program, GLint * state_tokens)
1092{
1093 switch (*(*inst)++) {
1094 case STATE_MATERIAL_PARSER:
1095 state_tokens[0] = STATE_MATERIAL;
1096 state_tokens[1] = parse_face_type (inst);
1097 switch (*(*inst)++) {
1098 case MATERIAL_AMBIENT:
1099 state_tokens[2] = STATE_AMBIENT;
1100 break;
1101 case MATERIAL_DIFFUSE:
1102 state_tokens[2] = STATE_DIFFUSE;
1103 break;
1104 case MATERIAL_SPECULAR:
1105 state_tokens[2] = STATE_SPECULAR;
1106 break;
1107 case MATERIAL_EMISSION:
1108 state_tokens[2] = STATE_EMISSION;
1109 break;
1110 case MATERIAL_SHININESS:
1111 state_tokens[2] = STATE_SHININESS;
1112 break;
1113 }
1114 break;
1115
1116 case STATE_LIGHT_PARSER:
1117 state_tokens[0] = STATE_LIGHT;
1118 state_tokens[1] = parse_integer (inst, Program);
1119
1120 /* Check the value of state_tokens[1] against the # of lights */
1121 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001122 program_error(ctx, Program->Position, "Invalid Light Number");
1123 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001124 return 1;
1125 }
1126
1127 switch (*(*inst)++) {
1128 case LIGHT_AMBIENT:
1129 state_tokens[2] = STATE_AMBIENT;
1130 break;
1131 case LIGHT_DIFFUSE:
1132 state_tokens[2] = STATE_DIFFUSE;
1133 break;
1134 case LIGHT_SPECULAR:
1135 state_tokens[2] = STATE_SPECULAR;
1136 break;
1137 case LIGHT_POSITION:
1138 state_tokens[2] = STATE_POSITION;
1139 break;
1140 case LIGHT_ATTENUATION:
1141 state_tokens[2] = STATE_ATTENUATION;
1142 break;
1143 case LIGHT_HALF:
1144 state_tokens[2] = STATE_HALF;
1145 break;
1146 case LIGHT_SPOT_DIRECTION:
1147 state_tokens[2] = STATE_SPOT_DIRECTION;
1148 break;
1149 }
1150 break;
1151
1152 case STATE_LIGHT_MODEL:
1153 switch (*(*inst)++) {
1154 case LIGHT_MODEL_AMBIENT:
1155 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1156 break;
1157 case LIGHT_MODEL_SCENECOLOR:
1158 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1159 state_tokens[1] = parse_face_type (inst);
1160 break;
1161 }
1162 break;
1163
1164 case STATE_LIGHT_PROD:
1165 state_tokens[0] = STATE_LIGHTPROD;
1166 state_tokens[1] = parse_integer (inst, Program);
1167
1168 /* Check the value of state_tokens[1] against the # of lights */
1169 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001170 program_error(ctx, Program->Position, "Invalid Light Number");
1171 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001172 return 1;
1173 }
1174
1175 state_tokens[2] = parse_face_type (inst);
1176 switch (*(*inst)++) {
1177 case LIGHT_PROD_AMBIENT:
1178 state_tokens[3] = STATE_AMBIENT;
1179 break;
1180 case LIGHT_PROD_DIFFUSE:
1181 state_tokens[3] = STATE_DIFFUSE;
1182 break;
1183 case LIGHT_PROD_SPECULAR:
1184 state_tokens[3] = STATE_SPECULAR;
1185 break;
1186 }
1187 break;
1188
1189
1190 case STATE_FOG:
1191 switch (*(*inst)++) {
1192 case FOG_COLOR:
1193 state_tokens[0] = STATE_FOG_COLOR;
1194 break;
1195 case FOG_PARAMS:
1196 state_tokens[0] = STATE_FOG_PARAMS;
1197 break;
1198 }
1199 break;
1200
1201 case STATE_TEX_ENV:
1202 state_tokens[1] = parse_integer (inst, Program);
1203 switch (*(*inst)++) {
1204 case TEX_ENV_COLOR:
1205 state_tokens[0] = STATE_TEXENV_COLOR;
1206 break;
1207 }
1208 break;
1209
1210 case STATE_TEX_GEN:
1211 {
1212 GLuint type, coord;
1213
1214 state_tokens[0] = STATE_TEXGEN;
1215 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1216
1217 if (parse_texcoord_num (ctx, inst, Program, &coord))
1218 return 1;
1219 state_tokens[1] = coord;
1220
1221 /* EYE or OBJECT */
1222 type = *(*inst++);
1223
1224 /* 0 - s, 1 - t, 2 - r, 3 - q */
1225 coord = *(*inst++);
1226
1227 if (type == TEX_GEN_EYE) {
1228 switch (coord) {
1229 case COMPONENT_X:
1230 state_tokens[2] = STATE_TEXGEN_EYE_S;
1231 break;
1232 case COMPONENT_Y:
1233 state_tokens[2] = STATE_TEXGEN_EYE_T;
1234 break;
1235 case COMPONENT_Z:
1236 state_tokens[2] = STATE_TEXGEN_EYE_R;
1237 break;
1238 case COMPONENT_W:
1239 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1240 break;
1241 }
1242 }
1243 else {
1244 switch (coord) {
1245 case COMPONENT_X:
1246 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1247 break;
1248 case COMPONENT_Y:
1249 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1250 break;
1251 case COMPONENT_Z:
1252 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1253 break;
1254 case COMPONENT_W:
1255 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1256 break;
1257 }
1258 }
1259 }
1260 break;
1261
1262 case STATE_DEPTH:
1263 switch (*(*inst)++) {
1264 case DEPTH_RANGE:
1265 state_tokens[0] = STATE_DEPTH_RANGE;
1266 break;
1267 }
1268 break;
1269
1270 case STATE_CLIP_PLANE:
1271 state_tokens[0] = STATE_CLIPPLANE;
1272 state_tokens[1] = parse_integer (inst, Program);
1273 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1274 return 1;
1275 break;
1276
1277 case STATE_POINT:
1278 switch (*(*inst++)) {
1279 case POINT_SIZE:
1280 state_tokens[0] = STATE_POINT_SIZE;
1281 break;
1282
1283 case POINT_ATTENUATION:
1284 state_tokens[0] = STATE_POINT_ATTENUATION;
1285 break;
1286 }
1287 break;
1288
1289 /* XXX: I think this is the correct format for a matrix row */
1290 case STATE_MATRIX_ROWS:
1291 state_tokens[0] = STATE_MATRIX;
1292 if (parse_matrix
1293 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1294 &state_tokens[5]))
1295 return 1;
1296
1297 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1298
1299 if ((**inst) != 0) { /* Either the last row, 0 */
1300 state_tokens[4] = parse_integer (inst, Program);
1301 if (state_tokens[4] < state_tokens[3]) {
Brian Paula088f162006-09-05 23:08:51 +00001302 program_error(ctx, Program->Position,
1303 "Second matrix index less than the first");
1304 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001305 return 1;
1306 }
1307 }
1308 else {
1309 state_tokens[4] = state_tokens[3];
1310 (*inst)++;
1311 }
1312 break;
1313 }
1314
1315 return 0;
1316}
1317
1318/**
1319 * This parses a state string (rather, the binary version of it) into
1320 * a 6-token similar for the state fetching code in program.c
1321 *
1322 * One might ask, why fetch these parameters into just like you fetch
1323 * state when they are already stored in other places?
1324 *
1325 * Because of array offsets -> We can stick env/local parameters in the
1326 * middle of a parameter array and then index someplace into the array
1327 * when we execute.
1328 *
1329 * One optimization might be to only do this for the cases where the
1330 * env/local parameters end up inside of an array, and leave the
1331 * single parameters (or arrays of pure env/local pareameters) in their
1332 * respective register files.
1333 *
1334 * For ENV parameters, the format is:
1335 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1336 * state_tokens[1] = STATE_ENV
1337 * state_tokens[2] = the parameter index
1338 *
1339 * for LOCAL parameters, the format is:
1340 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1341 * state_tokens[1] = STATE_LOCAL
1342 * state_tokens[2] = the parameter index
1343 *
1344 * \param inst - the start in the binary arry to start working from
1345 * \param state_tokens - the storage for the 6-token state description
1346 * \return - 0 on sucess, 1 on failure
1347 */
1348static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001349parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001350 struct arb_program *Program, GLint * state_tokens)
1351{
1352 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1353 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1354 else
1355 state_tokens[0] = STATE_VERTEX_PROGRAM;
1356
1357
1358 switch (*(*inst)++) {
1359 case PROGRAM_PARAM_ENV:
1360 state_tokens[1] = STATE_ENV;
1361 state_tokens[2] = parse_integer (inst, Program);
1362
1363 /* Check state_tokens[2] against the number of ENV parameters available */
1364 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001365 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001366 ||
1367 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001368 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001369 program_error(ctx, Program->Position,
1370 "Invalid Program Env Parameter");
1371 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001372 return 1;
1373 }
1374
1375 break;
1376
1377 case PROGRAM_PARAM_LOCAL:
1378 state_tokens[1] = STATE_LOCAL;
1379 state_tokens[2] = parse_integer (inst, Program);
1380
1381 /* Check state_tokens[2] against the number of LOCAL parameters available */
1382 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001383 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001384 ||
1385 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001386 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001387 program_error(ctx, Program->Position,
1388 "Invalid Program Local Parameter");
1389 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001390 return 1;
1391 }
1392 break;
1393 }
1394
1395 return 0;
1396}
1397
1398/**
1399 * For ARB_vertex_program, programs are not allowed to use both an explicit
1400 * vertex attribute and a generic vertex attribute corresponding to the same
1401 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1402 *
1403 * This will walk our var_cache and make sure that nobody does anything fishy.
1404 *
1405 * \return 0 on sucess, 1 on error
1406 */
1407static GLuint
1408generic_attrib_check(struct var_cache *vc_head)
1409{
1410 int a;
1411 struct var_cache *curr;
1412 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1413 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1414
1415 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1416 explicitAttrib[a] = GL_FALSE;
1417 genericAttrib[a] = GL_FALSE;
1418 }
1419
1420 curr = vc_head;
1421 while (curr) {
1422 if (curr->type == vt_attrib) {
1423 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001424 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001425 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001426 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001427 }
1428
1429 curr = curr->next;
1430 }
1431
1432 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1433 if ((explicitAttrib[a]) && (genericAttrib[a]))
1434 return 1;
1435 }
1436
1437 return 0;
1438}
1439
1440/**
1441 * This will handle the binding side of an ATTRIB var declaration
1442 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001443 * \param inputReg returns the input register index, one of the
1444 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001445 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001446 */
1447static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001448parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001449 struct arb_program *Program,
1450 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001451{
Jouk Jansen40322e12004-04-05 08:50:36 +00001452 GLint err = 0;
1453
1454 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001455
Jouk Jansen40322e12004-04-05 08:50:36 +00001456 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1457 switch (*(*inst)++) {
1458 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001459 {
1460 GLint coord;
1461 err = parse_color_type (ctx, inst, Program, &coord);
1462 *inputReg = FRAG_ATTRIB_COL0 + coord;
1463 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001464 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001465 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001466 {
1467 GLuint texcoord;
1468 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1469 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1470 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001471 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001473 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001475 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001476 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001477 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001478 default:
1479 err = 1;
1480 break;
1481 }
1482 }
1483 else {
1484 switch (*(*inst)++) {
1485 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001486 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001487 break;
1488
1489 case VERTEX_ATTRIB_WEIGHT:
1490 {
1491 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001492 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001493 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001494#if 1
1495 /* hack for Warcraft (see bug 8060) */
1496 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1497#else
Brian Paula088f162006-09-05 23:08:51 +00001498 program_error(ctx, Program->Position,
1499 "ARB_vertex_blend not supported");
Brian Paul3a557502006-09-05 23:15:29 +00001500#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001501 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001502 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001503
1504 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001505 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001506 break;
1507
1508 case VERTEX_ATTRIB_COLOR:
1509 {
1510 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001511 err = parse_color_type (ctx, inst, Program, &color);
1512 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001513 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001514 }
1515 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001516 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001517 }
1518 }
1519 break;
1520
1521 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001522 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001523 break;
1524
1525 case VERTEX_ATTRIB_TEXCOORD:
1526 {
1527 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001528 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001529 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001530 }
1531 break;
1532
Jouk Jansen40322e12004-04-05 08:50:36 +00001533 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001534 /* Not supported at this time */
1535 {
1536 const char *msg = "ARB_palette_matrix not supported";
1537 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001538 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001539 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001540 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001541
1542 case VERTEX_ATTRIB_GENERIC:
1543 {
1544 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001545 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001546 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001547 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001548 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1549 * attributes do not alias the conventional vertex
1550 * attributes.
1551 */
1552 if (attrib > 0)
1553 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001554 else
1555 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001556 }
1557 }
1558 break;
1559
1560 default:
1561 err = 1;
1562 break;
1563 }
1564 }
1565
Jouk Jansen40322e12004-04-05 08:50:36 +00001566 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001567 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001568 }
1569
Brian Paulde997602005-11-12 17:53:14 +00001570 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001571
1572 return err;
1573}
1574
Brian Paul7aebaf32005-10-30 21:23:23 +00001575
Jouk Jansen40322e12004-04-05 08:50:36 +00001576/**
1577 * This translates between a binary token for an output variable type
1578 * and the mesa token for the same thing.
1579 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001580 * \param inst The parsed tokens
1581 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001582 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001583 */
1584static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001585parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001586 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001587{
Brian Paul7aebaf32005-10-30 21:23:23 +00001588 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001589
Brian Paul7aebaf32005-10-30 21:23:23 +00001590 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001591 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001592 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001593 GLuint out_color;
1594
Brian Paulbe76b7f2004-10-04 14:40:05 +00001595 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001596 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001597 */
1598 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001599 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001600 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001601 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001602 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001603 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1604 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001605 }
1606 break;
1607
1608 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001609 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001610 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001611 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001612 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001613 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001614 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001615 GLint color_type;
1616 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001617 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1618 if (err)
1619 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001620
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001622 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001623 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001624 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001625 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001627 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 }
1629 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001630 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001631 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001632 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001633 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001634 }
1635 /* primary color */
1636 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001637 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001638 }
1639 }
1640 }
1641 break;
1642
1643 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001644 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001645 break;
1646
1647 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001648 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001649 break;
1650
1651 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001652 {
1653 GLuint unit;
1654 if (parse_texcoord_num (ctx, inst, Program, &unit))
1655 return 1;
1656 *outputReg = VERT_RESULT_TEX0 + unit;
1657 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001658 break;
1659 }
1660
Brian Paulde997602005-11-12 17:53:14 +00001661 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001662
1663 return 0;
1664}
1665
Brian Paul7aebaf32005-10-30 21:23:23 +00001666
Jouk Jansen40322e12004-04-05 08:50:36 +00001667/**
1668 * This handles the declaration of ATTRIB variables
1669 *
1670 * XXX: Still needs
1671 * parse_vert_attrib_binding(), or something like that
1672 *
1673 * \return 0 on sucess, 1 on error
1674 */
1675static GLint
Brian Paula088f162006-09-05 23:08:51 +00001676parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001677 struct arb_program *Program)
1678{
1679 GLuint found;
1680 char *error_msg;
1681 struct var_cache *attrib_var;
1682
1683 attrib_var = parse_string (inst, vc_head, Program, &found);
1684 Program->Position = parse_position (inst);
1685 if (found) {
1686 error_msg = (char *)
1687 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1688 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1689 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001690 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001691 _mesa_free (error_msg);
1692 return 1;
1693 }
1694
1695 attrib_var->type = vt_attrib;
1696
Brian Paul7aebaf32005-10-30 21:23:23 +00001697 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001698 &attrib_var->attrib_is_generic))
1699 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001700
Brian Paul7aebaf32005-10-30 21:23:23 +00001701 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001702 program_error(ctx, Program->Position,
1703 "Cannot use both a generic vertex attribute "
1704 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001705 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001706 }
1707
1708 Program->Base.NumAttributes++;
1709 return 0;
1710}
1711
1712/**
1713 * \param use -- TRUE if we're called when declaring implicit parameters,
1714 * FALSE if we're declaraing variables. This has to do with
1715 * if we get a signed or unsigned float for scalar constants
1716 */
1717static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001718parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001719 struct var_cache *param_var,
1720 struct arb_program *Program, GLboolean use)
1721{
1722 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001723 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001724 GLint state_tokens[6];
1725 GLfloat const_values[4];
1726
Jouk Jansen40322e12004-04-05 08:50:36 +00001727 switch (*(*inst)++) {
1728 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001729 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1730 return 1;
1731
1732 /* If we adding STATE_MATRIX that has multiple rows, we need to
1733 * unroll it and call _mesa_add_state_reference() for each row
1734 */
1735 if ((state_tokens[0] == STATE_MATRIX)
1736 && (state_tokens[3] != state_tokens[4])) {
1737 GLint row;
1738 GLint first_row = state_tokens[3];
1739 GLint last_row = state_tokens[4];
1740
1741 for (row = first_row; row <= last_row; row++) {
1742 state_tokens[3] = state_tokens[4] = row;
1743
Brian Paulde997602005-11-12 17:53:14 +00001744 idx = _mesa_add_state_reference(Program->Base.Parameters,
1745 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001746 if (param_var->param_binding_begin == ~0U)
1747 param_var->param_binding_begin = idx;
1748 param_var->param_binding_length++;
1749 Program->Base.NumParameters++;
1750 }
1751 }
1752 else {
Brian Paulde997602005-11-12 17:53:14 +00001753 idx = _mesa_add_state_reference(Program->Base.Parameters,
1754 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001755 if (param_var->param_binding_begin == ~0U)
1756 param_var->param_binding_begin = idx;
1757 param_var->param_binding_length++;
1758 Program->Base.NumParameters++;
1759 }
1760 break;
1761
1762 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001763 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1764 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001765 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001766 if (param_var->param_binding_begin == ~0U)
1767 param_var->param_binding_begin = idx;
1768 param_var->param_binding_length++;
1769 Program->Base.NumParameters++;
1770
1771 /* Check if there is more: 0 -> we're done, else its an integer */
1772 if (**inst) {
1773 GLuint out_of_range, new_idx;
1774 GLuint start_idx = state_tokens[2] + 1;
1775 GLuint end_idx = parse_integer (inst, Program);
1776
1777 out_of_range = 0;
1778 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1779 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001780 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001781 || ((state_tokens[1] == STATE_LOCAL)
1782 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001783 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001784 out_of_range = 1;
1785 }
1786 else {
1787 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001788 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001789 || ((state_tokens[1] == STATE_LOCAL)
1790 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001791 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001792 out_of_range = 1;
1793 }
1794 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001795 program_error(ctx, Program->Position,
1796 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001797 return 1;
1798 }
1799
1800 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1801 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001802 idx = _mesa_add_state_reference(Program->Base.Parameters,
1803 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001804 param_var->param_binding_length++;
1805 Program->Base.NumParameters++;
1806 }
1807 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001808 else {
1809 (*inst)++;
1810 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001811 break;
1812
1813 case PARAM_CONSTANT:
1814 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001815 idx = _mesa_add_named_constant(Program->Base.Parameters,
1816 (char *) param_var->name,
1817 const_values);
Jouk Jansen40322e12004-04-05 08:50:36 +00001818 if (param_var->param_binding_begin == ~0U)
1819 param_var->param_binding_begin = idx;
1820 param_var->param_binding_length++;
1821 Program->Base.NumParameters++;
1822 break;
1823
1824 default:
Brian Paula088f162006-09-05 23:08:51 +00001825 program_error(ctx, Program->Position,
1826 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001827 return 1;
1828 }
1829
1830 /* Make sure we haven't blown past our parameter limits */
1831 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1832 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001833 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001834 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1835 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001836 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001837 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001838 return 1;
1839 }
1840
1841 return err;
1842}
1843
Brian Paul7aebaf32005-10-30 21:23:23 +00001844
Jouk Jansen40322e12004-04-05 08:50:36 +00001845/**
1846 * This picks out PARAM program parameter bindings.
1847 *
1848 * XXX: This needs to be stressed & tested
1849 *
1850 * \return 0 on sucess, 1 on error
1851 */
1852static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001853parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001854 struct arb_program *Program)
1855{
Brian Paulbd997cd2004-07-20 21:12:56 +00001856 GLuint found, err;
1857 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001858 struct var_cache *param_var;
1859
1860 err = 0;
1861 param_var = parse_string (inst, vc_head, Program, &found);
1862 Program->Position = parse_position (inst);
1863
1864 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001865 char *error_msg = (char *)
1866 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001867 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1868 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001869 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001870 _mesa_free (error_msg);
1871 return 1;
1872 }
1873
1874 specified_length = parse_integer (inst, Program);
1875
1876 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001877 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001878 return 1;
1879 }
1880
1881 param_var->type = vt_param;
1882 param_var->param_binding_length = 0;
1883
1884 /* Right now, everything is shoved into the main state register file.
1885 *
1886 * In the future, it would be nice to leave things ENV/LOCAL params
1887 * in their respective register files, if possible
1888 */
1889 param_var->param_binding_type = PROGRAM_STATE_VAR;
1890
1891 /* Remember to:
1892 * * - add each guy to the parameter list
1893 * * - increment the param_var->param_binding_len
1894 * * - store the param_var->param_binding_begin for the first one
1895 * * - compare the actual len to the specified len at the end
1896 */
1897 while (**inst != PARAM_NULL) {
1898 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1899 return 1;
1900 }
1901
1902 /* Test array length here! */
1903 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001904 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001905 program_error(ctx, Program->Position,
1906 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001907 }
1908 }
1909
1910 (*inst)++;
1911
1912 return 0;
1913}
1914
1915/**
1916 *
1917 */
1918static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001919parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001920 struct arb_program *Program, struct var_cache **new_var)
1921{
1922 struct var_cache *param_var;
1923
1924 /* First, insert a dummy entry into the var_cache */
1925 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001926 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001927 param_var->type = vt_param;
1928
1929 param_var->param_binding_length = 0;
1930 /* Don't fill in binding_begin; We use the default value of -1
1931 * to tell if its already initialized, elsewhere.
1932 *
1933 * param_var->param_binding_begin = 0;
1934 */
1935 param_var->param_binding_type = PROGRAM_STATE_VAR;
1936
1937 var_cache_append (vc_head, param_var);
1938
1939 /* Then fill it with juicy parameter goodness */
1940 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1941 return 1;
1942
1943 *new_var = param_var;
1944
1945 return 0;
1946}
1947
1948
1949/**
1950 * This handles the declaration of TEMP variables
1951 *
1952 * \return 0 on sucess, 1 on error
1953 */
1954static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001955parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001956 struct arb_program *Program)
1957{
1958 GLuint found;
1959 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001960
1961 while (**inst != 0) {
1962 temp_var = parse_string (inst, vc_head, Program, &found);
1963 Program->Position = parse_position (inst);
1964 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001965 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001966 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1967 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1968 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001969 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001970 _mesa_free (error_msg);
1971 return 1;
1972 }
1973
1974 temp_var->type = vt_temp;
1975
1976 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1977 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001978 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001979 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1980 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001981 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00001982 program_error(ctx, Program->Position,
1983 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00001984 return 1;
1985 }
1986
1987 temp_var->temp_binding = Program->Base.NumTemporaries;
1988 Program->Base.NumTemporaries++;
1989 }
1990 (*inst)++;
1991
1992 return 0;
1993}
1994
1995/**
1996 * This handles variables of the OUTPUT variety
1997 *
1998 * \return 0 on sucess, 1 on error
1999 */
2000static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002001parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002002 struct arb_program *Program)
2003{
2004 GLuint found;
2005 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002006 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002007
2008 output_var = parse_string (inst, vc_head, Program, &found);
2009 Program->Position = parse_position (inst);
2010 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002011 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002012 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2013 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2014 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002015 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002016 _mesa_free (error_msg);
2017 return 1;
2018 }
2019
2020 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002021
2022 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2023 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002024}
2025
2026/**
2027 * This handles variables of the ALIAS kind
2028 *
2029 * \return 0 on sucess, 1 on error
2030 */
2031static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002032parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002033 struct arb_program *Program)
2034{
2035 GLuint found;
2036 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002037
2038 temp_var = parse_string (inst, vc_head, Program, &found);
2039 Program->Position = parse_position (inst);
2040
2041 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002042 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002043 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2044 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2045 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002046 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002047 _mesa_free (error_msg);
2048 return 1;
2049 }
2050
2051 temp_var->type = vt_alias;
2052 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2053 Program->Position = parse_position (inst);
2054
2055 if (!found)
2056 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002057 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002058 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2059 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2060 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002061 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002062 _mesa_free (error_msg);
2063 return 1;
2064 }
2065
2066 return 0;
2067}
2068
2069/**
2070 * This handles variables of the ADDRESS kind
2071 *
2072 * \return 0 on sucess, 1 on error
2073 */
2074static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002075parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002076 struct arb_program *Program)
2077{
2078 GLuint found;
2079 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002080
2081 while (**inst != 0) {
2082 temp_var = parse_string (inst, vc_head, Program, &found);
2083 Program->Position = parse_position (inst);
2084 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002085 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002086 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2087 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2088 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002089 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002090 _mesa_free (error_msg);
2091 return 1;
2092 }
2093
2094 temp_var->type = vt_address;
2095
2096 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002097 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002098 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002099 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002100 return 1;
2101 }
2102
2103 temp_var->address_binding = Program->Base.NumAddressRegs;
2104 Program->Base.NumAddressRegs++;
2105 }
2106 (*inst)++;
2107
2108 return 0;
2109}
2110
2111/**
2112 * Parse a program declaration
2113 *
2114 * \return 0 on sucess, 1 on error
2115 */
2116static GLint
Brian Paula088f162006-09-05 23:08:51 +00002117parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002118 struct arb_program *Program)
2119{
2120 GLint err = 0;
2121
2122 switch (*(*inst)++) {
2123 case ADDRESS:
2124 err = parse_address (ctx, inst, vc_head, Program);
2125 break;
2126
2127 case ALIAS:
2128 err = parse_alias (ctx, inst, vc_head, Program);
2129 break;
2130
2131 case ATTRIB:
2132 err = parse_attrib (ctx, inst, vc_head, Program);
2133 break;
2134
2135 case OUTPUT:
2136 err = parse_output (ctx, inst, vc_head, Program);
2137 break;
2138
2139 case PARAM:
2140 err = parse_param (ctx, inst, vc_head, Program);
2141 break;
2142
2143 case TEMP:
2144 err = parse_temp (ctx, inst, vc_head, Program);
2145 break;
2146 }
2147
2148 return err;
2149}
2150
2151/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002152 * Handle the parsing out of a masked destination register, either for a
2153 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002154 *
2155 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002156 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002157 * position invariant
2158 *
2159 * \param File - The register file we write to
2160 * \param Index - The register index we write to
2161 * \param WriteMask - The mask controlling which components we write (1->write)
2162 *
2163 * \return 0 on sucess, 1 on error
2164 */
2165static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002166parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002167 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002168 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002169{
Brian Paul7aebaf32005-10-30 21:23:23 +00002170 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002171 struct var_cache *dst;
2172
2173 /* We either have a result register specified, or a
2174 * variable that may or may not be writable
2175 */
2176 switch (*(*inst)++) {
2177 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002178 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002179 return 1;
2180 *File = PROGRAM_OUTPUT;
2181 break;
2182
2183 case REGISTER_ESTABLISHED_NAME:
2184 dst = parse_string (inst, vc_head, Program, &result);
2185 Program->Position = parse_position (inst);
2186
2187 /* If the name has never been added to our symbol table, we're hosed */
2188 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002189 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002190 return 1;
2191 }
2192
2193 switch (dst->type) {
2194 case vt_output:
2195 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002196 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002197 break;
2198
2199 case vt_temp:
2200 *File = PROGRAM_TEMPORARY;
2201 *Index = dst->temp_binding;
2202 break;
2203
2204 /* If the var type is not vt_output or vt_temp, no go */
2205 default:
Brian Paula088f162006-09-05 23:08:51 +00002206 program_error(ctx, Program->Position,
2207 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002208 return 1;
2209 }
2210 break;
2211
2212 default:
Brian Paula088f162006-09-05 23:08:51 +00002213 program_error(ctx, Program->Position,
2214 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002215 return 1;
2216 }
2217
2218
2219 /* Position invariance test */
2220 /* This test is done now in syntax portion - when position invariance OPTION
2221 is specified, "result.position" rule is disabled so there is no way
2222 to write the position
2223 */
2224 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2225 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002226 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002227 "Vertex program specified position invariance and wrote vertex position");
2228 }*/
2229
2230 /* And then the mask.
2231 * w,a -> bit 0
2232 * z,b -> bit 1
2233 * y,g -> bit 2
2234 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002235 *
2236 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002237 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002238 tmp = (GLint) *(*inst)++;
2239 *WriteMask = (((tmp>>3) & 0x1) |
2240 ((tmp>>1) & 0x2) |
2241 ((tmp<<1) & 0x4) |
2242 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002243
2244 return 0;
2245}
2246
2247
2248/**
2249 * Handle the parsing of a address register
2250 *
2251 * \param Index - The register index we write to
2252 *
2253 * \return 0 on sucess, 1 on error
2254 */
2255static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002256parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002257 struct var_cache **vc_head,
2258 struct arb_program *Program, GLint * Index)
2259{
2260 struct var_cache *dst;
2261 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002262
2263 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002264
2265 dst = parse_string (inst, vc_head, Program, &result);
2266 Program->Position = parse_position (inst);
2267
2268 /* If the name has never been added to our symbol table, we're hosed */
2269 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002270 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002271 return 1;
2272 }
2273
2274 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002275 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002276 return 1;
2277 }
2278
2279 return 0;
2280}
2281
Brian Paul8e8fa632005-07-01 02:03:33 +00002282#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002283/**
2284 * Handle the parsing out of a masked address register
2285 *
2286 * \param Index - The register index we write to
2287 * \param WriteMask - The mask controlling which components we write (1->write)
2288 *
2289 * \return 0 on sucess, 1 on error
2290 */
2291static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002292parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002293 struct var_cache **vc_head,
2294 struct arb_program *Program, GLint * Index,
2295 GLboolean * WriteMask)
2296{
2297 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2298 return 1;
2299
2300 /* This should be 0x8 */
2301 (*inst)++;
2302
2303 /* Writemask of .x is implied */
2304 WriteMask[0] = 1;
2305 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2306
2307 return 0;
2308}
Brian Paul8e8fa632005-07-01 02:03:33 +00002309#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002310
2311/**
2312 * Parse out a swizzle mask.
2313 *
Brian Paul32df89e2005-10-29 18:26:43 +00002314 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002315 *
2316 * The len parameter allows us to grab 4 components for a vector
2317 * swizzle, or just 1 component for a scalar src register selection
2318 */
Brian Paul32df89e2005-10-29 18:26:43 +00002319static void
Brian Paula088f162006-09-05 23:08:51 +00002320parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002321{
Brian Paul32df89e2005-10-29 18:26:43 +00002322 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002323
Brian Paul32df89e2005-10-29 18:26:43 +00002324 for (i = 0; i < 4; i++)
2325 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002326
Brian Paul32df89e2005-10-29 18:26:43 +00002327 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002328 switch (*(*inst)++) {
2329 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002330 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002331 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002332 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002333 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002334 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002335 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002336 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002337 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002338 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002339 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002340 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002341 default:
2342 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2343 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002344 }
2345 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002346}
2347
Jouk Jansen40322e12004-04-05 08:50:36 +00002348
Brian Paul32df89e2005-10-29 18:26:43 +00002349/**
2350 * Parse an extended swizzle mask which is a sequence of
2351 * four x/y/z/w/0/1 tokens.
2352 * \return swizzle four swizzle values
2353 * \return negateMask four element bitfield
2354 */
2355static void
Brian Paula088f162006-09-05 23:08:51 +00002356parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002357 GLubyte *negateMask)
2358{
2359 GLint i;
2360
2361 *negateMask = 0x0;
2362 for (i = 0; i < 4; i++) {
2363 GLubyte swz;
2364 if (parse_sign(inst) == -1)
2365 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002366
2367 swz = *(*inst)++;
2368
2369 switch (swz) {
2370 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002371 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002372 break;
2373 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002374 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002375 break;
2376 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002377 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002378 break;
2379 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002380 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002381 break;
2382 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002383 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002384 break;
2385 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002386 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002388 default:
2389 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2390 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002391 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002392 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002393}
2394
2395
2396static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002397parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2398 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002399 struct arb_program *Program,
2400 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002401 GLboolean *IsRelOffset )
2402{
2403 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002404 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002405 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002406
Keith Whitwell7c26b612005-04-21 14:46:57 +00002407 *IsRelOffset = 0;
2408
Jouk Jansen40322e12004-04-05 08:50:36 +00002409 /* And the binding for the src */
2410 switch (*(*inst)++) {
2411 case REGISTER_ATTRIB:
2412 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002413 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002414 return 1;
2415 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002416 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002417
2418 /* We need to insert a dummy variable into the var_cache so we can
2419 * catch generic vertex attrib aliasing errors
2420 */
2421 var_cache_create(&src);
2422 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002423 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002424 src->attrib_binding = binding;
2425 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002426 var_cache_append(vc_head, src);
2427 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002428 program_error(ctx, Program->Position,
2429 "Cannot use both a generic vertex attribute "
2430 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002431 return 1;
2432 }
2433 break;
2434
2435 case REGISTER_PARAM:
2436 switch (**inst) {
2437 case PARAM_ARRAY_ELEMENT:
2438 (*inst)++;
2439 src = parse_string (inst, vc_head, Program, &found);
2440 Program->Position = parse_position (inst);
2441
2442 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002443 program_error(ctx, Program->Position,
2444 "2: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002445 return 1;
2446 }
2447
Brian Paul95801792005-12-06 15:41:43 +00002448 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002449
2450 switch (*(*inst)++) {
2451 case ARRAY_INDEX_ABSOLUTE:
2452 offset = parse_integer (inst, Program);
2453
2454 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002455 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002456 program_error(ctx, Program->Position,
2457 "Index out of range");
2458 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002459 return 1;
2460 }
2461
2462 *Index = src->param_binding_begin + offset;
2463 break;
2464
2465 case ARRAY_INDEX_RELATIVE:
2466 {
2467 GLint addr_reg_idx, rel_off;
2468
2469 /* First, grab the address regiseter */
2470 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2471 return 1;
2472
2473 /* And the .x */
2474 ((*inst)++);
2475 ((*inst)++);
2476 ((*inst)++);
2477 ((*inst)++);
2478
2479 /* Then the relative offset */
2480 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2481
2482 /* And store it properly */
2483 *Index = src->param_binding_begin + rel_off;
2484 *IsRelOffset = 1;
2485 }
2486 break;
2487 }
2488 break;
2489
2490 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002491 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2492 return 1;
2493
Brian Paul95801792005-12-06 15:41:43 +00002494 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002495 *Index = src->param_binding_begin;
2496 break;
2497 }
2498 break;
2499
2500 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002501 src = parse_string (inst, vc_head, Program, &found);
2502 Program->Position = parse_position (inst);
2503
2504 /* If the name has never been added to our symbol table, we're hosed */
2505 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002506 program_error(ctx, Program->Position,
2507 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002508 return 1;
2509 }
2510
2511 switch (src->type) {
2512 case vt_attrib:
2513 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002514 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002515 break;
2516
2517 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2518 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002519 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002520 *Index = src->param_binding_begin;
2521 break;
2522
2523 case vt_temp:
2524 *File = PROGRAM_TEMPORARY;
2525 *Index = src->temp_binding;
2526 break;
2527
2528 /* If the var type is vt_output no go */
2529 default:
Brian Paula088f162006-09-05 23:08:51 +00002530 program_error(ctx, Program->Position,
2531 "destination register is read only");
2532 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002533 return 1;
2534 }
2535 break;
2536
2537 default:
Brian Paula088f162006-09-05 23:08:51 +00002538 program_error(ctx, Program->Position,
2539 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002540 return 1;
2541 }
2542
2543 return 0;
2544}
2545
2546/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002547 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002548 */
2549static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002550parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002551 struct var_cache **vc_head,
2552 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002553 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002554{
Brian Paul7aebaf32005-10-30 21:23:23 +00002555 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002556 GLint index;
2557 GLboolean negate;
2558 GLubyte swizzle[4];
2559 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002560
Jouk Jansen40322e12004-04-05 08:50:36 +00002561 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002562 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002563
2564 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002565 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002566 return 1;
2567
2568 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002569 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002570
Brian Paul32df89e2005-10-29 18:26:43 +00002571 reg->File = file;
2572 reg->Index = index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002573 reg->Abs = 0; /* NV only */
2574 reg->NegateAbs = 0; /* NV only */
Brian Paul32df89e2005-10-29 18:26:43 +00002575 reg->NegateBase = negate;
2576 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002577 return 0;
2578}
2579
Jouk Jansen40322e12004-04-05 08:50:36 +00002580
Brian Paulb7fc1c32006-08-30 23:38:03 +00002581/**
2582 * Parse fragment program destination register.
2583 * \return 1 if error, 0 if no error.
2584 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002585static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002586parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002587 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002588 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002589{
Brian Paul7aebaf32005-10-30 21:23:23 +00002590 GLint mask;
2591 GLuint idx;
2592 enum register_file file;
2593
Keith Whitwell7c26b612005-04-21 14:46:57 +00002594 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002595 return 1;
2596
Keith Whitwell7c26b612005-04-21 14:46:57 +00002597 reg->CondMask = 0; /* NV only */
2598 reg->CondSwizzle = 0; /* NV only */
2599 reg->File = file;
2600 reg->Index = idx;
2601 reg->WriteMask = mask;
2602 return 0;
2603}
2604
2605
Brian Paul7aebaf32005-10-30 21:23:23 +00002606/**
2607 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002608 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002609 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002610static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002611parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002612 struct var_cache **vc_head,
2613 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002614 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002615{
Brian Paul7aebaf32005-10-30 21:23:23 +00002616 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002617 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002618 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002619 GLubyte Swizzle[4];
2620 GLboolean IsRelOffset;
2621
2622 /* Grab the sign */
2623 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2624
2625 /* And the src reg */
2626 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2627 return 1;
2628
2629 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002630 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002631
Keith Whitwell7c26b612005-04-21 14:46:57 +00002632 reg->File = File;
2633 reg->Index = Index;
2634 reg->Abs = 0; /* NV only */
2635 reg->NegateAbs = 0; /* NV only */
2636 reg->NegateBase = Negate;
2637 reg->Swizzle = (Swizzle[0] << 0);
2638
Jouk Jansen40322e12004-04-05 08:50:36 +00002639 return 0;
2640}
2641
Keith Whitwell7c26b612005-04-21 14:46:57 +00002642
Jouk Jansen40322e12004-04-05 08:50:36 +00002643/**
2644 * This is a big mother that handles getting opcodes into the instruction
2645 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002646 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002647 */
2648static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002649parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002650 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002651 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002652{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002653 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002654 GLuint texcoord;
2655 GLubyte instClass, type, code;
2656 GLboolean rel;
2657
Brian Paul7e807512005-11-05 17:10:45 +00002658 _mesa_init_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002659
2660 /* Record the position in the program string for debugging */
2661 fp->StringPos = Program->Position;
2662
2663 /* OP_ALU_INST or OP_TEX_INST */
2664 instClass = *(*inst)++;
2665
2666 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2667 * OP_TEX_{SAMPLE, KIL}
2668 */
2669 type = *(*inst)++;
2670
2671 /* The actual opcode name */
2672 code = *(*inst)++;
2673
2674 /* Increment the correct count */
2675 switch (instClass) {
2676 case OP_ALU_INST:
2677 Program->NumAluInstructions++;
2678 break;
2679 case OP_TEX_INST:
2680 Program->NumTexInstructions++;
2681 break;
2682 }
2683
Jouk Jansen40322e12004-04-05 08:50:36 +00002684 switch (type) {
2685 case OP_ALU_VECTOR:
2686 switch (code) {
2687 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002688 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002689 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002690 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002691 break;
2692
2693 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002694 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002695 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002696 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002697 break;
2698
2699 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002700 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002701 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002702 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002703 break;
2704
2705 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002706 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002707 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002708 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002709 break;
2710
2711 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002712 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002713 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002714 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002715 break;
2716 }
2717
Keith Whitwell7c26b612005-04-21 14:46:57 +00002718 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002719 return 1;
2720
Keith Whitwell7c26b612005-04-21 14:46:57 +00002721 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002722 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002723 break;
2724
2725 case OP_ALU_SCALAR:
2726 switch (code) {
2727 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002728 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002729 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002730 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002731 break;
2732
2733 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002734 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002735 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002736 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002737 break;
2738
2739 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002740 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002741 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002742 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002743 break;
2744
2745 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002746 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002748 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002749 break;
2750
2751 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002752 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002754 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002755 break;
2756
2757 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002758 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002759 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002760 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002761 break;
2762
2763 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002764 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002765 case OP_SCS:
2766
Brian Paul7e807512005-11-05 17:10:45 +00002767 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002768 break;
2769 }
2770
Keith Whitwell7c26b612005-04-21 14:46:57 +00002771 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002772 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002773
2774 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002776 break;
2777
2778 case OP_ALU_BINSC:
2779 switch (code) {
2780 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002781 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002783 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002784 break;
2785 }
2786
Keith Whitwell7c26b612005-04-21 14:46:57 +00002787 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002788 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002789
Jouk Jansen40322e12004-04-05 08:50:36 +00002790 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002791 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002792 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 }
2794 break;
2795
2796
2797 case OP_ALU_BIN:
2798 switch (code) {
2799 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002800 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002801 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002802 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002803 break;
2804
2805 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002806 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002808 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002809 break;
2810
2811 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002812 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002814 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002815 break;
2816
2817 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002818 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002820 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002821 break;
2822
2823 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002824 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002826 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002827 break;
2828
2829 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002830 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002832 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002833 break;
2834
2835 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002836 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002837 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002838 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002839 break;
2840
2841 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002842 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002843 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002844 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 break;
2846
2847 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002848 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002850 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002851 break;
2852
2853 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002854 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002856 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002857 break;
2858
2859 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002860 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002861 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002862 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 break;
2864
2865 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002866 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002867 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002868 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002869 break;
2870 }
2871
Keith Whitwell7c26b612005-04-21 14:46:57 +00002872 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002873 return 1;
2874 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002875 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2876 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002877 }
2878 break;
2879
2880 case OP_ALU_TRI:
2881 switch (code) {
2882 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002883 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002884 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002885 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002886 break;
2887
2888 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002889 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002890 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002891 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002892 break;
2893
2894 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002895 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002896 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002897 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002898 break;
2899 }
2900
Keith Whitwell7c26b612005-04-21 14:46:57 +00002901 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002902 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002903
Jouk Jansen40322e12004-04-05 08:50:36 +00002904 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002905 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2906 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002907 }
2908 break;
2909
2910 case OP_ALU_SWZ:
2911 switch (code) {
2912 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002913 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002915 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002916 break;
2917 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002918 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002919 return 1;
2920
Keith Whitwell7c26b612005-04-21 14:46:57 +00002921 {
Brian Paul32df89e2005-10-29 18:26:43 +00002922 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002923 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002924 enum register_file file;
2925 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002926
Brian Paul32df89e2005-10-29 18:26:43 +00002927 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002928 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002929 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2930 fp->SrcReg[0].File = file;
2931 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002932 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002933 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2934 swizzle[1],
2935 swizzle[2],
2936 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002937 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002938 break;
2939
2940 case OP_TEX_SAMPLE:
2941 switch (code) {
2942 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002943 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002944 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002945 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002946 break;
2947
2948 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002949 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002950 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002951 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002952 break;
2953
2954 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002955 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002956 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002957 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002958 break;
2959 }
2960
Keith Whitwell7c26b612005-04-21 14:46:57 +00002961 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002962 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002963
2964 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002965 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002966
2967 /* texImageUnit */
2968 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2969 return 1;
2970 fp->TexSrcUnit = texcoord;
2971
2972 /* texTarget */
2973 switch (*(*inst)++) {
2974 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00002975 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002976 break;
2977 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00002978 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002979 break;
2980 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00002981 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002982 break;
2983 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00002984 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002985 break;
2986 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00002987 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002988 break;
2989 case TEXTARGET_SHADOW1D:
2990 case TEXTARGET_SHADOW2D:
2991 case TEXTARGET_SHADOWRECT:
2992 /* TODO ARB_fragment_program_shadow code */
2993 break;
2994 }
Brian Paulb7fc1c32006-08-30 23:38:03 +00002995 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
2996 /* Check that both "2D" and "CUBE" (for example) aren't both used */
2997 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00002998 program_error(ctx, Program->Position,
2999 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003000 return 1;
3001 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003002 break;
3003
3004 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003005 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003006 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003007 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003008 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003010 default:
3011 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3012 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003013 }
3014
3015 return 0;
3016}
3017
Keith Whitwell7c26b612005-04-21 14:46:57 +00003018static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003019parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003020 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003021 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003022{
Brian Paul7aebaf32005-10-30 21:23:23 +00003023 GLint mask;
3024 GLuint idx;
3025 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003026
3027 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3028 return 1;
3029
3030 reg->File = file;
3031 reg->Index = idx;
3032 reg->WriteMask = mask;
3033 return 0;
3034}
3035
3036/**
3037 * Handle the parsing out of a masked address register
3038 *
3039 * \param Index - The register index we write to
3040 * \param WriteMask - The mask controlling which components we write (1->write)
3041 *
3042 * \return 0 on sucess, 1 on error
3043 */
3044static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003045parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003046 struct var_cache **vc_head,
3047 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003048 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003049{
3050 GLint idx;
3051
3052 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3053 return 1;
3054
3055 /* This should be 0x8 */
3056 (*inst)++;
3057
3058 reg->File = PROGRAM_ADDRESS;
3059 reg->Index = idx;
3060
3061 /* Writemask of .x is implied */
3062 reg->WriteMask = 0x1;
3063 return 0;
3064}
3065
3066/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003067 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003068 */
3069static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003070parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003071 struct var_cache **vc_head,
3072 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003073 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003074{
Brian Paul7aebaf32005-10-30 21:23:23 +00003075 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003076 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003077 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003078 GLubyte swizzle[4];
3079 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003080
3081 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003082 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003083
3084 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003085 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003086 return 1;
3087
3088 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003089 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003090
Brian Paul32df89e2005-10-29 18:26:43 +00003091 reg->File = file;
3092 reg->Index = index;
3093 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3094 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003095 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003096 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003097 return 0;
3098}
3099
3100
3101static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003102parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003103 struct var_cache **vc_head,
3104 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003105 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003106{
Brian Paul7aebaf32005-10-30 21:23:23 +00003107 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003108 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003109 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003110 GLubyte Swizzle[4];
3111 GLboolean IsRelOffset;
3112
3113 /* Grab the sign */
3114 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3115
3116 /* And the src reg */
3117 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3118 return 1;
3119
3120 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003121 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003122
3123 reg->File = File;
3124 reg->Index = Index;
3125 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003126 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003127 reg->RelAddr = IsRelOffset;
3128 return 0;
3129}
3130
3131
Jouk Jansen40322e12004-04-05 08:50:36 +00003132/**
3133 * This is a big mother that handles getting opcodes into the instruction
3134 * and handling the src & dst registers for vertex program instructions
3135 */
3136static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003137parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003138 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003139 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003140{
3141 GLint a;
3142 GLubyte type, code;
3143
3144 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3145 type = *(*inst)++;
3146
3147 /* The actual opcode name */
3148 code = *(*inst)++;
3149
Brian Paul7e807512005-11-05 17:10:45 +00003150 _mesa_init_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003151 /* Record the position in the program string for debugging */
3152 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003153
3154 switch (type) {
3155 /* XXX: */
3156 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003157 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003158
3159 /* Remember to set SrcReg.RelAddr; */
3160
3161 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003162 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003163 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003164
Jouk Jansen40322e12004-04-05 08:50:36 +00003165 vp->DstReg.File = PROGRAM_ADDRESS;
3166
3167 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003168 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003169 return 1;
3170
3171 break;
3172
3173 case OP_ALU_VECTOR:
3174 switch (code) {
3175 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003176 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003177 break;
3178 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003179 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003180 break;
3181 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003182 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003183 break;
3184 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003185 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003186 break;
3187 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003188 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003189 break;
3190 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003191
3192 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003193 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003194
3195 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003196 return 1;
3197 break;
3198
3199 case OP_ALU_SCALAR:
3200 switch (code) {
3201 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003202 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003203 break;
3204 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003205 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003206 break;
3207 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003208 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003209 break;
3210 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003211 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003212 break;
3213 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003214 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003215 break;
3216 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003217 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003218 break;
3219 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003220 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003221 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003222
3223 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003224 return 1;
3225 break;
3226
3227 case OP_ALU_BINSC:
3228 switch (code) {
3229 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003230 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003231 break;
3232 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003233 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003234 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003235
Jouk Jansen40322e12004-04-05 08:50:36 +00003236 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003237 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003238 return 1;
3239 }
3240 break;
3241
3242 case OP_ALU_BIN:
3243 switch (code) {
3244 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003245 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003246 break;
3247 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003248 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003249 break;
3250 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003251 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003252 break;
3253 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003254 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003255 break;
3256 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003257 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003258 break;
3259 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003260 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003261 break;
3262 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003263 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003264 break;
3265 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003266 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003267 break;
3268 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003269 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003270 break;
3271 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003272 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003273 break;
3274 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003275 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 break;
3277 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003278 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003279 break;
3280 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003281 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003282 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003283
Jouk Jansen40322e12004-04-05 08:50:36 +00003284 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003285 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003286 return 1;
3287 }
3288 break;
3289
3290 case OP_ALU_TRI:
3291 switch (code) {
3292 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003293 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 break;
3295 }
3296
Keith Whitwell7c26b612005-04-21 14:46:57 +00003297 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003298 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003299
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003301 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003302 return 1;
3303 }
3304 break;
3305
3306 case OP_ALU_SWZ:
3307 switch (code) {
3308 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003309 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003310 break;
3311 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003312 {
Brian Paul32df89e2005-10-29 18:26:43 +00003313 GLubyte swizzle[4];
3314 GLubyte negateMask;
3315 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003316 enum register_file file;
3317 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003318
Keith Whitwell7c26b612005-04-21 14:46:57 +00003319 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3320 return 1;
3321
Brian Paul32df89e2005-10-29 18:26:43 +00003322 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003323 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003324 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3325 vp->SrcReg[0].File = file;
3326 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003327 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003328 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3329 swizzle[1],
3330 swizzle[2],
3331 swizzle[3]);
3332 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003333 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003334 break;
3335 }
3336 return 0;
3337}
3338
3339#if DEBUG_PARSING
3340
3341static GLvoid
3342print_state_token (GLint token)
3343{
3344 switch (token) {
3345 case STATE_MATERIAL:
3346 fprintf (stderr, "STATE_MATERIAL ");
3347 break;
3348 case STATE_LIGHT:
3349 fprintf (stderr, "STATE_LIGHT ");
3350 break;
3351
3352 case STATE_LIGHTMODEL_AMBIENT:
3353 fprintf (stderr, "STATE_AMBIENT ");
3354 break;
3355
3356 case STATE_LIGHTMODEL_SCENECOLOR:
3357 fprintf (stderr, "STATE_SCENECOLOR ");
3358 break;
3359
3360 case STATE_LIGHTPROD:
3361 fprintf (stderr, "STATE_LIGHTPROD ");
3362 break;
3363
3364 case STATE_TEXGEN:
3365 fprintf (stderr, "STATE_TEXGEN ");
3366 break;
3367
3368 case STATE_FOG_COLOR:
3369 fprintf (stderr, "STATE_FOG_COLOR ");
3370 break;
3371
3372 case STATE_FOG_PARAMS:
3373 fprintf (stderr, "STATE_FOG_PARAMS ");
3374 break;
3375
3376 case STATE_CLIPPLANE:
3377 fprintf (stderr, "STATE_CLIPPLANE ");
3378 break;
3379
3380 case STATE_POINT_SIZE:
3381 fprintf (stderr, "STATE_POINT_SIZE ");
3382 break;
3383
3384 case STATE_POINT_ATTENUATION:
3385 fprintf (stderr, "STATE_ATTENUATION ");
3386 break;
3387
3388 case STATE_MATRIX:
3389 fprintf (stderr, "STATE_MATRIX ");
3390 break;
3391
3392 case STATE_MODELVIEW:
3393 fprintf (stderr, "STATE_MODELVIEW ");
3394 break;
3395
3396 case STATE_PROJECTION:
3397 fprintf (stderr, "STATE_PROJECTION ");
3398 break;
3399
3400 case STATE_MVP:
3401 fprintf (stderr, "STATE_MVP ");
3402 break;
3403
3404 case STATE_TEXTURE:
3405 fprintf (stderr, "STATE_TEXTURE ");
3406 break;
3407
3408 case STATE_PROGRAM:
3409 fprintf (stderr, "STATE_PROGRAM ");
3410 break;
3411
3412 case STATE_MATRIX_INVERSE:
3413 fprintf (stderr, "STATE_INVERSE ");
3414 break;
3415
3416 case STATE_MATRIX_TRANSPOSE:
3417 fprintf (stderr, "STATE_TRANSPOSE ");
3418 break;
3419
3420 case STATE_MATRIX_INVTRANS:
3421 fprintf (stderr, "STATE_INVTRANS ");
3422 break;
3423
3424 case STATE_AMBIENT:
3425 fprintf (stderr, "STATE_AMBIENT ");
3426 break;
3427
3428 case STATE_DIFFUSE:
3429 fprintf (stderr, "STATE_DIFFUSE ");
3430 break;
3431
3432 case STATE_SPECULAR:
3433 fprintf (stderr, "STATE_SPECULAR ");
3434 break;
3435
3436 case STATE_EMISSION:
3437 fprintf (stderr, "STATE_EMISSION ");
3438 break;
3439
3440 case STATE_SHININESS:
3441 fprintf (stderr, "STATE_SHININESS ");
3442 break;
3443
3444 case STATE_HALF:
3445 fprintf (stderr, "STATE_HALF ");
3446 break;
3447
3448 case STATE_POSITION:
3449 fprintf (stderr, "STATE_POSITION ");
3450 break;
3451
3452 case STATE_ATTENUATION:
3453 fprintf (stderr, "STATE_ATTENUATION ");
3454 break;
3455
3456 case STATE_SPOT_DIRECTION:
3457 fprintf (stderr, "STATE_DIRECTION ");
3458 break;
3459
3460 case STATE_TEXGEN_EYE_S:
3461 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3462 break;
3463
3464 case STATE_TEXGEN_EYE_T:
3465 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3466 break;
3467
3468 case STATE_TEXGEN_EYE_R:
3469 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3470 break;
3471
3472 case STATE_TEXGEN_EYE_Q:
3473 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3474 break;
3475
3476 case STATE_TEXGEN_OBJECT_S:
3477 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3478 break;
3479
3480 case STATE_TEXGEN_OBJECT_T:
3481 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3482 break;
3483
3484 case STATE_TEXGEN_OBJECT_R:
3485 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3486 break;
3487
3488 case STATE_TEXGEN_OBJECT_Q:
3489 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3490 break;
3491
3492 case STATE_TEXENV_COLOR:
3493 fprintf (stderr, "STATE_TEXENV_COLOR ");
3494 break;
3495
3496 case STATE_DEPTH_RANGE:
3497 fprintf (stderr, "STATE_DEPTH_RANGE ");
3498 break;
3499
3500 case STATE_VERTEX_PROGRAM:
3501 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3502 break;
3503
3504 case STATE_FRAGMENT_PROGRAM:
3505 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3506 break;
3507
3508 case STATE_ENV:
3509 fprintf (stderr, "STATE_ENV ");
3510 break;
3511
3512 case STATE_LOCAL:
3513 fprintf (stderr, "STATE_LOCAL ");
3514 break;
3515
3516 }
3517 fprintf (stderr, "[%d] ", token);
3518}
3519
3520
3521static GLvoid
3522debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3523 struct arb_program *Program)
3524{
3525 struct var_cache *vc;
3526 GLint a, b;
3527
3528 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3529
3530 /* First of all, print out the contents of the var_cache */
3531 vc = vc_head;
3532 while (vc) {
3533 fprintf (stderr, "[%x]\n", vc);
3534 switch (vc->type) {
3535 case vt_none:
3536 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3537 break;
3538 case vt_attrib:
3539 fprintf (stderr, "ATTRIB %s\n", vc->name);
3540 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3541 break;
3542 case vt_param:
3543 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3544 vc->param_binding_begin, vc->param_binding_length);
3545 b = vc->param_binding_begin;
3546 for (a = 0; a < vc->param_binding_length; a++) {
3547 fprintf (stderr, "%s\n",
3548 Program->Parameters->Parameters[a + b].Name);
3549 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3550 print_state_token (Program->Parameters->Parameters[a + b].
3551 StateIndexes[0]);
3552 print_state_token (Program->Parameters->Parameters[a + b].
3553 StateIndexes[1]);
3554 print_state_token (Program->Parameters->Parameters[a + b].
3555 StateIndexes[2]);
3556 print_state_token (Program->Parameters->Parameters[a + b].
3557 StateIndexes[3]);
3558 print_state_token (Program->Parameters->Parameters[a + b].
3559 StateIndexes[4]);
3560 print_state_token (Program->Parameters->Parameters[a + b].
3561 StateIndexes[5]);
3562 }
3563 else
3564 fprintf (stderr, "%f %f %f %f\n",
3565 Program->Parameters->Parameters[a + b].Values[0],
3566 Program->Parameters->Parameters[a + b].Values[1],
3567 Program->Parameters->Parameters[a + b].Values[2],
3568 Program->Parameters->Parameters[a + b].Values[3]);
3569 }
3570 break;
3571 case vt_temp:
3572 fprintf (stderr, "TEMP %s\n", vc->name);
3573 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3574 break;
3575 case vt_output:
3576 fprintf (stderr, "OUTPUT %s\n", vc->name);
3577 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3578 break;
3579 case vt_alias:
3580 fprintf (stderr, "ALIAS %s\n", vc->name);
3581 fprintf (stderr, " binding: 0x%x (%s)\n",
3582 vc->alias_binding, vc->alias_binding->name);
3583 break;
3584 }
3585 vc = vc->next;
3586 }
3587}
3588
Brian Paul72030e02005-11-03 03:30:34 +00003589#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003590
3591
3592/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003593 * The main loop for parsing a fragment or vertex program
3594 *
Brian Paul72030e02005-11-03 03:30:34 +00003595 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003596 */
Brian Paul72030e02005-11-03 03:30:34 +00003597static GLint
Brian Paula088f162006-09-05 23:08:51 +00003598parse_instructions(GLcontext * ctx, const GLubyte * inst,
3599 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003600{
Brian Paul72030e02005-11-03 03:30:34 +00003601 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3602 ? ctx->Const.FragmentProgram.MaxInstructions
3603 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003604 GLint err = 0;
3605
Brian Paul72030e02005-11-03 03:30:34 +00003606 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3607
Jouk Jansen40322e12004-04-05 08:50:36 +00003608 Program->MajorVersion = (GLuint) * inst++;
3609 Program->MinorVersion = (GLuint) * inst++;
3610
3611 while (*inst != END) {
3612 switch (*inst++) {
3613
3614 case OPTION:
3615 switch (*inst++) {
3616 case ARB_PRECISION_HINT_FASTEST:
3617 Program->PrecisionOption = GL_FASTEST;
3618 break;
3619
3620 case ARB_PRECISION_HINT_NICEST:
3621 Program->PrecisionOption = GL_NICEST;
3622 break;
3623
3624 case ARB_FOG_EXP:
3625 Program->FogOption = GL_EXP;
3626 break;
3627
3628 case ARB_FOG_EXP2:
3629 Program->FogOption = GL_EXP2;
3630 break;
3631
3632 case ARB_FOG_LINEAR:
3633 Program->FogOption = GL_LINEAR;
3634 break;
3635
3636 case ARB_POSITION_INVARIANT:
3637 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003638 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003639 break;
3640
3641 case ARB_FRAGMENT_PROGRAM_SHADOW:
3642 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3643 /* TODO ARB_fragment_program_shadow code */
3644 }
3645 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003646
3647 case ARB_DRAW_BUFFERS:
3648 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3649 /* do nothing for now */
3650 }
3651 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003652 }
3653 break;
3654
3655 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003656 /* check length */
3657 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003658 program_error(ctx, Program->Position,
3659 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003660 return 1;
3661 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003662 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003663 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003664 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003665 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003666 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003667 }
3668 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003669 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003670 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003671 }
3672
Brian Paul72030e02005-11-03 03:30:34 +00003673 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003674 Program->Base.NumInstructions++;
3675 break;
3676
3677 case DECLARATION:
3678 err = parse_declaration (ctx, &inst, vc_head, Program);
3679 break;
3680
3681 default:
3682 break;
3683 }
3684
3685 if (err)
3686 break;
3687 }
3688
3689 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003690 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003691 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul8c41a142005-11-19 15:36:28 +00003692 _mesa_init_instruction(Program->Base.Instructions + numInst);
3693 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003694 /* YYY Wrong Position in program, whatever, at least not random -> crash
3695 Program->Position = parse_position (&inst);
3696 */
Brian Paul8c41a142005-11-19 15:36:28 +00003697 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003698 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003699 Program->Base.NumInstructions++;
3700
Brian Paul05051032005-11-01 04:36:33 +00003701 /*
3702 * Initialize native counts to logical counts. The device driver may
3703 * change them if program is translated into a hardware program.
3704 */
3705 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3706 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3707 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3708 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3709 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003710
Jouk Jansen40322e12004-04-05 08:50:36 +00003711 return err;
3712}
3713
Brian Paul7aebaf32005-10-30 21:23:23 +00003714
Jouk Jansen40322e12004-04-05 08:50:36 +00003715/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003716__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003717#include "grammar_syn.h"
3718;
3719
Brian Paul7aebaf32005-10-30 21:23:23 +00003720
Brian Paul8c41a142005-11-19 15:36:28 +00003721/**
3722 * Set a grammar parameter.
3723 * \param name the grammar parameter
3724 * \param value the new parameter value
3725 * \return 0 if OK, 1 if error
3726 */
3727static int
3728set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003729{
3730 char error_msg[300];
3731 GLint error_pos;
3732
Brian Paul8c41a142005-11-19 15:36:28 +00003733 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003734 return 0;
3735
3736 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3737 _mesa_set_program_error (ctx, error_pos, error_msg);
3738 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3739 return 1;
3740}
3741
Brian Paul8c41a142005-11-19 15:36:28 +00003742
3743/**
3744 * Enable support for the given language option in the parser.
3745 * \return 1 if OK, 0 if error
3746 */
3747static int
3748enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003749{
Brian Paul8c41a142005-11-19 15:36:28 +00003750 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003751}
3752
Brian Paul8c41a142005-11-19 15:36:28 +00003753
3754/**
3755 * Enable parser extensions based on which OpenGL extensions are supported
3756 * by this rendering context.
3757 *
3758 * \return GL_TRUE if OK, GL_FALSE if error.
3759 */
3760static GLboolean
3761enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003762{
Brian Paul8c41a142005-11-19 15:36:28 +00003763#if 0
3764 /* These are not supported at this time */
3765 if ((ctx->Extensions.ARB_vertex_blend ||
3766 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003767 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003768 return GL_FALSE;
3769 if (ctx->Extensions.ARB_matrix_palette
3770 && !enable_ext(ctx, id, "matrix_palette"))
3771 return GL_FALSE;
3772 if (ctx->Extensions.ARB_fragment_program_shadow
3773 && !enable_ext(ctx, id, "fragment_program_shadow"))
3774 return GL_FALSE;
3775#endif
3776 if (ctx->Extensions.EXT_point_parameters
3777 && !enable_ext(ctx, id, "point_parameters"))
3778 return GL_FALSE;
3779 if (ctx->Extensions.EXT_secondary_color
3780 && !enable_ext(ctx, id, "secondary_color"))
3781 return GL_FALSE;
3782 if (ctx->Extensions.EXT_fog_coord
3783 && !enable_ext(ctx, id, "fog_coord"))
3784 return GL_FALSE;
3785 if (ctx->Extensions.NV_texture_rectangle
3786 && !enable_ext(ctx, id, "texture_rectangle"))
3787 return GL_FALSE;
3788 if (ctx->Extensions.ARB_draw_buffers
3789 && !enable_ext(ctx, id, "draw_buffers"))
3790 return GL_FALSE;
3791
Brian Paul3a557502006-09-05 23:15:29 +00003792#if 1
3793 /* hack for Warcraft (see bug 8060) */
3794 enable_ext(ctx, id, "vertex_blend");
3795#endif
3796
Brian Paul8c41a142005-11-19 15:36:28 +00003797 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003798}
3799
Brian Paul8c41a142005-11-19 15:36:28 +00003800
Jouk Jansen40322e12004-04-05 08:50:36 +00003801/**
3802 * This kicks everything off.
3803 *
3804 * \param ctx - The GL Context
3805 * \param str - The program string
3806 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003807 * \param program - The arb_program struct to return all the parsed info in
3808 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003809 */
Brian Paul8c41a142005-11-19 15:36:28 +00003810static GLboolean
3811_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3812 const GLubyte *str, GLsizei len,
3813 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003814{
3815 GLint a, err, error_pos;
3816 char error_msg[300];
3817 GLuint parsed_len;
3818 struct var_cache *vc_head;
3819 grammar arbprogram_syn_id;
3820 GLubyte *parsed, *inst;
3821 GLubyte *strz = NULL;
3822 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3823
Brian Paul8c41a142005-11-19 15:36:28 +00003824 /* set the program target before parsing */
3825 program->Base.Target = target;
3826
Brian Paul7f76b8f2004-09-10 01:05:39 +00003827 /* Reset error state */
3828 _mesa_set_program_error(ctx, -1, NULL);
3829
Brian Paul8c41a142005-11-19 15:36:28 +00003830 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003831 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003832 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003833 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003834 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003835
3836 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3837 if (grammar_syn_id == 0) {
3838 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003839 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003840 _mesa_set_program_error (ctx, error_pos, error_msg);
3841 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003842 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003843 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003844 }
3845
Brian Paul8c41a142005-11-19 15:36:28 +00003846 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3847 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003848
Brian Paul49a80ca2006-04-28 15:40:11 +00003849 /* 'parsed' is unused here */
3850 _mesa_free (parsed);
3851 parsed = NULL;
3852
Brian Paul45703642005-10-29 15:52:31 +00003853 /* NOTE: we can't destroy grammar_syn_id right here because
3854 * grammar_destroy() can reset the last error
3855 */
Brian Paul8c41a142005-11-19 15:36:28 +00003856 if (err) {
3857 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003858 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3859 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003860 _mesa_error (ctx, GL_INVALID_OPERATION,
3861 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003862 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003863 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003864 }
3865
3866 grammar_destroy (grammar_syn_id);
3867
3868 arbprogram_syn_is_ok = 1;
3869 }
3870
3871 /* create the grammar object */
3872 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3873 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003874 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003875 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3876 _mesa_set_program_error (ctx, error_pos, error_msg);
3877 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003878 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003879 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003880 }
3881
3882 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003883 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003884 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3885 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003886 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003887 }
3888
Brian Paul8c41a142005-11-19 15:36:28 +00003889 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3890 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003891 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003892 }
3893
3894 /* check for NULL character occurences */
3895 {
Brian Paul8c41a142005-11-19 15:36:28 +00003896 GLint i;
3897 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003898 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003899 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003900 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003901 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003902 }
Brian Paul8c41a142005-11-19 15:36:28 +00003903 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003904 }
3905
3906 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003907 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003908 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003909 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3910 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003911 return GL_FALSE;
3912 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003913 _mesa_memcpy (strz, str, len);
3914 strz[len] = '\0';
3915
Michal Krolb80bc052004-10-21 14:09:54 +00003916 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003917 err = !grammar_fast_check(arbprogram_syn_id, strz,
3918 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003919
3920 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003921 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003922 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3923 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003924
Brian Paulb3aefd12005-09-19 20:12:32 +00003925#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003926 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003927 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003928 int line, col;
3929 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003930 fprintf(stderr, "program: %s\n", (char *) strz);
3931 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003932 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3933 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003934 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3935 } while (0)
3936#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003937
Brian Paula088f162006-09-05 23:08:51 +00003938 _mesa_free(strz);
3939 _mesa_free(parsed);
3940
Jouk Jansen40322e12004-04-05 08:50:36 +00003941 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003942 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003943 }
3944
Jouk Jansen40322e12004-04-05 08:50:36 +00003945 grammar_destroy (arbprogram_syn_id);
3946
Brian Paul8c41a142005-11-19 15:36:28 +00003947 /*
3948 * Program string is syntactically correct at this point
3949 * Parse the tokenized version of the program now, generating
3950 * vertex/fragment program instructions.
3951 */
3952
Jouk Jansen40322e12004-04-05 08:50:36 +00003953 /* Initialize the arb_program struct */
3954 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003955 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003956 program->Base.NumInstructions =
3957 program->Base.NumTemporaries =
3958 program->Base.NumParameters =
3959 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003960 program->Base.Parameters = _mesa_new_parameter_list ();
3961 program->Base.InputsRead = 0x0;
3962 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003963 program->Position = 0;
3964 program->MajorVersion = program->MinorVersion = 0;
3965 program->PrecisionOption = GL_DONT_CARE;
3966 program->FogOption = GL_NONE;
3967 program->HintPositionInvariant = GL_FALSE;
3968 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003969 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003970 program->NumAluInstructions =
3971 program->NumTexInstructions =
3972 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003973 program->UsesKill = 0;
3974
Jouk Jansen40322e12004-04-05 08:50:36 +00003975 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003976 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003977
3978 /* Start examining the tokens in the array */
3979 inst = parsed;
3980
3981 /* Check the grammer rev */
3982 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003983 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003984 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003985 }
3986 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003987 /* ignore program target */
3988 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003989 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003990 }
3991
3992 /*debug_variables(ctx, vc_head, program); */
3993
3994 /* We're done with the parsed binary array */
3995 var_cache_destroy (&vc_head);
3996
3997 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003998
Brian Paul8c41a142005-11-19 15:36:28 +00003999 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4000 * to size [ap.Base.NumInstructions].
4001 */
Brian Paula7543902006-08-24 21:58:32 +00004002 program->Base.Instructions
4003 = _mesa_realloc_instructions(program->Base.Instructions,
4004 MAX_INSTRUCTIONS,
4005 program->Base.NumInstructions);
4006
Brian Paul45703642005-10-29 15:52:31 +00004007 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004008}
Brian Paul8c41a142005-11-19 15:36:28 +00004009
4010
4011
4012void
4013_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4014 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004015 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004016{
4017 struct arb_program ap;
4018 GLuint i;
4019
4020 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004021 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004022 /* Error in the program. Just return. */
4023 return;
4024 }
4025
4026 /* Copy the relevant contents of the arb_program struct into the
4027 * fragment_program struct.
4028 */
4029 program->Base.String = ap.Base.String;
4030 program->Base.NumInstructions = ap.Base.NumInstructions;
4031 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4032 program->Base.NumParameters = ap.Base.NumParameters;
4033 program->Base.NumAttributes = ap.Base.NumAttributes;
4034 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004035 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4036 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4037 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4038 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4039 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004040 program->NumAluInstructions = ap.NumAluInstructions;
4041 program->NumTexInstructions = ap.NumTexInstructions;
4042 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004043 program->NumNativeAluInstructions = ap.NumAluInstructions;
4044 program->NumNativeTexInstructions = ap.NumTexInstructions;
4045 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004046 program->Base.InputsRead = ap.Base.InputsRead;
4047 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4048 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4049 program->TexturesUsed[i] = ap.TexturesUsed[i];
4050 program->FogOption = ap.FogOption;
4051
4052 if (program->Base.Instructions)
4053 _mesa_free(program->Base.Instructions);
4054 program->Base.Instructions = ap.Base.Instructions;
4055
4056 if (program->Base.Parameters)
4057 _mesa_free_parameter_list(program->Base.Parameters);
4058 program->Base.Parameters = ap.Base.Parameters;
4059
4060#if DEBUG_FP
4061 _mesa_print_program(&program.Base);
4062#endif
4063}
4064
4065
4066
4067/**
4068 * Parse the vertex program string. If success, update the given
4069 * vertex_program object with the new program. Else, leave the vertex_program
4070 * object unchanged.
4071 */
4072void
4073_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4074 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004075 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004076{
4077 struct arb_program ap;
4078
4079 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4080
Brian Paul95801792005-12-06 15:41:43 +00004081 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004082 /* Error in the program. Just return. */
4083 return;
4084 }
4085
4086 /* Copy the relevant contents of the arb_program struct into the
4087 * vertex_program struct.
4088 */
4089 program->Base.String = ap.Base.String;
4090 program->Base.NumInstructions = ap.Base.NumInstructions;
4091 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4092 program->Base.NumParameters = ap.Base.NumParameters;
4093 program->Base.NumAttributes = ap.Base.NumAttributes;
4094 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004095 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4096 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4097 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4098 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4099 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004100 program->Base.InputsRead = ap.Base.InputsRead;
4101 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4102 program->IsPositionInvariant = ap.HintPositionInvariant;
4103
4104 if (program->Base.Instructions)
4105 _mesa_free(program->Base.Instructions);
4106 program->Base.Instructions = ap.Base.Instructions;
4107
4108 if (program->Base.Parameters)
4109 _mesa_free_parameter_list(program->Base.Parameters);
4110 program->Base.Parameters = ap.Base.Parameters;
4111
4112#if DEBUG_VP
4113 _mesa_print_program(&program->Base);
4114#endif
4115}