blob: 0ac35a576823eae94bb2f30b29f9dc170646baba [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*/
Michal Krolb80bc052004-10-21 14:09:54 +0000184#define REVISION 0x09
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
Jouk Jansen40322e12004-04-05 08:50:36 +0000212/* GL_ARB_fragment_program instruction class */
213#define OP_ALU_INST 0x00
214#define OP_TEX_INST 0x01
215
216/* GL_ARB_vertex_program instruction class */
217/* OP_ALU_INST */
218
219/* GL_ARB_fragment_program instruction type */
220#define OP_ALU_VECTOR 0x00
221#define OP_ALU_SCALAR 0x01
222#define OP_ALU_BINSC 0x02
223#define OP_ALU_BIN 0x03
224#define OP_ALU_TRI 0x04
225#define OP_ALU_SWZ 0x05
226#define OP_TEX_SAMPLE 0x06
227#define OP_TEX_KIL 0x07
228
229/* GL_ARB_vertex_program instruction type */
230#define OP_ALU_ARL 0x08
231/* OP_ALU_VECTOR */
232/* OP_ALU_SCALAR */
233/* OP_ALU_BINSC */
234/* OP_ALU_BIN */
235/* OP_ALU_TRI */
236/* OP_ALU_SWZ */
237
238/* GL_ARB_fragment_program instruction code */
239#define OP_ABS 0x00
240#define OP_ABS_SAT 0x1B
241#define OP_FLR 0x09
242#define OP_FLR_SAT 0x26
243#define OP_FRC 0x0A
244#define OP_FRC_SAT 0x27
245#define OP_LIT 0x0C
246#define OP_LIT_SAT 0x2A
247#define OP_MOV 0x11
248#define OP_MOV_SAT 0x30
249#define OP_COS 0x1F
250#define OP_COS_SAT 0x20
251#define OP_EX2 0x07
252#define OP_EX2_SAT 0x25
253#define OP_LG2 0x0B
254#define OP_LG2_SAT 0x29
255#define OP_RCP 0x14
256#define OP_RCP_SAT 0x33
257#define OP_RSQ 0x15
258#define OP_RSQ_SAT 0x34
259#define OP_SIN 0x38
260#define OP_SIN_SAT 0x39
261#define OP_SCS 0x35
262#define OP_SCS_SAT 0x36
263#define OP_POW 0x13
264#define OP_POW_SAT 0x32
265#define OP_ADD 0x01
266#define OP_ADD_SAT 0x1C
267#define OP_DP3 0x03
268#define OP_DP3_SAT 0x21
269#define OP_DP4 0x04
270#define OP_DP4_SAT 0x22
271#define OP_DPH 0x05
272#define OP_DPH_SAT 0x23
273#define OP_DST 0x06
274#define OP_DST_SAT 0x24
275#define OP_MAX 0x0F
276#define OP_MAX_SAT 0x2E
277#define OP_MIN 0x10
278#define OP_MIN_SAT 0x2F
279#define OP_MUL 0x12
280#define OP_MUL_SAT 0x31
281#define OP_SGE 0x16
282#define OP_SGE_SAT 0x37
283#define OP_SLT 0x17
284#define OP_SLT_SAT 0x3A
285#define OP_SUB 0x18
286#define OP_SUB_SAT 0x3B
287#define OP_XPD 0x1A
288#define OP_XPD_SAT 0x43
289#define OP_CMP 0x1D
290#define OP_CMP_SAT 0x1E
291#define OP_LRP 0x2B
292#define OP_LRP_SAT 0x2C
293#define OP_MAD 0x0E
294#define OP_MAD_SAT 0x2D
295#define OP_SWZ 0x19
296#define OP_SWZ_SAT 0x3C
297#define OP_TEX 0x3D
298#define OP_TEX_SAT 0x3E
299#define OP_TXB 0x3F
300#define OP_TXB_SAT 0x40
301#define OP_TXP 0x41
302#define OP_TXP_SAT 0x42
303#define OP_KIL 0x28
304
305/* GL_ARB_vertex_program instruction code */
306#define OP_ARL 0x02
307/* OP_ABS */
308/* OP_FLR */
309/* OP_FRC */
310/* OP_LIT */
311/* OP_MOV */
312/* OP_EX2 */
313#define OP_EXP 0x08
314/* OP_LG2 */
315#define OP_LOG 0x0D
316/* OP_RCP */
317/* OP_RSQ */
318/* OP_POW */
319/* OP_ADD */
320/* OP_DP3 */
321/* OP_DP4 */
322/* OP_DPH */
323/* OP_DST */
324/* OP_MAX */
325/* OP_MIN */
326/* OP_MUL */
327/* OP_SGE */
328/* OP_SLT */
329/* OP_SUB */
330/* OP_XPD */
331/* OP_MAD */
332/* OP_SWZ */
333
334/* fragment attribute binding */
335#define FRAGMENT_ATTRIB_COLOR 0x01
336#define FRAGMENT_ATTRIB_TEXCOORD 0x02
337#define FRAGMENT_ATTRIB_FOGCOORD 0x03
338#define FRAGMENT_ATTRIB_POSITION 0x04
339
340/* vertex attribute binding */
341#define VERTEX_ATTRIB_POSITION 0x01
342#define VERTEX_ATTRIB_WEIGHT 0x02
343#define VERTEX_ATTRIB_NORMAL 0x03
344#define VERTEX_ATTRIB_COLOR 0x04
345#define VERTEX_ATTRIB_FOGCOORD 0x05
346#define VERTEX_ATTRIB_TEXCOORD 0x06
347#define VERTEX_ATTRIB_MATRIXINDEX 0x07
348#define VERTEX_ATTRIB_GENERIC 0x08
349
350/* fragment result binding */
351#define FRAGMENT_RESULT_COLOR 0x01
352#define FRAGMENT_RESULT_DEPTH 0x02
353
354/* vertex result binding */
355#define VERTEX_RESULT_POSITION 0x01
356#define VERTEX_RESULT_COLOR 0x02
357#define VERTEX_RESULT_FOGCOORD 0x03
358#define VERTEX_RESULT_POINTSIZE 0x04
359#define VERTEX_RESULT_TEXCOORD 0x05
360
361/* texture target */
362#define TEXTARGET_1D 0x01
363#define TEXTARGET_2D 0x02
364#define TEXTARGET_3D 0x03
365#define TEXTARGET_RECT 0x04
366#define TEXTARGET_CUBE 0x05
367/* GL_ARB_fragment_program_shadow */
368#define TEXTARGET_SHADOW1D 0x06
369#define TEXTARGET_SHADOW2D 0x07
370#define TEXTARGET_SHADOWRECT 0x08
371
372/* face type */
373#define FACE_FRONT 0x00
374#define FACE_BACK 0x01
375
376/* color type */
377#define COLOR_PRIMARY 0x00
378#define COLOR_SECONDARY 0x01
379
380/* component */
381#define COMPONENT_X 0x00
382#define COMPONENT_Y 0x01
383#define COMPONENT_Z 0x02
384#define COMPONENT_W 0x03
385#define COMPONENT_0 0x04
386#define COMPONENT_1 0x05
387
388/* array index type */
389#define ARRAY_INDEX_ABSOLUTE 0x00
390#define ARRAY_INDEX_RELATIVE 0x01
391
392/* matrix name */
393#define MATRIX_MODELVIEW 0x01
394#define MATRIX_PROJECTION 0x02
395#define MATRIX_MVP 0x03
396#define MATRIX_TEXTURE 0x04
397#define MATRIX_PALETTE 0x05
398#define MATRIX_PROGRAM 0x06
399
400/* matrix modifier */
401#define MATRIX_MODIFIER_IDENTITY 0x00
402#define MATRIX_MODIFIER_INVERSE 0x01
403#define MATRIX_MODIFIER_TRANSPOSE 0x02
404#define MATRIX_MODIFIER_INVTRANS 0x03
405
406/* constant type */
407#define CONSTANT_SCALAR 0x01
408#define CONSTANT_VECTOR 0x02
409
410/* program param type */
411#define PROGRAM_PARAM_ENV 0x01
412#define PROGRAM_PARAM_LOCAL 0x02
413
414/* register type */
415#define REGISTER_ATTRIB 0x01
416#define REGISTER_PARAM 0x02
417#define REGISTER_RESULT 0x03
418#define REGISTER_ESTABLISHED_NAME 0x04
419
420/* param binding */
421#define PARAM_NULL 0x00
422#define PARAM_ARRAY_ELEMENT 0x01
423#define PARAM_STATE_ELEMENT 0x02
424#define PARAM_PROGRAM_ELEMENT 0x03
425#define PARAM_PROGRAM_ELEMENTS 0x04
426#define PARAM_CONSTANT 0x05
427
428/* param state property */
429#define STATE_MATERIAL_PARSER 0x01
430#define STATE_LIGHT_PARSER 0x02
431#define STATE_LIGHT_MODEL 0x03
432#define STATE_LIGHT_PROD 0x04
433#define STATE_FOG 0x05
434#define STATE_MATRIX_ROWS 0x06
435/* GL_ARB_fragment_program */
436#define STATE_TEX_ENV 0x07
437#define STATE_DEPTH 0x08
438/* GL_ARB_vertex_program */
439#define STATE_TEX_GEN 0x09
440#define STATE_CLIP_PLANE 0x0A
441#define STATE_POINT 0x0B
442
443/* state material property */
444#define MATERIAL_AMBIENT 0x01
445#define MATERIAL_DIFFUSE 0x02
446#define MATERIAL_SPECULAR 0x03
447#define MATERIAL_EMISSION 0x04
448#define MATERIAL_SHININESS 0x05
449
450/* state light property */
451#define LIGHT_AMBIENT 0x01
452#define LIGHT_DIFFUSE 0x02
453#define LIGHT_SPECULAR 0x03
454#define LIGHT_POSITION 0x04
455#define LIGHT_ATTENUATION 0x05
456#define LIGHT_HALF 0x06
457#define LIGHT_SPOT_DIRECTION 0x07
458
459/* state light model property */
460#define LIGHT_MODEL_AMBIENT 0x01
461#define LIGHT_MODEL_SCENECOLOR 0x02
462
463/* state light product property */
464#define LIGHT_PROD_AMBIENT 0x01
465#define LIGHT_PROD_DIFFUSE 0x02
466#define LIGHT_PROD_SPECULAR 0x03
467
468/* state texture environment property */
469#define TEX_ENV_COLOR 0x01
470
471/* state texture generation coord property */
472#define TEX_GEN_EYE 0x01
473#define TEX_GEN_OBJECT 0x02
474
475/* state fog property */
476#define FOG_COLOR 0x01
477#define FOG_PARAMS 0x02
478
479/* state depth property */
480#define DEPTH_RANGE 0x01
481
482/* state point parameters property */
483#define POINT_SIZE 0x01
484#define POINT_ATTENUATION 0x02
485
486/* declaration */
487#define ATTRIB 0x01
488#define PARAM 0x02
489#define TEMP 0x03
490#define OUTPUT 0x04
491#define ALIAS 0x05
492/* GL_ARB_vertex_program */
493#define ADDRESS 0x06
494
495/*-----------------------------------------------------------------------
496 * From here on down is the semantic checking portion
497 *
498 */
499
500/**
501 * Variable Table Handling functions
502 */
503typedef enum
504{
505 vt_none,
506 vt_address,
507 vt_attrib,
508 vt_param,
509 vt_temp,
510 vt_output,
511 vt_alias
512} var_type;
513
514
Brian Paul7aebaf32005-10-30 21:23:23 +0000515/**
516 * Setting an explicit field for each of the binding properties is a bit
517 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000518 */
519struct var_cache
520{
Brian Paul6a769d92006-04-28 15:42:15 +0000521 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000522 var_type type;
523 GLuint address_binding; /* The index of the address register we should
524 * be using */
525 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000526 GLuint attrib_is_generic; /* If the attrib was specified through a generic
527 * vertex attrib */
528 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000529 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000530 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
531 * that this is aliased to */
532 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
533 * PROGRAM_ENV_PARAM} */
534 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
535 * the tokens representing our bound state (or constants)
536 * start */
537 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
538 * we take up with our state tokens or constants. Note that
539 * this is _not_ the same as the number of param registers
540 * we eventually use */
541 struct var_cache *next;
542};
543
544static GLvoid
545var_cache_create (struct var_cache **va)
546{
547 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
548 if (*va) {
549 (**va).name = NULL;
550 (**va).type = vt_none;
551 (**va).attrib_binding = ~0;
552 (**va).attrib_is_generic = 0;
553 (**va).temp_binding = ~0;
554 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000555 (**va).param_binding_type = ~0;
556 (**va).param_binding_begin = ~0;
557 (**va).param_binding_length = ~0;
558 (**va).alias_binding = NULL;
559 (**va).next = NULL;
560 }
561}
562
563static GLvoid
564var_cache_destroy (struct var_cache **va)
565{
566 if (*va) {
567 var_cache_destroy (&(**va).next);
568 _mesa_free (*va);
569 *va = NULL;
570 }
571}
572
573static GLvoid
574var_cache_append (struct var_cache **va, struct var_cache *nv)
575{
576 if (*va)
577 var_cache_append (&(**va).next, nv);
578 else
579 *va = nv;
580}
581
582static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000583var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000584{
Brian Paul0a360cf2005-01-17 01:21:03 +0000585 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000586
587 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000588 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000589 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000590 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000591 return va;
592 }
593
594 va = va->next;
595 }
596
597 return NULL;
598}
599
Brian Paula088f162006-09-05 23:08:51 +0000600
601
602/**
603 * Called when an error is detected while parsing/compiling a program.
604 * Sets the ctx->Program.ErrorString field to descript and records a
605 * GL_INVALID_OPERATION error.
606 * \param position position of error in program string
607 * \param descrip verbose error description
608 */
609static void
610program_error(GLcontext *ctx, GLint position, const char *descrip)
611{
612 if (descrip) {
613 const char *prefix = "glProgramString(", *suffix = ")";
614 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
615 _mesa_strlen(prefix) +
616 _mesa_strlen(suffix) + 1);
617 if (str) {
618 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
619 _mesa_error(ctx, GL_INVALID_OPERATION, str);
620 _mesa_free(str);
621 }
622 }
623 _mesa_set_program_error(ctx, position, descrip);
624}
625
626
627
Jouk Jansen40322e12004-04-05 08:50:36 +0000628/**
629 * constructs an integer from 4 GLubytes in LE format
630 */
631static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000632parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000633{
634 GLuint value;
635
636 value = (GLuint) (*(*inst)++);
637 value += (GLuint) (*(*inst)++) * 0x100;
638 value += (GLuint) (*(*inst)++) * 0x10000;
639 value += (GLuint) (*(*inst)++) * 0x1000000;
640
641 return value;
642}
643
644/**
645 * This will, given a string, lookup the string as a variable name in the
646 * var cache. If the name is found, the var cache node corresponding to the
647 * var name is returned. If it is not found, a new entry is allocated
648 *
649 * \param I Points into the binary array where the string identifier begins
650 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
651 * \return The location on the var_cache corresponding the the string starting at I
652 */
653static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000654parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000655 struct arb_program *Program, GLuint * found)
656{
Brian Paula088f162006-09-05 23:08:51 +0000657 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000658 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000659 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000660
661 *inst += _mesa_strlen ((char *) i) + 1;
662
663 va = var_cache_find (*vc_head, i);
664
665 if (va) {
666 *found = 1;
667 return va;
668 }
669
670 *found = 0;
671 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000672 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000673
674 var_cache_append (vc_head, va);
675
676 return va;
677}
678
679static char *
Brian Paula088f162006-09-05 23:08:51 +0000680parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000681{
Brian Paula088f162006-09-05 23:08:51 +0000682 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000683 (void) Program;
684
Jouk Jansen40322e12004-04-05 08:50:36 +0000685 *inst += _mesa_strlen ((char *) i) + 1;
686
687 return (char *) i;
688}
689
690/**
Brian Paul05908952004-06-08 15:20:23 +0000691 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000692 */
Brian Paul05908952004-06-08 15:20:23 +0000693static GLint
Brian Paula088f162006-09-05 23:08:51 +0000694parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000695{
696 /*return *(*inst)++ != '+'; */
697
698 if (**inst == '-') {
699 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000700 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000701 }
702 else if (**inst == '+') {
703 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000704 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000705 }
706
Brian Paul05908952004-06-08 15:20:23 +0000707 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000708}
709
710/**
711 * parses and returns signed integer
712 */
713static GLint
Brian Paula088f162006-09-05 23:08:51 +0000714parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000715{
716 GLint sign;
717 GLint value;
718
719 /* check if *inst points to '+' or '-'
720 * if yes, grab the sign and increment *inst
721 */
722 sign = parse_sign (inst);
723
724 /* now check if *inst points to 0
725 * if yes, increment the *inst and return the default value
726 */
727 if (**inst == 0) {
728 (*inst)++;
729 return 0;
730 }
731
732 /* parse the integer as you normally would do it */
733 value = _mesa_atoi (parse_string_without_adding (inst, Program));
734
735 /* now, after terminating 0 there is a position
736 * to parse it - parse_position()
737 */
738 Program->Position = parse_position (inst);
739
Brian Paul05908952004-06-08 15:20:23 +0000740 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000741}
742
743/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000744 Accumulate this string of digits, and return them as
745 a large integer represented in floating point (for range).
746 If scale is not NULL, also accumulates a power-of-ten
747 integer scale factor that represents the number of digits
748 in the string.
749*/
750static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000751parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000752{
753 GLdouble value = 0.0;
754 GLdouble oscale = 1.0;
755
756 if (**inst == 0) { /* this string of digits is empty-- do nothing */
757 (*inst)++;
758 }
759 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000760 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000761 GLubyte digit = *((*inst)++);
762 value = value * 10.0 + (GLint) (digit - '0');
763 oscale *= 10.0;
764 }
765 assert(**inst == 0); /* integer string should end with 0 */
766 (*inst)++; /* skip over terminating 0 */
767 Program->Position = parse_position(inst); /* skip position (from integer) */
768 }
769 if (scale)
770 *scale = oscale;
771 return value;
772}
773
774/**
775 Parse an unsigned floating-point number from this stream of tokenized
776 characters. Example floating-point formats supported:
777 12.34
778 12
779 0.34
780 .34
781 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000782 */
783static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000784parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000785{
Brian Paul1ff8f502005-02-16 15:08:29 +0000786 GLint exponent;
787 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000788
Brian Paul1ff8f502005-02-16 15:08:29 +0000789 whole = parse_float_string(inst, Program, 0);
790 fraction = parse_float_string(inst, Program, &fracScale);
791
792 /* Parse signed exponent */
793 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000794
Brian Paul1ff8f502005-02-16 15:08:29 +0000795 /* Assemble parts of floating-point number: */
796 return (GLfloat) ((whole + fraction / fracScale) *
797 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000798}
799
800
801/**
802 */
803static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000804parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000805{
Brian Paul05908952004-06-08 15:20:23 +0000806 GLint sign = parse_sign (inst);
807 GLfloat value = parse_float (inst, Program);
808 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000809}
810
811/**
812 * This picks out a constant value from the parsed array. The constant vector is r
813 * returned in the *values array, which should be of length 4.
814 *
815 * \param values - The 4 component vector with the constant value in it
816 */
817static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000818parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000819 GLboolean use)
820{
821 GLuint components, i;
822
823
824 switch (*(*inst)++) {
825 case CONSTANT_SCALAR:
826 if (use == GL_TRUE) {
827 values[0] =
828 values[1] =
829 values[2] = values[3] = parse_float (inst, Program);
830 }
831 else {
832 values[0] =
833 values[1] =
834 values[2] = values[3] = parse_signed_float (inst, Program);
835 }
836
837 break;
838 case CONSTANT_VECTOR:
839 values[0] = values[1] = values[2] = 0;
840 values[3] = 1;
841 components = *(*inst)++;
842 for (i = 0; i < components; i++) {
843 values[i] = parse_signed_float (inst, Program);
844 }
845 break;
846 }
847}
848
849/**
850 * \param offset The offset from the address register that we should
851 * address
852 *
853 * \return 0 on sucess, 1 on error
854 */
855static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000856parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
857 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000858{
Brian Paul6a769d92006-04-28 15:42:15 +0000859 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000860 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000861 return 0;
862}
863
864/**
865 * \param color 0 if color type is primary, 1 if color type is secondary
866 * \return 0 on sucess, 1 on error
867 */
868static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000869parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000870 GLint * color)
871{
Brian Paula6c423d2004-08-25 15:59:48 +0000872 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000873 *color = *(*inst)++ != COLOR_PRIMARY;
874 return 0;
875}
876
877/**
878 * Get an integer corresponding to a generic vertex attribute.
879 *
880 * \return 0 on sucess, 1 on error
881 */
882static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000883parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000884 struct arb_program *Program, GLuint *attrib)
885{
Brian Paulbd997cd2004-07-20 21:12:56 +0000886 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000887
Michal Krolbb38cad2006-04-11 11:41:11 +0000888 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000889 {
Brian Paula088f162006-09-05 23:08:51 +0000890 program_error(ctx, Program->Position,
891 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000892 return 1;
893 }
894
Brian Paulbd997cd2004-07-20 21:12:56 +0000895 *attrib = (GLuint) i;
896
Jouk Jansen40322e12004-04-05 08:50:36 +0000897 return 0;
898}
899
900
901/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000902 * \param color The index of the color buffer to write into
903 * \return 0 on sucess, 1 on error
904 */
905static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000906parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000907 struct arb_program *Program, GLuint * color)
908{
909 GLint i = parse_integer (inst, Program);
910
911 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000912 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000913 return 1;
914 }
915
916 *color = (GLuint) i;
917 return 0;
918}
919
920
921/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000922 * \param coord The texture unit index
923 * \return 0 on sucess, 1 on error
924 */
925static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000926parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000927 struct arb_program *Program, GLuint * coord)
928{
Brian Paulbd997cd2004-07-20 21:12:56 +0000929 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000930
Brian Paula6c423d2004-08-25 15:59:48 +0000931 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000932 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000933 return 1;
934 }
935
Brian Paulbd997cd2004-07-20 21:12:56 +0000936 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000937 return 0;
938}
939
940/**
941 * \param coord The weight index
942 * \return 0 on sucess, 1 on error
943 */
944static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000945parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000946 GLint * coord)
947{
948 *coord = parse_integer (inst, Program);
949
950 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000951 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000952 return 1;
953 }
954
955 return 0;
956}
957
958/**
959 * \param coord The clip plane index
960 * \return 0 on sucess, 1 on error
961 */
962static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000963parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000964 struct arb_program *Program, GLint * coord)
965{
966 *coord = parse_integer (inst, Program);
967
968 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +0000969 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000970 return 1;
971 }
972
973 return 0;
974}
975
976
977/**
978 * \return 0 on front face, 1 on back face
979 */
980static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000981parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000982{
983 switch (*(*inst)++) {
984 case FACE_FRONT:
985 return 0;
986
987 case FACE_BACK:
988 return 1;
989 }
990 return 0;
991}
992
993
994/**
995 * Given a matrix and a modifier token on the binary array, return tokens
996 * that _mesa_fetch_state() [program.c] can understand.
997 *
998 * \param matrix - the matrix we are talking about
999 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1000 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1001 * \return 0 on sucess, 1 on failure
1002 */
1003static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001004parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001005 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1006{
1007 GLubyte mat = *(*inst)++;
1008
1009 *matrix_idx = 0;
1010
1011 switch (mat) {
1012 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001013 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001014 *matrix_idx = parse_integer (inst, Program);
1015 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001016 program_error(ctx, Program->Position,
1017 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001018 return 1;
1019 }
1020 break;
1021
1022 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001023 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001024 break;
1025
1026 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001027 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001028 break;
1029
1030 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001031 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001032 *matrix_idx = parse_integer (inst, Program);
1033 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001034 program_error(ctx, Program->Position, "Invalid Texture Unit");
1035 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001036 return 1;
1037 }
1038 break;
1039
1040 /* This is not currently supported (ARB_matrix_palette) */
1041 case MATRIX_PALETTE:
1042 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001043 program_error(ctx, Program->Position,
1044 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001045 return 1;
1046 break;
1047
1048 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001049 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001050 *matrix_idx = parse_integer (inst, Program);
1051 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001052 program_error(ctx, Program->Position, "Invalid Program Matrix");
1053 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001054 return 1;
1055 }
1056 break;
1057 }
1058
1059 switch (*(*inst)++) {
1060 case MATRIX_MODIFIER_IDENTITY:
1061 *matrix_modifier = 0;
1062 break;
1063 case MATRIX_MODIFIER_INVERSE:
1064 *matrix_modifier = STATE_MATRIX_INVERSE;
1065 break;
1066 case MATRIX_MODIFIER_TRANSPOSE:
1067 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1068 break;
1069 case MATRIX_MODIFIER_INVTRANS:
1070 *matrix_modifier = STATE_MATRIX_INVTRANS;
1071 break;
1072 }
1073
1074 return 0;
1075}
1076
1077
1078/**
1079 * This parses a state string (rather, the binary version of it) into
1080 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1081 *
1082 * \param inst - the start in the binary arry to start working from
1083 * \param state_tokens - the storage for the 6-token state description
1084 * \return - 0 on sucess, 1 on error
1085 */
1086static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001087parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001088 struct arb_program *Program, GLint * state_tokens)
1089{
1090 switch (*(*inst)++) {
1091 case STATE_MATERIAL_PARSER:
1092 state_tokens[0] = STATE_MATERIAL;
1093 state_tokens[1] = parse_face_type (inst);
1094 switch (*(*inst)++) {
1095 case MATERIAL_AMBIENT:
1096 state_tokens[2] = STATE_AMBIENT;
1097 break;
1098 case MATERIAL_DIFFUSE:
1099 state_tokens[2] = STATE_DIFFUSE;
1100 break;
1101 case MATERIAL_SPECULAR:
1102 state_tokens[2] = STATE_SPECULAR;
1103 break;
1104 case MATERIAL_EMISSION:
1105 state_tokens[2] = STATE_EMISSION;
1106 break;
1107 case MATERIAL_SHININESS:
1108 state_tokens[2] = STATE_SHININESS;
1109 break;
1110 }
1111 break;
1112
1113 case STATE_LIGHT_PARSER:
1114 state_tokens[0] = STATE_LIGHT;
1115 state_tokens[1] = parse_integer (inst, Program);
1116
1117 /* Check the value of state_tokens[1] against the # of lights */
1118 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001119 program_error(ctx, Program->Position, "Invalid Light Number");
1120 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001121 return 1;
1122 }
1123
1124 switch (*(*inst)++) {
1125 case LIGHT_AMBIENT:
1126 state_tokens[2] = STATE_AMBIENT;
1127 break;
1128 case LIGHT_DIFFUSE:
1129 state_tokens[2] = STATE_DIFFUSE;
1130 break;
1131 case LIGHT_SPECULAR:
1132 state_tokens[2] = STATE_SPECULAR;
1133 break;
1134 case LIGHT_POSITION:
1135 state_tokens[2] = STATE_POSITION;
1136 break;
1137 case LIGHT_ATTENUATION:
1138 state_tokens[2] = STATE_ATTENUATION;
1139 break;
1140 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001141 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001142 break;
1143 case LIGHT_SPOT_DIRECTION:
1144 state_tokens[2] = STATE_SPOT_DIRECTION;
1145 break;
1146 }
1147 break;
1148
1149 case STATE_LIGHT_MODEL:
1150 switch (*(*inst)++) {
1151 case LIGHT_MODEL_AMBIENT:
1152 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1153 break;
1154 case LIGHT_MODEL_SCENECOLOR:
1155 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1156 state_tokens[1] = parse_face_type (inst);
1157 break;
1158 }
1159 break;
1160
1161 case STATE_LIGHT_PROD:
1162 state_tokens[0] = STATE_LIGHTPROD;
1163 state_tokens[1] = parse_integer (inst, Program);
1164
1165 /* Check the value of state_tokens[1] against the # of lights */
1166 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001167 program_error(ctx, Program->Position, "Invalid Light Number");
1168 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001169 return 1;
1170 }
1171
1172 state_tokens[2] = parse_face_type (inst);
1173 switch (*(*inst)++) {
1174 case LIGHT_PROD_AMBIENT:
1175 state_tokens[3] = STATE_AMBIENT;
1176 break;
1177 case LIGHT_PROD_DIFFUSE:
1178 state_tokens[3] = STATE_DIFFUSE;
1179 break;
1180 case LIGHT_PROD_SPECULAR:
1181 state_tokens[3] = STATE_SPECULAR;
1182 break;
1183 }
1184 break;
1185
1186
1187 case STATE_FOG:
1188 switch (*(*inst)++) {
1189 case FOG_COLOR:
Brian65319522007-02-21 11:08:21 -07001190 state_tokens[0] = STATE_FOG;
1191 state_tokens[1] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001192 break;
1193 case FOG_PARAMS:
Brian65319522007-02-21 11:08:21 -07001194 state_tokens[0] = STATE_FOG;
1195 state_tokens[1] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001196 break;
1197 }
1198 break;
1199
1200 case STATE_TEX_ENV:
1201 state_tokens[1] = parse_integer (inst, Program);
1202 switch (*(*inst)++) {
1203 case TEX_ENV_COLOR:
1204 state_tokens[0] = STATE_TEXENV_COLOR;
1205 break;
1206 }
1207 break;
1208
1209 case STATE_TEX_GEN:
1210 {
1211 GLuint type, coord;
1212
1213 state_tokens[0] = STATE_TEXGEN;
1214 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1215
1216 if (parse_texcoord_num (ctx, inst, Program, &coord))
1217 return 1;
1218 state_tokens[1] = coord;
1219
1220 /* EYE or OBJECT */
1221 type = *(*inst++);
1222
1223 /* 0 - s, 1 - t, 2 - r, 3 - q */
1224 coord = *(*inst++);
1225
1226 if (type == TEX_GEN_EYE) {
1227 switch (coord) {
1228 case COMPONENT_X:
1229 state_tokens[2] = STATE_TEXGEN_EYE_S;
1230 break;
1231 case COMPONENT_Y:
1232 state_tokens[2] = STATE_TEXGEN_EYE_T;
1233 break;
1234 case COMPONENT_Z:
1235 state_tokens[2] = STATE_TEXGEN_EYE_R;
1236 break;
1237 case COMPONENT_W:
1238 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1239 break;
1240 }
1241 }
1242 else {
1243 switch (coord) {
1244 case COMPONENT_X:
1245 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1246 break;
1247 case COMPONENT_Y:
1248 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1249 break;
1250 case COMPONENT_Z:
1251 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1252 break;
1253 case COMPONENT_W:
1254 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1255 break;
1256 }
1257 }
1258 }
1259 break;
1260
1261 case STATE_DEPTH:
1262 switch (*(*inst)++) {
1263 case DEPTH_RANGE:
1264 state_tokens[0] = STATE_DEPTH_RANGE;
1265 break;
1266 }
1267 break;
1268
1269 case STATE_CLIP_PLANE:
1270 state_tokens[0] = STATE_CLIPPLANE;
1271 state_tokens[1] = parse_integer (inst, Program);
1272 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1273 return 1;
1274 break;
1275
1276 case STATE_POINT:
1277 switch (*(*inst++)) {
1278 case POINT_SIZE:
Brianf958aab2007-02-21 15:23:11 -07001279 state_tokens[0] = STATE_POINT;
1280 state_tokens[1] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001281 break;
1282
1283 case POINT_ATTENUATION:
Brianf958aab2007-02-21 15:23:11 -07001284 state_tokens[0] = STATE_POINT;
1285 state_tokens[1] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001286 break;
1287 }
1288 break;
1289
1290 /* XXX: I think this is the correct format for a matrix row */
1291 case STATE_MATRIX_ROWS:
Jouk Jansen40322e12004-04-05 08:50:36 +00001292 if (parse_matrix
Brian65319522007-02-21 11:08:21 -07001293 (ctx, inst, Program, &state_tokens[0], &state_tokens[1],
1294 &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001295 return 1;
1296
Brian65319522007-02-21 11:08:21 -07001297 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001298
1299 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001300 state_tokens[3] = parse_integer (inst, Program);
1301 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001302 program_error(ctx, Program->Position,
1303 "Second matrix index less than the first");
1304 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001305 return 1;
1306 }
1307 }
1308 else {
Brian65319522007-02-21 11:08:21 -07001309 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001310 (*inst)++;
1311 }
1312 break;
1313 }
1314
1315 return 0;
1316}
1317
1318/**
1319 * This parses a state string (rather, the binary version of it) into
1320 * a 6-token similar for the state fetching code in program.c
1321 *
1322 * One might ask, why fetch these parameters into just like you fetch
1323 * state when they are already stored in other places?
1324 *
1325 * Because of array offsets -> We can stick env/local parameters in the
1326 * middle of a parameter array and then index someplace into the array
1327 * when we execute.
1328 *
1329 * One optimization might be to only do this for the cases where the
1330 * env/local parameters end up inside of an array, and leave the
1331 * single parameters (or arrays of pure env/local pareameters) in their
1332 * respective register files.
1333 *
1334 * For ENV parameters, the format is:
1335 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1336 * state_tokens[1] = STATE_ENV
1337 * state_tokens[2] = the parameter index
1338 *
1339 * for LOCAL parameters, the format is:
1340 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1341 * state_tokens[1] = STATE_LOCAL
1342 * state_tokens[2] = the parameter index
1343 *
1344 * \param inst - the start in the binary arry to start working from
1345 * \param state_tokens - the storage for the 6-token state description
1346 * \return - 0 on sucess, 1 on failure
1347 */
1348static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001349parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001350 struct arb_program *Program, GLint * state_tokens)
1351{
1352 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1353 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1354 else
1355 state_tokens[0] = STATE_VERTEX_PROGRAM;
1356
1357
1358 switch (*(*inst)++) {
1359 case PROGRAM_PARAM_ENV:
1360 state_tokens[1] = STATE_ENV;
1361 state_tokens[2] = parse_integer (inst, Program);
1362
1363 /* Check state_tokens[2] against the number of ENV parameters available */
1364 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001365 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001366 ||
1367 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001368 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001369 program_error(ctx, Program->Position,
1370 "Invalid Program Env Parameter");
1371 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001372 return 1;
1373 }
1374
1375 break;
1376
1377 case PROGRAM_PARAM_LOCAL:
1378 state_tokens[1] = STATE_LOCAL;
1379 state_tokens[2] = parse_integer (inst, Program);
1380
1381 /* Check state_tokens[2] against the number of LOCAL parameters available */
1382 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001383 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001384 ||
1385 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001386 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001387 program_error(ctx, Program->Position,
1388 "Invalid Program Local Parameter");
1389 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001390 return 1;
1391 }
1392 break;
1393 }
1394
1395 return 0;
1396}
1397
1398/**
1399 * For ARB_vertex_program, programs are not allowed to use both an explicit
1400 * vertex attribute and a generic vertex attribute corresponding to the same
1401 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1402 *
1403 * This will walk our var_cache and make sure that nobody does anything fishy.
1404 *
1405 * \return 0 on sucess, 1 on error
1406 */
1407static GLuint
1408generic_attrib_check(struct var_cache *vc_head)
1409{
1410 int a;
1411 struct var_cache *curr;
1412 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1413 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1414
1415 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1416 explicitAttrib[a] = GL_FALSE;
1417 genericAttrib[a] = GL_FALSE;
1418 }
1419
1420 curr = vc_head;
1421 while (curr) {
1422 if (curr->type == vt_attrib) {
1423 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001424 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001425 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001426 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001427 }
1428
1429 curr = curr->next;
1430 }
1431
1432 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1433 if ((explicitAttrib[a]) && (genericAttrib[a]))
1434 return 1;
1435 }
1436
1437 return 0;
1438}
1439
1440/**
1441 * This will handle the binding side of an ATTRIB var declaration
1442 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001443 * \param inputReg returns the input register index, one of the
1444 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001445 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001446 */
1447static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001448parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001449 struct arb_program *Program,
1450 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001451{
Jouk Jansen40322e12004-04-05 08:50:36 +00001452 GLint err = 0;
1453
1454 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001455
Jouk Jansen40322e12004-04-05 08:50:36 +00001456 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1457 switch (*(*inst)++) {
1458 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001459 {
1460 GLint coord;
1461 err = parse_color_type (ctx, inst, Program, &coord);
1462 *inputReg = FRAG_ATTRIB_COL0 + coord;
1463 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001464 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001465 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001466 {
Brian8fa6f732007-02-01 09:24:41 -07001467 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001468 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1469 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1470 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001471 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001473 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001475 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001476 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001477 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001478 default:
1479 err = 1;
1480 break;
1481 }
1482 }
1483 else {
1484 switch (*(*inst)++) {
1485 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001486 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001487 break;
1488
1489 case VERTEX_ATTRIB_WEIGHT:
1490 {
1491 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001492 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001493 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001494#if 1
1495 /* hack for Warcraft (see bug 8060) */
1496 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001497 break;
Brian Paul3a557502006-09-05 23:15:29 +00001498#else
Brian Paula088f162006-09-05 23:08:51 +00001499 program_error(ctx, Program->Position,
1500 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001501 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001502#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001503 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001504
1505 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001506 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001507 break;
1508
1509 case VERTEX_ATTRIB_COLOR:
1510 {
1511 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001512 err = parse_color_type (ctx, inst, Program, &color);
1513 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001514 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001515 }
1516 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001517 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001518 }
1519 }
1520 break;
1521
1522 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001523 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001524 break;
1525
1526 case VERTEX_ATTRIB_TEXCOORD:
1527 {
Brian8fa6f732007-02-01 09:24:41 -07001528 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001529 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001530 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001531 }
1532 break;
1533
Jouk Jansen40322e12004-04-05 08:50:36 +00001534 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001535 /* Not supported at this time */
1536 {
1537 const char *msg = "ARB_palette_matrix not supported";
1538 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001539 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001540 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001541 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001542
1543 case VERTEX_ATTRIB_GENERIC:
1544 {
1545 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001546 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001547 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001548 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001549 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1550 * attributes do not alias the conventional vertex
1551 * attributes.
1552 */
1553 if (attrib > 0)
1554 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001555 else
1556 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001557 }
1558 }
1559 break;
1560
1561 default:
1562 err = 1;
1563 break;
1564 }
1565 }
1566
Jouk Jansen40322e12004-04-05 08:50:36 +00001567 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001568 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001569 }
1570
Brian Paulde997602005-11-12 17:53:14 +00001571 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001572
1573 return err;
1574}
1575
Brian Paul7aebaf32005-10-30 21:23:23 +00001576
Jouk Jansen40322e12004-04-05 08:50:36 +00001577/**
1578 * This translates between a binary token for an output variable type
1579 * and the mesa token for the same thing.
1580 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001581 * \param inst The parsed tokens
1582 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001583 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001584 */
1585static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001586parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001587 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001588{
Brian Paul7aebaf32005-10-30 21:23:23 +00001589 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001590
Brian Paul7aebaf32005-10-30 21:23:23 +00001591 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001592 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001593 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001594 GLuint out_color;
1595
Brian Paulbe76b7f2004-10-04 14:40:05 +00001596 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001597 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001598 */
1599 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001600 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001601 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001602 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001603 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001604 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1605 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001606 }
1607 break;
1608
1609 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001610 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001611 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001612 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001613 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001614 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001615 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001616 GLint color_type;
1617 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001618 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1619 if (err)
1620 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001621
Jouk Jansen40322e12004-04-05 08:50:36 +00001622 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001623 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001625 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001627 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001628 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001629 }
1630 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001631 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001632 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001633 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001634 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001635 }
1636 /* primary color */
1637 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001638 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001639 }
1640 }
1641 }
1642 break;
1643
1644 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001645 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001646 break;
1647
1648 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001649 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001650 break;
1651
1652 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001653 {
1654 GLuint unit;
1655 if (parse_texcoord_num (ctx, inst, Program, &unit))
1656 return 1;
1657 *outputReg = VERT_RESULT_TEX0 + unit;
1658 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001659 break;
1660 }
1661
Brian Paulde997602005-11-12 17:53:14 +00001662 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001663
1664 return 0;
1665}
1666
Brian Paul7aebaf32005-10-30 21:23:23 +00001667
Jouk Jansen40322e12004-04-05 08:50:36 +00001668/**
1669 * This handles the declaration of ATTRIB variables
1670 *
1671 * XXX: Still needs
1672 * parse_vert_attrib_binding(), or something like that
1673 *
1674 * \return 0 on sucess, 1 on error
1675 */
1676static GLint
Brian Paula088f162006-09-05 23:08:51 +00001677parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001678 struct arb_program *Program)
1679{
1680 GLuint found;
1681 char *error_msg;
1682 struct var_cache *attrib_var;
1683
1684 attrib_var = parse_string (inst, vc_head, Program, &found);
1685 Program->Position = parse_position (inst);
1686 if (found) {
1687 error_msg = (char *)
1688 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1689 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1690 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001691 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001692 _mesa_free (error_msg);
1693 return 1;
1694 }
1695
1696 attrib_var->type = vt_attrib;
1697
Brian Paul7aebaf32005-10-30 21:23:23 +00001698 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001699 &attrib_var->attrib_is_generic))
1700 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001701
Brian Paul7aebaf32005-10-30 21:23:23 +00001702 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001703 program_error(ctx, Program->Position,
1704 "Cannot use both a generic vertex attribute "
1705 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001706 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001707 }
1708
1709 Program->Base.NumAttributes++;
1710 return 0;
1711}
1712
1713/**
1714 * \param use -- TRUE if we're called when declaring implicit parameters,
1715 * FALSE if we're declaraing variables. This has to do with
1716 * if we get a signed or unsigned float for scalar constants
1717 */
1718static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001719parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001720 struct var_cache *param_var,
1721 struct arb_program *Program, GLboolean use)
1722{
1723 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001724 GLuint err = 0;
Brianb7978af2007-01-09 19:17:17 -07001725 GLint state_tokens[STATE_LENGTH];
Jouk Jansen40322e12004-04-05 08:50:36 +00001726 GLfloat const_values[4];
1727
Jouk Jansen40322e12004-04-05 08:50:36 +00001728 switch (*(*inst)++) {
1729 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001730 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1731 return 1;
1732
1733 /* If we adding STATE_MATRIX that has multiple rows, we need to
1734 * unroll it and call _mesa_add_state_reference() for each row
1735 */
Brian65319522007-02-21 11:08:21 -07001736 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1737 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1738 state_tokens[0] == STATE_MVP_MATRIX ||
1739 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1740 state_tokens[0] == STATE_PROGRAM_MATRIX)
1741 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001742 GLint row;
Brian65319522007-02-21 11:08:21 -07001743 const GLint first_row = state_tokens[2];
1744 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001745
1746 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001747 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001748
Brian Paulde997602005-11-12 17:53:14 +00001749 idx = _mesa_add_state_reference(Program->Base.Parameters,
1750 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001751 if (param_var->param_binding_begin == ~0U)
1752 param_var->param_binding_begin = idx;
1753 param_var->param_binding_length++;
1754 Program->Base.NumParameters++;
1755 }
1756 }
1757 else {
Brian Paulde997602005-11-12 17:53:14 +00001758 idx = _mesa_add_state_reference(Program->Base.Parameters,
1759 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001760 if (param_var->param_binding_begin == ~0U)
1761 param_var->param_binding_begin = idx;
1762 param_var->param_binding_length++;
1763 Program->Base.NumParameters++;
1764 }
1765 break;
1766
1767 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001768 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1769 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001770 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001771 if (param_var->param_binding_begin == ~0U)
1772 param_var->param_binding_begin = idx;
1773 param_var->param_binding_length++;
1774 Program->Base.NumParameters++;
1775
1776 /* Check if there is more: 0 -> we're done, else its an integer */
1777 if (**inst) {
1778 GLuint out_of_range, new_idx;
1779 GLuint start_idx = state_tokens[2] + 1;
1780 GLuint end_idx = parse_integer (inst, Program);
1781
1782 out_of_range = 0;
1783 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1784 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001785 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001786 || ((state_tokens[1] == STATE_LOCAL)
1787 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001788 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001789 out_of_range = 1;
1790 }
1791 else {
1792 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001793 && (end_idx >= ctx->Const.VertexProgram.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.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001797 out_of_range = 1;
1798 }
1799 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001800 program_error(ctx, Program->Position,
1801 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001802 return 1;
1803 }
1804
1805 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1806 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001807 idx = _mesa_add_state_reference(Program->Base.Parameters,
1808 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001809 param_var->param_binding_length++;
1810 Program->Base.NumParameters++;
1811 }
1812 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001813 else {
1814 (*inst)++;
1815 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001816 break;
1817
1818 case PARAM_CONSTANT:
1819 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001820 idx = _mesa_add_named_constant(Program->Base.Parameters,
1821 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001822 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001823 if (param_var->param_binding_begin == ~0U)
1824 param_var->param_binding_begin = idx;
1825 param_var->param_binding_length++;
1826 Program->Base.NumParameters++;
1827 break;
1828
1829 default:
Brian Paula088f162006-09-05 23:08:51 +00001830 program_error(ctx, Program->Position,
1831 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001832 return 1;
1833 }
1834
1835 /* Make sure we haven't blown past our parameter limits */
1836 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1837 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001838 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001839 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1840 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001841 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001842 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001843 return 1;
1844 }
1845
1846 return err;
1847}
1848
Brian Paul7aebaf32005-10-30 21:23:23 +00001849
Jouk Jansen40322e12004-04-05 08:50:36 +00001850/**
1851 * This picks out PARAM program parameter bindings.
1852 *
1853 * XXX: This needs to be stressed & tested
1854 *
1855 * \return 0 on sucess, 1 on error
1856 */
1857static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001858parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001859 struct arb_program *Program)
1860{
Brian Paulbd997cd2004-07-20 21:12:56 +00001861 GLuint found, err;
1862 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001863 struct var_cache *param_var;
1864
1865 err = 0;
1866 param_var = parse_string (inst, vc_head, Program, &found);
1867 Program->Position = parse_position (inst);
1868
1869 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001870 char *error_msg = (char *)
1871 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001872 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1873 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001874 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001875 _mesa_free (error_msg);
1876 return 1;
1877 }
1878
1879 specified_length = parse_integer (inst, Program);
1880
1881 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001882 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001883 return 1;
1884 }
1885
1886 param_var->type = vt_param;
1887 param_var->param_binding_length = 0;
1888
1889 /* Right now, everything is shoved into the main state register file.
1890 *
1891 * In the future, it would be nice to leave things ENV/LOCAL params
1892 * in their respective register files, if possible
1893 */
1894 param_var->param_binding_type = PROGRAM_STATE_VAR;
1895
1896 /* Remember to:
1897 * * - add each guy to the parameter list
1898 * * - increment the param_var->param_binding_len
1899 * * - store the param_var->param_binding_begin for the first one
1900 * * - compare the actual len to the specified len at the end
1901 */
1902 while (**inst != PARAM_NULL) {
1903 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1904 return 1;
1905 }
1906
1907 /* Test array length here! */
1908 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001909 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001910 program_error(ctx, Program->Position,
1911 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001912 }
1913 }
1914
1915 (*inst)++;
1916
1917 return 0;
1918}
1919
1920/**
1921 *
1922 */
1923static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001924parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001925 struct arb_program *Program, struct var_cache **new_var)
1926{
1927 struct var_cache *param_var;
1928
1929 /* First, insert a dummy entry into the var_cache */
1930 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001931 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001932 param_var->type = vt_param;
1933
1934 param_var->param_binding_length = 0;
1935 /* Don't fill in binding_begin; We use the default value of -1
1936 * to tell if its already initialized, elsewhere.
1937 *
1938 * param_var->param_binding_begin = 0;
1939 */
1940 param_var->param_binding_type = PROGRAM_STATE_VAR;
1941
1942 var_cache_append (vc_head, param_var);
1943
1944 /* Then fill it with juicy parameter goodness */
1945 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1946 return 1;
1947
1948 *new_var = param_var;
1949
1950 return 0;
1951}
1952
1953
1954/**
1955 * This handles the declaration of TEMP variables
1956 *
1957 * \return 0 on sucess, 1 on error
1958 */
1959static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001960parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001961 struct arb_program *Program)
1962{
1963 GLuint found;
1964 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001965
1966 while (**inst != 0) {
1967 temp_var = parse_string (inst, vc_head, Program, &found);
1968 Program->Position = parse_position (inst);
1969 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001970 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001971 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1972 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1973 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001974 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001975 _mesa_free (error_msg);
1976 return 1;
1977 }
1978
1979 temp_var->type = vt_temp;
1980
1981 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1982 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001983 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001984 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1985 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001986 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00001987 program_error(ctx, Program->Position,
1988 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00001989 return 1;
1990 }
1991
1992 temp_var->temp_binding = Program->Base.NumTemporaries;
1993 Program->Base.NumTemporaries++;
1994 }
1995 (*inst)++;
1996
1997 return 0;
1998}
1999
2000/**
2001 * This handles variables of the OUTPUT variety
2002 *
2003 * \return 0 on sucess, 1 on error
2004 */
2005static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002006parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002007 struct arb_program *Program)
2008{
2009 GLuint found;
2010 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002011 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002012
2013 output_var = parse_string (inst, vc_head, Program, &found);
2014 Program->Position = parse_position (inst);
2015 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002016 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002017 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2018 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2019 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002020 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002021 _mesa_free (error_msg);
2022 return 1;
2023 }
2024
2025 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002026
2027 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2028 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002029}
2030
2031/**
2032 * This handles variables of the ALIAS kind
2033 *
2034 * \return 0 on sucess, 1 on error
2035 */
2036static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002037parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002038 struct arb_program *Program)
2039{
2040 GLuint found;
2041 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002042
2043 temp_var = parse_string (inst, vc_head, Program, &found);
2044 Program->Position = parse_position (inst);
2045
2046 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002047 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002048 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2049 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2050 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002051 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002052 _mesa_free (error_msg);
2053 return 1;
2054 }
2055
2056 temp_var->type = vt_alias;
2057 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2058 Program->Position = parse_position (inst);
2059
2060 if (!found)
2061 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002062 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002063 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2064 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2065 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002066 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002067 _mesa_free (error_msg);
2068 return 1;
2069 }
2070
2071 return 0;
2072}
2073
2074/**
2075 * This handles variables of the ADDRESS kind
2076 *
2077 * \return 0 on sucess, 1 on error
2078 */
2079static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002080parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002081 struct arb_program *Program)
2082{
2083 GLuint found;
2084 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002085
2086 while (**inst != 0) {
2087 temp_var = parse_string (inst, vc_head, Program, &found);
2088 Program->Position = parse_position (inst);
2089 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002090 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002091 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2092 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2093 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002094 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002095 _mesa_free (error_msg);
2096 return 1;
2097 }
2098
2099 temp_var->type = vt_address;
2100
2101 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002102 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002103 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002104 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002105 return 1;
2106 }
2107
2108 temp_var->address_binding = Program->Base.NumAddressRegs;
2109 Program->Base.NumAddressRegs++;
2110 }
2111 (*inst)++;
2112
2113 return 0;
2114}
2115
2116/**
2117 * Parse a program declaration
2118 *
2119 * \return 0 on sucess, 1 on error
2120 */
2121static GLint
Brian Paula088f162006-09-05 23:08:51 +00002122parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002123 struct arb_program *Program)
2124{
2125 GLint err = 0;
2126
2127 switch (*(*inst)++) {
2128 case ADDRESS:
2129 err = parse_address (ctx, inst, vc_head, Program);
2130 break;
2131
2132 case ALIAS:
2133 err = parse_alias (ctx, inst, vc_head, Program);
2134 break;
2135
2136 case ATTRIB:
2137 err = parse_attrib (ctx, inst, vc_head, Program);
2138 break;
2139
2140 case OUTPUT:
2141 err = parse_output (ctx, inst, vc_head, Program);
2142 break;
2143
2144 case PARAM:
2145 err = parse_param (ctx, inst, vc_head, Program);
2146 break;
2147
2148 case TEMP:
2149 err = parse_temp (ctx, inst, vc_head, Program);
2150 break;
2151 }
2152
2153 return err;
2154}
2155
2156/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002157 * Handle the parsing out of a masked destination register, either for a
2158 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002159 *
2160 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002161 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002162 * position invariant
2163 *
2164 * \param File - The register file we write to
2165 * \param Index - The register index we write to
2166 * \param WriteMask - The mask controlling which components we write (1->write)
2167 *
2168 * \return 0 on sucess, 1 on error
2169 */
2170static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002171parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002172 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002173 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002174{
Brian Paul7aebaf32005-10-30 21:23:23 +00002175 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002176 struct var_cache *dst;
2177
2178 /* We either have a result register specified, or a
2179 * variable that may or may not be writable
2180 */
2181 switch (*(*inst)++) {
2182 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002183 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002184 return 1;
2185 *File = PROGRAM_OUTPUT;
2186 break;
2187
2188 case REGISTER_ESTABLISHED_NAME:
2189 dst = parse_string (inst, vc_head, Program, &result);
2190 Program->Position = parse_position (inst);
2191
2192 /* If the name has never been added to our symbol table, we're hosed */
2193 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002194 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002195 return 1;
2196 }
2197
2198 switch (dst->type) {
2199 case vt_output:
2200 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002201 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002202 break;
2203
2204 case vt_temp:
2205 *File = PROGRAM_TEMPORARY;
2206 *Index = dst->temp_binding;
2207 break;
2208
2209 /* If the var type is not vt_output or vt_temp, no go */
2210 default:
Brian Paula088f162006-09-05 23:08:51 +00002211 program_error(ctx, Program->Position,
2212 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002213 return 1;
2214 }
2215 break;
2216
2217 default:
Brian Paula088f162006-09-05 23:08:51 +00002218 program_error(ctx, Program->Position,
2219 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002220 return 1;
2221 }
2222
2223
2224 /* Position invariance test */
2225 /* This test is done now in syntax portion - when position invariance OPTION
2226 is specified, "result.position" rule is disabled so there is no way
2227 to write the position
2228 */
2229 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2230 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002231 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002232 "Vertex program specified position invariance and wrote vertex position");
2233 }*/
2234
2235 /* And then the mask.
2236 * w,a -> bit 0
2237 * z,b -> bit 1
2238 * y,g -> bit 2
2239 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002240 *
2241 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002242 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002243 tmp = (GLint) *(*inst)++;
2244 *WriteMask = (((tmp>>3) & 0x1) |
2245 ((tmp>>1) & 0x2) |
2246 ((tmp<<1) & 0x4) |
2247 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002248
2249 return 0;
2250}
2251
2252
2253/**
2254 * Handle the parsing of a address register
2255 *
2256 * \param Index - The register index we write to
2257 *
2258 * \return 0 on sucess, 1 on error
2259 */
2260static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002261parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002262 struct var_cache **vc_head,
2263 struct arb_program *Program, GLint * Index)
2264{
2265 struct var_cache *dst;
2266 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002267
2268 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002269
2270 dst = parse_string (inst, vc_head, Program, &result);
2271 Program->Position = parse_position (inst);
2272
2273 /* If the name has never been added to our symbol table, we're hosed */
2274 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002275 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002276 return 1;
2277 }
2278
2279 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002280 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002281 return 1;
2282 }
2283
2284 return 0;
2285}
2286
Brian Paul8e8fa632005-07-01 02:03:33 +00002287#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002288/**
2289 * Handle the parsing out of a masked address register
2290 *
2291 * \param Index - The register index we write to
2292 * \param WriteMask - The mask controlling which components we write (1->write)
2293 *
2294 * \return 0 on sucess, 1 on error
2295 */
2296static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002297parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002298 struct var_cache **vc_head,
2299 struct arb_program *Program, GLint * Index,
2300 GLboolean * WriteMask)
2301{
2302 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2303 return 1;
2304
2305 /* This should be 0x8 */
2306 (*inst)++;
2307
2308 /* Writemask of .x is implied */
2309 WriteMask[0] = 1;
2310 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2311
2312 return 0;
2313}
Brian Paul8e8fa632005-07-01 02:03:33 +00002314#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002315
2316/**
2317 * Parse out a swizzle mask.
2318 *
Brian Paul32df89e2005-10-29 18:26:43 +00002319 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002320 *
2321 * The len parameter allows us to grab 4 components for a vector
2322 * swizzle, or just 1 component for a scalar src register selection
2323 */
Brian Paul32df89e2005-10-29 18:26:43 +00002324static void
Brian Paula088f162006-09-05 23:08:51 +00002325parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002326{
Brian Paul32df89e2005-10-29 18:26:43 +00002327 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002328
Brian Paul32df89e2005-10-29 18:26:43 +00002329 for (i = 0; i < 4; i++)
2330 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002331
Brian Paul32df89e2005-10-29 18:26:43 +00002332 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002333 switch (*(*inst)++) {
2334 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002335 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002336 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002337 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002338 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002339 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002340 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002341 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002342 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002343 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002344 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002345 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002346 default:
2347 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2348 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002349 }
2350 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002351}
2352
Jouk Jansen40322e12004-04-05 08:50:36 +00002353
Brian Paul32df89e2005-10-29 18:26:43 +00002354/**
2355 * Parse an extended swizzle mask which is a sequence of
2356 * four x/y/z/w/0/1 tokens.
2357 * \return swizzle four swizzle values
2358 * \return negateMask four element bitfield
2359 */
2360static void
Brian Paula088f162006-09-05 23:08:51 +00002361parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002362 GLubyte *negateMask)
2363{
2364 GLint i;
2365
2366 *negateMask = 0x0;
2367 for (i = 0; i < 4; i++) {
2368 GLubyte swz;
2369 if (parse_sign(inst) == -1)
2370 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002371
2372 swz = *(*inst)++;
2373
2374 switch (swz) {
2375 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002376 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002377 break;
2378 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002379 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002380 break;
2381 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002382 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002383 break;
2384 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002385 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002386 break;
2387 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002388 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002389 break;
2390 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002391 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002392 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002393 default:
2394 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2395 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002396 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002397 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002398}
2399
2400
2401static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002402parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2403 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002404 struct arb_program *Program,
2405 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002406 GLboolean *IsRelOffset )
2407{
2408 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002409 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002410 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002411
Keith Whitwell7c26b612005-04-21 14:46:57 +00002412 *IsRelOffset = 0;
2413
Jouk Jansen40322e12004-04-05 08:50:36 +00002414 /* And the binding for the src */
2415 switch (*(*inst)++) {
2416 case REGISTER_ATTRIB:
2417 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002418 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002419 return 1;
2420 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002421 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002422
2423 /* We need to insert a dummy variable into the var_cache so we can
2424 * catch generic vertex attrib aliasing errors
2425 */
2426 var_cache_create(&src);
2427 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002428 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002429 src->attrib_binding = binding;
2430 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002431 var_cache_append(vc_head, src);
2432 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002433 program_error(ctx, Program->Position,
2434 "Cannot use both a generic vertex attribute "
2435 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002436 return 1;
2437 }
2438 break;
2439
2440 case REGISTER_PARAM:
2441 switch (**inst) {
2442 case PARAM_ARRAY_ELEMENT:
2443 (*inst)++;
2444 src = parse_string (inst, vc_head, Program, &found);
2445 Program->Position = parse_position (inst);
2446
2447 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002448 program_error(ctx, Program->Position,
2449 "2: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002450 return 1;
2451 }
2452
Brian Paul95801792005-12-06 15:41:43 +00002453 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002454
2455 switch (*(*inst)++) {
2456 case ARRAY_INDEX_ABSOLUTE:
2457 offset = parse_integer (inst, Program);
2458
2459 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002460 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002461 program_error(ctx, Program->Position,
2462 "Index out of range");
2463 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002464 return 1;
2465 }
2466
2467 *Index = src->param_binding_begin + offset;
2468 break;
2469
2470 case ARRAY_INDEX_RELATIVE:
2471 {
2472 GLint addr_reg_idx, rel_off;
2473
2474 /* First, grab the address regiseter */
2475 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2476 return 1;
2477
2478 /* And the .x */
2479 ((*inst)++);
2480 ((*inst)++);
2481 ((*inst)++);
2482 ((*inst)++);
2483
2484 /* Then the relative offset */
2485 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2486
2487 /* And store it properly */
2488 *Index = src->param_binding_begin + rel_off;
2489 *IsRelOffset = 1;
2490 }
2491 break;
2492 }
2493 break;
2494
2495 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002496 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2497 return 1;
2498
Brian Paul95801792005-12-06 15:41:43 +00002499 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002500 *Index = src->param_binding_begin;
2501 break;
2502 }
2503 break;
2504
2505 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002506 src = parse_string (inst, vc_head, Program, &found);
2507 Program->Position = parse_position (inst);
2508
2509 /* If the name has never been added to our symbol table, we're hosed */
2510 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002511 program_error(ctx, Program->Position,
2512 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002513 return 1;
2514 }
2515
2516 switch (src->type) {
2517 case vt_attrib:
2518 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002519 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002520 break;
2521
2522 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2523 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002524 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002525 *Index = src->param_binding_begin;
2526 break;
2527
2528 case vt_temp:
2529 *File = PROGRAM_TEMPORARY;
2530 *Index = src->temp_binding;
2531 break;
2532
2533 /* If the var type is vt_output no go */
2534 default:
Brian Paula088f162006-09-05 23:08:51 +00002535 program_error(ctx, Program->Position,
2536 "destination register is read only");
2537 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002538 return 1;
2539 }
2540 break;
2541
2542 default:
Brian Paula088f162006-09-05 23:08:51 +00002543 program_error(ctx, Program->Position,
2544 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002545 return 1;
2546 }
2547
2548 return 0;
2549}
2550
2551/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002552 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002553 */
2554static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002555parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002556 struct var_cache **vc_head,
2557 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002558 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002559{
Brian Paul7aebaf32005-10-30 21:23:23 +00002560 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002561 GLint index;
2562 GLboolean negate;
2563 GLubyte swizzle[4];
2564 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002565
Jouk Jansen40322e12004-04-05 08:50:36 +00002566 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002567 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002568
2569 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002570 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002571 return 1;
2572
2573 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002574 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002575
Brian Paul32df89e2005-10-29 18:26:43 +00002576 reg->File = file;
2577 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002578 reg->NegateBase = negate;
2579 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002580 return 0;
2581}
2582
Jouk Jansen40322e12004-04-05 08:50:36 +00002583
Brian Paulb7fc1c32006-08-30 23:38:03 +00002584/**
2585 * Parse fragment program destination register.
2586 * \return 1 if error, 0 if no error.
2587 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002588static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002589parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002590 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002591 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002592{
Brian Paul7aebaf32005-10-30 21:23:23 +00002593 GLint mask;
2594 GLuint idx;
2595 enum register_file file;
2596
Keith Whitwell7c26b612005-04-21 14:46:57 +00002597 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002598 return 1;
2599
Keith Whitwell7c26b612005-04-21 14:46:57 +00002600 reg->File = file;
2601 reg->Index = idx;
2602 reg->WriteMask = mask;
2603 return 0;
2604}
2605
2606
Brian Paul7aebaf32005-10-30 21:23:23 +00002607/**
2608 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002609 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002610 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002611static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002612parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002613 struct var_cache **vc_head,
2614 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002615 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002616{
Brian Paul7aebaf32005-10-30 21:23:23 +00002617 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002618 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002619 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002620 GLubyte Swizzle[4];
2621 GLboolean IsRelOffset;
2622
2623 /* Grab the sign */
2624 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2625
2626 /* And the src reg */
2627 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2628 return 1;
2629
2630 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002631 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002632
Keith Whitwell7c26b612005-04-21 14:46:57 +00002633 reg->File = File;
2634 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002635 reg->NegateBase = Negate;
2636 reg->Swizzle = (Swizzle[0] << 0);
2637
Jouk Jansen40322e12004-04-05 08:50:36 +00002638 return 0;
2639}
2640
Keith Whitwell7c26b612005-04-21 14:46:57 +00002641
Jouk Jansen40322e12004-04-05 08:50:36 +00002642/**
2643 * This is a big mother that handles getting opcodes into the instruction
2644 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002645 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002646 */
2647static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002648parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002649 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002650 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002651{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002652 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002653 GLuint texcoord;
2654 GLubyte instClass, type, code;
2655 GLboolean rel;
2656
Brian Pauld6272e02006-10-29 18:03:16 +00002657 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002658
2659 /* Record the position in the program string for debugging */
2660 fp->StringPos = Program->Position;
2661
2662 /* OP_ALU_INST or OP_TEX_INST */
2663 instClass = *(*inst)++;
2664
2665 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2666 * OP_TEX_{SAMPLE, KIL}
2667 */
2668 type = *(*inst)++;
2669
2670 /* The actual opcode name */
2671 code = *(*inst)++;
2672
2673 /* Increment the correct count */
2674 switch (instClass) {
2675 case OP_ALU_INST:
2676 Program->NumAluInstructions++;
2677 break;
2678 case OP_TEX_INST:
2679 Program->NumTexInstructions++;
2680 break;
2681 }
2682
Jouk Jansen40322e12004-04-05 08:50:36 +00002683 switch (type) {
2684 case OP_ALU_VECTOR:
2685 switch (code) {
2686 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002687 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002688 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002689 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002690 break;
2691
2692 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002693 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002694 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002695 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002696 break;
2697
2698 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002699 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002700 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002701 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002702 break;
2703
2704 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002705 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002706 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002707 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002708 break;
2709
2710 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002711 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002712 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002713 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002714 break;
2715 }
2716
Keith Whitwell7c26b612005-04-21 14:46:57 +00002717 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002718 return 1;
2719
Keith Whitwell7c26b612005-04-21 14:46:57 +00002720 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002721 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002722 break;
2723
2724 case OP_ALU_SCALAR:
2725 switch (code) {
2726 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002727 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002728 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002729 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002730 break;
2731
2732 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002733 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002734 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002735 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002736 break;
2737
2738 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002739 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002740 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002741 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002742 break;
2743
2744 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002745 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002746 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002747 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002748 break;
2749
2750 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002751 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002752 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002753 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 break;
2755
2756 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002757 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002758 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002759 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 break;
2761
2762 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002763 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002764 case OP_SCS:
2765
Brian Paul7e807512005-11-05 17:10:45 +00002766 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 break;
2768 }
2769
Keith Whitwell7c26b612005-04-21 14:46:57 +00002770 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002771 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002772
2773 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002774 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 break;
2776
2777 case OP_ALU_BINSC:
2778 switch (code) {
2779 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002780 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002781 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002782 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 break;
2784 }
2785
Keith Whitwell7c26b612005-04-21 14:46:57 +00002786 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002788
Jouk Jansen40322e12004-04-05 08:50:36 +00002789 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002790 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002792 }
2793 break;
2794
2795
2796 case OP_ALU_BIN:
2797 switch (code) {
2798 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002799 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002801 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002802 break;
2803
2804 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002805 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002806 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002807 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002808 break;
2809
2810 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002811 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002812 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002813 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002814 break;
2815
2816 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002817 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002818 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002819 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002820 break;
2821
2822 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002823 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002824 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002825 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002826 break;
2827
2828 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002829 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002830 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002831 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002832 break;
2833
2834 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002835 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002836 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002837 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002838 break;
2839
2840 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002841 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002842 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002843 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 break;
2845
2846 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002847 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002848 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002849 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002850 break;
2851
2852 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002853 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002854 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002855 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002856 break;
2857
2858 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002859 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002860 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002861 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002862 break;
2863
2864 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002865 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002866 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002867 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002868 break;
2869 }
2870
Keith Whitwell7c26b612005-04-21 14:46:57 +00002871 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002872 return 1;
2873 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002874 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2875 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002876 }
2877 break;
2878
2879 case OP_ALU_TRI:
2880 switch (code) {
2881 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002882 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002884 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 break;
2886
2887 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002888 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002890 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 break;
2892
2893 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002894 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002895 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002896 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 break;
2898 }
2899
Keith Whitwell7c26b612005-04-21 14:46:57 +00002900 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002901 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002902
Jouk Jansen40322e12004-04-05 08:50:36 +00002903 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002904 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2905 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002906 }
2907 break;
2908
2909 case OP_ALU_SWZ:
2910 switch (code) {
2911 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002912 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002913 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002914 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002915 break;
2916 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002917 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002918 return 1;
2919
Keith Whitwell7c26b612005-04-21 14:46:57 +00002920 {
Brian Paul32df89e2005-10-29 18:26:43 +00002921 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002922 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002923 enum register_file file;
2924 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002925
Brian Paul32df89e2005-10-29 18:26:43 +00002926 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002927 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002928 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2929 fp->SrcReg[0].File = file;
2930 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002931 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002932 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2933 swizzle[1],
2934 swizzle[2],
2935 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002936 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002937 break;
2938
2939 case OP_TEX_SAMPLE:
2940 switch (code) {
2941 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002942 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002943 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002944 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002945 break;
2946
2947 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002948 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002949 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002950 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002951 break;
2952
2953 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002954 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002955 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002956 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002957 break;
2958 }
2959
Keith Whitwell7c26b612005-04-21 14:46:57 +00002960 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002961 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002962
2963 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002964 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002965
2966 /* texImageUnit */
2967 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2968 return 1;
2969 fp->TexSrcUnit = texcoord;
2970
2971 /* texTarget */
2972 switch (*(*inst)++) {
2973 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00002974 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002975 break;
2976 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00002977 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002978 break;
2979 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00002980 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002981 break;
2982 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00002983 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002984 break;
2985 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00002986 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002987 break;
2988 case TEXTARGET_SHADOW1D:
2989 case TEXTARGET_SHADOW2D:
2990 case TEXTARGET_SHADOWRECT:
2991 /* TODO ARB_fragment_program_shadow code */
2992 break;
2993 }
Brian Paulb7fc1c32006-08-30 23:38:03 +00002994 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
2995 /* Check that both "2D" and "CUBE" (for example) aren't both used */
2996 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00002997 program_error(ctx, Program->Position,
2998 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00002999 return 1;
3000 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003001 break;
3002
3003 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003004 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003005 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003006 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003007 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003008 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003009 default:
3010 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3011 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003012 }
3013
3014 return 0;
3015}
3016
Keith Whitwell7c26b612005-04-21 14:46:57 +00003017static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003018parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003019 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003020 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003021{
Brian Paul7aebaf32005-10-30 21:23:23 +00003022 GLint mask;
3023 GLuint idx;
3024 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003025
3026 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3027 return 1;
3028
3029 reg->File = file;
3030 reg->Index = idx;
3031 reg->WriteMask = mask;
3032 return 0;
3033}
3034
3035/**
3036 * Handle the parsing out of a masked address register
3037 *
3038 * \param Index - The register index we write to
3039 * \param WriteMask - The mask controlling which components we write (1->write)
3040 *
3041 * \return 0 on sucess, 1 on error
3042 */
3043static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003044parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003045 struct var_cache **vc_head,
3046 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003047 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003048{
3049 GLint idx;
3050
3051 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3052 return 1;
3053
3054 /* This should be 0x8 */
3055 (*inst)++;
3056
3057 reg->File = PROGRAM_ADDRESS;
3058 reg->Index = idx;
3059
3060 /* Writemask of .x is implied */
3061 reg->WriteMask = 0x1;
3062 return 0;
3063}
3064
3065/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003066 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003067 */
3068static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003069parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003070 struct var_cache **vc_head,
3071 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003072 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003073{
Brian Paul7aebaf32005-10-30 21:23:23 +00003074 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003075 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003076 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003077 GLubyte swizzle[4];
3078 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003079
3080 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003081 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003082
3083 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003084 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003085 return 1;
3086
3087 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003088 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003089
Brian Paul32df89e2005-10-29 18:26:43 +00003090 reg->File = file;
3091 reg->Index = index;
3092 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3093 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003094 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003095 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003096 return 0;
3097}
3098
3099
3100static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003101parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003102 struct var_cache **vc_head,
3103 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003104 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003105{
Brian Paul7aebaf32005-10-30 21:23:23 +00003106 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003107 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003108 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003109 GLubyte Swizzle[4];
3110 GLboolean IsRelOffset;
3111
3112 /* Grab the sign */
3113 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3114
3115 /* And the src reg */
3116 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3117 return 1;
3118
3119 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003120 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003121
3122 reg->File = File;
3123 reg->Index = Index;
3124 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003125 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003126 reg->RelAddr = IsRelOffset;
3127 return 0;
3128}
3129
3130
Jouk Jansen40322e12004-04-05 08:50:36 +00003131/**
3132 * This is a big mother that handles getting opcodes into the instruction
3133 * and handling the src & dst registers for vertex program instructions
3134 */
3135static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003136parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003137 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003138 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003139{
3140 GLint a;
3141 GLubyte type, code;
3142
3143 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3144 type = *(*inst)++;
3145
3146 /* The actual opcode name */
3147 code = *(*inst)++;
3148
Brian Pauld6272e02006-10-29 18:03:16 +00003149 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003150 /* Record the position in the program string for debugging */
3151 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003152
3153 switch (type) {
3154 /* XXX: */
3155 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003156 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003157
3158 /* Remember to set SrcReg.RelAddr; */
3159
3160 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003161 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003162 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003163
Jouk Jansen40322e12004-04-05 08:50:36 +00003164 vp->DstReg.File = PROGRAM_ADDRESS;
3165
3166 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003167 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003168 return 1;
3169
3170 break;
3171
3172 case OP_ALU_VECTOR:
3173 switch (code) {
3174 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003175 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003176 break;
3177 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003178 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003179 break;
3180 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003181 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003182 break;
3183 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003184 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003185 break;
3186 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003187 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003188 break;
3189 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003190
3191 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003192 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003193
3194 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003195 return 1;
3196 break;
3197
3198 case OP_ALU_SCALAR:
3199 switch (code) {
3200 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003201 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003202 break;
3203 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003204 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003205 break;
3206 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003207 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003208 break;
3209 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003210 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003211 break;
3212 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003213 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003214 break;
3215 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003216 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003217 break;
3218 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003219 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003220 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003221
3222 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003223 return 1;
3224 break;
3225
3226 case OP_ALU_BINSC:
3227 switch (code) {
3228 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003229 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003230 break;
3231 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003232 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003233 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003234
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003236 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003237 return 1;
3238 }
3239 break;
3240
3241 case OP_ALU_BIN:
3242 switch (code) {
3243 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003244 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 break;
3246 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003247 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 break;
3249 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003250 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 break;
3252 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003253 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 break;
3255 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003256 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003257 break;
3258 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003259 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003260 break;
3261 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003262 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003263 break;
3264 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003265 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003266 break;
3267 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003268 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003269 break;
3270 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003271 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003272 break;
3273 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003274 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003275 break;
3276 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003277 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003278 break;
3279 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003280 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003281 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003282
Jouk Jansen40322e12004-04-05 08:50:36 +00003283 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003284 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003285 return 1;
3286 }
3287 break;
3288
3289 case OP_ALU_TRI:
3290 switch (code) {
3291 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003292 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003293 break;
3294 }
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 < 3; 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_SWZ:
3306 switch (code) {
3307 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003308 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 break;
3310 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003311 {
Brian Paul32df89e2005-10-29 18:26:43 +00003312 GLubyte swizzle[4];
3313 GLubyte negateMask;
3314 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003315 enum register_file file;
3316 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003317
Keith Whitwell7c26b612005-04-21 14:46:57 +00003318 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3319 return 1;
3320
Brian Paul32df89e2005-10-29 18:26:43 +00003321 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003322 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003323 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3324 vp->SrcReg[0].File = file;
3325 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003326 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003327 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3328 swizzle[1],
3329 swizzle[2],
3330 swizzle[3]);
3331 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003332 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003333 break;
3334 }
3335 return 0;
3336}
3337
3338#if DEBUG_PARSING
3339
3340static GLvoid
3341print_state_token (GLint token)
3342{
3343 switch (token) {
3344 case STATE_MATERIAL:
3345 fprintf (stderr, "STATE_MATERIAL ");
3346 break;
3347 case STATE_LIGHT:
3348 fprintf (stderr, "STATE_LIGHT ");
3349 break;
3350
3351 case STATE_LIGHTMODEL_AMBIENT:
3352 fprintf (stderr, "STATE_AMBIENT ");
3353 break;
3354
3355 case STATE_LIGHTMODEL_SCENECOLOR:
3356 fprintf (stderr, "STATE_SCENECOLOR ");
3357 break;
3358
3359 case STATE_LIGHTPROD:
3360 fprintf (stderr, "STATE_LIGHTPROD ");
3361 break;
3362
3363 case STATE_TEXGEN:
3364 fprintf (stderr, "STATE_TEXGEN ");
3365 break;
3366
Brian65319522007-02-21 11:08:21 -07003367 case STATE_FOG:
3368 fprintf (stderr, "STATE_FOG ");
3369 break;
3370
Jouk Jansen40322e12004-04-05 08:50:36 +00003371 case STATE_FOG_COLOR:
3372 fprintf (stderr, "STATE_FOG_COLOR ");
3373 break;
3374
3375 case STATE_FOG_PARAMS:
3376 fprintf (stderr, "STATE_FOG_PARAMS ");
3377 break;
3378
3379 case STATE_CLIPPLANE:
3380 fprintf (stderr, "STATE_CLIPPLANE ");
3381 break;
3382
Brianf958aab2007-02-21 15:23:11 -07003383 case STATE_POINT:
3384 fprintf (stderr, "STATE_POINT ");
3385 break;
3386
Jouk Jansen40322e12004-04-05 08:50:36 +00003387 case STATE_POINT_SIZE:
3388 fprintf (stderr, "STATE_POINT_SIZE ");
3389 break;
3390
3391 case STATE_POINT_ATTENUATION:
3392 fprintf (stderr, "STATE_ATTENUATION ");
3393 break;
3394
3395 case STATE_MATRIX:
3396 fprintf (stderr, "STATE_MATRIX ");
3397 break;
3398
3399 case STATE_MODELVIEW:
3400 fprintf (stderr, "STATE_MODELVIEW ");
3401 break;
3402
3403 case STATE_PROJECTION:
3404 fprintf (stderr, "STATE_PROJECTION ");
3405 break;
3406
3407 case STATE_MVP:
3408 fprintf (stderr, "STATE_MVP ");
3409 break;
3410
3411 case STATE_TEXTURE:
3412 fprintf (stderr, "STATE_TEXTURE ");
3413 break;
3414
3415 case STATE_PROGRAM:
3416 fprintf (stderr, "STATE_PROGRAM ");
3417 break;
3418
3419 case STATE_MATRIX_INVERSE:
3420 fprintf (stderr, "STATE_INVERSE ");
3421 break;
3422
3423 case STATE_MATRIX_TRANSPOSE:
3424 fprintf (stderr, "STATE_TRANSPOSE ");
3425 break;
3426
3427 case STATE_MATRIX_INVTRANS:
3428 fprintf (stderr, "STATE_INVTRANS ");
3429 break;
3430
3431 case STATE_AMBIENT:
3432 fprintf (stderr, "STATE_AMBIENT ");
3433 break;
3434
3435 case STATE_DIFFUSE:
3436 fprintf (stderr, "STATE_DIFFUSE ");
3437 break;
3438
3439 case STATE_SPECULAR:
3440 fprintf (stderr, "STATE_SPECULAR ");
3441 break;
3442
3443 case STATE_EMISSION:
3444 fprintf (stderr, "STATE_EMISSION ");
3445 break;
3446
3447 case STATE_SHININESS:
3448 fprintf (stderr, "STATE_SHININESS ");
3449 break;
3450
3451 case STATE_HALF:
3452 fprintf (stderr, "STATE_HALF ");
3453 break;
3454
3455 case STATE_POSITION:
3456 fprintf (stderr, "STATE_POSITION ");
3457 break;
3458
3459 case STATE_ATTENUATION:
3460 fprintf (stderr, "STATE_ATTENUATION ");
3461 break;
3462
3463 case STATE_SPOT_DIRECTION:
3464 fprintf (stderr, "STATE_DIRECTION ");
3465 break;
3466
3467 case STATE_TEXGEN_EYE_S:
3468 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3469 break;
3470
3471 case STATE_TEXGEN_EYE_T:
3472 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3473 break;
3474
3475 case STATE_TEXGEN_EYE_R:
3476 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3477 break;
3478
3479 case STATE_TEXGEN_EYE_Q:
3480 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3481 break;
3482
3483 case STATE_TEXGEN_OBJECT_S:
3484 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3485 break;
3486
3487 case STATE_TEXGEN_OBJECT_T:
3488 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3489 break;
3490
3491 case STATE_TEXGEN_OBJECT_R:
3492 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3493 break;
3494
3495 case STATE_TEXGEN_OBJECT_Q:
3496 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3497 break;
3498
3499 case STATE_TEXENV_COLOR:
3500 fprintf (stderr, "STATE_TEXENV_COLOR ");
3501 break;
3502
3503 case STATE_DEPTH_RANGE:
3504 fprintf (stderr, "STATE_DEPTH_RANGE ");
3505 break;
3506
3507 case STATE_VERTEX_PROGRAM:
3508 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3509 break;
3510
3511 case STATE_FRAGMENT_PROGRAM:
3512 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3513 break;
3514
3515 case STATE_ENV:
3516 fprintf (stderr, "STATE_ENV ");
3517 break;
3518
3519 case STATE_LOCAL:
3520 fprintf (stderr, "STATE_LOCAL ");
3521 break;
3522
3523 }
3524 fprintf (stderr, "[%d] ", token);
3525}
3526
3527
3528static GLvoid
3529debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3530 struct arb_program *Program)
3531{
3532 struct var_cache *vc;
3533 GLint a, b;
3534
3535 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3536
3537 /* First of all, print out the contents of the var_cache */
3538 vc = vc_head;
3539 while (vc) {
3540 fprintf (stderr, "[%x]\n", vc);
3541 switch (vc->type) {
3542 case vt_none:
3543 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3544 break;
3545 case vt_attrib:
3546 fprintf (stderr, "ATTRIB %s\n", vc->name);
3547 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3548 break;
3549 case vt_param:
3550 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3551 vc->param_binding_begin, vc->param_binding_length);
3552 b = vc->param_binding_begin;
3553 for (a = 0; a < vc->param_binding_length; a++) {
3554 fprintf (stderr, "%s\n",
3555 Program->Parameters->Parameters[a + b].Name);
3556 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3557 print_state_token (Program->Parameters->Parameters[a + b].
3558 StateIndexes[0]);
3559 print_state_token (Program->Parameters->Parameters[a + b].
3560 StateIndexes[1]);
3561 print_state_token (Program->Parameters->Parameters[a + b].
3562 StateIndexes[2]);
3563 print_state_token (Program->Parameters->Parameters[a + b].
3564 StateIndexes[3]);
3565 print_state_token (Program->Parameters->Parameters[a + b].
3566 StateIndexes[4]);
3567 print_state_token (Program->Parameters->Parameters[a + b].
3568 StateIndexes[5]);
3569 }
3570 else
3571 fprintf (stderr, "%f %f %f %f\n",
3572 Program->Parameters->Parameters[a + b].Values[0],
3573 Program->Parameters->Parameters[a + b].Values[1],
3574 Program->Parameters->Parameters[a + b].Values[2],
3575 Program->Parameters->Parameters[a + b].Values[3]);
3576 }
3577 break;
3578 case vt_temp:
3579 fprintf (stderr, "TEMP %s\n", vc->name);
3580 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3581 break;
3582 case vt_output:
3583 fprintf (stderr, "OUTPUT %s\n", vc->name);
3584 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3585 break;
3586 case vt_alias:
3587 fprintf (stderr, "ALIAS %s\n", vc->name);
3588 fprintf (stderr, " binding: 0x%x (%s)\n",
3589 vc->alias_binding, vc->alias_binding->name);
3590 break;
3591 }
3592 vc = vc->next;
3593 }
3594}
3595
Brian Paul72030e02005-11-03 03:30:34 +00003596#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003597
3598
3599/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003600 * The main loop for parsing a fragment or vertex program
3601 *
Brian Paul72030e02005-11-03 03:30:34 +00003602 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003603 */
Brian Paul72030e02005-11-03 03:30:34 +00003604static GLint
Brian Paula088f162006-09-05 23:08:51 +00003605parse_instructions(GLcontext * ctx, const GLubyte * inst,
3606 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003607{
Brian Paul72030e02005-11-03 03:30:34 +00003608 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3609 ? ctx->Const.FragmentProgram.MaxInstructions
3610 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003611 GLint err = 0;
3612
Brian Paul72030e02005-11-03 03:30:34 +00003613 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3614
Jouk Jansen40322e12004-04-05 08:50:36 +00003615 Program->MajorVersion = (GLuint) * inst++;
3616 Program->MinorVersion = (GLuint) * inst++;
3617
3618 while (*inst != END) {
3619 switch (*inst++) {
3620
3621 case OPTION:
3622 switch (*inst++) {
3623 case ARB_PRECISION_HINT_FASTEST:
3624 Program->PrecisionOption = GL_FASTEST;
3625 break;
3626
3627 case ARB_PRECISION_HINT_NICEST:
3628 Program->PrecisionOption = GL_NICEST;
3629 break;
3630
3631 case ARB_FOG_EXP:
3632 Program->FogOption = GL_EXP;
3633 break;
3634
3635 case ARB_FOG_EXP2:
3636 Program->FogOption = GL_EXP2;
3637 break;
3638
3639 case ARB_FOG_LINEAR:
3640 Program->FogOption = GL_LINEAR;
3641 break;
3642
3643 case ARB_POSITION_INVARIANT:
3644 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003645 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003646 break;
3647
3648 case ARB_FRAGMENT_PROGRAM_SHADOW:
3649 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3650 /* TODO ARB_fragment_program_shadow code */
3651 }
3652 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003653
3654 case ARB_DRAW_BUFFERS:
3655 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3656 /* do nothing for now */
3657 }
3658 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003659 }
3660 break;
3661
3662 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003663 /* check length */
3664 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003665 program_error(ctx, Program->Position,
3666 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003667 return 1;
3668 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003669 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003670 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003671 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003672 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003673 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003674 }
3675 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003676 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003677 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003678 }
3679
Brian Paul72030e02005-11-03 03:30:34 +00003680 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003681 Program->Base.NumInstructions++;
3682 break;
3683
3684 case DECLARATION:
3685 err = parse_declaration (ctx, &inst, vc_head, Program);
3686 break;
3687
3688 default:
3689 break;
3690 }
3691
3692 if (err)
3693 break;
3694 }
3695
3696 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003697 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003698 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003699 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003700 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003701 /* YYY Wrong Position in program, whatever, at least not random -> crash
3702 Program->Position = parse_position (&inst);
3703 */
Brian Paul8c41a142005-11-19 15:36:28 +00003704 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003705 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003706 Program->Base.NumInstructions++;
3707
Brian Paul05051032005-11-01 04:36:33 +00003708 /*
3709 * Initialize native counts to logical counts. The device driver may
3710 * change them if program is translated into a hardware program.
3711 */
3712 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3713 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3714 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3715 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3716 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003717
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 return err;
3719}
3720
Brian Paul7aebaf32005-10-30 21:23:23 +00003721
Jouk Jansen40322e12004-04-05 08:50:36 +00003722/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003723__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003724#include "grammar_syn.h"
3725;
3726
Brian Paul7aebaf32005-10-30 21:23:23 +00003727
Brian Paul8c41a142005-11-19 15:36:28 +00003728/**
3729 * Set a grammar parameter.
3730 * \param name the grammar parameter
3731 * \param value the new parameter value
3732 * \return 0 if OK, 1 if error
3733 */
3734static int
3735set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003736{
3737 char error_msg[300];
3738 GLint error_pos;
3739
Brian Paul8c41a142005-11-19 15:36:28 +00003740 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003741 return 0;
3742
3743 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3744 _mesa_set_program_error (ctx, error_pos, error_msg);
3745 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3746 return 1;
3747}
3748
Brian Paul8c41a142005-11-19 15:36:28 +00003749
3750/**
3751 * Enable support for the given language option in the parser.
3752 * \return 1 if OK, 0 if error
3753 */
3754static int
3755enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003756{
Brian Paul8c41a142005-11-19 15:36:28 +00003757 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003758}
3759
Brian Paul8c41a142005-11-19 15:36:28 +00003760
3761/**
3762 * Enable parser extensions based on which OpenGL extensions are supported
3763 * by this rendering context.
3764 *
3765 * \return GL_TRUE if OK, GL_FALSE if error.
3766 */
3767static GLboolean
3768enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003769{
Brian Paul8c41a142005-11-19 15:36:28 +00003770#if 0
3771 /* These are not supported at this time */
3772 if ((ctx->Extensions.ARB_vertex_blend ||
3773 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003774 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003775 return GL_FALSE;
3776 if (ctx->Extensions.ARB_matrix_palette
3777 && !enable_ext(ctx, id, "matrix_palette"))
3778 return GL_FALSE;
3779 if (ctx->Extensions.ARB_fragment_program_shadow
3780 && !enable_ext(ctx, id, "fragment_program_shadow"))
3781 return GL_FALSE;
3782#endif
3783 if (ctx->Extensions.EXT_point_parameters
3784 && !enable_ext(ctx, id, "point_parameters"))
3785 return GL_FALSE;
3786 if (ctx->Extensions.EXT_secondary_color
3787 && !enable_ext(ctx, id, "secondary_color"))
3788 return GL_FALSE;
3789 if (ctx->Extensions.EXT_fog_coord
3790 && !enable_ext(ctx, id, "fog_coord"))
3791 return GL_FALSE;
3792 if (ctx->Extensions.NV_texture_rectangle
3793 && !enable_ext(ctx, id, "texture_rectangle"))
3794 return GL_FALSE;
3795 if (ctx->Extensions.ARB_draw_buffers
3796 && !enable_ext(ctx, id, "draw_buffers"))
3797 return GL_FALSE;
3798
Brian Paul3a557502006-09-05 23:15:29 +00003799#if 1
3800 /* hack for Warcraft (see bug 8060) */
3801 enable_ext(ctx, id, "vertex_blend");
3802#endif
3803
Brian Paul8c41a142005-11-19 15:36:28 +00003804 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003805}
3806
Brian Paul8c41a142005-11-19 15:36:28 +00003807
Jouk Jansen40322e12004-04-05 08:50:36 +00003808/**
3809 * This kicks everything off.
3810 *
3811 * \param ctx - The GL Context
3812 * \param str - The program string
3813 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003814 * \param program - The arb_program struct to return all the parsed info in
3815 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003816 */
Brian Paul8c41a142005-11-19 15:36:28 +00003817static GLboolean
3818_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3819 const GLubyte *str, GLsizei len,
3820 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003821{
3822 GLint a, err, error_pos;
3823 char error_msg[300];
3824 GLuint parsed_len;
3825 struct var_cache *vc_head;
3826 grammar arbprogram_syn_id;
3827 GLubyte *parsed, *inst;
3828 GLubyte *strz = NULL;
3829 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3830
Brian Paul8c41a142005-11-19 15:36:28 +00003831 /* set the program target before parsing */
3832 program->Base.Target = target;
3833
Brian Paul7f76b8f2004-09-10 01:05:39 +00003834 /* Reset error state */
3835 _mesa_set_program_error(ctx, -1, NULL);
3836
Brian Paul8c41a142005-11-19 15:36:28 +00003837 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003838 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003839 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003840 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003841 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003842
3843 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3844 if (grammar_syn_id == 0) {
3845 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003846 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003847 _mesa_set_program_error (ctx, error_pos, error_msg);
3848 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003849 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003850 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003851 }
3852
Brian Paul8c41a142005-11-19 15:36:28 +00003853 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3854 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003855
Brian Paul49a80ca2006-04-28 15:40:11 +00003856 /* 'parsed' is unused here */
3857 _mesa_free (parsed);
3858 parsed = NULL;
3859
Brian Paul45703642005-10-29 15:52:31 +00003860 /* NOTE: we can't destroy grammar_syn_id right here because
3861 * grammar_destroy() can reset the last error
3862 */
Brian Paul8c41a142005-11-19 15:36:28 +00003863 if (err) {
3864 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003865 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3866 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003867 _mesa_error (ctx, GL_INVALID_OPERATION,
3868 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003869 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003870 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003871 }
3872
3873 grammar_destroy (grammar_syn_id);
3874
3875 arbprogram_syn_is_ok = 1;
3876 }
3877
3878 /* create the grammar object */
3879 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3880 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003881 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003882 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3883 _mesa_set_program_error (ctx, error_pos, error_msg);
3884 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003885 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003886 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003887 }
3888
3889 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003890 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003891 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3892 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003893 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003894 }
3895
Brian Paul8c41a142005-11-19 15:36:28 +00003896 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3897 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003898 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003899 }
3900
3901 /* check for NULL character occurences */
3902 {
Brian Paul8c41a142005-11-19 15:36:28 +00003903 GLint i;
3904 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003905 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003906 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003907 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003908 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003909 }
Brian Paul8c41a142005-11-19 15:36:28 +00003910 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003911 }
3912
3913 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003914 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003915 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003916 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3917 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003918 return GL_FALSE;
3919 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003920 _mesa_memcpy (strz, str, len);
3921 strz[len] = '\0';
3922
Michal Krolb80bc052004-10-21 14:09:54 +00003923 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003924 err = !grammar_fast_check(arbprogram_syn_id, strz,
3925 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003926
3927 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003928 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003929 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3930 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003931
Brian Paulb3aefd12005-09-19 20:12:32 +00003932#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003933 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003934 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003935 int line, col;
3936 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003937 fprintf(stderr, "program: %s\n", (char *) strz);
3938 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003939 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3940 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003941 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3942 } while (0)
3943#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003944
Brian Paula088f162006-09-05 23:08:51 +00003945 _mesa_free(strz);
3946 _mesa_free(parsed);
3947
Jouk Jansen40322e12004-04-05 08:50:36 +00003948 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003949 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003950 }
3951
Jouk Jansen40322e12004-04-05 08:50:36 +00003952 grammar_destroy (arbprogram_syn_id);
3953
Brian Paul8c41a142005-11-19 15:36:28 +00003954 /*
3955 * Program string is syntactically correct at this point
3956 * Parse the tokenized version of the program now, generating
3957 * vertex/fragment program instructions.
3958 */
3959
Jouk Jansen40322e12004-04-05 08:50:36 +00003960 /* Initialize the arb_program struct */
3961 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003962 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003963 program->Base.NumInstructions =
3964 program->Base.NumTemporaries =
3965 program->Base.NumParameters =
3966 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003967 program->Base.Parameters = _mesa_new_parameter_list ();
3968 program->Base.InputsRead = 0x0;
3969 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003970 program->Position = 0;
3971 program->MajorVersion = program->MinorVersion = 0;
3972 program->PrecisionOption = GL_DONT_CARE;
3973 program->FogOption = GL_NONE;
3974 program->HintPositionInvariant = GL_FALSE;
3975 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003976 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003977 program->NumAluInstructions =
3978 program->NumTexInstructions =
3979 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003980 program->UsesKill = 0;
3981
Jouk Jansen40322e12004-04-05 08:50:36 +00003982 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003983 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003984
3985 /* Start examining the tokens in the array */
3986 inst = parsed;
3987
3988 /* Check the grammer rev */
3989 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003990 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003991 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003992 }
3993 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003994 /* ignore program target */
3995 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003996 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003997 }
3998
3999 /*debug_variables(ctx, vc_head, program); */
4000
4001 /* We're done with the parsed binary array */
4002 var_cache_destroy (&vc_head);
4003
4004 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00004005
Brian Paul8c41a142005-11-19 15:36:28 +00004006 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4007 * to size [ap.Base.NumInstructions].
4008 */
Brian Paula7543902006-08-24 21:58:32 +00004009 program->Base.Instructions
4010 = _mesa_realloc_instructions(program->Base.Instructions,
4011 MAX_INSTRUCTIONS,
4012 program->Base.NumInstructions);
4013
Brian Paul45703642005-10-29 15:52:31 +00004014 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004015}
Brian Paul8c41a142005-11-19 15:36:28 +00004016
4017
4018
4019void
4020_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4021 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004022 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004023{
4024 struct arb_program ap;
4025 GLuint i;
4026
4027 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004028 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004029 /* Error in the program. Just return. */
4030 return;
4031 }
4032
4033 /* Copy the relevant contents of the arb_program struct into the
4034 * fragment_program struct.
4035 */
4036 program->Base.String = ap.Base.String;
4037 program->Base.NumInstructions = ap.Base.NumInstructions;
4038 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4039 program->Base.NumParameters = ap.Base.NumParameters;
4040 program->Base.NumAttributes = ap.Base.NumAttributes;
4041 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004042 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4043 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4044 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4045 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4046 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07004047 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
4048 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
4049 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
4050 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
4051 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
4052 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004053 program->Base.InputsRead = ap.Base.InputsRead;
4054 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4055 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
Brianc9db2232007-01-04 17:22:19 -07004056 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Brian Paul8c41a142005-11-19 15:36:28 +00004057 program->FogOption = ap.FogOption;
4058
4059 if (program->Base.Instructions)
4060 _mesa_free(program->Base.Instructions);
4061 program->Base.Instructions = ap.Base.Instructions;
4062
4063 if (program->Base.Parameters)
4064 _mesa_free_parameter_list(program->Base.Parameters);
4065 program->Base.Parameters = ap.Base.Parameters;
4066
4067#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00004068 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
4069 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00004070#endif
4071}
4072
4073
4074
4075/**
4076 * Parse the vertex program string. If success, update the given
4077 * vertex_program object with the new program. Else, leave the vertex_program
4078 * object unchanged.
4079 */
4080void
4081_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4082 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004083 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004084{
4085 struct arb_program ap;
4086
4087 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4088
Brian Paul95801792005-12-06 15:41:43 +00004089 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004090 /* Error in the program. Just return. */
4091 return;
4092 }
4093
4094 /* Copy the relevant contents of the arb_program struct into the
4095 * vertex_program struct.
4096 */
4097 program->Base.String = ap.Base.String;
4098 program->Base.NumInstructions = ap.Base.NumInstructions;
4099 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4100 program->Base.NumParameters = ap.Base.NumParameters;
4101 program->Base.NumAttributes = ap.Base.NumAttributes;
4102 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004103 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4104 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4105 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4106 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4107 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004108 program->Base.InputsRead = ap.Base.InputsRead;
4109 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4110 program->IsPositionInvariant = ap.HintPositionInvariant;
4111
4112 if (program->Base.Instructions)
4113 _mesa_free(program->Base.Instructions);
4114 program->Base.Instructions = ap.Base.Instructions;
4115
4116 if (program->Base.Parameters)
4117 _mesa_free_parameter_list(program->Base.Parameters);
4118 program->Base.Parameters = ap.Base.Parameters;
4119
4120#if DEBUG_VP
Brian Paul11a54c32006-11-15 19:54:25 +00004121 _mesa_printf("____________Vertex program %u __________\n", program->Base.ID);
Brian Paul8c41a142005-11-19 15:36:28 +00004122 _mesa_print_program(&program->Base);
4123#endif
4124}