blob: 9af3fd0764da078f8ca26669166fa8faea70fd35 [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brianb7978af2007-01-09 19:17:17 -07003 * Version: 6.5.3
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Brianb7978af2007-01-09 19:17:17 -07005 * Copyright (C) 1999-2007 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"
Brianc0551f02006-12-14 15:02:37 -070038#include "prog_parameter.h"
39#include "prog_statevars.h"
Brian Paul8c41a142005-11-19 15:36:28 +000040#include "context.h"
Brian Paul6211a142006-08-24 23:37:13 +000041#include "macros.h"
Brian Paul8c41a142005-11-19 15:36:28 +000042#include "mtypes.h"
Brianc0551f02006-12-14 15:02:37 -070043#include "prog_instruction.h"
Brian Paul8c41a142005-11-19 15:36:28 +000044
45
Brian Paul6211a142006-08-24 23:37:13 +000046/* For ARB programs, use the NV instruction limits */
47#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
48 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000049
50
51/**
52 * This is basically a union of the vertex_program and fragment_program
53 * structs that we can use to parse the program into
54 *
55 * XXX we can probably get rid of this entirely someday.
56 */
57struct arb_program
58{
Brian Paul122629f2006-07-20 16:49:57 +000059 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000060
61 GLuint Position; /* Just used for error reporting while parsing */
62 GLuint MajorVersion;
63 GLuint MinorVersion;
64
65 /* ARB_vertex_progmra options */
66 GLboolean HintPositionInvariant;
67
68 /* ARB_fragment_progmra options */
69 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
70 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
71
72 /* ARB_fragment_program specifics */
73 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
74 GLuint NumAluInstructions;
75 GLuint NumTexInstructions;
76 GLuint NumTexIndirections;
77
78 GLboolean UsesKill;
79};
80
Ian Romanick9bdfee32005-07-18 12:31:24 +000081
Brian Paula6c423d2004-08-25 15:59:48 +000082
Jouk Jansen40322e12004-04-05 08:50:36 +000083/* TODO:
84 * Fragment Program Stuff:
85 * -----------------------------------------------------
86 *
87 * - things from Michal's email
88 * + overflow on atoi
89 * + not-overflowing floats (don't use parse_integer..)
90 * + can remove range checking in arbparse.c
91 *
92 * - check all limits of number of various variables
93 * + parameters
94 *
95 * - test! test! test!
96 *
97 * Vertex Program Stuff:
98 * -----------------------------------------------------
99 * - Optimize param array usage and count limits correctly, see spec,
100 * section 2.14.3.7
101 * + Record if an array is reference absolutly or relatively (or both)
102 * + For absolute arrays, store a bitmap of accesses
103 * + For single parameters, store an access flag
104 * + After parsing, make a parameter cleanup and merging pass, where
105 * relative arrays are layed out first, followed by abs arrays, and
106 * finally single state.
107 * + Remap offsets for param src and dst registers
108 * + Now we can properly count parameter usage
109 *
110 * - Multiple state binding errors in param arrays (see spec, just before
111 * section 2.14.3.3)
112 * - grep for XXX
113 *
114 * Mesa Stuff
115 * -----------------------------------------------------
116 * - User clipping planes vs. PositionInvariant
117 * - Is it sufficient to just multiply by the mvp to transform in the
118 * PositionInvariant case? Or do we need something more involved?
119 *
120 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
121 * - fetch state listed in program_parameters list
122 * + WTF should this go???
123 * + currently in nvvertexec.c and s_nvfragprog.c
124 *
125 * - allow for multiple address registers (and fetch address regs properly)
126 *
127 * Cosmetic Stuff
128 * -----------------------------------------------------
129 * - remove any leftover unused grammer.c stuff (dict_ ?)
130 * - fix grammer.c error handling so its not static
131 * - #ifdef around stuff pertaining to extentions
132 *
133 * Outstanding Questions:
134 * -----------------------------------------------------
135 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
136 * what gets hacked off because of this:
137 * + VERTEX_ATTRIB_MATRIXINDEX
138 * + VERTEX_ATTRIB_WEIGHT
139 * + MATRIX_MODELVIEW
140 * + MATRIX_PALETTE
141 *
142 * - When can we fetch env/local params from their own register files, and
143 * when to we have to fetch them into the main state register file?
144 * (think arrays)
145 *
146 * Grammar Changes:
147 * -----------------------------------------------------
148 */
149
150/* Changes since moving the file to shader directory
151
1522004-III-4 ------------------------------------------------------------
153- added #include "grammar_mesa.h"
154- removed grammar specific code part (it resides now in grammar.c)
155- added GL_ARB_fragment_program_shadow tokens
156- modified #include "arbparse_syn.h"
157- major changes inside _mesa_parse_arb_program()
158- check the program string for '\0' characters
159- copy the program string to a one-byte-longer location to have
160 it null-terminated
161- position invariance test (not writing to result.position) moved
162 to syntax part
163*/
164
165typedef GLubyte *production;
166
Brian Paul11a54c32006-11-15 19:54:25 +0000167
Jouk Jansen40322e12004-04-05 08:50:36 +0000168/**
169 * This is the text describing the rules to parse the grammar
170 */
Brian Paul11a54c32006-11-15 19:54:25 +0000171LONGSTRING static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000172#include "arbprogram_syn.h"
173;
174
175/**
176 * These should match up with the values defined in arbprogram.syn
177 */
178
179/*
180 Changes:
181 - changed and merged V_* and F_* opcode values to OP_*.
182 - added GL_ARB_fragment_program_shadow specific tokens (michal)
183*/
Ian Romanickbb372f12007-05-16 15:34:22 -0700184#define REVISION 0x0a
Jouk Jansen40322e12004-04-05 08:50:36 +0000185
186/* program type */
187#define FRAGMENT_PROGRAM 0x01
188#define VERTEX_PROGRAM 0x02
189
190/* program section */
191#define OPTION 0x01
192#define INSTRUCTION 0x02
193#define DECLARATION 0x03
194#define END 0x04
195
Michal Krolb80bc052004-10-21 14:09:54 +0000196/* GL_ARB_fragment_program option */
197#define ARB_PRECISION_HINT_FASTEST 0x00
198#define ARB_PRECISION_HINT_NICEST 0x01
199#define ARB_FOG_EXP 0x02
200#define ARB_FOG_EXP2 0x03
201#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000202
Michal Krolb80bc052004-10-21 14:09:54 +0000203/* GL_ARB_vertex_program option */
204#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000205
Michal Krolb80bc052004-10-21 14:09:54 +0000206/* GL_ARB_fragment_program_shadow option */
207#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000208
Michal Krolb80bc052004-10-21 14:09:54 +0000209/* GL_ARB_draw_buffers option */
210#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000211
Ian Romanickbb372f12007-05-16 15:34:22 -0700212/* GL_MESA_texture_array option */
213#define MESA_TEXTURE_ARRAY 0x08
214
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
Ian Romanickbb372f12007-05-16 15:34:22 -0700374/* GL_MESA_texture_array */
375#define TEXTARGET_1D_ARRAY 0x09
376#define TEXTARGET_2D_ARRAY 0x0a
Ian Romanick69358e72007-06-05 09:24:27 -0700377#define TEXTARGET_SHADOW1D_ARRAY 0x0b
378#define TEXTARGET_SHADOW2D_ARRAY 0x0c
Jouk Jansen40322e12004-04-05 08:50:36 +0000379
380/* face type */
381#define FACE_FRONT 0x00
382#define FACE_BACK 0x01
383
384/* color type */
385#define COLOR_PRIMARY 0x00
386#define COLOR_SECONDARY 0x01
387
388/* component */
389#define COMPONENT_X 0x00
390#define COMPONENT_Y 0x01
391#define COMPONENT_Z 0x02
392#define COMPONENT_W 0x03
393#define COMPONENT_0 0x04
394#define COMPONENT_1 0x05
395
396/* array index type */
397#define ARRAY_INDEX_ABSOLUTE 0x00
398#define ARRAY_INDEX_RELATIVE 0x01
399
400/* matrix name */
401#define MATRIX_MODELVIEW 0x01
402#define MATRIX_PROJECTION 0x02
403#define MATRIX_MVP 0x03
404#define MATRIX_TEXTURE 0x04
405#define MATRIX_PALETTE 0x05
406#define MATRIX_PROGRAM 0x06
407
408/* matrix modifier */
409#define MATRIX_MODIFIER_IDENTITY 0x00
410#define MATRIX_MODIFIER_INVERSE 0x01
411#define MATRIX_MODIFIER_TRANSPOSE 0x02
412#define MATRIX_MODIFIER_INVTRANS 0x03
413
414/* constant type */
415#define CONSTANT_SCALAR 0x01
416#define CONSTANT_VECTOR 0x02
417
418/* program param type */
419#define PROGRAM_PARAM_ENV 0x01
420#define PROGRAM_PARAM_LOCAL 0x02
421
422/* register type */
423#define REGISTER_ATTRIB 0x01
424#define REGISTER_PARAM 0x02
425#define REGISTER_RESULT 0x03
426#define REGISTER_ESTABLISHED_NAME 0x04
427
428/* param binding */
429#define PARAM_NULL 0x00
430#define PARAM_ARRAY_ELEMENT 0x01
431#define PARAM_STATE_ELEMENT 0x02
432#define PARAM_PROGRAM_ELEMENT 0x03
433#define PARAM_PROGRAM_ELEMENTS 0x04
434#define PARAM_CONSTANT 0x05
435
436/* param state property */
437#define STATE_MATERIAL_PARSER 0x01
438#define STATE_LIGHT_PARSER 0x02
439#define STATE_LIGHT_MODEL 0x03
440#define STATE_LIGHT_PROD 0x04
441#define STATE_FOG 0x05
442#define STATE_MATRIX_ROWS 0x06
443/* GL_ARB_fragment_program */
444#define STATE_TEX_ENV 0x07
445#define STATE_DEPTH 0x08
446/* GL_ARB_vertex_program */
447#define STATE_TEX_GEN 0x09
448#define STATE_CLIP_PLANE 0x0A
449#define STATE_POINT 0x0B
450
451/* state material property */
452#define MATERIAL_AMBIENT 0x01
453#define MATERIAL_DIFFUSE 0x02
454#define MATERIAL_SPECULAR 0x03
455#define MATERIAL_EMISSION 0x04
456#define MATERIAL_SHININESS 0x05
457
458/* state light property */
459#define LIGHT_AMBIENT 0x01
460#define LIGHT_DIFFUSE 0x02
461#define LIGHT_SPECULAR 0x03
462#define LIGHT_POSITION 0x04
463#define LIGHT_ATTENUATION 0x05
464#define LIGHT_HALF 0x06
465#define LIGHT_SPOT_DIRECTION 0x07
466
467/* state light model property */
468#define LIGHT_MODEL_AMBIENT 0x01
469#define LIGHT_MODEL_SCENECOLOR 0x02
470
471/* state light product property */
472#define LIGHT_PROD_AMBIENT 0x01
473#define LIGHT_PROD_DIFFUSE 0x02
474#define LIGHT_PROD_SPECULAR 0x03
475
476/* state texture environment property */
477#define TEX_ENV_COLOR 0x01
478
479/* state texture generation coord property */
480#define TEX_GEN_EYE 0x01
481#define TEX_GEN_OBJECT 0x02
482
483/* state fog property */
484#define FOG_COLOR 0x01
485#define FOG_PARAMS 0x02
486
487/* state depth property */
488#define DEPTH_RANGE 0x01
489
490/* state point parameters property */
491#define POINT_SIZE 0x01
492#define POINT_ATTENUATION 0x02
493
494/* declaration */
495#define ATTRIB 0x01
496#define PARAM 0x02
497#define TEMP 0x03
498#define OUTPUT 0x04
499#define ALIAS 0x05
500/* GL_ARB_vertex_program */
501#define ADDRESS 0x06
502
503/*-----------------------------------------------------------------------
504 * From here on down is the semantic checking portion
505 *
506 */
507
508/**
509 * Variable Table Handling functions
510 */
511typedef enum
512{
513 vt_none,
514 vt_address,
515 vt_attrib,
516 vt_param,
517 vt_temp,
518 vt_output,
519 vt_alias
520} var_type;
521
522
Brian Paul7aebaf32005-10-30 21:23:23 +0000523/**
524 * Setting an explicit field for each of the binding properties is a bit
525 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000526 */
527struct var_cache
528{
Brian Paul6a769d92006-04-28 15:42:15 +0000529 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000530 var_type type;
531 GLuint address_binding; /* The index of the address register we should
532 * be using */
533 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000534 GLuint attrib_is_generic; /* If the attrib was specified through a generic
535 * vertex attrib */
536 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000537 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000538 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
539 * that this is aliased to */
540 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
541 * PROGRAM_ENV_PARAM} */
542 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
543 * the tokens representing our bound state (or constants)
544 * start */
545 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
546 * we take up with our state tokens or constants. Note that
547 * this is _not_ the same as the number of param registers
548 * we eventually use */
549 struct var_cache *next;
550};
551
552static GLvoid
553var_cache_create (struct var_cache **va)
554{
555 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
556 if (*va) {
557 (**va).name = NULL;
558 (**va).type = vt_none;
559 (**va).attrib_binding = ~0;
560 (**va).attrib_is_generic = 0;
561 (**va).temp_binding = ~0;
562 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000563 (**va).param_binding_type = ~0;
564 (**va).param_binding_begin = ~0;
565 (**va).param_binding_length = ~0;
566 (**va).alias_binding = NULL;
567 (**va).next = NULL;
568 }
569}
570
571static GLvoid
572var_cache_destroy (struct var_cache **va)
573{
574 if (*va) {
575 var_cache_destroy (&(**va).next);
576 _mesa_free (*va);
577 *va = NULL;
578 }
579}
580
581static GLvoid
582var_cache_append (struct var_cache **va, struct var_cache *nv)
583{
584 if (*va)
585 var_cache_append (&(**va).next, nv);
586 else
587 *va = nv;
588}
589
590static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000591var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000592{
Brian Paul0a360cf2005-01-17 01:21:03 +0000593 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000594
595 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000596 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000597 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000598 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000599 return va;
600 }
601
602 va = va->next;
603 }
604
605 return NULL;
606}
607
Brian Paula088f162006-09-05 23:08:51 +0000608
609
610/**
611 * Called when an error is detected while parsing/compiling a program.
612 * Sets the ctx->Program.ErrorString field to descript and records a
613 * GL_INVALID_OPERATION error.
614 * \param position position of error in program string
615 * \param descrip verbose error description
616 */
617static void
618program_error(GLcontext *ctx, GLint position, const char *descrip)
619{
620 if (descrip) {
621 const char *prefix = "glProgramString(", *suffix = ")";
622 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
623 _mesa_strlen(prefix) +
624 _mesa_strlen(suffix) + 1);
625 if (str) {
626 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
627 _mesa_error(ctx, GL_INVALID_OPERATION, str);
628 _mesa_free(str);
629 }
630 }
631 _mesa_set_program_error(ctx, position, descrip);
632}
633
634
635
Jouk Jansen40322e12004-04-05 08:50:36 +0000636/**
637 * constructs an integer from 4 GLubytes in LE format
638 */
639static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000640parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000641{
642 GLuint value;
643
644 value = (GLuint) (*(*inst)++);
645 value += (GLuint) (*(*inst)++) * 0x100;
646 value += (GLuint) (*(*inst)++) * 0x10000;
647 value += (GLuint) (*(*inst)++) * 0x1000000;
648
649 return value;
650}
651
652/**
653 * This will, given a string, lookup the string as a variable name in the
654 * var cache. If the name is found, the var cache node corresponding to the
655 * var name is returned. If it is not found, a new entry is allocated
656 *
657 * \param I Points into the binary array where the string identifier begins
658 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
659 * \return The location on the var_cache corresponding the the string starting at I
660 */
661static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000662parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000663 struct arb_program *Program, GLuint * found)
664{
Brian Paula088f162006-09-05 23:08:51 +0000665 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000666 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000667 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000668
669 *inst += _mesa_strlen ((char *) i) + 1;
670
671 va = var_cache_find (*vc_head, i);
672
673 if (va) {
674 *found = 1;
675 return va;
676 }
677
678 *found = 0;
679 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000680 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000681
682 var_cache_append (vc_head, va);
683
684 return va;
685}
686
687static char *
Brian Paula088f162006-09-05 23:08:51 +0000688parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000689{
Brian Paula088f162006-09-05 23:08:51 +0000690 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000691 (void) Program;
692
Jouk Jansen40322e12004-04-05 08:50:36 +0000693 *inst += _mesa_strlen ((char *) i) + 1;
694
695 return (char *) i;
696}
697
698/**
Brian Paul05908952004-06-08 15:20:23 +0000699 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000700 */
Brian Paul05908952004-06-08 15:20:23 +0000701static GLint
Brian Paula088f162006-09-05 23:08:51 +0000702parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000703{
704 /*return *(*inst)++ != '+'; */
705
706 if (**inst == '-') {
707 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000708 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000709 }
710 else if (**inst == '+') {
711 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000712 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000713 }
714
Brian Paul05908952004-06-08 15:20:23 +0000715 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000716}
717
718/**
719 * parses and returns signed integer
720 */
721static GLint
Brian Paula088f162006-09-05 23:08:51 +0000722parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000723{
724 GLint sign;
725 GLint value;
726
727 /* check if *inst points to '+' or '-'
728 * if yes, grab the sign and increment *inst
729 */
730 sign = parse_sign (inst);
731
732 /* now check if *inst points to 0
733 * if yes, increment the *inst and return the default value
734 */
735 if (**inst == 0) {
736 (*inst)++;
737 return 0;
738 }
739
740 /* parse the integer as you normally would do it */
741 value = _mesa_atoi (parse_string_without_adding (inst, Program));
742
743 /* now, after terminating 0 there is a position
744 * to parse it - parse_position()
745 */
746 Program->Position = parse_position (inst);
747
Brian Paul05908952004-06-08 15:20:23 +0000748 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000749}
750
751/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000752 Accumulate this string of digits, and return them as
753 a large integer represented in floating point (for range).
754 If scale is not NULL, also accumulates a power-of-ten
755 integer scale factor that represents the number of digits
756 in the string.
757*/
758static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000759parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000760{
761 GLdouble value = 0.0;
762 GLdouble oscale = 1.0;
763
764 if (**inst == 0) { /* this string of digits is empty-- do nothing */
765 (*inst)++;
766 }
767 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000768 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000769 GLubyte digit = *((*inst)++);
770 value = value * 10.0 + (GLint) (digit - '0');
771 oscale *= 10.0;
772 }
773 assert(**inst == 0); /* integer string should end with 0 */
774 (*inst)++; /* skip over terminating 0 */
775 Program->Position = parse_position(inst); /* skip position (from integer) */
776 }
777 if (scale)
778 *scale = oscale;
779 return value;
780}
781
782/**
783 Parse an unsigned floating-point number from this stream of tokenized
784 characters. Example floating-point formats supported:
785 12.34
786 12
787 0.34
788 .34
789 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000790 */
791static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000792parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000793{
Brian Paul1ff8f502005-02-16 15:08:29 +0000794 GLint exponent;
795 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000796
Brian Paul1ff8f502005-02-16 15:08:29 +0000797 whole = parse_float_string(inst, Program, 0);
798 fraction = parse_float_string(inst, Program, &fracScale);
799
800 /* Parse signed exponent */
801 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000802
Brian Paul1ff8f502005-02-16 15:08:29 +0000803 /* Assemble parts of floating-point number: */
804 return (GLfloat) ((whole + fraction / fracScale) *
805 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000806}
807
808
809/**
810 */
811static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000812parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000813{
Brian Paul05908952004-06-08 15:20:23 +0000814 GLint sign = parse_sign (inst);
815 GLfloat value = parse_float (inst, Program);
816 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000817}
818
819/**
820 * This picks out a constant value from the parsed array. The constant vector is r
821 * returned in the *values array, which should be of length 4.
822 *
823 * \param values - The 4 component vector with the constant value in it
824 */
825static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000826parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000827 GLboolean use)
828{
829 GLuint components, i;
830
831
832 switch (*(*inst)++) {
833 case CONSTANT_SCALAR:
834 if (use == GL_TRUE) {
835 values[0] =
836 values[1] =
837 values[2] = values[3] = parse_float (inst, Program);
838 }
839 else {
840 values[0] =
841 values[1] =
842 values[2] = values[3] = parse_signed_float (inst, Program);
843 }
844
845 break;
846 case CONSTANT_VECTOR:
847 values[0] = values[1] = values[2] = 0;
848 values[3] = 1;
849 components = *(*inst)++;
850 for (i = 0; i < components; i++) {
851 values[i] = parse_signed_float (inst, Program);
852 }
853 break;
854 }
855}
856
857/**
858 * \param offset The offset from the address register that we should
859 * address
860 *
861 * \return 0 on sucess, 1 on error
862 */
863static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000864parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
865 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000866{
Brian Paul6a769d92006-04-28 15:42:15 +0000867 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000868 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000869 return 0;
870}
871
872/**
873 * \param color 0 if color type is primary, 1 if color type is secondary
874 * \return 0 on sucess, 1 on error
875 */
876static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000877parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000878 GLint * color)
879{
Brian Paula6c423d2004-08-25 15:59:48 +0000880 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000881 *color = *(*inst)++ != COLOR_PRIMARY;
882 return 0;
883}
884
885/**
886 * Get an integer corresponding to a generic vertex attribute.
887 *
888 * \return 0 on sucess, 1 on error
889 */
890static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000891parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000892 struct arb_program *Program, GLuint *attrib)
893{
Brian Paulbd997cd2004-07-20 21:12:56 +0000894 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000895
Michal Krolbb38cad2006-04-11 11:41:11 +0000896 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000897 {
Brian Paula088f162006-09-05 23:08:51 +0000898 program_error(ctx, Program->Position,
899 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000900 return 1;
901 }
902
Brian Paulbd997cd2004-07-20 21:12:56 +0000903 *attrib = (GLuint) i;
904
Jouk Jansen40322e12004-04-05 08:50:36 +0000905 return 0;
906}
907
908
909/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000910 * \param color The index of the color buffer to write into
911 * \return 0 on sucess, 1 on error
912 */
913static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000914parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000915 struct arb_program *Program, GLuint * color)
916{
917 GLint i = parse_integer (inst, Program);
918
919 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000920 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000921 return 1;
922 }
923
924 *color = (GLuint) i;
925 return 0;
926}
927
928
929/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000930 * \param coord The texture unit index
931 * \return 0 on sucess, 1 on error
932 */
933static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000934parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000935 struct arb_program *Program, GLuint * coord)
936{
Brian Paulbd997cd2004-07-20 21:12:56 +0000937 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000938
Brian Paula6c423d2004-08-25 15:59:48 +0000939 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000940 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000941 return 1;
942 }
943
Brian Paulbd997cd2004-07-20 21:12:56 +0000944 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000945 return 0;
946}
947
948/**
949 * \param coord The weight index
950 * \return 0 on sucess, 1 on error
951 */
952static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000953parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000954 GLint * coord)
955{
956 *coord = parse_integer (inst, Program);
957
958 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000959 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000960 return 1;
961 }
962
963 return 0;
964}
965
966/**
967 * \param coord The clip plane index
968 * \return 0 on sucess, 1 on error
969 */
970static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000971parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000972 struct arb_program *Program, GLint * coord)
973{
974 *coord = parse_integer (inst, Program);
975
976 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +0000977 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000978 return 1;
979 }
980
981 return 0;
982}
983
984
985/**
986 * \return 0 on front face, 1 on back face
987 */
988static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000989parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000990{
991 switch (*(*inst)++) {
992 case FACE_FRONT:
993 return 0;
994
995 case FACE_BACK:
996 return 1;
997 }
998 return 0;
999}
1000
1001
1002/**
1003 * Given a matrix and a modifier token on the binary array, return tokens
1004 * that _mesa_fetch_state() [program.c] can understand.
1005 *
1006 * \param matrix - the matrix we are talking about
1007 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1008 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1009 * \return 0 on sucess, 1 on failure
1010 */
1011static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001012parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001013 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1014{
1015 GLubyte mat = *(*inst)++;
1016
1017 *matrix_idx = 0;
1018
1019 switch (mat) {
1020 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001021 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001022 *matrix_idx = parse_integer (inst, Program);
1023 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001024 program_error(ctx, Program->Position,
1025 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001026 return 1;
1027 }
1028 break;
1029
1030 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001031 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001032 break;
1033
1034 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001035 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001036 break;
1037
1038 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001039 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001040 *matrix_idx = parse_integer (inst, Program);
1041 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001042 program_error(ctx, Program->Position, "Invalid Texture Unit");
1043 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001044 return 1;
1045 }
1046 break;
1047
1048 /* This is not currently supported (ARB_matrix_palette) */
1049 case MATRIX_PALETTE:
1050 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001051 program_error(ctx, Program->Position,
1052 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001053 return 1;
1054 break;
1055
1056 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001057 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001058 *matrix_idx = parse_integer (inst, Program);
1059 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001060 program_error(ctx, Program->Position, "Invalid Program Matrix");
1061 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001062 return 1;
1063 }
1064 break;
1065 }
1066
1067 switch (*(*inst)++) {
1068 case MATRIX_MODIFIER_IDENTITY:
1069 *matrix_modifier = 0;
1070 break;
1071 case MATRIX_MODIFIER_INVERSE:
1072 *matrix_modifier = STATE_MATRIX_INVERSE;
1073 break;
1074 case MATRIX_MODIFIER_TRANSPOSE:
1075 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1076 break;
1077 case MATRIX_MODIFIER_INVTRANS:
1078 *matrix_modifier = STATE_MATRIX_INVTRANS;
1079 break;
1080 }
1081
1082 return 0;
1083}
1084
1085
1086/**
1087 * This parses a state string (rather, the binary version of it) into
1088 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1089 *
1090 * \param inst - the start in the binary arry to start working from
1091 * \param state_tokens - the storage for the 6-token state description
1092 * \return - 0 on sucess, 1 on error
1093 */
1094static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001095parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001096 struct arb_program *Program,
1097 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001098{
1099 switch (*(*inst)++) {
1100 case STATE_MATERIAL_PARSER:
1101 state_tokens[0] = STATE_MATERIAL;
1102 state_tokens[1] = parse_face_type (inst);
1103 switch (*(*inst)++) {
1104 case MATERIAL_AMBIENT:
1105 state_tokens[2] = STATE_AMBIENT;
1106 break;
1107 case MATERIAL_DIFFUSE:
1108 state_tokens[2] = STATE_DIFFUSE;
1109 break;
1110 case MATERIAL_SPECULAR:
1111 state_tokens[2] = STATE_SPECULAR;
1112 break;
1113 case MATERIAL_EMISSION:
1114 state_tokens[2] = STATE_EMISSION;
1115 break;
1116 case MATERIAL_SHININESS:
1117 state_tokens[2] = STATE_SHININESS;
1118 break;
1119 }
1120 break;
1121
1122 case STATE_LIGHT_PARSER:
1123 state_tokens[0] = STATE_LIGHT;
1124 state_tokens[1] = parse_integer (inst, Program);
1125
1126 /* Check the value of state_tokens[1] against the # of lights */
1127 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001128 program_error(ctx, Program->Position, "Invalid Light Number");
1129 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001130 return 1;
1131 }
1132
1133 switch (*(*inst)++) {
1134 case LIGHT_AMBIENT:
1135 state_tokens[2] = STATE_AMBIENT;
1136 break;
1137 case LIGHT_DIFFUSE:
1138 state_tokens[2] = STATE_DIFFUSE;
1139 break;
1140 case LIGHT_SPECULAR:
1141 state_tokens[2] = STATE_SPECULAR;
1142 break;
1143 case LIGHT_POSITION:
1144 state_tokens[2] = STATE_POSITION;
1145 break;
1146 case LIGHT_ATTENUATION:
1147 state_tokens[2] = STATE_ATTENUATION;
1148 break;
1149 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001150 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001151 break;
1152 case LIGHT_SPOT_DIRECTION:
1153 state_tokens[2] = STATE_SPOT_DIRECTION;
1154 break;
1155 }
1156 break;
1157
1158 case STATE_LIGHT_MODEL:
1159 switch (*(*inst)++) {
1160 case LIGHT_MODEL_AMBIENT:
1161 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1162 break;
1163 case LIGHT_MODEL_SCENECOLOR:
1164 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1165 state_tokens[1] = parse_face_type (inst);
1166 break;
1167 }
1168 break;
1169
1170 case STATE_LIGHT_PROD:
1171 state_tokens[0] = STATE_LIGHTPROD;
1172 state_tokens[1] = parse_integer (inst, Program);
1173
1174 /* Check the value of state_tokens[1] against the # of lights */
1175 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001176 program_error(ctx, Program->Position, "Invalid Light Number");
1177 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001178 return 1;
1179 }
1180
1181 state_tokens[2] = parse_face_type (inst);
1182 switch (*(*inst)++) {
1183 case LIGHT_PROD_AMBIENT:
1184 state_tokens[3] = STATE_AMBIENT;
1185 break;
1186 case LIGHT_PROD_DIFFUSE:
1187 state_tokens[3] = STATE_DIFFUSE;
1188 break;
1189 case LIGHT_PROD_SPECULAR:
1190 state_tokens[3] = STATE_SPECULAR;
1191 break;
1192 }
1193 break;
1194
1195
1196 case STATE_FOG:
1197 switch (*(*inst)++) {
1198 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001199 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001200 break;
1201 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001202 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001203 break;
1204 }
1205 break;
1206
1207 case STATE_TEX_ENV:
1208 state_tokens[1] = parse_integer (inst, Program);
1209 switch (*(*inst)++) {
1210 case TEX_ENV_COLOR:
1211 state_tokens[0] = STATE_TEXENV_COLOR;
1212 break;
1213 }
1214 break;
1215
1216 case STATE_TEX_GEN:
1217 {
1218 GLuint type, coord;
1219
1220 state_tokens[0] = STATE_TEXGEN;
1221 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1222
1223 if (parse_texcoord_num (ctx, inst, Program, &coord))
1224 return 1;
1225 state_tokens[1] = coord;
1226
1227 /* EYE or OBJECT */
1228 type = *(*inst++);
1229
1230 /* 0 - s, 1 - t, 2 - r, 3 - q */
1231 coord = *(*inst++);
1232
1233 if (type == TEX_GEN_EYE) {
1234 switch (coord) {
1235 case COMPONENT_X:
1236 state_tokens[2] = STATE_TEXGEN_EYE_S;
1237 break;
1238 case COMPONENT_Y:
1239 state_tokens[2] = STATE_TEXGEN_EYE_T;
1240 break;
1241 case COMPONENT_Z:
1242 state_tokens[2] = STATE_TEXGEN_EYE_R;
1243 break;
1244 case COMPONENT_W:
1245 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1246 break;
1247 }
1248 }
1249 else {
1250 switch (coord) {
1251 case COMPONENT_X:
1252 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1253 break;
1254 case COMPONENT_Y:
1255 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1256 break;
1257 case COMPONENT_Z:
1258 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1259 break;
1260 case COMPONENT_W:
1261 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1262 break;
1263 }
1264 }
1265 }
1266 break;
1267
1268 case STATE_DEPTH:
1269 switch (*(*inst)++) {
1270 case DEPTH_RANGE:
1271 state_tokens[0] = STATE_DEPTH_RANGE;
1272 break;
1273 }
1274 break;
1275
1276 case STATE_CLIP_PLANE:
1277 state_tokens[0] = STATE_CLIPPLANE;
1278 state_tokens[1] = parse_integer (inst, Program);
Brianaa9d22a2007-02-23 11:21:03 -07001279 if (parse_clipplane_num (ctx, inst, Program,
1280 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001281 return 1;
1282 break;
1283
1284 case STATE_POINT:
1285 switch (*(*inst++)) {
1286 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001287 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001288 break;
1289
1290 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001291 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001292 break;
1293 }
1294 break;
1295
1296 /* XXX: I think this is the correct format for a matrix row */
1297 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001298 if (parse_matrix(ctx, inst, Program,
1299 (GLint *) &state_tokens[0],
1300 (GLint *) &state_tokens[1],
1301 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001302 return 1;
1303
Brian65319522007-02-21 11:08:21 -07001304 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001305
1306 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001307 state_tokens[3] = parse_integer (inst, Program);
1308 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001309 program_error(ctx, Program->Position,
1310 "Second matrix index less than the first");
1311 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001312 return 1;
1313 }
1314 }
1315 else {
Brian65319522007-02-21 11:08:21 -07001316 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001317 (*inst)++;
1318 }
1319 break;
1320 }
1321
1322 return 0;
1323}
1324
1325/**
1326 * This parses a state string (rather, the binary version of it) into
1327 * a 6-token similar for the state fetching code in program.c
1328 *
1329 * One might ask, why fetch these parameters into just like you fetch
1330 * state when they are already stored in other places?
1331 *
1332 * Because of array offsets -> We can stick env/local parameters in the
1333 * middle of a parameter array and then index someplace into the array
1334 * when we execute.
1335 *
1336 * One optimization might be to only do this for the cases where the
1337 * env/local parameters end up inside of an array, and leave the
1338 * single parameters (or arrays of pure env/local pareameters) in their
1339 * respective register files.
1340 *
1341 * For ENV parameters, the format is:
1342 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1343 * state_tokens[1] = STATE_ENV
1344 * state_tokens[2] = the parameter index
1345 *
1346 * for LOCAL parameters, the format is:
1347 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1348 * state_tokens[1] = STATE_LOCAL
1349 * state_tokens[2] = the parameter index
1350 *
1351 * \param inst - the start in the binary arry to start working from
1352 * \param state_tokens - the storage for the 6-token state description
1353 * \return - 0 on sucess, 1 on failure
1354 */
1355static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001356parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001357 struct arb_program *Program,
1358 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001359{
1360 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1361 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1362 else
1363 state_tokens[0] = STATE_VERTEX_PROGRAM;
1364
1365
1366 switch (*(*inst)++) {
1367 case PROGRAM_PARAM_ENV:
1368 state_tokens[1] = STATE_ENV;
1369 state_tokens[2] = parse_integer (inst, Program);
1370
1371 /* Check state_tokens[2] against the number of ENV parameters available */
1372 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001373 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001374 ||
1375 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001376 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001377 program_error(ctx, Program->Position,
1378 "Invalid Program Env Parameter");
1379 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001380 return 1;
1381 }
1382
1383 break;
1384
1385 case PROGRAM_PARAM_LOCAL:
1386 state_tokens[1] = STATE_LOCAL;
1387 state_tokens[2] = parse_integer (inst, Program);
1388
1389 /* Check state_tokens[2] against the number of LOCAL parameters available */
1390 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001391 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001392 ||
1393 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001394 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001395 program_error(ctx, Program->Position,
1396 "Invalid Program Local Parameter");
1397 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001398 return 1;
1399 }
1400 break;
1401 }
1402
1403 return 0;
1404}
1405
1406/**
1407 * For ARB_vertex_program, programs are not allowed to use both an explicit
1408 * vertex attribute and a generic vertex attribute corresponding to the same
1409 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1410 *
1411 * This will walk our var_cache and make sure that nobody does anything fishy.
1412 *
1413 * \return 0 on sucess, 1 on error
1414 */
1415static GLuint
1416generic_attrib_check(struct var_cache *vc_head)
1417{
1418 int a;
1419 struct var_cache *curr;
1420 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1421 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1422
1423 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1424 explicitAttrib[a] = GL_FALSE;
1425 genericAttrib[a] = GL_FALSE;
1426 }
1427
1428 curr = vc_head;
1429 while (curr) {
1430 if (curr->type == vt_attrib) {
1431 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001432 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001433 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001434 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001435 }
1436
1437 curr = curr->next;
1438 }
1439
1440 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1441 if ((explicitAttrib[a]) && (genericAttrib[a]))
1442 return 1;
1443 }
1444
1445 return 0;
1446}
1447
1448/**
1449 * This will handle the binding side of an ATTRIB var declaration
1450 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001451 * \param inputReg returns the input register index, one of the
1452 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001453 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001454 */
1455static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001456parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001457 struct arb_program *Program,
1458 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001459{
Jouk Jansen40322e12004-04-05 08:50:36 +00001460 GLint err = 0;
1461
1462 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001463
Jouk Jansen40322e12004-04-05 08:50:36 +00001464 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1465 switch (*(*inst)++) {
1466 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001467 {
1468 GLint coord;
1469 err = parse_color_type (ctx, inst, Program, &coord);
1470 *inputReg = FRAG_ATTRIB_COL0 + coord;
1471 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001473 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001474 {
Brian8fa6f732007-02-01 09:24:41 -07001475 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001476 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1477 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1478 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001479 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001480 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001481 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001482 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001483 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001484 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001485 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001486 default:
1487 err = 1;
1488 break;
1489 }
1490 }
1491 else {
1492 switch (*(*inst)++) {
1493 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001494 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001495 break;
1496
1497 case VERTEX_ATTRIB_WEIGHT:
1498 {
1499 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001500 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001501 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001502#if 1
1503 /* hack for Warcraft (see bug 8060) */
1504 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001505 break;
Brian Paul3a557502006-09-05 23:15:29 +00001506#else
Brian Paula088f162006-09-05 23:08:51 +00001507 program_error(ctx, Program->Position,
1508 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001509 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001510#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001511 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001512
1513 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001514 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001515 break;
1516
1517 case VERTEX_ATTRIB_COLOR:
1518 {
1519 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001520 err = parse_color_type (ctx, inst, Program, &color);
1521 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001522 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001523 }
1524 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001525 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 }
1527 }
1528 break;
1529
1530 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001531 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001532 break;
1533
1534 case VERTEX_ATTRIB_TEXCOORD:
1535 {
Brian8fa6f732007-02-01 09:24:41 -07001536 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001537 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001538 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001539 }
1540 break;
1541
Jouk Jansen40322e12004-04-05 08:50:36 +00001542 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001543 /* Not supported at this time */
1544 {
1545 const char *msg = "ARB_palette_matrix not supported";
1546 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001547 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001548 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001549 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001550
1551 case VERTEX_ATTRIB_GENERIC:
1552 {
1553 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001554 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001555 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001556 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001557 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1558 * attributes do not alias the conventional vertex
1559 * attributes.
1560 */
1561 if (attrib > 0)
1562 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001563 else
1564 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001565 }
1566 }
1567 break;
1568
1569 default:
1570 err = 1;
1571 break;
1572 }
1573 }
1574
Jouk Jansen40322e12004-04-05 08:50:36 +00001575 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001576 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001577 }
1578
Brian Paulde997602005-11-12 17:53:14 +00001579 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001580
1581 return err;
1582}
1583
Brian Paul7aebaf32005-10-30 21:23:23 +00001584
Jouk Jansen40322e12004-04-05 08:50:36 +00001585/**
1586 * This translates between a binary token for an output variable type
1587 * and the mesa token for the same thing.
1588 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001589 * \param inst The parsed tokens
1590 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001591 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001592 */
1593static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001594parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001595 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001596{
Brian Paul7aebaf32005-10-30 21:23:23 +00001597 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001598
Brian Paul7aebaf32005-10-30 21:23:23 +00001599 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001600 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001601 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001602 GLuint out_color;
1603
Brian Paulbe76b7f2004-10-04 14:40:05 +00001604 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001605 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001606 */
1607 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001608 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001609 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001610 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001611 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001612 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1613 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001614 }
1615 break;
1616
1617 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001618 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001619 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001620 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001622 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001623 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 GLint color_type;
1625 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001626 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1627 if (err)
1628 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001629
Jouk Jansen40322e12004-04-05 08:50:36 +00001630 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001631 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001632 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001633 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001634 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001635 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001636 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001637 }
1638 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001639 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001640 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001642 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001643 }
1644 /* primary color */
1645 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001646 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001647 }
1648 }
1649 }
1650 break;
1651
1652 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001653 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001654 break;
1655
1656 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001657 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001658 break;
1659
1660 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001661 {
1662 GLuint unit;
1663 if (parse_texcoord_num (ctx, inst, Program, &unit))
1664 return 1;
1665 *outputReg = VERT_RESULT_TEX0 + unit;
1666 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001667 break;
1668 }
1669
Brian Paulde997602005-11-12 17:53:14 +00001670 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001671
1672 return 0;
1673}
1674
Brian Paul7aebaf32005-10-30 21:23:23 +00001675
Jouk Jansen40322e12004-04-05 08:50:36 +00001676/**
1677 * This handles the declaration of ATTRIB variables
1678 *
1679 * XXX: Still needs
1680 * parse_vert_attrib_binding(), or something like that
1681 *
1682 * \return 0 on sucess, 1 on error
1683 */
1684static GLint
Brian Paula088f162006-09-05 23:08:51 +00001685parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001686 struct arb_program *Program)
1687{
1688 GLuint found;
1689 char *error_msg;
1690 struct var_cache *attrib_var;
1691
1692 attrib_var = parse_string (inst, vc_head, Program, &found);
1693 Program->Position = parse_position (inst);
1694 if (found) {
1695 error_msg = (char *)
1696 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1697 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1698 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001699 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001700 _mesa_free (error_msg);
1701 return 1;
1702 }
1703
1704 attrib_var->type = vt_attrib;
1705
Brian Paul7aebaf32005-10-30 21:23:23 +00001706 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001707 &attrib_var->attrib_is_generic))
1708 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001709
Brian Paul7aebaf32005-10-30 21:23:23 +00001710 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001711 program_error(ctx, Program->Position,
1712 "Cannot use both a generic vertex attribute "
1713 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001714 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001715 }
1716
1717 Program->Base.NumAttributes++;
1718 return 0;
1719}
1720
1721/**
1722 * \param use -- TRUE if we're called when declaring implicit parameters,
1723 * FALSE if we're declaraing variables. This has to do with
1724 * if we get a signed or unsigned float for scalar constants
1725 */
1726static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001727parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001728 struct var_cache *param_var,
1729 struct arb_program *Program, GLboolean use)
1730{
1731 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001732 GLuint err = 0;
Brianaa9d22a2007-02-23 11:21:03 -07001733 gl_state_index state_tokens[STATE_LENGTH];
Jouk Jansen40322e12004-04-05 08:50:36 +00001734 GLfloat const_values[4];
1735
Jouk Jansen40322e12004-04-05 08:50:36 +00001736 switch (*(*inst)++) {
1737 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001738 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1739 return 1;
1740
1741 /* If we adding STATE_MATRIX that has multiple rows, we need to
1742 * unroll it and call _mesa_add_state_reference() for each row
1743 */
Brian65319522007-02-21 11:08:21 -07001744 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1745 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1746 state_tokens[0] == STATE_MVP_MATRIX ||
1747 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1748 state_tokens[0] == STATE_PROGRAM_MATRIX)
1749 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001750 GLint row;
Brian65319522007-02-21 11:08:21 -07001751 const GLint first_row = state_tokens[2];
1752 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001753
1754 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001755 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001756
Brian Paulde997602005-11-12 17:53:14 +00001757 idx = _mesa_add_state_reference(Program->Base.Parameters,
1758 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001759 if (param_var->param_binding_begin == ~0U)
1760 param_var->param_binding_begin = idx;
1761 param_var->param_binding_length++;
1762 Program->Base.NumParameters++;
1763 }
1764 }
1765 else {
Brian Paulde997602005-11-12 17:53:14 +00001766 idx = _mesa_add_state_reference(Program->Base.Parameters,
1767 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001768 if (param_var->param_binding_begin == ~0U)
1769 param_var->param_binding_begin = idx;
1770 param_var->param_binding_length++;
1771 Program->Base.NumParameters++;
1772 }
1773 break;
1774
1775 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001776 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1777 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001778 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001779 if (param_var->param_binding_begin == ~0U)
1780 param_var->param_binding_begin = idx;
1781 param_var->param_binding_length++;
1782 Program->Base.NumParameters++;
1783
1784 /* Check if there is more: 0 -> we're done, else its an integer */
1785 if (**inst) {
1786 GLuint out_of_range, new_idx;
1787 GLuint start_idx = state_tokens[2] + 1;
1788 GLuint end_idx = parse_integer (inst, Program);
1789
1790 out_of_range = 0;
1791 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1792 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001793 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001794 || ((state_tokens[1] == STATE_LOCAL)
1795 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001796 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001797 out_of_range = 1;
1798 }
1799 else {
1800 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001801 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001802 || ((state_tokens[1] == STATE_LOCAL)
1803 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001804 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001805 out_of_range = 1;
1806 }
1807 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001808 program_error(ctx, Program->Position,
1809 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001810 return 1;
1811 }
1812
1813 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1814 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001815 idx = _mesa_add_state_reference(Program->Base.Parameters,
1816 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001817 param_var->param_binding_length++;
1818 Program->Base.NumParameters++;
1819 }
1820 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001821 else {
1822 (*inst)++;
1823 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001824 break;
1825
1826 case PARAM_CONSTANT:
1827 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001828 idx = _mesa_add_named_constant(Program->Base.Parameters,
1829 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001830 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001831 if (param_var->param_binding_begin == ~0U)
1832 param_var->param_binding_begin = idx;
1833 param_var->param_binding_length++;
1834 Program->Base.NumParameters++;
1835 break;
1836
1837 default:
Brian Paula088f162006-09-05 23:08:51 +00001838 program_error(ctx, Program->Position,
1839 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001840 return 1;
1841 }
1842
1843 /* Make sure we haven't blown past our parameter limits */
1844 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1845 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001846 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001847 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1848 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001849 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001850 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001851 return 1;
1852 }
1853
1854 return err;
1855}
1856
Brian Paul7aebaf32005-10-30 21:23:23 +00001857
Jouk Jansen40322e12004-04-05 08:50:36 +00001858/**
1859 * This picks out PARAM program parameter bindings.
1860 *
1861 * XXX: This needs to be stressed & tested
1862 *
1863 * \return 0 on sucess, 1 on error
1864 */
1865static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001866parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001867 struct arb_program *Program)
1868{
Brian Paulbd997cd2004-07-20 21:12:56 +00001869 GLuint found, err;
1870 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001871 struct var_cache *param_var;
1872
1873 err = 0;
1874 param_var = parse_string (inst, vc_head, Program, &found);
1875 Program->Position = parse_position (inst);
1876
1877 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001878 char *error_msg = (char *)
1879 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001880 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1881 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001882 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001883 _mesa_free (error_msg);
1884 return 1;
1885 }
1886
1887 specified_length = parse_integer (inst, Program);
1888
1889 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001890 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001891 return 1;
1892 }
1893
1894 param_var->type = vt_param;
1895 param_var->param_binding_length = 0;
1896
1897 /* Right now, everything is shoved into the main state register file.
1898 *
1899 * In the future, it would be nice to leave things ENV/LOCAL params
1900 * in their respective register files, if possible
1901 */
1902 param_var->param_binding_type = PROGRAM_STATE_VAR;
1903
1904 /* Remember to:
1905 * * - add each guy to the parameter list
1906 * * - increment the param_var->param_binding_len
1907 * * - store the param_var->param_binding_begin for the first one
1908 * * - compare the actual len to the specified len at the end
1909 */
1910 while (**inst != PARAM_NULL) {
1911 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1912 return 1;
1913 }
1914
1915 /* Test array length here! */
1916 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001917 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001918 program_error(ctx, Program->Position,
1919 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001920 }
1921 }
1922
1923 (*inst)++;
1924
1925 return 0;
1926}
1927
1928/**
1929 *
1930 */
1931static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001932parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001933 struct arb_program *Program, struct var_cache **new_var)
1934{
1935 struct var_cache *param_var;
1936
1937 /* First, insert a dummy entry into the var_cache */
1938 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001939 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001940 param_var->type = vt_param;
1941
1942 param_var->param_binding_length = 0;
1943 /* Don't fill in binding_begin; We use the default value of -1
1944 * to tell if its already initialized, elsewhere.
1945 *
1946 * param_var->param_binding_begin = 0;
1947 */
1948 param_var->param_binding_type = PROGRAM_STATE_VAR;
1949
1950 var_cache_append (vc_head, param_var);
1951
1952 /* Then fill it with juicy parameter goodness */
1953 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1954 return 1;
1955
1956 *new_var = param_var;
1957
1958 return 0;
1959}
1960
1961
1962/**
1963 * This handles the declaration of TEMP variables
1964 *
1965 * \return 0 on sucess, 1 on error
1966 */
1967static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001968parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001969 struct arb_program *Program)
1970{
1971 GLuint found;
1972 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001973
1974 while (**inst != 0) {
1975 temp_var = parse_string (inst, vc_head, Program, &found);
1976 Program->Position = parse_position (inst);
1977 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001978 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001979 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1980 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1981 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001982 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001983 _mesa_free (error_msg);
1984 return 1;
1985 }
1986
1987 temp_var->type = vt_temp;
1988
1989 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1990 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001991 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001992 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1993 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001994 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00001995 program_error(ctx, Program->Position,
1996 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00001997 return 1;
1998 }
1999
2000 temp_var->temp_binding = Program->Base.NumTemporaries;
2001 Program->Base.NumTemporaries++;
2002 }
2003 (*inst)++;
2004
2005 return 0;
2006}
2007
2008/**
2009 * This handles variables of the OUTPUT variety
2010 *
2011 * \return 0 on sucess, 1 on error
2012 */
2013static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002014parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002015 struct arb_program *Program)
2016{
2017 GLuint found;
2018 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002019 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002020
2021 output_var = parse_string (inst, vc_head, Program, &found);
2022 Program->Position = parse_position (inst);
2023 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002024 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002025 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2026 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2027 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002028 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002029 _mesa_free (error_msg);
2030 return 1;
2031 }
2032
2033 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002034
2035 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2036 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002037}
2038
2039/**
2040 * This handles variables of the ALIAS kind
2041 *
2042 * \return 0 on sucess, 1 on error
2043 */
2044static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002045parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002046 struct arb_program *Program)
2047{
2048 GLuint found;
2049 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002050
2051 temp_var = parse_string (inst, vc_head, Program, &found);
2052 Program->Position = parse_position (inst);
2053
2054 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002055 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002056 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2057 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2058 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002059 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002060 _mesa_free (error_msg);
2061 return 1;
2062 }
2063
2064 temp_var->type = vt_alias;
2065 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2066 Program->Position = parse_position (inst);
2067
2068 if (!found)
2069 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002070 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002071 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2072 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2073 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002074 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002075 _mesa_free (error_msg);
2076 return 1;
2077 }
2078
2079 return 0;
2080}
2081
2082/**
2083 * This handles variables of the ADDRESS kind
2084 *
2085 * \return 0 on sucess, 1 on error
2086 */
2087static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002088parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002089 struct arb_program *Program)
2090{
2091 GLuint found;
2092 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002093
2094 while (**inst != 0) {
2095 temp_var = parse_string (inst, vc_head, Program, &found);
2096 Program->Position = parse_position (inst);
2097 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002098 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002099 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2100 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2101 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002102 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002103 _mesa_free (error_msg);
2104 return 1;
2105 }
2106
2107 temp_var->type = vt_address;
2108
2109 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002110 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002111 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002112 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002113 return 1;
2114 }
2115
2116 temp_var->address_binding = Program->Base.NumAddressRegs;
2117 Program->Base.NumAddressRegs++;
2118 }
2119 (*inst)++;
2120
2121 return 0;
2122}
2123
2124/**
2125 * Parse a program declaration
2126 *
2127 * \return 0 on sucess, 1 on error
2128 */
2129static GLint
Brian Paula088f162006-09-05 23:08:51 +00002130parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002131 struct arb_program *Program)
2132{
2133 GLint err = 0;
2134
2135 switch (*(*inst)++) {
2136 case ADDRESS:
2137 err = parse_address (ctx, inst, vc_head, Program);
2138 break;
2139
2140 case ALIAS:
2141 err = parse_alias (ctx, inst, vc_head, Program);
2142 break;
2143
2144 case ATTRIB:
2145 err = parse_attrib (ctx, inst, vc_head, Program);
2146 break;
2147
2148 case OUTPUT:
2149 err = parse_output (ctx, inst, vc_head, Program);
2150 break;
2151
2152 case PARAM:
2153 err = parse_param (ctx, inst, vc_head, Program);
2154 break;
2155
2156 case TEMP:
2157 err = parse_temp (ctx, inst, vc_head, Program);
2158 break;
2159 }
2160
2161 return err;
2162}
2163
2164/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002165 * Handle the parsing out of a masked destination register, either for a
2166 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002167 *
2168 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002169 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002170 * position invariant
2171 *
2172 * \param File - The register file we write to
2173 * \param Index - The register index we write to
2174 * \param WriteMask - The mask controlling which components we write (1->write)
2175 *
2176 * \return 0 on sucess, 1 on error
2177 */
2178static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002179parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002180 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002181 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002182{
Brian Paul7aebaf32005-10-30 21:23:23 +00002183 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002184 struct var_cache *dst;
2185
2186 /* We either have a result register specified, or a
2187 * variable that may or may not be writable
2188 */
2189 switch (*(*inst)++) {
2190 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002191 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002192 return 1;
2193 *File = PROGRAM_OUTPUT;
2194 break;
2195
2196 case REGISTER_ESTABLISHED_NAME:
2197 dst = parse_string (inst, vc_head, Program, &result);
2198 Program->Position = parse_position (inst);
2199
2200 /* If the name has never been added to our symbol table, we're hosed */
2201 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002202 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002203 return 1;
2204 }
2205
2206 switch (dst->type) {
2207 case vt_output:
2208 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002209 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002210 break;
2211
2212 case vt_temp:
2213 *File = PROGRAM_TEMPORARY;
2214 *Index = dst->temp_binding;
2215 break;
2216
2217 /* If the var type is not vt_output or vt_temp, no go */
2218 default:
Brian Paula088f162006-09-05 23:08:51 +00002219 program_error(ctx, Program->Position,
2220 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002221 return 1;
2222 }
2223 break;
2224
2225 default:
Brian Paula088f162006-09-05 23:08:51 +00002226 program_error(ctx, Program->Position,
2227 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002228 return 1;
2229 }
2230
2231
2232 /* Position invariance test */
2233 /* This test is done now in syntax portion - when position invariance OPTION
2234 is specified, "result.position" rule is disabled so there is no way
2235 to write the position
2236 */
2237 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2238 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002239 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002240 "Vertex program specified position invariance and wrote vertex position");
2241 }*/
2242
2243 /* And then the mask.
2244 * w,a -> bit 0
2245 * z,b -> bit 1
2246 * y,g -> bit 2
2247 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002248 *
2249 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002250 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002251 tmp = (GLint) *(*inst)++;
2252 *WriteMask = (((tmp>>3) & 0x1) |
2253 ((tmp>>1) & 0x2) |
2254 ((tmp<<1) & 0x4) |
2255 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002256
2257 return 0;
2258}
2259
2260
2261/**
2262 * Handle the parsing of a address register
2263 *
2264 * \param Index - The register index we write to
2265 *
2266 * \return 0 on sucess, 1 on error
2267 */
2268static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002269parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002270 struct var_cache **vc_head,
2271 struct arb_program *Program, GLint * Index)
2272{
2273 struct var_cache *dst;
2274 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002275
2276 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002277
2278 dst = parse_string (inst, vc_head, Program, &result);
2279 Program->Position = parse_position (inst);
2280
2281 /* If the name has never been added to our symbol table, we're hosed */
2282 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002283 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002284 return 1;
2285 }
2286
2287 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002288 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002289 return 1;
2290 }
2291
2292 return 0;
2293}
2294
Brian Paul8e8fa632005-07-01 02:03:33 +00002295#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002296/**
2297 * Handle the parsing out of a masked address register
2298 *
2299 * \param Index - The register index we write to
2300 * \param WriteMask - The mask controlling which components we write (1->write)
2301 *
2302 * \return 0 on sucess, 1 on error
2303 */
2304static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002305parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002306 struct var_cache **vc_head,
2307 struct arb_program *Program, GLint * Index,
2308 GLboolean * WriteMask)
2309{
2310 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2311 return 1;
2312
2313 /* This should be 0x8 */
2314 (*inst)++;
2315
2316 /* Writemask of .x is implied */
2317 WriteMask[0] = 1;
2318 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2319
2320 return 0;
2321}
Brian Paul8e8fa632005-07-01 02:03:33 +00002322#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002323
2324/**
2325 * Parse out a swizzle mask.
2326 *
Brian Paul32df89e2005-10-29 18:26:43 +00002327 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002328 *
2329 * The len parameter allows us to grab 4 components for a vector
2330 * swizzle, or just 1 component for a scalar src register selection
2331 */
Brian Paul32df89e2005-10-29 18:26:43 +00002332static void
Brian Paula088f162006-09-05 23:08:51 +00002333parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002334{
Brian Paul32df89e2005-10-29 18:26:43 +00002335 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002336
Brian Paul32df89e2005-10-29 18:26:43 +00002337 for (i = 0; i < 4; i++)
2338 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002339
Brian Paul32df89e2005-10-29 18:26:43 +00002340 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002341 switch (*(*inst)++) {
2342 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002343 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002344 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002345 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002346 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002347 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002348 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002349 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002350 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002351 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002352 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002353 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002354 default:
2355 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2356 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002357 }
2358 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002359}
2360
Jouk Jansen40322e12004-04-05 08:50:36 +00002361
Brian Paul32df89e2005-10-29 18:26:43 +00002362/**
2363 * Parse an extended swizzle mask which is a sequence of
2364 * four x/y/z/w/0/1 tokens.
2365 * \return swizzle four swizzle values
2366 * \return negateMask four element bitfield
2367 */
2368static void
Brian Paula088f162006-09-05 23:08:51 +00002369parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002370 GLubyte *negateMask)
2371{
2372 GLint i;
2373
2374 *negateMask = 0x0;
2375 for (i = 0; i < 4; i++) {
2376 GLubyte swz;
2377 if (parse_sign(inst) == -1)
2378 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002379
2380 swz = *(*inst)++;
2381
2382 switch (swz) {
2383 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002384 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002385 break;
2386 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002387 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002388 break;
2389 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002390 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002391 break;
2392 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002393 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002394 break;
2395 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002396 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002397 break;
2398 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002399 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002400 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002401 default:
2402 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2403 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002404 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002405 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002406}
2407
2408
2409static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002410parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2411 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002412 struct arb_program *Program,
2413 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002414 GLboolean *IsRelOffset )
2415{
2416 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002417 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002418 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002419
Keith Whitwell7c26b612005-04-21 14:46:57 +00002420 *IsRelOffset = 0;
2421
Jouk Jansen40322e12004-04-05 08:50:36 +00002422 /* And the binding for the src */
2423 switch (*(*inst)++) {
2424 case REGISTER_ATTRIB:
2425 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002426 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002427 return 1;
2428 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002429 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002430
2431 /* We need to insert a dummy variable into the var_cache so we can
2432 * catch generic vertex attrib aliasing errors
2433 */
2434 var_cache_create(&src);
2435 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002436 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002437 src->attrib_binding = binding;
2438 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002439 var_cache_append(vc_head, src);
2440 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002441 program_error(ctx, Program->Position,
2442 "Cannot use both a generic vertex attribute "
2443 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002444 return 1;
2445 }
2446 break;
2447
2448 case REGISTER_PARAM:
2449 switch (**inst) {
2450 case PARAM_ARRAY_ELEMENT:
2451 (*inst)++;
2452 src = parse_string (inst, vc_head, Program, &found);
2453 Program->Position = parse_position (inst);
2454
2455 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002456 program_error(ctx, Program->Position,
2457 "2: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002458 return 1;
2459 }
2460
Brian Paul95801792005-12-06 15:41:43 +00002461 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002462
2463 switch (*(*inst)++) {
2464 case ARRAY_INDEX_ABSOLUTE:
2465 offset = parse_integer (inst, Program);
2466
2467 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002468 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002469 program_error(ctx, Program->Position,
2470 "Index out of range");
2471 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002472 return 1;
2473 }
2474
2475 *Index = src->param_binding_begin + offset;
2476 break;
2477
2478 case ARRAY_INDEX_RELATIVE:
2479 {
2480 GLint addr_reg_idx, rel_off;
2481
2482 /* First, grab the address regiseter */
2483 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2484 return 1;
2485
2486 /* And the .x */
2487 ((*inst)++);
2488 ((*inst)++);
2489 ((*inst)++);
2490 ((*inst)++);
2491
2492 /* Then the relative offset */
2493 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2494
2495 /* And store it properly */
2496 *Index = src->param_binding_begin + rel_off;
2497 *IsRelOffset = 1;
2498 }
2499 break;
2500 }
2501 break;
2502
2503 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002504 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2505 return 1;
2506
Brian Paul95801792005-12-06 15:41:43 +00002507 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002508 *Index = src->param_binding_begin;
2509 break;
2510 }
2511 break;
2512
2513 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002514 src = parse_string (inst, vc_head, Program, &found);
2515 Program->Position = parse_position (inst);
2516
2517 /* If the name has never been added to our symbol table, we're hosed */
2518 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002519 program_error(ctx, Program->Position,
2520 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002521 return 1;
2522 }
2523
2524 switch (src->type) {
2525 case vt_attrib:
2526 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002527 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002528 break;
2529
2530 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2531 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002532 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002533 *Index = src->param_binding_begin;
2534 break;
2535
2536 case vt_temp:
2537 *File = PROGRAM_TEMPORARY;
2538 *Index = src->temp_binding;
2539 break;
2540
2541 /* If the var type is vt_output no go */
2542 default:
Brian Paula088f162006-09-05 23:08:51 +00002543 program_error(ctx, Program->Position,
2544 "destination register is read only");
2545 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002546 return 1;
2547 }
2548 break;
2549
2550 default:
Brian Paula088f162006-09-05 23:08:51 +00002551 program_error(ctx, Program->Position,
2552 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002553 return 1;
2554 }
2555
2556 return 0;
2557}
2558
2559/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002560 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002561 */
2562static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002563parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002564 struct var_cache **vc_head,
2565 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002566 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002567{
Brian Paul7aebaf32005-10-30 21:23:23 +00002568 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002569 GLint index;
2570 GLboolean negate;
2571 GLubyte swizzle[4];
2572 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002573
Jouk Jansen40322e12004-04-05 08:50:36 +00002574 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002575 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002576
2577 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002578 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002579 return 1;
2580
2581 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002582 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002583
Brian Paul32df89e2005-10-29 18:26:43 +00002584 reg->File = file;
2585 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002586 reg->NegateBase = negate;
2587 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002588 return 0;
2589}
2590
Jouk Jansen40322e12004-04-05 08:50:36 +00002591
Brian Paulb7fc1c32006-08-30 23:38:03 +00002592/**
2593 * Parse fragment program destination register.
2594 * \return 1 if error, 0 if no error.
2595 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002596static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002597parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002598 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002599 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002600{
Brian Paul7aebaf32005-10-30 21:23:23 +00002601 GLint mask;
2602 GLuint idx;
2603 enum register_file file;
2604
Keith Whitwell7c26b612005-04-21 14:46:57 +00002605 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002606 return 1;
2607
Keith Whitwell7c26b612005-04-21 14:46:57 +00002608 reg->File = file;
2609 reg->Index = idx;
2610 reg->WriteMask = mask;
2611 return 0;
2612}
2613
2614
Brian Paul7aebaf32005-10-30 21:23:23 +00002615/**
2616 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002617 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002618 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002619static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002620parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002621 struct var_cache **vc_head,
2622 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002623 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002624{
Brian Paul7aebaf32005-10-30 21:23:23 +00002625 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002626 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002627 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002628 GLubyte Swizzle[4];
2629 GLboolean IsRelOffset;
2630
2631 /* Grab the sign */
2632 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2633
2634 /* And the src reg */
2635 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2636 return 1;
2637
2638 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002639 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002640
Keith Whitwell7c26b612005-04-21 14:46:57 +00002641 reg->File = File;
2642 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002643 reg->NegateBase = Negate;
2644 reg->Swizzle = (Swizzle[0] << 0);
2645
Jouk Jansen40322e12004-04-05 08:50:36 +00002646 return 0;
2647}
2648
Keith Whitwell7c26b612005-04-21 14:46:57 +00002649
Jouk Jansen40322e12004-04-05 08:50:36 +00002650/**
2651 * This is a big mother that handles getting opcodes into the instruction
2652 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002653 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002654 */
2655static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002656parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002657 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002658 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002659{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002660 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002661 GLuint texcoord;
2662 GLubyte instClass, type, code;
2663 GLboolean rel;
2664
Brian Pauld6272e02006-10-29 18:03:16 +00002665 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002666
2667 /* Record the position in the program string for debugging */
2668 fp->StringPos = Program->Position;
2669
2670 /* OP_ALU_INST or OP_TEX_INST */
2671 instClass = *(*inst)++;
2672
2673 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2674 * OP_TEX_{SAMPLE, KIL}
2675 */
2676 type = *(*inst)++;
2677
2678 /* The actual opcode name */
2679 code = *(*inst)++;
2680
2681 /* Increment the correct count */
2682 switch (instClass) {
2683 case OP_ALU_INST:
2684 Program->NumAluInstructions++;
2685 break;
2686 case OP_TEX_INST:
2687 Program->NumTexInstructions++;
2688 break;
2689 }
2690
Jouk Jansen40322e12004-04-05 08:50:36 +00002691 switch (type) {
2692 case OP_ALU_VECTOR:
2693 switch (code) {
2694 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002695 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002696 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002697 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002698 break;
2699
2700 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002701 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002702 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002703 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002704 break;
2705
2706 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002707 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002708 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002709 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002710 break;
2711
2712 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002713 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002714 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002715 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002716 break;
2717
2718 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002719 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002720 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002721 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002722 break;
2723 }
2724
Keith Whitwell7c26b612005-04-21 14:46:57 +00002725 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002726 return 1;
2727
Keith Whitwell7c26b612005-04-21 14:46:57 +00002728 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002729 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002730 break;
2731
2732 case OP_ALU_SCALAR:
2733 switch (code) {
2734 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002735 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002736 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002737 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002738 break;
2739
2740 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002741 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002742 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002743 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002744 break;
2745
2746 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002747 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002748 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002749 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002750 break;
2751
2752 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002753 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002755 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002756 break;
2757
2758 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002759 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002761 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002762 break;
2763
2764 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002765 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002766 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002767 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002768 break;
2769
2770 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002771 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002772 case OP_SCS:
2773
Brian Paul7e807512005-11-05 17:10:45 +00002774 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 break;
2776 }
2777
Keith Whitwell7c26b612005-04-21 14:46:57 +00002778 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002779 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002780
2781 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 break;
2784
2785 case OP_ALU_BINSC:
2786 switch (code) {
2787 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002788 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002789 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002790 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 break;
2792 }
2793
Keith Whitwell7c26b612005-04-21 14:46:57 +00002794 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002795 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002796
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002798 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002799 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 }
2801 break;
2802
2803
2804 case OP_ALU_BIN:
2805 switch (code) {
2806 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002807 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002808 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002809 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002810 break;
2811
2812 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002813 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002814 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002815 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002816 break;
2817
2818 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002819 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002820 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002821 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002822 break;
2823
2824 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002825 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002826 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002827 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002828 break;
2829
2830 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002831 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002832 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002833 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002834 break;
2835
2836 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002837 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002838 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002839 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002840 break;
2841
2842 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002843 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002845 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002846 break;
2847
2848 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002849 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002850 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002851 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002852 break;
2853
2854 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002855 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002856 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002857 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002858 break;
2859
2860 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002861 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002862 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002863 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002864 break;
2865
2866 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002867 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002868 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002869 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002870 break;
2871
2872 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002873 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002874 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002875 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002876 break;
2877 }
2878
Keith Whitwell7c26b612005-04-21 14:46:57 +00002879 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002880 return 1;
2881 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002882 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2883 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002884 }
2885 break;
2886
2887 case OP_ALU_TRI:
2888 switch (code) {
2889 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002890 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002892 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 break;
2894
2895 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002896 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002898 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 break;
2900
2901 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002902 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002903 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002904 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002905 break;
2906 }
2907
Keith Whitwell7c26b612005-04-21 14:46:57 +00002908 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002909 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002910
Jouk Jansen40322e12004-04-05 08:50:36 +00002911 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002912 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2913 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 }
2915 break;
2916
2917 case OP_ALU_SWZ:
2918 switch (code) {
2919 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002920 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002921 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002922 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002923 break;
2924 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002925 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002926 return 1;
2927
Keith Whitwell7c26b612005-04-21 14:46:57 +00002928 {
Brian Paul32df89e2005-10-29 18:26:43 +00002929 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002930 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002931 enum register_file file;
2932 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002933
Brian Paul32df89e2005-10-29 18:26:43 +00002934 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002935 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002936 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2937 fp->SrcReg[0].File = file;
2938 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002939 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002940 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2941 swizzle[1],
2942 swizzle[2],
2943 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002944 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002945 break;
2946
2947 case OP_TEX_SAMPLE:
2948 switch (code) {
2949 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002950 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002951 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002952 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002953 break;
2954
2955 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002956 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002957 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002958 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002959 break;
2960
2961 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002962 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002963 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002964 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002965 break;
2966 }
2967
Keith Whitwell7c26b612005-04-21 14:46:57 +00002968 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002969 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002970
2971 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002972 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002973
2974 /* texImageUnit */
2975 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2976 return 1;
2977 fp->TexSrcUnit = texcoord;
2978
2979 /* texTarget */
2980 switch (*(*inst)++) {
2981 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00002982 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002983 break;
2984 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00002985 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002986 break;
2987 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00002988 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002989 break;
2990 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00002991 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002992 break;
2993 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00002994 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002995 break;
Ian Romanick69358e72007-06-05 09:24:27 -07002996 case TEXTARGET_SHADOW1D:
2997 case TEXTARGET_SHADOW2D:
2998 case TEXTARGET_SHADOW1D_ARRAY:
2999 case TEXTARGET_SHADOW2D_ARRAY:
3000 case TEXTARGET_SHADOWRECT:
3001 /* TODO ARB_fragment_program_shadow code */
3002 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003003 case TEXTARGET_1D_ARRAY:
3004 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3005 break;
3006 case TEXTARGET_2D_ARRAY:
3007 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3008 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 }
Brian Paulb7fc1c32006-08-30 23:38:03 +00003010 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3011 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3012 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003013 program_error(ctx, Program->Position,
3014 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003015 return 1;
3016 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003017 break;
3018
3019 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003020 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003021 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003022 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003023 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003024 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003025 default:
3026 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3027 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003028 }
3029
3030 return 0;
3031}
3032
Keith Whitwell7c26b612005-04-21 14:46:57 +00003033static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003034parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003035 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003036 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003037{
Brian Paul7aebaf32005-10-30 21:23:23 +00003038 GLint mask;
3039 GLuint idx;
3040 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003041
3042 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3043 return 1;
3044
3045 reg->File = file;
3046 reg->Index = idx;
3047 reg->WriteMask = mask;
3048 return 0;
3049}
3050
3051/**
3052 * Handle the parsing out of a masked address register
3053 *
3054 * \param Index - The register index we write to
3055 * \param WriteMask - The mask controlling which components we write (1->write)
3056 *
3057 * \return 0 on sucess, 1 on error
3058 */
3059static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003060parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003061 struct var_cache **vc_head,
3062 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003063 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003064{
3065 GLint idx;
3066
3067 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3068 return 1;
3069
3070 /* This should be 0x8 */
3071 (*inst)++;
3072
3073 reg->File = PROGRAM_ADDRESS;
3074 reg->Index = idx;
3075
3076 /* Writemask of .x is implied */
3077 reg->WriteMask = 0x1;
3078 return 0;
3079}
3080
3081/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003082 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003083 */
3084static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003085parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003086 struct var_cache **vc_head,
3087 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003088 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003089{
Brian Paul7aebaf32005-10-30 21:23:23 +00003090 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003091 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003092 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003093 GLubyte swizzle[4];
3094 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003095
3096 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003097 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003098
3099 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003100 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003101 return 1;
3102
3103 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003104 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003105
Brian Paul32df89e2005-10-29 18:26:43 +00003106 reg->File = file;
3107 reg->Index = index;
3108 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3109 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003110 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003111 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003112 return 0;
3113}
3114
3115
3116static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003117parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003118 struct var_cache **vc_head,
3119 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003120 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003121{
Brian Paul7aebaf32005-10-30 21:23:23 +00003122 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003123 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003124 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003125 GLubyte Swizzle[4];
3126 GLboolean IsRelOffset;
3127
3128 /* Grab the sign */
3129 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3130
3131 /* And the src reg */
3132 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3133 return 1;
3134
3135 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003136 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003137
3138 reg->File = File;
3139 reg->Index = Index;
3140 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003141 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003142 reg->RelAddr = IsRelOffset;
3143 return 0;
3144}
3145
3146
Jouk Jansen40322e12004-04-05 08:50:36 +00003147/**
3148 * This is a big mother that handles getting opcodes into the instruction
3149 * and handling the src & dst registers for vertex program instructions
3150 */
3151static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003152parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003153 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003154 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003155{
3156 GLint a;
3157 GLubyte type, code;
3158
3159 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3160 type = *(*inst)++;
3161
3162 /* The actual opcode name */
3163 code = *(*inst)++;
3164
Brian Pauld6272e02006-10-29 18:03:16 +00003165 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003166 /* Record the position in the program string for debugging */
3167 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003168
3169 switch (type) {
3170 /* XXX: */
3171 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003172 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003173
3174 /* Remember to set SrcReg.RelAddr; */
3175
3176 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003177 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003178 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003179
Jouk Jansen40322e12004-04-05 08:50:36 +00003180 vp->DstReg.File = PROGRAM_ADDRESS;
3181
3182 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003183 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003184 return 1;
3185
3186 break;
3187
3188 case OP_ALU_VECTOR:
3189 switch (code) {
3190 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003191 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003192 break;
3193 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003194 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003195 break;
3196 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003197 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003198 break;
3199 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003200 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003201 break;
3202 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003203 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003204 break;
3205 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003206
3207 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003208 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003209
3210 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003211 return 1;
3212 break;
3213
3214 case OP_ALU_SCALAR:
3215 switch (code) {
3216 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003217 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003218 break;
3219 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003220 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003221 break;
3222 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003223 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003224 break;
3225 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003226 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003227 break;
3228 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003229 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003230 break;
3231 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003232 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003233 break;
3234 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003235 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003236 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003237
3238 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003239 return 1;
3240 break;
3241
3242 case OP_ALU_BINSC:
3243 switch (code) {
3244 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003245 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003246 break;
3247 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003248 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003249 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003250
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003252 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003253 return 1;
3254 }
3255 break;
3256
3257 case OP_ALU_BIN:
3258 switch (code) {
3259 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003260 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003261 break;
3262 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003263 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003264 break;
3265 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003266 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003267 break;
3268 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003269 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003270 break;
3271 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003272 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003273 break;
3274 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003275 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 break;
3277 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003278 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003279 break;
3280 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003281 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003282 break;
3283 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003284 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003285 break;
3286 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003287 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 break;
3289 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003290 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003291 break;
3292 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003293 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 break;
3295 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003296 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003298
Jouk Jansen40322e12004-04-05 08:50:36 +00003299 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003300 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003301 return 1;
3302 }
3303 break;
3304
3305 case OP_ALU_TRI:
3306 switch (code) {
3307 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003308 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 break;
3310 }
3311
Keith Whitwell7c26b612005-04-21 14:46:57 +00003312 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003313 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003314
Jouk Jansen40322e12004-04-05 08:50:36 +00003315 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003316 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003317 return 1;
3318 }
3319 break;
3320
3321 case OP_ALU_SWZ:
3322 switch (code) {
3323 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003324 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003325 break;
3326 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003327 {
Brian Paul32df89e2005-10-29 18:26:43 +00003328 GLubyte swizzle[4];
3329 GLubyte negateMask;
3330 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003331 enum register_file file;
3332 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003333
Keith Whitwell7c26b612005-04-21 14:46:57 +00003334 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3335 return 1;
3336
Brian Paul32df89e2005-10-29 18:26:43 +00003337 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003338 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003339 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3340 vp->SrcReg[0].File = file;
3341 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003342 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003343 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3344 swizzle[1],
3345 swizzle[2],
3346 swizzle[3]);
3347 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003348 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003349 break;
3350 }
3351 return 0;
3352}
3353
3354#if DEBUG_PARSING
3355
3356static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003357debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3358 struct arb_program *Program)
3359{
3360 struct var_cache *vc;
3361 GLint a, b;
3362
Brianb618ac82007-02-22 09:39:25 -07003363 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003364
3365 /* First of all, print out the contents of the var_cache */
3366 vc = vc_head;
3367 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003368 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003369 switch (vc->type) {
3370 case vt_none:
3371 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3372 break;
3373 case vt_attrib:
3374 fprintf (stderr, "ATTRIB %s\n", vc->name);
3375 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3376 break;
3377 case vt_param:
3378 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3379 vc->param_binding_begin, vc->param_binding_length);
3380 b = vc->param_binding_begin;
3381 for (a = 0; a < vc->param_binding_length; a++) {
3382 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003383 Program->Base.Parameters->Parameters[a + b].Name);
3384 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3385 const char *s;
3386 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3387 [a + b].StateIndexes);
3388 fprintf(stderr, "%s\n", s);
3389 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003390 }
3391 else
3392 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003393 Program->Base.Parameters->ParameterValues[a + b][0],
3394 Program->Base.Parameters->ParameterValues[a + b][1],
3395 Program->Base.Parameters->ParameterValues[a + b][2],
3396 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003397 }
3398 break;
3399 case vt_temp:
3400 fprintf (stderr, "TEMP %s\n", vc->name);
3401 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3402 break;
3403 case vt_output:
3404 fprintf (stderr, "OUTPUT %s\n", vc->name);
3405 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3406 break;
3407 case vt_alias:
3408 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003409 fprintf (stderr, " binding: 0x%p (%s)\n",
3410 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003411 break;
Brianb618ac82007-02-22 09:39:25 -07003412 default:
3413 /* nothing */
3414 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003415 }
3416 vc = vc->next;
3417 }
3418}
3419
Brian Paul72030e02005-11-03 03:30:34 +00003420#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003421
3422
3423/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003424 * The main loop for parsing a fragment or vertex program
3425 *
Brian Paul72030e02005-11-03 03:30:34 +00003426 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003427 */
Brian Paul72030e02005-11-03 03:30:34 +00003428static GLint
Brian Paula088f162006-09-05 23:08:51 +00003429parse_instructions(GLcontext * ctx, const GLubyte * inst,
3430 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003431{
Brian Paul72030e02005-11-03 03:30:34 +00003432 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3433 ? ctx->Const.FragmentProgram.MaxInstructions
3434 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003435 GLint err = 0;
3436
Brian Paul72030e02005-11-03 03:30:34 +00003437 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3438
Jouk Jansen40322e12004-04-05 08:50:36 +00003439 Program->MajorVersion = (GLuint) * inst++;
3440 Program->MinorVersion = (GLuint) * inst++;
3441
3442 while (*inst != END) {
3443 switch (*inst++) {
3444
3445 case OPTION:
3446 switch (*inst++) {
3447 case ARB_PRECISION_HINT_FASTEST:
3448 Program->PrecisionOption = GL_FASTEST;
3449 break;
3450
3451 case ARB_PRECISION_HINT_NICEST:
3452 Program->PrecisionOption = GL_NICEST;
3453 break;
3454
3455 case ARB_FOG_EXP:
3456 Program->FogOption = GL_EXP;
3457 break;
3458
3459 case ARB_FOG_EXP2:
3460 Program->FogOption = GL_EXP2;
3461 break;
3462
3463 case ARB_FOG_LINEAR:
3464 Program->FogOption = GL_LINEAR;
3465 break;
3466
3467 case ARB_POSITION_INVARIANT:
3468 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003469 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003470 break;
3471
3472 case ARB_FRAGMENT_PROGRAM_SHADOW:
3473 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3474 /* TODO ARB_fragment_program_shadow code */
3475 }
3476 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003477
3478 case ARB_DRAW_BUFFERS:
3479 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3480 /* do nothing for now */
3481 }
3482 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003483
3484 case MESA_TEXTURE_ARRAY:
3485 /* do nothing for now */
3486 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003487 }
3488 break;
3489
3490 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003491 /* check length */
3492 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003493 program_error(ctx, Program->Position,
3494 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003495 return 1;
3496 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003497 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003498 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003499 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003500 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003501 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003502 }
3503 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003504 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003505 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003506 }
3507
Brian Paul72030e02005-11-03 03:30:34 +00003508 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003509 Program->Base.NumInstructions++;
3510 break;
3511
3512 case DECLARATION:
3513 err = parse_declaration (ctx, &inst, vc_head, Program);
3514 break;
3515
3516 default:
3517 break;
3518 }
3519
3520 if (err)
3521 break;
3522 }
3523
3524 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003525 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003526 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003527 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003528 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003529 /* YYY Wrong Position in program, whatever, at least not random -> crash
3530 Program->Position = parse_position (&inst);
3531 */
Brian Paul8c41a142005-11-19 15:36:28 +00003532 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003533 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003534 Program->Base.NumInstructions++;
3535
Brian Paul05051032005-11-01 04:36:33 +00003536 /*
3537 * Initialize native counts to logical counts. The device driver may
3538 * change them if program is translated into a hardware program.
3539 */
3540 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3541 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3542 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3543 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3544 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003545
Jouk Jansen40322e12004-04-05 08:50:36 +00003546 return err;
3547}
3548
Brian Paul7aebaf32005-10-30 21:23:23 +00003549
Jouk Jansen40322e12004-04-05 08:50:36 +00003550/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003551LONGSTRING static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003552#include "grammar_syn.h"
3553;
3554
Brian Paul7aebaf32005-10-30 21:23:23 +00003555
Brian Paul8c41a142005-11-19 15:36:28 +00003556/**
3557 * Set a grammar parameter.
3558 * \param name the grammar parameter
3559 * \param value the new parameter value
3560 * \return 0 if OK, 1 if error
3561 */
3562static int
3563set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003564{
3565 char error_msg[300];
3566 GLint error_pos;
3567
Brian Paul8c41a142005-11-19 15:36:28 +00003568 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003569 return 0;
3570
3571 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3572 _mesa_set_program_error (ctx, error_pos, error_msg);
3573 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3574 return 1;
3575}
3576
Brian Paul8c41a142005-11-19 15:36:28 +00003577
3578/**
3579 * Enable support for the given language option in the parser.
3580 * \return 1 if OK, 0 if error
3581 */
3582static int
3583enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003584{
Brian Paul8c41a142005-11-19 15:36:28 +00003585 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003586}
3587
Brian Paul8c41a142005-11-19 15:36:28 +00003588
3589/**
3590 * Enable parser extensions based on which OpenGL extensions are supported
3591 * by this rendering context.
3592 *
3593 * \return GL_TRUE if OK, GL_FALSE if error.
3594 */
3595static GLboolean
3596enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003597{
Brian Paul8c41a142005-11-19 15:36:28 +00003598#if 0
3599 /* These are not supported at this time */
3600 if ((ctx->Extensions.ARB_vertex_blend ||
3601 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003602 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003603 return GL_FALSE;
3604 if (ctx->Extensions.ARB_matrix_palette
3605 && !enable_ext(ctx, id, "matrix_palette"))
3606 return GL_FALSE;
3607 if (ctx->Extensions.ARB_fragment_program_shadow
3608 && !enable_ext(ctx, id, "fragment_program_shadow"))
3609 return GL_FALSE;
3610#endif
3611 if (ctx->Extensions.EXT_point_parameters
3612 && !enable_ext(ctx, id, "point_parameters"))
3613 return GL_FALSE;
3614 if (ctx->Extensions.EXT_secondary_color
3615 && !enable_ext(ctx, id, "secondary_color"))
3616 return GL_FALSE;
3617 if (ctx->Extensions.EXT_fog_coord
3618 && !enable_ext(ctx, id, "fog_coord"))
3619 return GL_FALSE;
3620 if (ctx->Extensions.NV_texture_rectangle
3621 && !enable_ext(ctx, id, "texture_rectangle"))
3622 return GL_FALSE;
3623 if (ctx->Extensions.ARB_draw_buffers
3624 && !enable_ext(ctx, id, "draw_buffers"))
3625 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003626 if (ctx->Extensions.MESA_texture_array
3627 && !enable_ext(ctx, id, "texture_array"))
3628 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003629#if 1
3630 /* hack for Warcraft (see bug 8060) */
3631 enable_ext(ctx, id, "vertex_blend");
3632#endif
3633
Brian Paul8c41a142005-11-19 15:36:28 +00003634 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003635}
3636
Brian Paul8c41a142005-11-19 15:36:28 +00003637
Jouk Jansen40322e12004-04-05 08:50:36 +00003638/**
3639 * This kicks everything off.
3640 *
3641 * \param ctx - The GL Context
3642 * \param str - The program string
3643 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003644 * \param program - The arb_program struct to return all the parsed info in
3645 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003646 */
Brian Paul8c41a142005-11-19 15:36:28 +00003647static GLboolean
3648_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3649 const GLubyte *str, GLsizei len,
3650 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003651{
3652 GLint a, err, error_pos;
3653 char error_msg[300];
3654 GLuint parsed_len;
3655 struct var_cache *vc_head;
3656 grammar arbprogram_syn_id;
3657 GLubyte *parsed, *inst;
3658 GLubyte *strz = NULL;
3659 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3660
Brian Paul8c41a142005-11-19 15:36:28 +00003661 /* set the program target before parsing */
3662 program->Base.Target = target;
3663
Brian Paul7f76b8f2004-09-10 01:05:39 +00003664 /* Reset error state */
3665 _mesa_set_program_error(ctx, -1, NULL);
3666
Brian Paul8c41a142005-11-19 15:36:28 +00003667 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003668 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003669 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003670 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003671 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003672
3673 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3674 if (grammar_syn_id == 0) {
3675 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003676 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003677 _mesa_set_program_error (ctx, error_pos, error_msg);
3678 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003679 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003680 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003681 }
3682
Brian Paul8c41a142005-11-19 15:36:28 +00003683 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3684 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003685
Brian Paul49a80ca2006-04-28 15:40:11 +00003686 /* 'parsed' is unused here */
3687 _mesa_free (parsed);
3688 parsed = NULL;
3689
Brian Paul45703642005-10-29 15:52:31 +00003690 /* NOTE: we can't destroy grammar_syn_id right here because
3691 * grammar_destroy() can reset the last error
3692 */
Brian Paul8c41a142005-11-19 15:36:28 +00003693 if (err) {
3694 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003695 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3696 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003697 _mesa_error (ctx, GL_INVALID_OPERATION,
3698 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003699 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003700 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003701 }
3702
3703 grammar_destroy (grammar_syn_id);
3704
3705 arbprogram_syn_is_ok = 1;
3706 }
3707
3708 /* create the grammar object */
3709 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3710 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003711 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003712 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3713 _mesa_set_program_error (ctx, error_pos, error_msg);
3714 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003715 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003716 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 }
3718
3719 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003720 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003721 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3722 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003723 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003724 }
3725
Brian Paul8c41a142005-11-19 15:36:28 +00003726 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3727 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003728 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003729 }
3730
3731 /* check for NULL character occurences */
3732 {
Brian Paul8c41a142005-11-19 15:36:28 +00003733 GLint i;
3734 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003735 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003736 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003737 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003738 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003739 }
Brian Paul8c41a142005-11-19 15:36:28 +00003740 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003741 }
3742
3743 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003744 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003745 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003746 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3747 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003748 return GL_FALSE;
3749 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003750 _mesa_memcpy (strz, str, len);
3751 strz[len] = '\0';
3752
Michal Krolb80bc052004-10-21 14:09:54 +00003753 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003754 err = !grammar_fast_check(arbprogram_syn_id, strz,
3755 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003756
3757 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003758 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003759 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3760 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003761
Brian Paulb3aefd12005-09-19 20:12:32 +00003762#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003763 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003764 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003765 int line, col;
3766 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003767 fprintf(stderr, "program: %s\n", (char *) strz);
3768 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003769 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3770 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003771 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3772 } while (0)
3773#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003774
Brian Paula088f162006-09-05 23:08:51 +00003775 _mesa_free(strz);
3776 _mesa_free(parsed);
3777
Jouk Jansen40322e12004-04-05 08:50:36 +00003778 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003779 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003780 }
3781
Jouk Jansen40322e12004-04-05 08:50:36 +00003782 grammar_destroy (arbprogram_syn_id);
3783
Brian Paul8c41a142005-11-19 15:36:28 +00003784 /*
3785 * Program string is syntactically correct at this point
3786 * Parse the tokenized version of the program now, generating
3787 * vertex/fragment program instructions.
3788 */
3789
Jouk Jansen40322e12004-04-05 08:50:36 +00003790 /* Initialize the arb_program struct */
3791 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003792 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003793 program->Base.NumInstructions =
3794 program->Base.NumTemporaries =
3795 program->Base.NumParameters =
3796 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003797 program->Base.Parameters = _mesa_new_parameter_list ();
3798 program->Base.InputsRead = 0x0;
3799 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003800 program->Position = 0;
3801 program->MajorVersion = program->MinorVersion = 0;
3802 program->PrecisionOption = GL_DONT_CARE;
3803 program->FogOption = GL_NONE;
3804 program->HintPositionInvariant = GL_FALSE;
3805 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003806 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003807 program->NumAluInstructions =
3808 program->NumTexInstructions =
3809 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003810 program->UsesKill = 0;
3811
Jouk Jansen40322e12004-04-05 08:50:36 +00003812 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003813 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003814
3815 /* Start examining the tokens in the array */
3816 inst = parsed;
3817
3818 /* Check the grammer rev */
3819 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003820 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003821 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003822 }
3823 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003824 /* ignore program target */
3825 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003826 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003827 }
3828
3829 /*debug_variables(ctx, vc_head, program); */
3830
3831 /* We're done with the parsed binary array */
3832 var_cache_destroy (&vc_head);
3833
3834 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003835
Brian Paul8c41a142005-11-19 15:36:28 +00003836 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3837 * to size [ap.Base.NumInstructions].
3838 */
Brian Paula7543902006-08-24 21:58:32 +00003839 program->Base.Instructions
3840 = _mesa_realloc_instructions(program->Base.Instructions,
3841 MAX_INSTRUCTIONS,
3842 program->Base.NumInstructions);
3843
Brian Paul45703642005-10-29 15:52:31 +00003844 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003845}
Brian Paul8c41a142005-11-19 15:36:28 +00003846
3847
3848
3849void
3850_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3851 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003852 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003853{
3854 struct arb_program ap;
3855 GLuint i;
3856
3857 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003858 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003859 /* Error in the program. Just return. */
3860 return;
3861 }
3862
3863 /* Copy the relevant contents of the arb_program struct into the
3864 * fragment_program struct.
3865 */
3866 program->Base.String = ap.Base.String;
3867 program->Base.NumInstructions = ap.Base.NumInstructions;
3868 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3869 program->Base.NumParameters = ap.Base.NumParameters;
3870 program->Base.NumAttributes = ap.Base.NumAttributes;
3871 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003872 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3873 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3874 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3875 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3876 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003877 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3878 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3879 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3880 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3881 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3882 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003883 program->Base.InputsRead = ap.Base.InputsRead;
3884 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3885 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
Brianc9db2232007-01-04 17:22:19 -07003886 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Brian Paul8c41a142005-11-19 15:36:28 +00003887 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003888 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003889
3890 if (program->Base.Instructions)
3891 _mesa_free(program->Base.Instructions);
3892 program->Base.Instructions = ap.Base.Instructions;
3893
3894 if (program->Base.Parameters)
3895 _mesa_free_parameter_list(program->Base.Parameters);
3896 program->Base.Parameters = ap.Base.Parameters;
3897
3898#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00003899 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
3900 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003901#endif
3902}
3903
3904
3905
3906/**
3907 * Parse the vertex program string. If success, update the given
3908 * vertex_program object with the new program. Else, leave the vertex_program
3909 * object unchanged.
3910 */
3911void
3912_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3913 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003914 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003915{
3916 struct arb_program ap;
3917
3918 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3919
Brian Paul95801792005-12-06 15:41:43 +00003920 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003921 /* Error in the program. Just return. */
3922 return;
3923 }
3924
3925 /* Copy the relevant contents of the arb_program struct into the
3926 * vertex_program struct.
3927 */
3928 program->Base.String = ap.Base.String;
3929 program->Base.NumInstructions = ap.Base.NumInstructions;
3930 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3931 program->Base.NumParameters = ap.Base.NumParameters;
3932 program->Base.NumAttributes = ap.Base.NumAttributes;
3933 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003934 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3935 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3936 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3937 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3938 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003939 program->Base.InputsRead = ap.Base.InputsRead;
3940 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3941 program->IsPositionInvariant = ap.HintPositionInvariant;
3942
3943 if (program->Base.Instructions)
3944 _mesa_free(program->Base.Instructions);
3945 program->Base.Instructions = ap.Base.Instructions;
3946
3947 if (program->Base.Parameters)
3948 _mesa_free_parameter_list(program->Base.Parameters);
3949 program->Base.Parameters = ap.Base.Parameters;
3950
3951#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01003952 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00003953 _mesa_print_program(&program->Base);
3954#endif
3955}