blob: 6058fc988928423004633d502dc3b809ea93fdd1 [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,
Brianaa9d22a2007-02-23 11:21:03 -07001088 struct arb_program *Program,
1089 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001090{
1091 switch (*(*inst)++) {
1092 case STATE_MATERIAL_PARSER:
1093 state_tokens[0] = STATE_MATERIAL;
1094 state_tokens[1] = parse_face_type (inst);
1095 switch (*(*inst)++) {
1096 case MATERIAL_AMBIENT:
1097 state_tokens[2] = STATE_AMBIENT;
1098 break;
1099 case MATERIAL_DIFFUSE:
1100 state_tokens[2] = STATE_DIFFUSE;
1101 break;
1102 case MATERIAL_SPECULAR:
1103 state_tokens[2] = STATE_SPECULAR;
1104 break;
1105 case MATERIAL_EMISSION:
1106 state_tokens[2] = STATE_EMISSION;
1107 break;
1108 case MATERIAL_SHININESS:
1109 state_tokens[2] = STATE_SHININESS;
1110 break;
1111 }
1112 break;
1113
1114 case STATE_LIGHT_PARSER:
1115 state_tokens[0] = STATE_LIGHT;
1116 state_tokens[1] = parse_integer (inst, Program);
1117
1118 /* Check the value of state_tokens[1] against the # of lights */
1119 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001120 program_error(ctx, Program->Position, "Invalid Light Number");
1121 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001122 return 1;
1123 }
1124
1125 switch (*(*inst)++) {
1126 case LIGHT_AMBIENT:
1127 state_tokens[2] = STATE_AMBIENT;
1128 break;
1129 case LIGHT_DIFFUSE:
1130 state_tokens[2] = STATE_DIFFUSE;
1131 break;
1132 case LIGHT_SPECULAR:
1133 state_tokens[2] = STATE_SPECULAR;
1134 break;
1135 case LIGHT_POSITION:
1136 state_tokens[2] = STATE_POSITION;
1137 break;
1138 case LIGHT_ATTENUATION:
1139 state_tokens[2] = STATE_ATTENUATION;
1140 break;
1141 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001142 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001143 break;
1144 case LIGHT_SPOT_DIRECTION:
1145 state_tokens[2] = STATE_SPOT_DIRECTION;
1146 break;
1147 }
1148 break;
1149
1150 case STATE_LIGHT_MODEL:
1151 switch (*(*inst)++) {
1152 case LIGHT_MODEL_AMBIENT:
1153 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1154 break;
1155 case LIGHT_MODEL_SCENECOLOR:
1156 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1157 state_tokens[1] = parse_face_type (inst);
1158 break;
1159 }
1160 break;
1161
1162 case STATE_LIGHT_PROD:
1163 state_tokens[0] = STATE_LIGHTPROD;
1164 state_tokens[1] = parse_integer (inst, Program);
1165
1166 /* Check the value of state_tokens[1] against the # of lights */
1167 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001168 program_error(ctx, Program->Position, "Invalid Light Number");
1169 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001170 return 1;
1171 }
1172
1173 state_tokens[2] = parse_face_type (inst);
1174 switch (*(*inst)++) {
1175 case LIGHT_PROD_AMBIENT:
1176 state_tokens[3] = STATE_AMBIENT;
1177 break;
1178 case LIGHT_PROD_DIFFUSE:
1179 state_tokens[3] = STATE_DIFFUSE;
1180 break;
1181 case LIGHT_PROD_SPECULAR:
1182 state_tokens[3] = STATE_SPECULAR;
1183 break;
1184 }
1185 break;
1186
1187
1188 case STATE_FOG:
1189 switch (*(*inst)++) {
1190 case FOG_COLOR:
Brian65319522007-02-21 11:08:21 -07001191 state_tokens[0] = STATE_FOG;
1192 state_tokens[1] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001193 break;
1194 case FOG_PARAMS:
Brian65319522007-02-21 11:08:21 -07001195 state_tokens[0] = STATE_FOG;
1196 state_tokens[1] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001197 break;
1198 }
1199 break;
1200
1201 case STATE_TEX_ENV:
1202 state_tokens[1] = parse_integer (inst, Program);
1203 switch (*(*inst)++) {
1204 case TEX_ENV_COLOR:
1205 state_tokens[0] = STATE_TEXENV_COLOR;
1206 break;
1207 }
1208 break;
1209
1210 case STATE_TEX_GEN:
1211 {
1212 GLuint type, coord;
1213
1214 state_tokens[0] = STATE_TEXGEN;
1215 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1216
1217 if (parse_texcoord_num (ctx, inst, Program, &coord))
1218 return 1;
1219 state_tokens[1] = coord;
1220
1221 /* EYE or OBJECT */
1222 type = *(*inst++);
1223
1224 /* 0 - s, 1 - t, 2 - r, 3 - q */
1225 coord = *(*inst++);
1226
1227 if (type == TEX_GEN_EYE) {
1228 switch (coord) {
1229 case COMPONENT_X:
1230 state_tokens[2] = STATE_TEXGEN_EYE_S;
1231 break;
1232 case COMPONENT_Y:
1233 state_tokens[2] = STATE_TEXGEN_EYE_T;
1234 break;
1235 case COMPONENT_Z:
1236 state_tokens[2] = STATE_TEXGEN_EYE_R;
1237 break;
1238 case COMPONENT_W:
1239 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1240 break;
1241 }
1242 }
1243 else {
1244 switch (coord) {
1245 case COMPONENT_X:
1246 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1247 break;
1248 case COMPONENT_Y:
1249 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1250 break;
1251 case COMPONENT_Z:
1252 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1253 break;
1254 case COMPONENT_W:
1255 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1256 break;
1257 }
1258 }
1259 }
1260 break;
1261
1262 case STATE_DEPTH:
1263 switch (*(*inst)++) {
1264 case DEPTH_RANGE:
1265 state_tokens[0] = STATE_DEPTH_RANGE;
1266 break;
1267 }
1268 break;
1269
1270 case STATE_CLIP_PLANE:
1271 state_tokens[0] = STATE_CLIPPLANE;
1272 state_tokens[1] = parse_integer (inst, Program);
Brianaa9d22a2007-02-23 11:21:03 -07001273 if (parse_clipplane_num (ctx, inst, Program,
1274 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001275 return 1;
1276 break;
1277
1278 case STATE_POINT:
1279 switch (*(*inst++)) {
1280 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001281 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001282 break;
1283
1284 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001285 state_tokens[0] = 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:
Brianaa9d22a2007-02-23 11:21:03 -07001292 if (parse_matrix(ctx, inst, Program,
1293 (GLint *) &state_tokens[0],
1294 (GLint *) &state_tokens[1],
1295 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001296 return 1;
1297
Brian65319522007-02-21 11:08:21 -07001298 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001299
1300 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001301 state_tokens[3] = parse_integer (inst, Program);
1302 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001303 program_error(ctx, Program->Position,
1304 "Second matrix index less than the first");
1305 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001306 return 1;
1307 }
1308 }
1309 else {
Brian65319522007-02-21 11:08:21 -07001310 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001311 (*inst)++;
1312 }
1313 break;
1314 }
1315
1316 return 0;
1317}
1318
1319/**
1320 * This parses a state string (rather, the binary version of it) into
1321 * a 6-token similar for the state fetching code in program.c
1322 *
1323 * One might ask, why fetch these parameters into just like you fetch
1324 * state when they are already stored in other places?
1325 *
1326 * Because of array offsets -> We can stick env/local parameters in the
1327 * middle of a parameter array and then index someplace into the array
1328 * when we execute.
1329 *
1330 * One optimization might be to only do this for the cases where the
1331 * env/local parameters end up inside of an array, and leave the
1332 * single parameters (or arrays of pure env/local pareameters) in their
1333 * respective register files.
1334 *
1335 * For ENV parameters, the format is:
1336 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1337 * state_tokens[1] = STATE_ENV
1338 * state_tokens[2] = the parameter index
1339 *
1340 * for LOCAL parameters, the format is:
1341 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1342 * state_tokens[1] = STATE_LOCAL
1343 * state_tokens[2] = the parameter index
1344 *
1345 * \param inst - the start in the binary arry to start working from
1346 * \param state_tokens - the storage for the 6-token state description
1347 * \return - 0 on sucess, 1 on failure
1348 */
1349static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001350parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001351 struct arb_program *Program,
1352 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001353{
1354 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1355 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1356 else
1357 state_tokens[0] = STATE_VERTEX_PROGRAM;
1358
1359
1360 switch (*(*inst)++) {
1361 case PROGRAM_PARAM_ENV:
1362 state_tokens[1] = STATE_ENV;
1363 state_tokens[2] = parse_integer (inst, Program);
1364
1365 /* Check state_tokens[2] against the number of ENV parameters available */
1366 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001367 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001368 ||
1369 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001370 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001371 program_error(ctx, Program->Position,
1372 "Invalid Program Env Parameter");
1373 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001374 return 1;
1375 }
1376
1377 break;
1378
1379 case PROGRAM_PARAM_LOCAL:
1380 state_tokens[1] = STATE_LOCAL;
1381 state_tokens[2] = parse_integer (inst, Program);
1382
1383 /* Check state_tokens[2] against the number of LOCAL parameters available */
1384 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001385 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001386 ||
1387 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001388 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001389 program_error(ctx, Program->Position,
1390 "Invalid Program Local Parameter");
1391 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001392 return 1;
1393 }
1394 break;
1395 }
1396
1397 return 0;
1398}
1399
1400/**
1401 * For ARB_vertex_program, programs are not allowed to use both an explicit
1402 * vertex attribute and a generic vertex attribute corresponding to the same
1403 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1404 *
1405 * This will walk our var_cache and make sure that nobody does anything fishy.
1406 *
1407 * \return 0 on sucess, 1 on error
1408 */
1409static GLuint
1410generic_attrib_check(struct var_cache *vc_head)
1411{
1412 int a;
1413 struct var_cache *curr;
1414 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1415 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1416
1417 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1418 explicitAttrib[a] = GL_FALSE;
1419 genericAttrib[a] = GL_FALSE;
1420 }
1421
1422 curr = vc_head;
1423 while (curr) {
1424 if (curr->type == vt_attrib) {
1425 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001426 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001427 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001428 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001429 }
1430
1431 curr = curr->next;
1432 }
1433
1434 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1435 if ((explicitAttrib[a]) && (genericAttrib[a]))
1436 return 1;
1437 }
1438
1439 return 0;
1440}
1441
1442/**
1443 * This will handle the binding side of an ATTRIB var declaration
1444 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001445 * \param inputReg returns the input register index, one of the
1446 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001447 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001448 */
1449static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001450parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001451 struct arb_program *Program,
1452 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001453{
Jouk Jansen40322e12004-04-05 08:50:36 +00001454 GLint err = 0;
1455
1456 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001457
Jouk Jansen40322e12004-04-05 08:50:36 +00001458 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1459 switch (*(*inst)++) {
1460 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001461 {
1462 GLint coord;
1463 err = parse_color_type (ctx, inst, Program, &coord);
1464 *inputReg = FRAG_ATTRIB_COL0 + coord;
1465 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001466 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001467 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001468 {
Brian8fa6f732007-02-01 09:24:41 -07001469 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001470 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1471 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1472 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001473 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001475 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001476 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001477 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001478 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001479 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001480 default:
1481 err = 1;
1482 break;
1483 }
1484 }
1485 else {
1486 switch (*(*inst)++) {
1487 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001488 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001489 break;
1490
1491 case VERTEX_ATTRIB_WEIGHT:
1492 {
1493 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001494 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001495 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001496#if 1
1497 /* hack for Warcraft (see bug 8060) */
1498 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001499 break;
Brian Paul3a557502006-09-05 23:15:29 +00001500#else
Brian Paula088f162006-09-05 23:08:51 +00001501 program_error(ctx, Program->Position,
1502 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001503 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001504#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001505 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001506
1507 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001508 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001509 break;
1510
1511 case VERTEX_ATTRIB_COLOR:
1512 {
1513 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001514 err = parse_color_type (ctx, inst, Program, &color);
1515 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001516 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001517 }
1518 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001519 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001520 }
1521 }
1522 break;
1523
1524 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001525 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 break;
1527
1528 case VERTEX_ATTRIB_TEXCOORD:
1529 {
Brian8fa6f732007-02-01 09:24:41 -07001530 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001531 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001532 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001533 }
1534 break;
1535
Jouk Jansen40322e12004-04-05 08:50:36 +00001536 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001537 /* Not supported at this time */
1538 {
1539 const char *msg = "ARB_palette_matrix not supported";
1540 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001541 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001542 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001543 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001544
1545 case VERTEX_ATTRIB_GENERIC:
1546 {
1547 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001548 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001549 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001550 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001551 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1552 * attributes do not alias the conventional vertex
1553 * attributes.
1554 */
1555 if (attrib > 0)
1556 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001557 else
1558 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001559 }
1560 }
1561 break;
1562
1563 default:
1564 err = 1;
1565 break;
1566 }
1567 }
1568
Jouk Jansen40322e12004-04-05 08:50:36 +00001569 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001570 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001571 }
1572
Brian Paulde997602005-11-12 17:53:14 +00001573 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001574
1575 return err;
1576}
1577
Brian Paul7aebaf32005-10-30 21:23:23 +00001578
Jouk Jansen40322e12004-04-05 08:50:36 +00001579/**
1580 * This translates between a binary token for an output variable type
1581 * and the mesa token for the same thing.
1582 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001583 * \param inst The parsed tokens
1584 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001585 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001586 */
1587static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001588parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001589 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001590{
Brian Paul7aebaf32005-10-30 21:23:23 +00001591 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001592
Brian Paul7aebaf32005-10-30 21:23:23 +00001593 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001594 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001595 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001596 GLuint out_color;
1597
Brian Paulbe76b7f2004-10-04 14:40:05 +00001598 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001599 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001600 */
1601 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001602 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001603 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001604 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001605 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001606 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1607 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001608 }
1609 break;
1610
1611 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001612 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001613 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001614 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001615 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001616 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001617 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001618 GLint color_type;
1619 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001620 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1621 if (err)
1622 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001623
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001625 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001627 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001629 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001630 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001631 }
1632 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001633 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001634 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001635 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001636 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001637 }
1638 /* primary color */
1639 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001640 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 }
1642 }
1643 }
1644 break;
1645
1646 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001647 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001648 break;
1649
1650 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001651 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001652 break;
1653
1654 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001655 {
1656 GLuint unit;
1657 if (parse_texcoord_num (ctx, inst, Program, &unit))
1658 return 1;
1659 *outputReg = VERT_RESULT_TEX0 + unit;
1660 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001661 break;
1662 }
1663
Brian Paulde997602005-11-12 17:53:14 +00001664 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001665
1666 return 0;
1667}
1668
Brian Paul7aebaf32005-10-30 21:23:23 +00001669
Jouk Jansen40322e12004-04-05 08:50:36 +00001670/**
1671 * This handles the declaration of ATTRIB variables
1672 *
1673 * XXX: Still needs
1674 * parse_vert_attrib_binding(), or something like that
1675 *
1676 * \return 0 on sucess, 1 on error
1677 */
1678static GLint
Brian Paula088f162006-09-05 23:08:51 +00001679parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001680 struct arb_program *Program)
1681{
1682 GLuint found;
1683 char *error_msg;
1684 struct var_cache *attrib_var;
1685
1686 attrib_var = parse_string (inst, vc_head, Program, &found);
1687 Program->Position = parse_position (inst);
1688 if (found) {
1689 error_msg = (char *)
1690 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1691 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1692 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001693 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001694 _mesa_free (error_msg);
1695 return 1;
1696 }
1697
1698 attrib_var->type = vt_attrib;
1699
Brian Paul7aebaf32005-10-30 21:23:23 +00001700 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001701 &attrib_var->attrib_is_generic))
1702 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001703
Brian Paul7aebaf32005-10-30 21:23:23 +00001704 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001705 program_error(ctx, Program->Position,
1706 "Cannot use both a generic vertex attribute "
1707 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001708 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001709 }
1710
1711 Program->Base.NumAttributes++;
1712 return 0;
1713}
1714
1715/**
1716 * \param use -- TRUE if we're called when declaring implicit parameters,
1717 * FALSE if we're declaraing variables. This has to do with
1718 * if we get a signed or unsigned float for scalar constants
1719 */
1720static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001721parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001722 struct var_cache *param_var,
1723 struct arb_program *Program, GLboolean use)
1724{
1725 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001726 GLuint err = 0;
Brianaa9d22a2007-02-23 11:21:03 -07001727 gl_state_index state_tokens[STATE_LENGTH];
Jouk Jansen40322e12004-04-05 08:50:36 +00001728 GLfloat const_values[4];
1729
Jouk Jansen40322e12004-04-05 08:50:36 +00001730 switch (*(*inst)++) {
1731 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001732 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1733 return 1;
1734
1735 /* If we adding STATE_MATRIX that has multiple rows, we need to
1736 * unroll it and call _mesa_add_state_reference() for each row
1737 */
Brian65319522007-02-21 11:08:21 -07001738 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1739 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1740 state_tokens[0] == STATE_MVP_MATRIX ||
1741 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1742 state_tokens[0] == STATE_PROGRAM_MATRIX)
1743 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001744 GLint row;
Brian65319522007-02-21 11:08:21 -07001745 const GLint first_row = state_tokens[2];
1746 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001747
1748 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001749 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001750
Brian Paulde997602005-11-12 17:53:14 +00001751 idx = _mesa_add_state_reference(Program->Base.Parameters,
1752 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001753 if (param_var->param_binding_begin == ~0U)
1754 param_var->param_binding_begin = idx;
1755 param_var->param_binding_length++;
1756 Program->Base.NumParameters++;
1757 }
1758 }
1759 else {
Brian Paulde997602005-11-12 17:53:14 +00001760 idx = _mesa_add_state_reference(Program->Base.Parameters,
1761 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001762 if (param_var->param_binding_begin == ~0U)
1763 param_var->param_binding_begin = idx;
1764 param_var->param_binding_length++;
1765 Program->Base.NumParameters++;
1766 }
1767 break;
1768
1769 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001770 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1771 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001772 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001773 if (param_var->param_binding_begin == ~0U)
1774 param_var->param_binding_begin = idx;
1775 param_var->param_binding_length++;
1776 Program->Base.NumParameters++;
1777
1778 /* Check if there is more: 0 -> we're done, else its an integer */
1779 if (**inst) {
1780 GLuint out_of_range, new_idx;
1781 GLuint start_idx = state_tokens[2] + 1;
1782 GLuint end_idx = parse_integer (inst, Program);
1783
1784 out_of_range = 0;
1785 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1786 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001787 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001788 || ((state_tokens[1] == STATE_LOCAL)
1789 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001790 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001791 out_of_range = 1;
1792 }
1793 else {
1794 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001795 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001796 || ((state_tokens[1] == STATE_LOCAL)
1797 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001798 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001799 out_of_range = 1;
1800 }
1801 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001802 program_error(ctx, Program->Position,
1803 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001804 return 1;
1805 }
1806
1807 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1808 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001809 idx = _mesa_add_state_reference(Program->Base.Parameters,
1810 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001811 param_var->param_binding_length++;
1812 Program->Base.NumParameters++;
1813 }
1814 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001815 else {
1816 (*inst)++;
1817 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001818 break;
1819
1820 case PARAM_CONSTANT:
1821 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001822 idx = _mesa_add_named_constant(Program->Base.Parameters,
1823 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001824 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001825 if (param_var->param_binding_begin == ~0U)
1826 param_var->param_binding_begin = idx;
1827 param_var->param_binding_length++;
1828 Program->Base.NumParameters++;
1829 break;
1830
1831 default:
Brian Paula088f162006-09-05 23:08:51 +00001832 program_error(ctx, Program->Position,
1833 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001834 return 1;
1835 }
1836
1837 /* Make sure we haven't blown past our parameter limits */
1838 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1839 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001840 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001841 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1842 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001843 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001844 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001845 return 1;
1846 }
1847
1848 return err;
1849}
1850
Brian Paul7aebaf32005-10-30 21:23:23 +00001851
Jouk Jansen40322e12004-04-05 08:50:36 +00001852/**
1853 * This picks out PARAM program parameter bindings.
1854 *
1855 * XXX: This needs to be stressed & tested
1856 *
1857 * \return 0 on sucess, 1 on error
1858 */
1859static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001860parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001861 struct arb_program *Program)
1862{
Brian Paulbd997cd2004-07-20 21:12:56 +00001863 GLuint found, err;
1864 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001865 struct var_cache *param_var;
1866
1867 err = 0;
1868 param_var = parse_string (inst, vc_head, Program, &found);
1869 Program->Position = parse_position (inst);
1870
1871 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001872 char *error_msg = (char *)
1873 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001874 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1875 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001876 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001877 _mesa_free (error_msg);
1878 return 1;
1879 }
1880
1881 specified_length = parse_integer (inst, Program);
1882
1883 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001884 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001885 return 1;
1886 }
1887
1888 param_var->type = vt_param;
1889 param_var->param_binding_length = 0;
1890
1891 /* Right now, everything is shoved into the main state register file.
1892 *
1893 * In the future, it would be nice to leave things ENV/LOCAL params
1894 * in their respective register files, if possible
1895 */
1896 param_var->param_binding_type = PROGRAM_STATE_VAR;
1897
1898 /* Remember to:
1899 * * - add each guy to the parameter list
1900 * * - increment the param_var->param_binding_len
1901 * * - store the param_var->param_binding_begin for the first one
1902 * * - compare the actual len to the specified len at the end
1903 */
1904 while (**inst != PARAM_NULL) {
1905 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1906 return 1;
1907 }
1908
1909 /* Test array length here! */
1910 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001911 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001912 program_error(ctx, Program->Position,
1913 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001914 }
1915 }
1916
1917 (*inst)++;
1918
1919 return 0;
1920}
1921
1922/**
1923 *
1924 */
1925static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001926parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001927 struct arb_program *Program, struct var_cache **new_var)
1928{
1929 struct var_cache *param_var;
1930
1931 /* First, insert a dummy entry into the var_cache */
1932 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001933 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001934 param_var->type = vt_param;
1935
1936 param_var->param_binding_length = 0;
1937 /* Don't fill in binding_begin; We use the default value of -1
1938 * to tell if its already initialized, elsewhere.
1939 *
1940 * param_var->param_binding_begin = 0;
1941 */
1942 param_var->param_binding_type = PROGRAM_STATE_VAR;
1943
1944 var_cache_append (vc_head, param_var);
1945
1946 /* Then fill it with juicy parameter goodness */
1947 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1948 return 1;
1949
1950 *new_var = param_var;
1951
1952 return 0;
1953}
1954
1955
1956/**
1957 * This handles the declaration of TEMP variables
1958 *
1959 * \return 0 on sucess, 1 on error
1960 */
1961static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001962parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001963 struct arb_program *Program)
1964{
1965 GLuint found;
1966 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001967
1968 while (**inst != 0) {
1969 temp_var = parse_string (inst, vc_head, Program, &found);
1970 Program->Position = parse_position (inst);
1971 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001972 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001973 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1974 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1975 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001976 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001977 _mesa_free (error_msg);
1978 return 1;
1979 }
1980
1981 temp_var->type = vt_temp;
1982
1983 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1984 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001985 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001986 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1987 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001988 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00001989 program_error(ctx, Program->Position,
1990 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00001991 return 1;
1992 }
1993
1994 temp_var->temp_binding = Program->Base.NumTemporaries;
1995 Program->Base.NumTemporaries++;
1996 }
1997 (*inst)++;
1998
1999 return 0;
2000}
2001
2002/**
2003 * This handles variables of the OUTPUT variety
2004 *
2005 * \return 0 on sucess, 1 on error
2006 */
2007static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002008parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002009 struct arb_program *Program)
2010{
2011 GLuint found;
2012 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002013 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002014
2015 output_var = parse_string (inst, vc_head, Program, &found);
2016 Program->Position = parse_position (inst);
2017 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002018 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002019 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2020 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2021 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002022 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002023 _mesa_free (error_msg);
2024 return 1;
2025 }
2026
2027 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002028
2029 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2030 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002031}
2032
2033/**
2034 * This handles variables of the ALIAS kind
2035 *
2036 * \return 0 on sucess, 1 on error
2037 */
2038static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002039parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002040 struct arb_program *Program)
2041{
2042 GLuint found;
2043 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002044
2045 temp_var = parse_string (inst, vc_head, Program, &found);
2046 Program->Position = parse_position (inst);
2047
2048 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002049 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002050 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2051 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2052 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002053 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002054 _mesa_free (error_msg);
2055 return 1;
2056 }
2057
2058 temp_var->type = vt_alias;
2059 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2060 Program->Position = parse_position (inst);
2061
2062 if (!found)
2063 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002064 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002065 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2066 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2067 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002068 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002069 _mesa_free (error_msg);
2070 return 1;
2071 }
2072
2073 return 0;
2074}
2075
2076/**
2077 * This handles variables of the ADDRESS kind
2078 *
2079 * \return 0 on sucess, 1 on error
2080 */
2081static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002082parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002083 struct arb_program *Program)
2084{
2085 GLuint found;
2086 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002087
2088 while (**inst != 0) {
2089 temp_var = parse_string (inst, vc_head, Program, &found);
2090 Program->Position = parse_position (inst);
2091 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002092 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002093 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2094 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2095 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002096 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002097 _mesa_free (error_msg);
2098 return 1;
2099 }
2100
2101 temp_var->type = vt_address;
2102
2103 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002104 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002105 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002106 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002107 return 1;
2108 }
2109
2110 temp_var->address_binding = Program->Base.NumAddressRegs;
2111 Program->Base.NumAddressRegs++;
2112 }
2113 (*inst)++;
2114
2115 return 0;
2116}
2117
2118/**
2119 * Parse a program declaration
2120 *
2121 * \return 0 on sucess, 1 on error
2122 */
2123static GLint
Brian Paula088f162006-09-05 23:08:51 +00002124parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002125 struct arb_program *Program)
2126{
2127 GLint err = 0;
2128
2129 switch (*(*inst)++) {
2130 case ADDRESS:
2131 err = parse_address (ctx, inst, vc_head, Program);
2132 break;
2133
2134 case ALIAS:
2135 err = parse_alias (ctx, inst, vc_head, Program);
2136 break;
2137
2138 case ATTRIB:
2139 err = parse_attrib (ctx, inst, vc_head, Program);
2140 break;
2141
2142 case OUTPUT:
2143 err = parse_output (ctx, inst, vc_head, Program);
2144 break;
2145
2146 case PARAM:
2147 err = parse_param (ctx, inst, vc_head, Program);
2148 break;
2149
2150 case TEMP:
2151 err = parse_temp (ctx, inst, vc_head, Program);
2152 break;
2153 }
2154
2155 return err;
2156}
2157
2158/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002159 * Handle the parsing out of a masked destination register, either for a
2160 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002161 *
2162 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002163 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002164 * position invariant
2165 *
2166 * \param File - The register file we write to
2167 * \param Index - The register index we write to
2168 * \param WriteMask - The mask controlling which components we write (1->write)
2169 *
2170 * \return 0 on sucess, 1 on error
2171 */
2172static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002173parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002174 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002175 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002176{
Brian Paul7aebaf32005-10-30 21:23:23 +00002177 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002178 struct var_cache *dst;
2179
2180 /* We either have a result register specified, or a
2181 * variable that may or may not be writable
2182 */
2183 switch (*(*inst)++) {
2184 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002185 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002186 return 1;
2187 *File = PROGRAM_OUTPUT;
2188 break;
2189
2190 case REGISTER_ESTABLISHED_NAME:
2191 dst = parse_string (inst, vc_head, Program, &result);
2192 Program->Position = parse_position (inst);
2193
2194 /* If the name has never been added to our symbol table, we're hosed */
2195 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002196 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002197 return 1;
2198 }
2199
2200 switch (dst->type) {
2201 case vt_output:
2202 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002203 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002204 break;
2205
2206 case vt_temp:
2207 *File = PROGRAM_TEMPORARY;
2208 *Index = dst->temp_binding;
2209 break;
2210
2211 /* If the var type is not vt_output or vt_temp, no go */
2212 default:
Brian Paula088f162006-09-05 23:08:51 +00002213 program_error(ctx, Program->Position,
2214 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002215 return 1;
2216 }
2217 break;
2218
2219 default:
Brian Paula088f162006-09-05 23:08:51 +00002220 program_error(ctx, Program->Position,
2221 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002222 return 1;
2223 }
2224
2225
2226 /* Position invariance test */
2227 /* This test is done now in syntax portion - when position invariance OPTION
2228 is specified, "result.position" rule is disabled so there is no way
2229 to write the position
2230 */
2231 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2232 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002233 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002234 "Vertex program specified position invariance and wrote vertex position");
2235 }*/
2236
2237 /* And then the mask.
2238 * w,a -> bit 0
2239 * z,b -> bit 1
2240 * y,g -> bit 2
2241 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002242 *
2243 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002244 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002245 tmp = (GLint) *(*inst)++;
2246 *WriteMask = (((tmp>>3) & 0x1) |
2247 ((tmp>>1) & 0x2) |
2248 ((tmp<<1) & 0x4) |
2249 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002250
2251 return 0;
2252}
2253
2254
2255/**
2256 * Handle the parsing of a address register
2257 *
2258 * \param Index - The register index we write to
2259 *
2260 * \return 0 on sucess, 1 on error
2261 */
2262static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002263parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002264 struct var_cache **vc_head,
2265 struct arb_program *Program, GLint * Index)
2266{
2267 struct var_cache *dst;
2268 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002269
2270 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002271
2272 dst = parse_string (inst, vc_head, Program, &result);
2273 Program->Position = parse_position (inst);
2274
2275 /* If the name has never been added to our symbol table, we're hosed */
2276 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002277 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002278 return 1;
2279 }
2280
2281 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002282 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002283 return 1;
2284 }
2285
2286 return 0;
2287}
2288
Brian Paul8e8fa632005-07-01 02:03:33 +00002289#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002290/**
2291 * Handle the parsing out of a masked address register
2292 *
2293 * \param Index - The register index we write to
2294 * \param WriteMask - The mask controlling which components we write (1->write)
2295 *
2296 * \return 0 on sucess, 1 on error
2297 */
2298static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002299parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002300 struct var_cache **vc_head,
2301 struct arb_program *Program, GLint * Index,
2302 GLboolean * WriteMask)
2303{
2304 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2305 return 1;
2306
2307 /* This should be 0x8 */
2308 (*inst)++;
2309
2310 /* Writemask of .x is implied */
2311 WriteMask[0] = 1;
2312 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2313
2314 return 0;
2315}
Brian Paul8e8fa632005-07-01 02:03:33 +00002316#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002317
2318/**
2319 * Parse out a swizzle mask.
2320 *
Brian Paul32df89e2005-10-29 18:26:43 +00002321 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002322 *
2323 * The len parameter allows us to grab 4 components for a vector
2324 * swizzle, or just 1 component for a scalar src register selection
2325 */
Brian Paul32df89e2005-10-29 18:26:43 +00002326static void
Brian Paula088f162006-09-05 23:08:51 +00002327parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002328{
Brian Paul32df89e2005-10-29 18:26:43 +00002329 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002330
Brian Paul32df89e2005-10-29 18:26:43 +00002331 for (i = 0; i < 4; i++)
2332 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002333
Brian Paul32df89e2005-10-29 18:26:43 +00002334 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002335 switch (*(*inst)++) {
2336 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002337 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002338 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002339 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002340 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002341 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002342 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002343 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002344 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002345 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002346 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002347 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002348 default:
2349 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2350 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002351 }
2352 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002353}
2354
Jouk Jansen40322e12004-04-05 08:50:36 +00002355
Brian Paul32df89e2005-10-29 18:26:43 +00002356/**
2357 * Parse an extended swizzle mask which is a sequence of
2358 * four x/y/z/w/0/1 tokens.
2359 * \return swizzle four swizzle values
2360 * \return negateMask four element bitfield
2361 */
2362static void
Brian Paula088f162006-09-05 23:08:51 +00002363parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002364 GLubyte *negateMask)
2365{
2366 GLint i;
2367
2368 *negateMask = 0x0;
2369 for (i = 0; i < 4; i++) {
2370 GLubyte swz;
2371 if (parse_sign(inst) == -1)
2372 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002373
2374 swz = *(*inst)++;
2375
2376 switch (swz) {
2377 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002378 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002379 break;
2380 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002381 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002382 break;
2383 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002384 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002385 break;
2386 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002387 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002388 break;
2389 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002390 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002391 break;
2392 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002393 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002394 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002395 default:
2396 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2397 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002398 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002399 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002400}
2401
2402
2403static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002404parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2405 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002406 struct arb_program *Program,
2407 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002408 GLboolean *IsRelOffset )
2409{
2410 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002411 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002412 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002413
Keith Whitwell7c26b612005-04-21 14:46:57 +00002414 *IsRelOffset = 0;
2415
Jouk Jansen40322e12004-04-05 08:50:36 +00002416 /* And the binding for the src */
2417 switch (*(*inst)++) {
2418 case REGISTER_ATTRIB:
2419 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002420 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002421 return 1;
2422 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002423 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002424
2425 /* We need to insert a dummy variable into the var_cache so we can
2426 * catch generic vertex attrib aliasing errors
2427 */
2428 var_cache_create(&src);
2429 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002430 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002431 src->attrib_binding = binding;
2432 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002433 var_cache_append(vc_head, src);
2434 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002435 program_error(ctx, Program->Position,
2436 "Cannot use both a generic vertex attribute "
2437 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002438 return 1;
2439 }
2440 break;
2441
2442 case REGISTER_PARAM:
2443 switch (**inst) {
2444 case PARAM_ARRAY_ELEMENT:
2445 (*inst)++;
2446 src = parse_string (inst, vc_head, Program, &found);
2447 Program->Position = parse_position (inst);
2448
2449 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002450 program_error(ctx, Program->Position,
2451 "2: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002452 return 1;
2453 }
2454
Brian Paul95801792005-12-06 15:41:43 +00002455 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002456
2457 switch (*(*inst)++) {
2458 case ARRAY_INDEX_ABSOLUTE:
2459 offset = parse_integer (inst, Program);
2460
2461 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002462 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002463 program_error(ctx, Program->Position,
2464 "Index out of range");
2465 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002466 return 1;
2467 }
2468
2469 *Index = src->param_binding_begin + offset;
2470 break;
2471
2472 case ARRAY_INDEX_RELATIVE:
2473 {
2474 GLint addr_reg_idx, rel_off;
2475
2476 /* First, grab the address regiseter */
2477 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2478 return 1;
2479
2480 /* And the .x */
2481 ((*inst)++);
2482 ((*inst)++);
2483 ((*inst)++);
2484 ((*inst)++);
2485
2486 /* Then the relative offset */
2487 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2488
2489 /* And store it properly */
2490 *Index = src->param_binding_begin + rel_off;
2491 *IsRelOffset = 1;
2492 }
2493 break;
2494 }
2495 break;
2496
2497 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002498 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2499 return 1;
2500
Brian Paul95801792005-12-06 15:41:43 +00002501 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002502 *Index = src->param_binding_begin;
2503 break;
2504 }
2505 break;
2506
2507 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002508 src = parse_string (inst, vc_head, Program, &found);
2509 Program->Position = parse_position (inst);
2510
2511 /* If the name has never been added to our symbol table, we're hosed */
2512 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002513 program_error(ctx, Program->Position,
2514 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002515 return 1;
2516 }
2517
2518 switch (src->type) {
2519 case vt_attrib:
2520 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002521 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002522 break;
2523
2524 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2525 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002526 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002527 *Index = src->param_binding_begin;
2528 break;
2529
2530 case vt_temp:
2531 *File = PROGRAM_TEMPORARY;
2532 *Index = src->temp_binding;
2533 break;
2534
2535 /* If the var type is vt_output no go */
2536 default:
Brian Paula088f162006-09-05 23:08:51 +00002537 program_error(ctx, Program->Position,
2538 "destination register is read only");
2539 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002540 return 1;
2541 }
2542 break;
2543
2544 default:
Brian Paula088f162006-09-05 23:08:51 +00002545 program_error(ctx, Program->Position,
2546 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002547 return 1;
2548 }
2549
2550 return 0;
2551}
2552
2553/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002554 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002555 */
2556static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002557parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002558 struct var_cache **vc_head,
2559 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002560 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002561{
Brian Paul7aebaf32005-10-30 21:23:23 +00002562 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002563 GLint index;
2564 GLboolean negate;
2565 GLubyte swizzle[4];
2566 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002567
Jouk Jansen40322e12004-04-05 08:50:36 +00002568 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002569 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002570
2571 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002572 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002573 return 1;
2574
2575 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002576 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002577
Brian Paul32df89e2005-10-29 18:26:43 +00002578 reg->File = file;
2579 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002580 reg->NegateBase = negate;
2581 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002582 return 0;
2583}
2584
Jouk Jansen40322e12004-04-05 08:50:36 +00002585
Brian Paulb7fc1c32006-08-30 23:38:03 +00002586/**
2587 * Parse fragment program destination register.
2588 * \return 1 if error, 0 if no error.
2589 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002590static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002591parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002592 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002593 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002594{
Brian Paul7aebaf32005-10-30 21:23:23 +00002595 GLint mask;
2596 GLuint idx;
2597 enum register_file file;
2598
Keith Whitwell7c26b612005-04-21 14:46:57 +00002599 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002600 return 1;
2601
Keith Whitwell7c26b612005-04-21 14:46:57 +00002602 reg->File = file;
2603 reg->Index = idx;
2604 reg->WriteMask = mask;
2605 return 0;
2606}
2607
2608
Brian Paul7aebaf32005-10-30 21:23:23 +00002609/**
2610 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002611 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002612 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002613static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002614parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002615 struct var_cache **vc_head,
2616 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002617 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002618{
Brian Paul7aebaf32005-10-30 21:23:23 +00002619 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002620 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002621 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002622 GLubyte Swizzle[4];
2623 GLboolean IsRelOffset;
2624
2625 /* Grab the sign */
2626 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2627
2628 /* And the src reg */
2629 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2630 return 1;
2631
2632 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002633 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002634
Keith Whitwell7c26b612005-04-21 14:46:57 +00002635 reg->File = File;
2636 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002637 reg->NegateBase = Negate;
2638 reg->Swizzle = (Swizzle[0] << 0);
2639
Jouk Jansen40322e12004-04-05 08:50:36 +00002640 return 0;
2641}
2642
Keith Whitwell7c26b612005-04-21 14:46:57 +00002643
Jouk Jansen40322e12004-04-05 08:50:36 +00002644/**
2645 * This is a big mother that handles getting opcodes into the instruction
2646 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002647 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002648 */
2649static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002650parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002651 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002652 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002653{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002654 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002655 GLuint texcoord;
2656 GLubyte instClass, type, code;
2657 GLboolean rel;
2658
Brian Pauld6272e02006-10-29 18:03:16 +00002659 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002660
2661 /* Record the position in the program string for debugging */
2662 fp->StringPos = Program->Position;
2663
2664 /* OP_ALU_INST or OP_TEX_INST */
2665 instClass = *(*inst)++;
2666
2667 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2668 * OP_TEX_{SAMPLE, KIL}
2669 */
2670 type = *(*inst)++;
2671
2672 /* The actual opcode name */
2673 code = *(*inst)++;
2674
2675 /* Increment the correct count */
2676 switch (instClass) {
2677 case OP_ALU_INST:
2678 Program->NumAluInstructions++;
2679 break;
2680 case OP_TEX_INST:
2681 Program->NumTexInstructions++;
2682 break;
2683 }
2684
Jouk Jansen40322e12004-04-05 08:50:36 +00002685 switch (type) {
2686 case OP_ALU_VECTOR:
2687 switch (code) {
2688 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002689 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002690 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002691 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002692 break;
2693
2694 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002695 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002696 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002697 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002698 break;
2699
2700 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002701 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002702 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002703 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002704 break;
2705
2706 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002707 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002708 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002709 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002710 break;
2711
2712 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002713 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002714 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002715 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002716 break;
2717 }
2718
Keith Whitwell7c26b612005-04-21 14:46:57 +00002719 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002720 return 1;
2721
Keith Whitwell7c26b612005-04-21 14:46:57 +00002722 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002723 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002724 break;
2725
2726 case OP_ALU_SCALAR:
2727 switch (code) {
2728 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002729 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002730 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002731 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002732 break;
2733
2734 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002735 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002736 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002737 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002738 break;
2739
2740 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002741 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002742 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002743 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002744 break;
2745
2746 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002747 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002748 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002749 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002750 break;
2751
2752 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002753 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002755 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002756 break;
2757
2758 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002759 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002761 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002762 break;
2763
2764 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002765 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002766 case OP_SCS:
2767
Brian Paul7e807512005-11-05 17:10:45 +00002768 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002769 break;
2770 }
2771
Keith Whitwell7c26b612005-04-21 14:46:57 +00002772 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002773 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002774
2775 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002776 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002777 break;
2778
2779 case OP_ALU_BINSC:
2780 switch (code) {
2781 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002782 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002784 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 break;
2786 }
2787
Keith Whitwell7c26b612005-04-21 14:46:57 +00002788 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002789 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002790
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002792 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002794 }
2795 break;
2796
2797
2798 case OP_ALU_BIN:
2799 switch (code) {
2800 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002801 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002802 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002803 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002804 break;
2805
2806 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002807 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002808 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002809 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002810 break;
2811
2812 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002813 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002814 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002815 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002816 break;
2817
2818 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002819 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002820 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002821 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002822 break;
2823
2824 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002825 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002826 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002827 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002828 break;
2829
2830 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002831 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002832 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002833 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002834 break;
2835
2836 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002837 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002838 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002839 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002840 break;
2841
2842 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002843 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002845 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002846 break;
2847
2848 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002849 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002850 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002851 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002852 break;
2853
2854 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002855 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002856 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002857 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002858 break;
2859
2860 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002861 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002862 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002863 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002864 break;
2865
2866 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002867 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002868 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002869 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002870 break;
2871 }
2872
Keith Whitwell7c26b612005-04-21 14:46:57 +00002873 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002874 return 1;
2875 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002876 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2877 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002878 }
2879 break;
2880
2881 case OP_ALU_TRI:
2882 switch (code) {
2883 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002884 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002886 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002887 break;
2888
2889 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002890 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002892 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 break;
2894
2895 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002896 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002898 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 break;
2900 }
2901
Keith Whitwell7c26b612005-04-21 14:46:57 +00002902 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002903 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002904
Jouk Jansen40322e12004-04-05 08:50:36 +00002905 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002906 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2907 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 }
2909 break;
2910
2911 case OP_ALU_SWZ:
2912 switch (code) {
2913 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002914 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002915 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002916 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002917 break;
2918 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002919 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002920 return 1;
2921
Keith Whitwell7c26b612005-04-21 14:46:57 +00002922 {
Brian Paul32df89e2005-10-29 18:26:43 +00002923 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002924 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002925 enum register_file file;
2926 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002927
Brian Paul32df89e2005-10-29 18:26:43 +00002928 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002929 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002930 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2931 fp->SrcReg[0].File = file;
2932 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002933 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002934 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2935 swizzle[1],
2936 swizzle[2],
2937 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002938 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002939 break;
2940
2941 case OP_TEX_SAMPLE:
2942 switch (code) {
2943 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002944 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002945 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002946 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002947 break;
2948
2949 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002950 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002951 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002952 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002953 break;
2954
2955 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002956 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002957 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002958 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002959 break;
2960 }
2961
Keith Whitwell7c26b612005-04-21 14:46:57 +00002962 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002963 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002964
2965 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002966 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002967
2968 /* texImageUnit */
2969 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2970 return 1;
2971 fp->TexSrcUnit = texcoord;
2972
2973 /* texTarget */
2974 switch (*(*inst)++) {
2975 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00002976 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002977 break;
2978 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00002979 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002980 break;
2981 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00002982 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002983 break;
2984 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00002985 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002986 break;
2987 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00002988 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002989 break;
2990 case TEXTARGET_SHADOW1D:
2991 case TEXTARGET_SHADOW2D:
2992 case TEXTARGET_SHADOWRECT:
2993 /* TODO ARB_fragment_program_shadow code */
2994 break;
2995 }
Brian Paulb7fc1c32006-08-30 23:38:03 +00002996 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
2997 /* Check that both "2D" and "CUBE" (for example) aren't both used */
2998 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00002999 program_error(ctx, Program->Position,
3000 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003001 return 1;
3002 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003003 break;
3004
3005 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003006 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003007 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003008 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003009 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003010 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003011 default:
3012 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3013 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003014 }
3015
3016 return 0;
3017}
3018
Keith Whitwell7c26b612005-04-21 14:46:57 +00003019static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003020parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003021 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003022 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003023{
Brian Paul7aebaf32005-10-30 21:23:23 +00003024 GLint mask;
3025 GLuint idx;
3026 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003027
3028 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3029 return 1;
3030
3031 reg->File = file;
3032 reg->Index = idx;
3033 reg->WriteMask = mask;
3034 return 0;
3035}
3036
3037/**
3038 * Handle the parsing out of a masked address register
3039 *
3040 * \param Index - The register index we write to
3041 * \param WriteMask - The mask controlling which components we write (1->write)
3042 *
3043 * \return 0 on sucess, 1 on error
3044 */
3045static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003046parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003047 struct var_cache **vc_head,
3048 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003049 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003050{
3051 GLint idx;
3052
3053 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3054 return 1;
3055
3056 /* This should be 0x8 */
3057 (*inst)++;
3058
3059 reg->File = PROGRAM_ADDRESS;
3060 reg->Index = idx;
3061
3062 /* Writemask of .x is implied */
3063 reg->WriteMask = 0x1;
3064 return 0;
3065}
3066
3067/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003068 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003069 */
3070static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003071parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003072 struct var_cache **vc_head,
3073 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003074 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003075{
Brian Paul7aebaf32005-10-30 21:23:23 +00003076 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003077 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003078 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003079 GLubyte swizzle[4];
3080 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003081
3082 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003083 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003084
3085 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003086 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003087 return 1;
3088
3089 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003090 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003091
Brian Paul32df89e2005-10-29 18:26:43 +00003092 reg->File = file;
3093 reg->Index = index;
3094 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3095 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003096 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003097 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003098 return 0;
3099}
3100
3101
3102static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003103parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003104 struct var_cache **vc_head,
3105 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003106 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003107{
Brian Paul7aebaf32005-10-30 21:23:23 +00003108 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003109 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003110 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003111 GLubyte Swizzle[4];
3112 GLboolean IsRelOffset;
3113
3114 /* Grab the sign */
3115 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3116
3117 /* And the src reg */
3118 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3119 return 1;
3120
3121 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003122 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003123
3124 reg->File = File;
3125 reg->Index = Index;
3126 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003127 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003128 reg->RelAddr = IsRelOffset;
3129 return 0;
3130}
3131
3132
Jouk Jansen40322e12004-04-05 08:50:36 +00003133/**
3134 * This is a big mother that handles getting opcodes into the instruction
3135 * and handling the src & dst registers for vertex program instructions
3136 */
3137static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003138parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003139 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003140 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003141{
3142 GLint a;
3143 GLubyte type, code;
3144
3145 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3146 type = *(*inst)++;
3147
3148 /* The actual opcode name */
3149 code = *(*inst)++;
3150
Brian Pauld6272e02006-10-29 18:03:16 +00003151 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003152 /* Record the position in the program string for debugging */
3153 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003154
3155 switch (type) {
3156 /* XXX: */
3157 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003158 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003159
3160 /* Remember to set SrcReg.RelAddr; */
3161
3162 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003163 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003164 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003165
Jouk Jansen40322e12004-04-05 08:50:36 +00003166 vp->DstReg.File = PROGRAM_ADDRESS;
3167
3168 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003169 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003170 return 1;
3171
3172 break;
3173
3174 case OP_ALU_VECTOR:
3175 switch (code) {
3176 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003177 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003178 break;
3179 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003180 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003181 break;
3182 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003183 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003184 break;
3185 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003186 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003187 break;
3188 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003189 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003190 break;
3191 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003192
3193 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003194 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003195
3196 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003197 return 1;
3198 break;
3199
3200 case OP_ALU_SCALAR:
3201 switch (code) {
3202 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003203 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003204 break;
3205 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003206 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003207 break;
3208 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003209 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003210 break;
3211 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003212 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003213 break;
3214 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003215 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003216 break;
3217 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003218 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003219 break;
3220 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003221 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003222 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003223
3224 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003225 return 1;
3226 break;
3227
3228 case OP_ALU_BINSC:
3229 switch (code) {
3230 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003231 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003232 break;
3233 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003234 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003236
Jouk Jansen40322e12004-04-05 08:50:36 +00003237 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003238 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003239 return 1;
3240 }
3241 break;
3242
3243 case OP_ALU_BIN:
3244 switch (code) {
3245 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003246 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003247 break;
3248 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003249 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003250 break;
3251 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003252 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003253 break;
3254 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003255 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003256 break;
3257 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003258 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003259 break;
3260 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003261 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003262 break;
3263 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003264 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003265 break;
3266 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003267 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003268 break;
3269 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003270 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003271 break;
3272 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003273 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003274 break;
3275 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003276 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003277 break;
3278 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003279 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003280 break;
3281 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003282 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003283 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003284
Jouk Jansen40322e12004-04-05 08:50:36 +00003285 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003286 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003287 return 1;
3288 }
3289 break;
3290
3291 case OP_ALU_TRI:
3292 switch (code) {
3293 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003294 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003295 break;
3296 }
3297
Keith Whitwell7c26b612005-04-21 14:46:57 +00003298 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003299 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003300
Jouk Jansen40322e12004-04-05 08:50:36 +00003301 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003302 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003303 return 1;
3304 }
3305 break;
3306
3307 case OP_ALU_SWZ:
3308 switch (code) {
3309 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003310 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003311 break;
3312 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003313 {
Brian Paul32df89e2005-10-29 18:26:43 +00003314 GLubyte swizzle[4];
3315 GLubyte negateMask;
3316 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003317 enum register_file file;
3318 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003319
Keith Whitwell7c26b612005-04-21 14:46:57 +00003320 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3321 return 1;
3322
Brian Paul32df89e2005-10-29 18:26:43 +00003323 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003324 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003325 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3326 vp->SrcReg[0].File = file;
3327 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003328 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003329 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3330 swizzle[1],
3331 swizzle[2],
3332 swizzle[3]);
3333 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003334 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003335 break;
3336 }
3337 return 0;
3338}
3339
3340#if DEBUG_PARSING
3341
3342static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003343debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3344 struct arb_program *Program)
3345{
3346 struct var_cache *vc;
3347 GLint a, b;
3348
Brianb618ac82007-02-22 09:39:25 -07003349 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003350
3351 /* First of all, print out the contents of the var_cache */
3352 vc = vc_head;
3353 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003354 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003355 switch (vc->type) {
3356 case vt_none:
3357 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3358 break;
3359 case vt_attrib:
3360 fprintf (stderr, "ATTRIB %s\n", vc->name);
3361 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3362 break;
3363 case vt_param:
3364 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3365 vc->param_binding_begin, vc->param_binding_length);
3366 b = vc->param_binding_begin;
3367 for (a = 0; a < vc->param_binding_length; a++) {
3368 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003369 Program->Base.Parameters->Parameters[a + b].Name);
3370 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3371 const char *s;
3372 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3373 [a + b].StateIndexes);
3374 fprintf(stderr, "%s\n", s);
3375 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003376 }
3377 else
3378 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003379 Program->Base.Parameters->ParameterValues[a + b][0],
3380 Program->Base.Parameters->ParameterValues[a + b][1],
3381 Program->Base.Parameters->ParameterValues[a + b][2],
3382 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003383 }
3384 break;
3385 case vt_temp:
3386 fprintf (stderr, "TEMP %s\n", vc->name);
3387 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3388 break;
3389 case vt_output:
3390 fprintf (stderr, "OUTPUT %s\n", vc->name);
3391 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3392 break;
3393 case vt_alias:
3394 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003395 fprintf (stderr, " binding: 0x%p (%s)\n",
3396 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003397 break;
Brianb618ac82007-02-22 09:39:25 -07003398 default:
3399 /* nothing */
3400 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003401 }
3402 vc = vc->next;
3403 }
3404}
3405
Brian Paul72030e02005-11-03 03:30:34 +00003406#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003407
3408
3409/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003410 * The main loop for parsing a fragment or vertex program
3411 *
Brian Paul72030e02005-11-03 03:30:34 +00003412 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003413 */
Brian Paul72030e02005-11-03 03:30:34 +00003414static GLint
Brian Paula088f162006-09-05 23:08:51 +00003415parse_instructions(GLcontext * ctx, const GLubyte * inst,
3416 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003417{
Brian Paul72030e02005-11-03 03:30:34 +00003418 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3419 ? ctx->Const.FragmentProgram.MaxInstructions
3420 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003421 GLint err = 0;
3422
Brian Paul72030e02005-11-03 03:30:34 +00003423 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3424
Jouk Jansen40322e12004-04-05 08:50:36 +00003425 Program->MajorVersion = (GLuint) * inst++;
3426 Program->MinorVersion = (GLuint) * inst++;
3427
3428 while (*inst != END) {
3429 switch (*inst++) {
3430
3431 case OPTION:
3432 switch (*inst++) {
3433 case ARB_PRECISION_HINT_FASTEST:
3434 Program->PrecisionOption = GL_FASTEST;
3435 break;
3436
3437 case ARB_PRECISION_HINT_NICEST:
3438 Program->PrecisionOption = GL_NICEST;
3439 break;
3440
3441 case ARB_FOG_EXP:
3442 Program->FogOption = GL_EXP;
3443 break;
3444
3445 case ARB_FOG_EXP2:
3446 Program->FogOption = GL_EXP2;
3447 break;
3448
3449 case ARB_FOG_LINEAR:
3450 Program->FogOption = GL_LINEAR;
3451 break;
3452
3453 case ARB_POSITION_INVARIANT:
3454 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003455 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003456 break;
3457
3458 case ARB_FRAGMENT_PROGRAM_SHADOW:
3459 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3460 /* TODO ARB_fragment_program_shadow code */
3461 }
3462 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003463
3464 case ARB_DRAW_BUFFERS:
3465 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3466 /* do nothing for now */
3467 }
3468 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003469 }
3470 break;
3471
3472 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003473 /* check length */
3474 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003475 program_error(ctx, Program->Position,
3476 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003477 return 1;
3478 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003479 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003480 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003481 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003482 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003483 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003484 }
3485 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003486 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003487 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003488 }
3489
Brian Paul72030e02005-11-03 03:30:34 +00003490 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003491 Program->Base.NumInstructions++;
3492 break;
3493
3494 case DECLARATION:
3495 err = parse_declaration (ctx, &inst, vc_head, Program);
3496 break;
3497
3498 default:
3499 break;
3500 }
3501
3502 if (err)
3503 break;
3504 }
3505
3506 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003507 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003508 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003509 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003510 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003511 /* YYY Wrong Position in program, whatever, at least not random -> crash
3512 Program->Position = parse_position (&inst);
3513 */
Brian Paul8c41a142005-11-19 15:36:28 +00003514 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003515 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003516 Program->Base.NumInstructions++;
3517
Brian Paul05051032005-11-01 04:36:33 +00003518 /*
3519 * Initialize native counts to logical counts. The device driver may
3520 * change them if program is translated into a hardware program.
3521 */
3522 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3523 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3524 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3525 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3526 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003527
Jouk Jansen40322e12004-04-05 08:50:36 +00003528 return err;
3529}
3530
Brian Paul7aebaf32005-10-30 21:23:23 +00003531
Jouk Jansen40322e12004-04-05 08:50:36 +00003532/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003533LONGSTRING static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003534#include "grammar_syn.h"
3535;
3536
Brian Paul7aebaf32005-10-30 21:23:23 +00003537
Brian Paul8c41a142005-11-19 15:36:28 +00003538/**
3539 * Set a grammar parameter.
3540 * \param name the grammar parameter
3541 * \param value the new parameter value
3542 * \return 0 if OK, 1 if error
3543 */
3544static int
3545set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003546{
3547 char error_msg[300];
3548 GLint error_pos;
3549
Brian Paul8c41a142005-11-19 15:36:28 +00003550 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003551 return 0;
3552
3553 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3554 _mesa_set_program_error (ctx, error_pos, error_msg);
3555 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3556 return 1;
3557}
3558
Brian Paul8c41a142005-11-19 15:36:28 +00003559
3560/**
3561 * Enable support for the given language option in the parser.
3562 * \return 1 if OK, 0 if error
3563 */
3564static int
3565enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003566{
Brian Paul8c41a142005-11-19 15:36:28 +00003567 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003568}
3569
Brian Paul8c41a142005-11-19 15:36:28 +00003570
3571/**
3572 * Enable parser extensions based on which OpenGL extensions are supported
3573 * by this rendering context.
3574 *
3575 * \return GL_TRUE if OK, GL_FALSE if error.
3576 */
3577static GLboolean
3578enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003579{
Brian Paul8c41a142005-11-19 15:36:28 +00003580#if 0
3581 /* These are not supported at this time */
3582 if ((ctx->Extensions.ARB_vertex_blend ||
3583 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003584 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003585 return GL_FALSE;
3586 if (ctx->Extensions.ARB_matrix_palette
3587 && !enable_ext(ctx, id, "matrix_palette"))
3588 return GL_FALSE;
3589 if (ctx->Extensions.ARB_fragment_program_shadow
3590 && !enable_ext(ctx, id, "fragment_program_shadow"))
3591 return GL_FALSE;
3592#endif
3593 if (ctx->Extensions.EXT_point_parameters
3594 && !enable_ext(ctx, id, "point_parameters"))
3595 return GL_FALSE;
3596 if (ctx->Extensions.EXT_secondary_color
3597 && !enable_ext(ctx, id, "secondary_color"))
3598 return GL_FALSE;
3599 if (ctx->Extensions.EXT_fog_coord
3600 && !enable_ext(ctx, id, "fog_coord"))
3601 return GL_FALSE;
3602 if (ctx->Extensions.NV_texture_rectangle
3603 && !enable_ext(ctx, id, "texture_rectangle"))
3604 return GL_FALSE;
3605 if (ctx->Extensions.ARB_draw_buffers
3606 && !enable_ext(ctx, id, "draw_buffers"))
3607 return GL_FALSE;
3608
Brian Paul3a557502006-09-05 23:15:29 +00003609#if 1
3610 /* hack for Warcraft (see bug 8060) */
3611 enable_ext(ctx, id, "vertex_blend");
3612#endif
3613
Brian Paul8c41a142005-11-19 15:36:28 +00003614 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003615}
3616
Brian Paul8c41a142005-11-19 15:36:28 +00003617
Jouk Jansen40322e12004-04-05 08:50:36 +00003618/**
3619 * This kicks everything off.
3620 *
3621 * \param ctx - The GL Context
3622 * \param str - The program string
3623 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003624 * \param program - The arb_program struct to return all the parsed info in
3625 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003626 */
Brian Paul8c41a142005-11-19 15:36:28 +00003627static GLboolean
3628_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3629 const GLubyte *str, GLsizei len,
3630 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003631{
3632 GLint a, err, error_pos;
3633 char error_msg[300];
3634 GLuint parsed_len;
3635 struct var_cache *vc_head;
3636 grammar arbprogram_syn_id;
3637 GLubyte *parsed, *inst;
3638 GLubyte *strz = NULL;
3639 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3640
Brian Paul8c41a142005-11-19 15:36:28 +00003641 /* set the program target before parsing */
3642 program->Base.Target = target;
3643
Brian Paul7f76b8f2004-09-10 01:05:39 +00003644 /* Reset error state */
3645 _mesa_set_program_error(ctx, -1, NULL);
3646
Brian Paul8c41a142005-11-19 15:36:28 +00003647 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003648 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003649 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003650 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003651 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003652
3653 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3654 if (grammar_syn_id == 0) {
3655 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003656 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003657 _mesa_set_program_error (ctx, error_pos, error_msg);
3658 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003659 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003660 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003661 }
3662
Brian Paul8c41a142005-11-19 15:36:28 +00003663 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3664 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003665
Brian Paul49a80ca2006-04-28 15:40:11 +00003666 /* 'parsed' is unused here */
3667 _mesa_free (parsed);
3668 parsed = NULL;
3669
Brian Paul45703642005-10-29 15:52:31 +00003670 /* NOTE: we can't destroy grammar_syn_id right here because
3671 * grammar_destroy() can reset the last error
3672 */
Brian Paul8c41a142005-11-19 15:36:28 +00003673 if (err) {
3674 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003675 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3676 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003677 _mesa_error (ctx, GL_INVALID_OPERATION,
3678 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003679 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003680 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003681 }
3682
3683 grammar_destroy (grammar_syn_id);
3684
3685 arbprogram_syn_is_ok = 1;
3686 }
3687
3688 /* create the grammar object */
3689 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3690 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003691 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003692 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3693 _mesa_set_program_error (ctx, error_pos, error_msg);
3694 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003695 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003696 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003697 }
3698
3699 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003700 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003701 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3702 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003703 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003704 }
3705
Brian Paul8c41a142005-11-19 15:36:28 +00003706 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3707 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003708 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003709 }
3710
3711 /* check for NULL character occurences */
3712 {
Brian Paul8c41a142005-11-19 15:36:28 +00003713 GLint i;
3714 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003715 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003716 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003718 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003719 }
Brian Paul8c41a142005-11-19 15:36:28 +00003720 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003721 }
3722
3723 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003724 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003725 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003726 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3727 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003728 return GL_FALSE;
3729 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003730 _mesa_memcpy (strz, str, len);
3731 strz[len] = '\0';
3732
Michal Krolb80bc052004-10-21 14:09:54 +00003733 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003734 err = !grammar_fast_check(arbprogram_syn_id, strz,
3735 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003736
3737 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003738 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003739 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3740 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003741
Brian Paulb3aefd12005-09-19 20:12:32 +00003742#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003743 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003744 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003745 int line, col;
3746 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003747 fprintf(stderr, "program: %s\n", (char *) strz);
3748 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003749 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3750 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003751 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3752 } while (0)
3753#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003754
Brian Paula088f162006-09-05 23:08:51 +00003755 _mesa_free(strz);
3756 _mesa_free(parsed);
3757
Jouk Jansen40322e12004-04-05 08:50:36 +00003758 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003759 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003760 }
3761
Jouk Jansen40322e12004-04-05 08:50:36 +00003762 grammar_destroy (arbprogram_syn_id);
3763
Brian Paul8c41a142005-11-19 15:36:28 +00003764 /*
3765 * Program string is syntactically correct at this point
3766 * Parse the tokenized version of the program now, generating
3767 * vertex/fragment program instructions.
3768 */
3769
Jouk Jansen40322e12004-04-05 08:50:36 +00003770 /* Initialize the arb_program struct */
3771 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003772 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003773 program->Base.NumInstructions =
3774 program->Base.NumTemporaries =
3775 program->Base.NumParameters =
3776 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003777 program->Base.Parameters = _mesa_new_parameter_list ();
3778 program->Base.InputsRead = 0x0;
3779 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003780 program->Position = 0;
3781 program->MajorVersion = program->MinorVersion = 0;
3782 program->PrecisionOption = GL_DONT_CARE;
3783 program->FogOption = GL_NONE;
3784 program->HintPositionInvariant = GL_FALSE;
3785 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003786 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003787 program->NumAluInstructions =
3788 program->NumTexInstructions =
3789 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003790 program->UsesKill = 0;
3791
Jouk Jansen40322e12004-04-05 08:50:36 +00003792 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003793 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003794
3795 /* Start examining the tokens in the array */
3796 inst = parsed;
3797
3798 /* Check the grammer rev */
3799 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003800 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003801 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003802 }
3803 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003804 /* ignore program target */
3805 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003806 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003807 }
3808
3809 /*debug_variables(ctx, vc_head, program); */
3810
3811 /* We're done with the parsed binary array */
3812 var_cache_destroy (&vc_head);
3813
3814 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003815
Brian Paul8c41a142005-11-19 15:36:28 +00003816 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3817 * to size [ap.Base.NumInstructions].
3818 */
Brian Paula7543902006-08-24 21:58:32 +00003819 program->Base.Instructions
3820 = _mesa_realloc_instructions(program->Base.Instructions,
3821 MAX_INSTRUCTIONS,
3822 program->Base.NumInstructions);
3823
Brian Paul45703642005-10-29 15:52:31 +00003824 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003825}
Brian Paul8c41a142005-11-19 15:36:28 +00003826
3827
3828
3829void
3830_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3831 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003832 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003833{
3834 struct arb_program ap;
3835 GLuint i;
3836
3837 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003838 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003839 /* Error in the program. Just return. */
3840 return;
3841 }
3842
3843 /* Copy the relevant contents of the arb_program struct into the
3844 * fragment_program struct.
3845 */
3846 program->Base.String = ap.Base.String;
3847 program->Base.NumInstructions = ap.Base.NumInstructions;
3848 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3849 program->Base.NumParameters = ap.Base.NumParameters;
3850 program->Base.NumAttributes = ap.Base.NumAttributes;
3851 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003852 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3853 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3854 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3855 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3856 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003857 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3858 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3859 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3860 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3861 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3862 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003863 program->Base.InputsRead = ap.Base.InputsRead;
3864 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3865 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
Brianc9db2232007-01-04 17:22:19 -07003866 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Brian Paul8c41a142005-11-19 15:36:28 +00003867 program->FogOption = ap.FogOption;
3868
3869 if (program->Base.Instructions)
3870 _mesa_free(program->Base.Instructions);
3871 program->Base.Instructions = ap.Base.Instructions;
3872
3873 if (program->Base.Parameters)
3874 _mesa_free_parameter_list(program->Base.Parameters);
3875 program->Base.Parameters = ap.Base.Parameters;
3876
3877#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00003878 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
3879 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003880#endif
3881}
3882
3883
3884
3885/**
3886 * Parse the vertex program string. If success, update the given
3887 * vertex_program object with the new program. Else, leave the vertex_program
3888 * object unchanged.
3889 */
3890void
3891_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3892 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003893 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003894{
3895 struct arb_program ap;
3896
3897 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3898
Brian Paul95801792005-12-06 15:41:43 +00003899 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003900 /* Error in the program. Just return. */
3901 return;
3902 }
3903
3904 /* Copy the relevant contents of the arb_program struct into the
3905 * vertex_program struct.
3906 */
3907 program->Base.String = ap.Base.String;
3908 program->Base.NumInstructions = ap.Base.NumInstructions;
3909 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3910 program->Base.NumParameters = ap.Base.NumParameters;
3911 program->Base.NumAttributes = ap.Base.NumAttributes;
3912 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003913 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3914 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3915 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3916 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3917 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003918 program->Base.InputsRead = ap.Base.InputsRead;
3919 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3920 program->IsPositionInvariant = ap.HintPositionInvariant;
3921
3922 if (program->Base.Instructions)
3923 _mesa_free(program->Base.Instructions);
3924 program->Base.Instructions = ap.Base.Instructions;
3925
3926 if (program->Base.Parameters)
3927 _mesa_free_parameter_list(program->Base.Parameters);
3928 program->Base.Parameters = ap.Base.Parameters;
3929
3930#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01003931 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00003932 _mesa_print_program(&program->Base);
3933#endif
3934}