blob: 83fc1951424c2f7b0b7f9e315dab541002515ac4 [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
Brianc223c6b2007-07-04 13:15:20 -060033#include "main/glheader.h"
34#include "main/imports.h"
35#include "shader/grammar/grammar_mesa.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000036#include "arbprogparse.h"
Brian Paul32df89e2005-10-29 18:26:43 +000037#include "program.h"
Brian Paulc0ef1662008-03-25 11:32:31 -060038#include "programopt.h"
Brianc0551f02006-12-14 15:02:37 -070039#include "prog_parameter.h"
40#include "prog_statevars.h"
Brian Paul8c41a142005-11-19 15:36:28 +000041#include "context.h"
Brian Paul6211a142006-08-24 23:37:13 +000042#include "macros.h"
Brian Paul8c41a142005-11-19 15:36:28 +000043#include "mtypes.h"
Brianc0551f02006-12-14 15:02:37 -070044#include "prog_instruction.h"
Brian Paul8c41a142005-11-19 15:36:28 +000045
46
Brian Paul6211a142006-08-24 23:37:13 +000047/* For ARB programs, use the NV instruction limits */
48#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
49 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000050
51
52/**
53 * This is basically a union of the vertex_program and fragment_program
54 * structs that we can use to parse the program into
55 *
56 * XXX we can probably get rid of this entirely someday.
57 */
58struct arb_program
59{
Brian Paul122629f2006-07-20 16:49:57 +000060 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000061
62 GLuint Position; /* Just used for error reporting while parsing */
63 GLuint MajorVersion;
64 GLuint MinorVersion;
65
66 /* ARB_vertex_progmra options */
67 GLboolean HintPositionInvariant;
68
69 /* ARB_fragment_progmra options */
70 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
71 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
72
73 /* ARB_fragment_program specifics */
74 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
Ian Romanick7b559a92007-06-07 13:58:50 -070075 GLbitfield ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +000076 GLuint NumAluInstructions;
77 GLuint NumTexInstructions;
78 GLuint NumTexIndirections;
79
80 GLboolean UsesKill;
81};
82
Ian Romanick9bdfee32005-07-18 12:31:24 +000083
Brian Paula6c423d2004-08-25 15:59:48 +000084
Jouk Jansen40322e12004-04-05 08:50:36 +000085/* TODO:
86 * Fragment Program Stuff:
87 * -----------------------------------------------------
88 *
89 * - things from Michal's email
90 * + overflow on atoi
91 * + not-overflowing floats (don't use parse_integer..)
92 * + can remove range checking in arbparse.c
93 *
94 * - check all limits of number of various variables
95 * + parameters
96 *
97 * - test! test! test!
98 *
99 * Vertex Program Stuff:
100 * -----------------------------------------------------
101 * - Optimize param array usage and count limits correctly, see spec,
102 * section 2.14.3.7
103 * + Record if an array is reference absolutly or relatively (or both)
104 * + For absolute arrays, store a bitmap of accesses
105 * + For single parameters, store an access flag
106 * + After parsing, make a parameter cleanup and merging pass, where
107 * relative arrays are layed out first, followed by abs arrays, and
108 * finally single state.
109 * + Remap offsets for param src and dst registers
110 * + Now we can properly count parameter usage
111 *
112 * - Multiple state binding errors in param arrays (see spec, just before
113 * section 2.14.3.3)
114 * - grep for XXX
115 *
116 * Mesa Stuff
117 * -----------------------------------------------------
118 * - User clipping planes vs. PositionInvariant
119 * - Is it sufficient to just multiply by the mvp to transform in the
120 * PositionInvariant case? Or do we need something more involved?
121 *
122 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
123 * - fetch state listed in program_parameters list
124 * + WTF should this go???
125 * + currently in nvvertexec.c and s_nvfragprog.c
126 *
127 * - allow for multiple address registers (and fetch address regs properly)
128 *
129 * Cosmetic Stuff
130 * -----------------------------------------------------
131 * - remove any leftover unused grammer.c stuff (dict_ ?)
132 * - fix grammer.c error handling so its not static
133 * - #ifdef around stuff pertaining to extentions
134 *
135 * Outstanding Questions:
136 * -----------------------------------------------------
137 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
138 * what gets hacked off because of this:
139 * + VERTEX_ATTRIB_MATRIXINDEX
140 * + VERTEX_ATTRIB_WEIGHT
141 * + MATRIX_MODELVIEW
142 * + MATRIX_PALETTE
143 *
144 * - When can we fetch env/local params from their own register files, and
145 * when to we have to fetch them into the main state register file?
146 * (think arrays)
147 *
148 * Grammar Changes:
149 * -----------------------------------------------------
150 */
151
152/* Changes since moving the file to shader directory
153
1542004-III-4 ------------------------------------------------------------
155- added #include "grammar_mesa.h"
156- removed grammar specific code part (it resides now in grammar.c)
157- added GL_ARB_fragment_program_shadow tokens
158- modified #include "arbparse_syn.h"
159- major changes inside _mesa_parse_arb_program()
160- check the program string for '\0' characters
161- copy the program string to a one-byte-longer location to have
162 it null-terminated
163- position invariance test (not writing to result.position) moved
164 to syntax part
165*/
166
167typedef GLubyte *production;
168
Brian Paul11a54c32006-11-15 19:54:25 +0000169
Jouk Jansen40322e12004-04-05 08:50:36 +0000170/**
171 * This is the text describing the rules to parse the grammar
172 */
Brian Paul11a54c32006-11-15 19:54:25 +0000173LONGSTRING static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000174#include "arbprogram_syn.h"
175;
176
177/**
178 * These should match up with the values defined in arbprogram.syn
179 */
180
181/*
182 Changes:
183 - changed and merged V_* and F_* opcode values to OP_*.
184 - added GL_ARB_fragment_program_shadow specific tokens (michal)
185*/
Ian Romanickbb372f12007-05-16 15:34:22 -0700186#define REVISION 0x0a
Jouk Jansen40322e12004-04-05 08:50:36 +0000187
188/* program type */
189#define FRAGMENT_PROGRAM 0x01
190#define VERTEX_PROGRAM 0x02
191
192/* program section */
193#define OPTION 0x01
194#define INSTRUCTION 0x02
195#define DECLARATION 0x03
196#define END 0x04
197
Michal Krolb80bc052004-10-21 14:09:54 +0000198/* GL_ARB_fragment_program option */
199#define ARB_PRECISION_HINT_FASTEST 0x00
200#define ARB_PRECISION_HINT_NICEST 0x01
201#define ARB_FOG_EXP 0x02
202#define ARB_FOG_EXP2 0x03
203#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000204
Michal Krolb80bc052004-10-21 14:09:54 +0000205/* GL_ARB_vertex_program option */
206#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000207
Michal Krolb80bc052004-10-21 14:09:54 +0000208/* GL_ARB_fragment_program_shadow option */
209#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000210
Michal Krolb80bc052004-10-21 14:09:54 +0000211/* GL_ARB_draw_buffers option */
212#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000213
Ian Romanickbb372f12007-05-16 15:34:22 -0700214/* GL_MESA_texture_array option */
215#define MESA_TEXTURE_ARRAY 0x08
216
Jouk Jansen40322e12004-04-05 08:50:36 +0000217/* GL_ARB_fragment_program instruction class */
218#define OP_ALU_INST 0x00
219#define OP_TEX_INST 0x01
220
221/* GL_ARB_vertex_program instruction class */
222/* OP_ALU_INST */
223
224/* GL_ARB_fragment_program instruction type */
225#define OP_ALU_VECTOR 0x00
226#define OP_ALU_SCALAR 0x01
227#define OP_ALU_BINSC 0x02
228#define OP_ALU_BIN 0x03
229#define OP_ALU_TRI 0x04
230#define OP_ALU_SWZ 0x05
231#define OP_TEX_SAMPLE 0x06
232#define OP_TEX_KIL 0x07
233
234/* GL_ARB_vertex_program instruction type */
235#define OP_ALU_ARL 0x08
236/* OP_ALU_VECTOR */
237/* OP_ALU_SCALAR */
238/* OP_ALU_BINSC */
239/* OP_ALU_BIN */
240/* OP_ALU_TRI */
241/* OP_ALU_SWZ */
242
243/* GL_ARB_fragment_program instruction code */
244#define OP_ABS 0x00
245#define OP_ABS_SAT 0x1B
246#define OP_FLR 0x09
247#define OP_FLR_SAT 0x26
248#define OP_FRC 0x0A
249#define OP_FRC_SAT 0x27
250#define OP_LIT 0x0C
251#define OP_LIT_SAT 0x2A
252#define OP_MOV 0x11
253#define OP_MOV_SAT 0x30
254#define OP_COS 0x1F
255#define OP_COS_SAT 0x20
256#define OP_EX2 0x07
257#define OP_EX2_SAT 0x25
258#define OP_LG2 0x0B
259#define OP_LG2_SAT 0x29
260#define OP_RCP 0x14
261#define OP_RCP_SAT 0x33
262#define OP_RSQ 0x15
263#define OP_RSQ_SAT 0x34
264#define OP_SIN 0x38
265#define OP_SIN_SAT 0x39
266#define OP_SCS 0x35
267#define OP_SCS_SAT 0x36
268#define OP_POW 0x13
269#define OP_POW_SAT 0x32
270#define OP_ADD 0x01
271#define OP_ADD_SAT 0x1C
272#define OP_DP3 0x03
273#define OP_DP3_SAT 0x21
274#define OP_DP4 0x04
275#define OP_DP4_SAT 0x22
276#define OP_DPH 0x05
277#define OP_DPH_SAT 0x23
278#define OP_DST 0x06
279#define OP_DST_SAT 0x24
280#define OP_MAX 0x0F
281#define OP_MAX_SAT 0x2E
282#define OP_MIN 0x10
283#define OP_MIN_SAT 0x2F
284#define OP_MUL 0x12
285#define OP_MUL_SAT 0x31
286#define OP_SGE 0x16
287#define OP_SGE_SAT 0x37
288#define OP_SLT 0x17
289#define OP_SLT_SAT 0x3A
290#define OP_SUB 0x18
291#define OP_SUB_SAT 0x3B
292#define OP_XPD 0x1A
293#define OP_XPD_SAT 0x43
294#define OP_CMP 0x1D
295#define OP_CMP_SAT 0x1E
296#define OP_LRP 0x2B
297#define OP_LRP_SAT 0x2C
298#define OP_MAD 0x0E
299#define OP_MAD_SAT 0x2D
300#define OP_SWZ 0x19
301#define OP_SWZ_SAT 0x3C
302#define OP_TEX 0x3D
303#define OP_TEX_SAT 0x3E
304#define OP_TXB 0x3F
305#define OP_TXB_SAT 0x40
306#define OP_TXP 0x41
307#define OP_TXP_SAT 0x42
308#define OP_KIL 0x28
309
310/* GL_ARB_vertex_program instruction code */
311#define OP_ARL 0x02
312/* OP_ABS */
313/* OP_FLR */
314/* OP_FRC */
315/* OP_LIT */
316/* OP_MOV */
317/* OP_EX2 */
318#define OP_EXP 0x08
319/* OP_LG2 */
320#define OP_LOG 0x0D
321/* OP_RCP */
322/* OP_RSQ */
323/* OP_POW */
324/* OP_ADD */
325/* OP_DP3 */
326/* OP_DP4 */
327/* OP_DPH */
328/* OP_DST */
329/* OP_MAX */
330/* OP_MIN */
331/* OP_MUL */
332/* OP_SGE */
333/* OP_SLT */
334/* OP_SUB */
335/* OP_XPD */
336/* OP_MAD */
337/* OP_SWZ */
338
339/* fragment attribute binding */
340#define FRAGMENT_ATTRIB_COLOR 0x01
341#define FRAGMENT_ATTRIB_TEXCOORD 0x02
342#define FRAGMENT_ATTRIB_FOGCOORD 0x03
343#define FRAGMENT_ATTRIB_POSITION 0x04
344
345/* vertex attribute binding */
346#define VERTEX_ATTRIB_POSITION 0x01
347#define VERTEX_ATTRIB_WEIGHT 0x02
348#define VERTEX_ATTRIB_NORMAL 0x03
349#define VERTEX_ATTRIB_COLOR 0x04
350#define VERTEX_ATTRIB_FOGCOORD 0x05
351#define VERTEX_ATTRIB_TEXCOORD 0x06
352#define VERTEX_ATTRIB_MATRIXINDEX 0x07
353#define VERTEX_ATTRIB_GENERIC 0x08
354
355/* fragment result binding */
356#define FRAGMENT_RESULT_COLOR 0x01
357#define FRAGMENT_RESULT_DEPTH 0x02
358
359/* vertex result binding */
360#define VERTEX_RESULT_POSITION 0x01
361#define VERTEX_RESULT_COLOR 0x02
362#define VERTEX_RESULT_FOGCOORD 0x03
363#define VERTEX_RESULT_POINTSIZE 0x04
364#define VERTEX_RESULT_TEXCOORD 0x05
365
366/* texture target */
367#define TEXTARGET_1D 0x01
368#define TEXTARGET_2D 0x02
369#define TEXTARGET_3D 0x03
370#define TEXTARGET_RECT 0x04
371#define TEXTARGET_CUBE 0x05
372/* GL_ARB_fragment_program_shadow */
373#define TEXTARGET_SHADOW1D 0x06
374#define TEXTARGET_SHADOW2D 0x07
375#define TEXTARGET_SHADOWRECT 0x08
Ian Romanickbb372f12007-05-16 15:34:22 -0700376/* GL_MESA_texture_array */
377#define TEXTARGET_1D_ARRAY 0x09
378#define TEXTARGET_2D_ARRAY 0x0a
Ian Romanick69358e72007-06-05 09:24:27 -0700379#define TEXTARGET_SHADOW1D_ARRAY 0x0b
380#define TEXTARGET_SHADOW2D_ARRAY 0x0c
Jouk Jansen40322e12004-04-05 08:50:36 +0000381
382/* face type */
383#define FACE_FRONT 0x00
384#define FACE_BACK 0x01
385
386/* color type */
387#define COLOR_PRIMARY 0x00
388#define COLOR_SECONDARY 0x01
389
390/* component */
391#define COMPONENT_X 0x00
392#define COMPONENT_Y 0x01
393#define COMPONENT_Z 0x02
394#define COMPONENT_W 0x03
395#define COMPONENT_0 0x04
396#define COMPONENT_1 0x05
397
398/* array index type */
399#define ARRAY_INDEX_ABSOLUTE 0x00
400#define ARRAY_INDEX_RELATIVE 0x01
401
402/* matrix name */
403#define MATRIX_MODELVIEW 0x01
404#define MATRIX_PROJECTION 0x02
405#define MATRIX_MVP 0x03
406#define MATRIX_TEXTURE 0x04
407#define MATRIX_PALETTE 0x05
408#define MATRIX_PROGRAM 0x06
409
410/* matrix modifier */
411#define MATRIX_MODIFIER_IDENTITY 0x00
412#define MATRIX_MODIFIER_INVERSE 0x01
413#define MATRIX_MODIFIER_TRANSPOSE 0x02
414#define MATRIX_MODIFIER_INVTRANS 0x03
415
416/* constant type */
417#define CONSTANT_SCALAR 0x01
418#define CONSTANT_VECTOR 0x02
419
420/* program param type */
421#define PROGRAM_PARAM_ENV 0x01
422#define PROGRAM_PARAM_LOCAL 0x02
423
424/* register type */
425#define REGISTER_ATTRIB 0x01
426#define REGISTER_PARAM 0x02
427#define REGISTER_RESULT 0x03
428#define REGISTER_ESTABLISHED_NAME 0x04
429
430/* param binding */
431#define PARAM_NULL 0x00
432#define PARAM_ARRAY_ELEMENT 0x01
433#define PARAM_STATE_ELEMENT 0x02
434#define PARAM_PROGRAM_ELEMENT 0x03
435#define PARAM_PROGRAM_ELEMENTS 0x04
436#define PARAM_CONSTANT 0x05
437
438/* param state property */
439#define STATE_MATERIAL_PARSER 0x01
440#define STATE_LIGHT_PARSER 0x02
441#define STATE_LIGHT_MODEL 0x03
442#define STATE_LIGHT_PROD 0x04
443#define STATE_FOG 0x05
444#define STATE_MATRIX_ROWS 0x06
445/* GL_ARB_fragment_program */
446#define STATE_TEX_ENV 0x07
447#define STATE_DEPTH 0x08
448/* GL_ARB_vertex_program */
449#define STATE_TEX_GEN 0x09
450#define STATE_CLIP_PLANE 0x0A
451#define STATE_POINT 0x0B
452
453/* state material property */
454#define MATERIAL_AMBIENT 0x01
455#define MATERIAL_DIFFUSE 0x02
456#define MATERIAL_SPECULAR 0x03
457#define MATERIAL_EMISSION 0x04
458#define MATERIAL_SHININESS 0x05
459
460/* state light property */
461#define LIGHT_AMBIENT 0x01
462#define LIGHT_DIFFUSE 0x02
463#define LIGHT_SPECULAR 0x03
464#define LIGHT_POSITION 0x04
465#define LIGHT_ATTENUATION 0x05
466#define LIGHT_HALF 0x06
467#define LIGHT_SPOT_DIRECTION 0x07
468
469/* state light model property */
470#define LIGHT_MODEL_AMBIENT 0x01
471#define LIGHT_MODEL_SCENECOLOR 0x02
472
473/* state light product property */
474#define LIGHT_PROD_AMBIENT 0x01
475#define LIGHT_PROD_DIFFUSE 0x02
476#define LIGHT_PROD_SPECULAR 0x03
477
478/* state texture environment property */
479#define TEX_ENV_COLOR 0x01
480
481/* state texture generation coord property */
482#define TEX_GEN_EYE 0x01
483#define TEX_GEN_OBJECT 0x02
484
485/* state fog property */
486#define FOG_COLOR 0x01
487#define FOG_PARAMS 0x02
488
489/* state depth property */
490#define DEPTH_RANGE 0x01
491
492/* state point parameters property */
493#define POINT_SIZE 0x01
494#define POINT_ATTENUATION 0x02
495
496/* declaration */
497#define ATTRIB 0x01
498#define PARAM 0x02
499#define TEMP 0x03
500#define OUTPUT 0x04
501#define ALIAS 0x05
502/* GL_ARB_vertex_program */
503#define ADDRESS 0x06
504
505/*-----------------------------------------------------------------------
506 * From here on down is the semantic checking portion
507 *
508 */
509
510/**
511 * Variable Table Handling functions
512 */
513typedef enum
514{
515 vt_none,
516 vt_address,
517 vt_attrib,
518 vt_param,
519 vt_temp,
520 vt_output,
521 vt_alias
522} var_type;
523
524
Brian Paul7aebaf32005-10-30 21:23:23 +0000525/**
526 * Setting an explicit field for each of the binding properties is a bit
527 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000528 */
529struct var_cache
530{
Brian Paul6a769d92006-04-28 15:42:15 +0000531 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000532 var_type type;
533 GLuint address_binding; /* The index of the address register we should
534 * be using */
535 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000536 GLuint attrib_is_generic; /* If the attrib was specified through a generic
537 * vertex attrib */
538 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000539 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000540 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
541 * that this is aliased to */
542 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
543 * PROGRAM_ENV_PARAM} */
544 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
545 * the tokens representing our bound state (or constants)
546 * start */
547 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
548 * we take up with our state tokens or constants. Note that
549 * this is _not_ the same as the number of param registers
550 * we eventually use */
551 struct var_cache *next;
552};
553
554static GLvoid
555var_cache_create (struct var_cache **va)
556{
557 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
558 if (*va) {
559 (**va).name = NULL;
560 (**va).type = vt_none;
561 (**va).attrib_binding = ~0;
562 (**va).attrib_is_generic = 0;
563 (**va).temp_binding = ~0;
564 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000565 (**va).param_binding_type = ~0;
566 (**va).param_binding_begin = ~0;
567 (**va).param_binding_length = ~0;
568 (**va).alias_binding = NULL;
569 (**va).next = NULL;
570 }
571}
572
573static GLvoid
574var_cache_destroy (struct var_cache **va)
575{
576 if (*va) {
577 var_cache_destroy (&(**va).next);
578 _mesa_free (*va);
579 *va = NULL;
580 }
581}
582
583static GLvoid
584var_cache_append (struct var_cache **va, struct var_cache *nv)
585{
586 if (*va)
587 var_cache_append (&(**va).next, nv);
588 else
589 *va = nv;
590}
591
592static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000593var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000594{
Brian Paul0a360cf2005-01-17 01:21:03 +0000595 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000596
597 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000598 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000599 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000600 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000601 return va;
602 }
603
604 va = va->next;
605 }
606
607 return NULL;
608}
609
Brian Paula088f162006-09-05 23:08:51 +0000610
611
612/**
613 * Called when an error is detected while parsing/compiling a program.
614 * Sets the ctx->Program.ErrorString field to descript and records a
615 * GL_INVALID_OPERATION error.
616 * \param position position of error in program string
617 * \param descrip verbose error description
618 */
619static void
620program_error(GLcontext *ctx, GLint position, const char *descrip)
621{
622 if (descrip) {
623 const char *prefix = "glProgramString(", *suffix = ")";
624 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
625 _mesa_strlen(prefix) +
626 _mesa_strlen(suffix) + 1);
627 if (str) {
628 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
629 _mesa_error(ctx, GL_INVALID_OPERATION, str);
630 _mesa_free(str);
631 }
632 }
633 _mesa_set_program_error(ctx, position, descrip);
634}
635
636
637
Jouk Jansen40322e12004-04-05 08:50:36 +0000638/**
639 * constructs an integer from 4 GLubytes in LE format
640 */
641static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000642parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000643{
644 GLuint value;
645
646 value = (GLuint) (*(*inst)++);
647 value += (GLuint) (*(*inst)++) * 0x100;
648 value += (GLuint) (*(*inst)++) * 0x10000;
649 value += (GLuint) (*(*inst)++) * 0x1000000;
650
651 return value;
652}
653
654/**
655 * This will, given a string, lookup the string as a variable name in the
656 * var cache. If the name is found, the var cache node corresponding to the
657 * var name is returned. If it is not found, a new entry is allocated
658 *
659 * \param I Points into the binary array where the string identifier begins
660 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
661 * \return The location on the var_cache corresponding the the string starting at I
662 */
663static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000664parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000665 struct arb_program *Program, GLuint * found)
666{
Brian Paula088f162006-09-05 23:08:51 +0000667 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000668 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000669 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000670
671 *inst += _mesa_strlen ((char *) i) + 1;
672
673 va = var_cache_find (*vc_head, i);
674
675 if (va) {
676 *found = 1;
677 return va;
678 }
679
680 *found = 0;
681 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000682 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000683
684 var_cache_append (vc_head, va);
685
686 return va;
687}
688
689static char *
Brian Paula088f162006-09-05 23:08:51 +0000690parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000691{
Brian Paula088f162006-09-05 23:08:51 +0000692 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000693 (void) Program;
694
Jouk Jansen40322e12004-04-05 08:50:36 +0000695 *inst += _mesa_strlen ((char *) i) + 1;
696
697 return (char *) i;
698}
699
700/**
Brian Paul05908952004-06-08 15:20:23 +0000701 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000702 */
Brian Paul05908952004-06-08 15:20:23 +0000703static GLint
Brian Paula088f162006-09-05 23:08:51 +0000704parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000705{
706 /*return *(*inst)++ != '+'; */
707
708 if (**inst == '-') {
709 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000710 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000711 }
712 else if (**inst == '+') {
713 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000714 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000715 }
716
Brian Paul05908952004-06-08 15:20:23 +0000717 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000718}
719
720/**
721 * parses and returns signed integer
722 */
723static GLint
Brian Paula088f162006-09-05 23:08:51 +0000724parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000725{
726 GLint sign;
727 GLint value;
728
729 /* check if *inst points to '+' or '-'
730 * if yes, grab the sign and increment *inst
731 */
732 sign = parse_sign (inst);
733
734 /* now check if *inst points to 0
735 * if yes, increment the *inst and return the default value
736 */
737 if (**inst == 0) {
738 (*inst)++;
739 return 0;
740 }
741
742 /* parse the integer as you normally would do it */
743 value = _mesa_atoi (parse_string_without_adding (inst, Program));
744
745 /* now, after terminating 0 there is a position
746 * to parse it - parse_position()
747 */
748 Program->Position = parse_position (inst);
749
Brian Paul05908952004-06-08 15:20:23 +0000750 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000751}
752
753/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000754 Accumulate this string of digits, and return them as
755 a large integer represented in floating point (for range).
756 If scale is not NULL, also accumulates a power-of-ten
757 integer scale factor that represents the number of digits
758 in the string.
759*/
760static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000761parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000762{
763 GLdouble value = 0.0;
764 GLdouble oscale = 1.0;
765
766 if (**inst == 0) { /* this string of digits is empty-- do nothing */
767 (*inst)++;
768 }
769 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000770 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000771 GLubyte digit = *((*inst)++);
772 value = value * 10.0 + (GLint) (digit - '0');
773 oscale *= 10.0;
774 }
775 assert(**inst == 0); /* integer string should end with 0 */
776 (*inst)++; /* skip over terminating 0 */
777 Program->Position = parse_position(inst); /* skip position (from integer) */
778 }
779 if (scale)
780 *scale = oscale;
781 return value;
782}
783
784/**
785 Parse an unsigned floating-point number from this stream of tokenized
786 characters. Example floating-point formats supported:
787 12.34
788 12
789 0.34
790 .34
791 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000792 */
793static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000794parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000795{
Brian Paul1ff8f502005-02-16 15:08:29 +0000796 GLint exponent;
797 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000798
Brian Paul1ff8f502005-02-16 15:08:29 +0000799 whole = parse_float_string(inst, Program, 0);
800 fraction = parse_float_string(inst, Program, &fracScale);
801
802 /* Parse signed exponent */
803 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000804
Brian Paul1ff8f502005-02-16 15:08:29 +0000805 /* Assemble parts of floating-point number: */
806 return (GLfloat) ((whole + fraction / fracScale) *
807 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000808}
809
810
811/**
812 */
813static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000814parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000815{
Brian Paul05908952004-06-08 15:20:23 +0000816 GLint sign = parse_sign (inst);
817 GLfloat value = parse_float (inst, Program);
818 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000819}
820
821/**
822 * This picks out a constant value from the parsed array. The constant vector is r
823 * returned in the *values array, which should be of length 4.
824 *
825 * \param values - The 4 component vector with the constant value in it
826 */
827static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000828parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000829 GLboolean use)
830{
831 GLuint components, i;
832
833
834 switch (*(*inst)++) {
835 case CONSTANT_SCALAR:
836 if (use == GL_TRUE) {
837 values[0] =
838 values[1] =
839 values[2] = values[3] = parse_float (inst, Program);
840 }
841 else {
842 values[0] =
843 values[1] =
844 values[2] = values[3] = parse_signed_float (inst, Program);
845 }
846
847 break;
848 case CONSTANT_VECTOR:
849 values[0] = values[1] = values[2] = 0;
850 values[3] = 1;
851 components = *(*inst)++;
852 for (i = 0; i < components; i++) {
853 values[i] = parse_signed_float (inst, Program);
854 }
855 break;
856 }
857}
858
859/**
860 * \param offset The offset from the address register that we should
861 * address
862 *
863 * \return 0 on sucess, 1 on error
864 */
865static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000866parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
867 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000868{
Brian Paul6a769d92006-04-28 15:42:15 +0000869 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000870 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000871 return 0;
872}
873
874/**
875 * \param color 0 if color type is primary, 1 if color type is secondary
876 * \return 0 on sucess, 1 on error
877 */
878static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000879parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000880 GLint * color)
881{
Brian Paula6c423d2004-08-25 15:59:48 +0000882 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000883 *color = *(*inst)++ != COLOR_PRIMARY;
884 return 0;
885}
886
887/**
888 * Get an integer corresponding to a generic vertex attribute.
889 *
890 * \return 0 on sucess, 1 on error
891 */
892static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000893parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000894 struct arb_program *Program, GLuint *attrib)
895{
Brian Paulbd997cd2004-07-20 21:12:56 +0000896 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000897
Michal Krolbb38cad2006-04-11 11:41:11 +0000898 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000899 {
Brian Paula088f162006-09-05 23:08:51 +0000900 program_error(ctx, Program->Position,
901 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000902 return 1;
903 }
904
Brian Paulbd997cd2004-07-20 21:12:56 +0000905 *attrib = (GLuint) i;
906
Jouk Jansen40322e12004-04-05 08:50:36 +0000907 return 0;
908}
909
910
911/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000912 * \param color The index of the color buffer to write into
913 * \return 0 on sucess, 1 on error
914 */
915static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000916parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000917 struct arb_program *Program, GLuint * color)
918{
919 GLint i = parse_integer (inst, Program);
920
921 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000922 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000923 return 1;
924 }
925
926 *color = (GLuint) i;
927 return 0;
928}
929
930
931/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000932 * \param coord The texture unit index
933 * \return 0 on sucess, 1 on error
934 */
935static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000936parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000937 struct arb_program *Program, GLuint * coord)
938{
Brian Paulbd997cd2004-07-20 21:12:56 +0000939 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000940
Brian Paula6c423d2004-08-25 15:59:48 +0000941 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000942 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000943 return 1;
944 }
945
Brian Paulbd997cd2004-07-20 21:12:56 +0000946 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000947 return 0;
948}
949
950/**
951 * \param coord The weight index
952 * \return 0 on sucess, 1 on error
953 */
954static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000955parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000956 GLint * coord)
957{
958 *coord = parse_integer (inst, Program);
959
960 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000961 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000962 return 1;
963 }
964
965 return 0;
966}
967
968/**
969 * \param coord The clip plane index
970 * \return 0 on sucess, 1 on error
971 */
972static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000973parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000974 struct arb_program *Program, GLint * coord)
975{
976 *coord = parse_integer (inst, Program);
977
978 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +0000979 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000980 return 1;
981 }
982
983 return 0;
984}
985
986
987/**
988 * \return 0 on front face, 1 on back face
989 */
990static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000991parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000992{
993 switch (*(*inst)++) {
994 case FACE_FRONT:
995 return 0;
996
997 case FACE_BACK:
998 return 1;
999 }
1000 return 0;
1001}
1002
1003
1004/**
1005 * Given a matrix and a modifier token on the binary array, return tokens
1006 * that _mesa_fetch_state() [program.c] can understand.
1007 *
1008 * \param matrix - the matrix we are talking about
1009 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1010 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1011 * \return 0 on sucess, 1 on failure
1012 */
1013static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001014parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001015 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1016{
1017 GLubyte mat = *(*inst)++;
1018
1019 *matrix_idx = 0;
1020
1021 switch (mat) {
1022 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001023 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001024 *matrix_idx = parse_integer (inst, Program);
1025 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001026 program_error(ctx, Program->Position,
1027 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001028 return 1;
1029 }
1030 break;
1031
1032 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001033 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001034 break;
1035
1036 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001037 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001038 break;
1039
1040 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001041 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001042 *matrix_idx = parse_integer (inst, Program);
1043 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001044 program_error(ctx, Program->Position, "Invalid Texture Unit");
1045 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001046 return 1;
1047 }
1048 break;
1049
1050 /* This is not currently supported (ARB_matrix_palette) */
1051 case MATRIX_PALETTE:
1052 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001053 program_error(ctx, Program->Position,
1054 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001055 return 1;
1056 break;
1057
1058 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001059 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001060 *matrix_idx = parse_integer (inst, Program);
1061 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001062 program_error(ctx, Program->Position, "Invalid Program Matrix");
1063 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001064 return 1;
1065 }
1066 break;
1067 }
1068
1069 switch (*(*inst)++) {
1070 case MATRIX_MODIFIER_IDENTITY:
1071 *matrix_modifier = 0;
1072 break;
1073 case MATRIX_MODIFIER_INVERSE:
1074 *matrix_modifier = STATE_MATRIX_INVERSE;
1075 break;
1076 case MATRIX_MODIFIER_TRANSPOSE:
1077 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1078 break;
1079 case MATRIX_MODIFIER_INVTRANS:
1080 *matrix_modifier = STATE_MATRIX_INVTRANS;
1081 break;
1082 }
1083
1084 return 0;
1085}
1086
1087
1088/**
1089 * This parses a state string (rather, the binary version of it) into
1090 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1091 *
1092 * \param inst - the start in the binary arry to start working from
1093 * \param state_tokens - the storage for the 6-token state description
1094 * \return - 0 on sucess, 1 on error
1095 */
1096static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001097parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001098 struct arb_program *Program,
1099 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001100{
1101 switch (*(*inst)++) {
1102 case STATE_MATERIAL_PARSER:
1103 state_tokens[0] = STATE_MATERIAL;
1104 state_tokens[1] = parse_face_type (inst);
1105 switch (*(*inst)++) {
1106 case MATERIAL_AMBIENT:
1107 state_tokens[2] = STATE_AMBIENT;
1108 break;
1109 case MATERIAL_DIFFUSE:
1110 state_tokens[2] = STATE_DIFFUSE;
1111 break;
1112 case MATERIAL_SPECULAR:
1113 state_tokens[2] = STATE_SPECULAR;
1114 break;
1115 case MATERIAL_EMISSION:
1116 state_tokens[2] = STATE_EMISSION;
1117 break;
1118 case MATERIAL_SHININESS:
1119 state_tokens[2] = STATE_SHININESS;
1120 break;
1121 }
1122 break;
1123
1124 case STATE_LIGHT_PARSER:
1125 state_tokens[0] = STATE_LIGHT;
1126 state_tokens[1] = parse_integer (inst, Program);
1127
1128 /* Check the value of state_tokens[1] against the # of lights */
1129 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001130 program_error(ctx, Program->Position, "Invalid Light Number");
1131 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001132 return 1;
1133 }
1134
1135 switch (*(*inst)++) {
1136 case LIGHT_AMBIENT:
1137 state_tokens[2] = STATE_AMBIENT;
1138 break;
1139 case LIGHT_DIFFUSE:
1140 state_tokens[2] = STATE_DIFFUSE;
1141 break;
1142 case LIGHT_SPECULAR:
1143 state_tokens[2] = STATE_SPECULAR;
1144 break;
1145 case LIGHT_POSITION:
1146 state_tokens[2] = STATE_POSITION;
1147 break;
1148 case LIGHT_ATTENUATION:
1149 state_tokens[2] = STATE_ATTENUATION;
1150 break;
1151 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001152 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001153 break;
1154 case LIGHT_SPOT_DIRECTION:
1155 state_tokens[2] = STATE_SPOT_DIRECTION;
1156 break;
1157 }
1158 break;
1159
1160 case STATE_LIGHT_MODEL:
1161 switch (*(*inst)++) {
1162 case LIGHT_MODEL_AMBIENT:
1163 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1164 break;
1165 case LIGHT_MODEL_SCENECOLOR:
1166 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1167 state_tokens[1] = parse_face_type (inst);
1168 break;
1169 }
1170 break;
1171
1172 case STATE_LIGHT_PROD:
1173 state_tokens[0] = STATE_LIGHTPROD;
1174 state_tokens[1] = parse_integer (inst, Program);
1175
1176 /* Check the value of state_tokens[1] against the # of lights */
1177 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001178 program_error(ctx, Program->Position, "Invalid Light Number");
1179 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001180 return 1;
1181 }
1182
1183 state_tokens[2] = parse_face_type (inst);
1184 switch (*(*inst)++) {
1185 case LIGHT_PROD_AMBIENT:
1186 state_tokens[3] = STATE_AMBIENT;
1187 break;
1188 case LIGHT_PROD_DIFFUSE:
1189 state_tokens[3] = STATE_DIFFUSE;
1190 break;
1191 case LIGHT_PROD_SPECULAR:
1192 state_tokens[3] = STATE_SPECULAR;
1193 break;
1194 }
1195 break;
1196
1197
1198 case STATE_FOG:
1199 switch (*(*inst)++) {
1200 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001201 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001202 break;
1203 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001204 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001205 break;
1206 }
1207 break;
1208
1209 case STATE_TEX_ENV:
1210 state_tokens[1] = parse_integer (inst, Program);
1211 switch (*(*inst)++) {
1212 case TEX_ENV_COLOR:
1213 state_tokens[0] = STATE_TEXENV_COLOR;
1214 break;
1215 }
1216 break;
1217
1218 case STATE_TEX_GEN:
1219 {
1220 GLuint type, coord;
1221
1222 state_tokens[0] = STATE_TEXGEN;
1223 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1224
1225 if (parse_texcoord_num (ctx, inst, Program, &coord))
1226 return 1;
1227 state_tokens[1] = coord;
1228
1229 /* EYE or OBJECT */
Brian7b91d872008-03-27 15:48:54 -06001230 type = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001231
1232 /* 0 - s, 1 - t, 2 - r, 3 - q */
Brian7b91d872008-03-27 15:48:54 -06001233 coord = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001234
1235 if (type == TEX_GEN_EYE) {
1236 switch (coord) {
1237 case COMPONENT_X:
1238 state_tokens[2] = STATE_TEXGEN_EYE_S;
1239 break;
1240 case COMPONENT_Y:
1241 state_tokens[2] = STATE_TEXGEN_EYE_T;
1242 break;
1243 case COMPONENT_Z:
1244 state_tokens[2] = STATE_TEXGEN_EYE_R;
1245 break;
1246 case COMPONENT_W:
1247 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1248 break;
Brian7b91d872008-03-27 15:48:54 -06001249 default:
1250 _mesa_problem(ctx, "bad texgen component in "
1251 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001252 }
1253 }
1254 else {
1255 switch (coord) {
1256 case COMPONENT_X:
1257 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1258 break;
1259 case COMPONENT_Y:
1260 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1261 break;
1262 case COMPONENT_Z:
1263 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1264 break;
1265 case COMPONENT_W:
1266 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1267 break;
Brian7b91d872008-03-27 15:48:54 -06001268 default:
1269 _mesa_problem(ctx, "bad texgen component in "
1270 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001271 }
1272 }
1273 }
1274 break;
1275
1276 case STATE_DEPTH:
1277 switch (*(*inst)++) {
1278 case DEPTH_RANGE:
1279 state_tokens[0] = STATE_DEPTH_RANGE;
1280 break;
1281 }
1282 break;
1283
1284 case STATE_CLIP_PLANE:
1285 state_tokens[0] = STATE_CLIPPLANE;
1286 state_tokens[1] = parse_integer (inst, Program);
Brianaa9d22a2007-02-23 11:21:03 -07001287 if (parse_clipplane_num (ctx, inst, Program,
1288 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001289 return 1;
1290 break;
1291
1292 case STATE_POINT:
Brian7b91d872008-03-27 15:48:54 -06001293 switch (*(*inst)++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001294 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001295 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001296 break;
1297
1298 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001299 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001300 break;
1301 }
1302 break;
1303
1304 /* XXX: I think this is the correct format for a matrix row */
1305 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001306 if (parse_matrix(ctx, inst, Program,
1307 (GLint *) &state_tokens[0],
1308 (GLint *) &state_tokens[1],
1309 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001310 return 1;
1311
Brian65319522007-02-21 11:08:21 -07001312 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001313
1314 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001315 state_tokens[3] = parse_integer (inst, Program);
1316 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001317 program_error(ctx, Program->Position,
1318 "Second matrix index less than the first");
1319 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001320 return 1;
1321 }
1322 }
1323 else {
Brian65319522007-02-21 11:08:21 -07001324 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001325 (*inst)++;
1326 }
1327 break;
1328 }
1329
1330 return 0;
1331}
1332
1333/**
1334 * This parses a state string (rather, the binary version of it) into
1335 * a 6-token similar for the state fetching code in program.c
1336 *
1337 * One might ask, why fetch these parameters into just like you fetch
1338 * state when they are already stored in other places?
1339 *
1340 * Because of array offsets -> We can stick env/local parameters in the
1341 * middle of a parameter array and then index someplace into the array
1342 * when we execute.
1343 *
1344 * One optimization might be to only do this for the cases where the
1345 * env/local parameters end up inside of an array, and leave the
1346 * single parameters (or arrays of pure env/local pareameters) in their
1347 * respective register files.
1348 *
1349 * For ENV parameters, the format is:
1350 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1351 * state_tokens[1] = STATE_ENV
1352 * state_tokens[2] = the parameter index
1353 *
1354 * for LOCAL parameters, the format is:
1355 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1356 * state_tokens[1] = STATE_LOCAL
1357 * state_tokens[2] = the parameter index
1358 *
1359 * \param inst - the start in the binary arry to start working from
1360 * \param state_tokens - the storage for the 6-token state description
1361 * \return - 0 on sucess, 1 on failure
1362 */
1363static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001364parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001365 struct arb_program *Program,
1366 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001367{
1368 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1369 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1370 else
1371 state_tokens[0] = STATE_VERTEX_PROGRAM;
1372
1373
1374 switch (*(*inst)++) {
1375 case PROGRAM_PARAM_ENV:
1376 state_tokens[1] = STATE_ENV;
1377 state_tokens[2] = parse_integer (inst, Program);
1378
1379 /* Check state_tokens[2] against the number of ENV parameters available */
1380 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001381 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001382 ||
1383 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001384 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001385 program_error(ctx, Program->Position,
1386 "Invalid Program Env Parameter");
1387 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001388 return 1;
1389 }
1390
1391 break;
1392
1393 case PROGRAM_PARAM_LOCAL:
1394 state_tokens[1] = STATE_LOCAL;
1395 state_tokens[2] = parse_integer (inst, Program);
1396
1397 /* Check state_tokens[2] against the number of LOCAL parameters available */
1398 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001399 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001400 ||
1401 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001402 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001403 program_error(ctx, Program->Position,
1404 "Invalid Program Local Parameter");
1405 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001406 return 1;
1407 }
1408 break;
1409 }
1410
1411 return 0;
1412}
1413
1414/**
1415 * For ARB_vertex_program, programs are not allowed to use both an explicit
1416 * vertex attribute and a generic vertex attribute corresponding to the same
1417 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1418 *
1419 * This will walk our var_cache and make sure that nobody does anything fishy.
1420 *
1421 * \return 0 on sucess, 1 on error
1422 */
1423static GLuint
1424generic_attrib_check(struct var_cache *vc_head)
1425{
1426 int a;
1427 struct var_cache *curr;
1428 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1429 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1430
1431 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1432 explicitAttrib[a] = GL_FALSE;
1433 genericAttrib[a] = GL_FALSE;
1434 }
1435
1436 curr = vc_head;
1437 while (curr) {
1438 if (curr->type == vt_attrib) {
1439 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001440 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001441 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001442 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001443 }
1444
1445 curr = curr->next;
1446 }
1447
1448 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1449 if ((explicitAttrib[a]) && (genericAttrib[a]))
1450 return 1;
1451 }
1452
1453 return 0;
1454}
1455
1456/**
1457 * This will handle the binding side of an ATTRIB var declaration
1458 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001459 * \param inputReg returns the input register index, one of the
1460 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001461 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001462 */
1463static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001464parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001465 struct arb_program *Program,
1466 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001467{
Jouk Jansen40322e12004-04-05 08:50:36 +00001468 GLint err = 0;
1469
1470 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001471
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1473 switch (*(*inst)++) {
1474 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001475 {
1476 GLint coord;
1477 err = parse_color_type (ctx, inst, Program, &coord);
1478 *inputReg = FRAG_ATTRIB_COL0 + coord;
1479 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001480 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001481 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001482 {
Brian8fa6f732007-02-01 09:24:41 -07001483 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001484 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1485 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1486 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001487 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001488 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001489 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001490 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001491 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001492 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001493 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001494 default:
1495 err = 1;
1496 break;
1497 }
1498 }
1499 else {
1500 switch (*(*inst)++) {
1501 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001502 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001503 break;
1504
1505 case VERTEX_ATTRIB_WEIGHT:
1506 {
1507 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001508 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001509 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001510#if 1
1511 /* hack for Warcraft (see bug 8060) */
1512 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001513 break;
Brian Paul3a557502006-09-05 23:15:29 +00001514#else
Brian Paula088f162006-09-05 23:08:51 +00001515 program_error(ctx, Program->Position,
1516 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001517 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001518#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001519 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001520
1521 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001522 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001523 break;
1524
1525 case VERTEX_ATTRIB_COLOR:
1526 {
1527 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001528 err = parse_color_type (ctx, inst, Program, &color);
1529 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001530 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001531 }
1532 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001533 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001534 }
1535 }
1536 break;
1537
1538 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001539 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001540 break;
1541
1542 case VERTEX_ATTRIB_TEXCOORD:
1543 {
Brian8fa6f732007-02-01 09:24:41 -07001544 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001545 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001546 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001547 }
1548 break;
1549
Jouk Jansen40322e12004-04-05 08:50:36 +00001550 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001551 /* Not supported at this time */
1552 {
1553 const char *msg = "ARB_palette_matrix not supported";
1554 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001555 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001556 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001557 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001558
1559 case VERTEX_ATTRIB_GENERIC:
1560 {
1561 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001562 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001563 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001564 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001565 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1566 * attributes do not alias the conventional vertex
1567 * attributes.
1568 */
1569 if (attrib > 0)
1570 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001571 else
1572 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001573 }
1574 }
1575 break;
1576
1577 default:
1578 err = 1;
1579 break;
1580 }
1581 }
1582
Jouk Jansen40322e12004-04-05 08:50:36 +00001583 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001584 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001585 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001586
1587 return err;
1588}
1589
Brian Paul7aebaf32005-10-30 21:23:23 +00001590
Jouk Jansen40322e12004-04-05 08:50:36 +00001591/**
1592 * This translates between a binary token for an output variable type
1593 * and the mesa token for the same thing.
1594 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001595 * \param inst The parsed tokens
1596 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001597 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001598 */
1599static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001600parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001601 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001602{
Brian Paul7aebaf32005-10-30 21:23:23 +00001603 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001604
Brian Paul7aebaf32005-10-30 21:23:23 +00001605 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001606 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001608 GLuint out_color;
1609
Brian Paulbe76b7f2004-10-04 14:40:05 +00001610 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001611 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001612 */
1613 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001614 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001615 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001616 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001618 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1619 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001620 }
1621 break;
1622
1623 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001625 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001626 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001627 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001629 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001630 GLint color_type;
1631 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001632 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1633 if (err)
1634 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001635
Jouk Jansen40322e12004-04-05 08:50:36 +00001636 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001637 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001638 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001639 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001640 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001642 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001643 }
1644 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001645 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001646 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001647 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001648 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001649 }
1650 /* primary color */
1651 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001652 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001653 }
1654 }
1655 }
1656 break;
1657
1658 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001659 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001660 break;
1661
1662 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001663 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001664 break;
1665
1666 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001667 {
1668 GLuint unit;
1669 if (parse_texcoord_num (ctx, inst, Program, &unit))
1670 return 1;
1671 *outputReg = VERT_RESULT_TEX0 + unit;
1672 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001673 break;
1674 }
1675
Brian Paulde997602005-11-12 17:53:14 +00001676 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001677
1678 return 0;
1679}
1680
Brian Paul7aebaf32005-10-30 21:23:23 +00001681
Jouk Jansen40322e12004-04-05 08:50:36 +00001682/**
1683 * This handles the declaration of ATTRIB variables
1684 *
1685 * XXX: Still needs
1686 * parse_vert_attrib_binding(), or something like that
1687 *
1688 * \return 0 on sucess, 1 on error
1689 */
1690static GLint
Brian Paula088f162006-09-05 23:08:51 +00001691parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001692 struct arb_program *Program)
1693{
1694 GLuint found;
1695 char *error_msg;
1696 struct var_cache *attrib_var;
1697
1698 attrib_var = parse_string (inst, vc_head, Program, &found);
1699 Program->Position = parse_position (inst);
1700 if (found) {
1701 error_msg = (char *)
1702 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07001703 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00001704 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001705 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001706 _mesa_free (error_msg);
1707 return 1;
1708 }
1709
1710 attrib_var->type = vt_attrib;
1711
Brian Paul7aebaf32005-10-30 21:23:23 +00001712 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001713 &attrib_var->attrib_is_generic))
1714 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001715
Brian Paul7aebaf32005-10-30 21:23:23 +00001716 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001717 program_error(ctx, Program->Position,
1718 "Cannot use both a generic vertex attribute "
1719 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001720 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001721 }
1722
1723 Program->Base.NumAttributes++;
1724 return 0;
1725}
1726
1727/**
1728 * \param use -- TRUE if we're called when declaring implicit parameters,
1729 * FALSE if we're declaraing variables. This has to do with
1730 * if we get a signed or unsigned float for scalar constants
1731 */
1732static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001733parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001734 struct var_cache *param_var,
1735 struct arb_program *Program, GLboolean use)
1736{
1737 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001738 GLuint err = 0;
Brianaa9d22a2007-02-23 11:21:03 -07001739 gl_state_index state_tokens[STATE_LENGTH];
Jouk Jansen40322e12004-04-05 08:50:36 +00001740 GLfloat const_values[4];
1741
Jouk Jansen40322e12004-04-05 08:50:36 +00001742 switch (*(*inst)++) {
1743 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001744 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1745 return 1;
1746
1747 /* If we adding STATE_MATRIX that has multiple rows, we need to
1748 * unroll it and call _mesa_add_state_reference() for each row
1749 */
Brian65319522007-02-21 11:08:21 -07001750 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1751 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1752 state_tokens[0] == STATE_MVP_MATRIX ||
1753 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1754 state_tokens[0] == STATE_PROGRAM_MATRIX)
1755 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001756 GLint row;
Brian65319522007-02-21 11:08:21 -07001757 const GLint first_row = state_tokens[2];
1758 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001759
1760 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001761 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001762
Brian Paulde997602005-11-12 17:53:14 +00001763 idx = _mesa_add_state_reference(Program->Base.Parameters,
1764 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001765 if (param_var->param_binding_begin == ~0U)
1766 param_var->param_binding_begin = idx;
1767 param_var->param_binding_length++;
1768 Program->Base.NumParameters++;
1769 }
1770 }
1771 else {
Brian Paulde997602005-11-12 17:53:14 +00001772 idx = _mesa_add_state_reference(Program->Base.Parameters,
1773 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001774 if (param_var->param_binding_begin == ~0U)
1775 param_var->param_binding_begin = idx;
1776 param_var->param_binding_length++;
1777 Program->Base.NumParameters++;
1778 }
1779 break;
1780
1781 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001782 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1783 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001784 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001785 if (param_var->param_binding_begin == ~0U)
1786 param_var->param_binding_begin = idx;
1787 param_var->param_binding_length++;
1788 Program->Base.NumParameters++;
1789
1790 /* Check if there is more: 0 -> we're done, else its an integer */
1791 if (**inst) {
1792 GLuint out_of_range, new_idx;
1793 GLuint start_idx = state_tokens[2] + 1;
1794 GLuint end_idx = parse_integer (inst, Program);
1795
1796 out_of_range = 0;
1797 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1798 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001799 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001800 || ((state_tokens[1] == STATE_LOCAL)
1801 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001802 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001803 out_of_range = 1;
1804 }
1805 else {
1806 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001807 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001808 || ((state_tokens[1] == STATE_LOCAL)
1809 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001810 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001811 out_of_range = 1;
1812 }
1813 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001814 program_error(ctx, Program->Position,
1815 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001816 return 1;
1817 }
1818
1819 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1820 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001821 idx = _mesa_add_state_reference(Program->Base.Parameters,
1822 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001823 param_var->param_binding_length++;
1824 Program->Base.NumParameters++;
1825 }
1826 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001827 else {
1828 (*inst)++;
1829 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001830 break;
1831
1832 case PARAM_CONSTANT:
Brian7cbfe8c2008-01-18 12:45:55 -07001833 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
Jouk Jansen40322e12004-04-05 08:50:36 +00001834 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001835 idx = _mesa_add_named_constant(Program->Base.Parameters,
1836 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001837 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001838 if (param_var->param_binding_begin == ~0U)
1839 param_var->param_binding_begin = idx;
Brian7cbfe8c2008-01-18 12:45:55 -07001840 param_var->param_binding_type = PROGRAM_CONSTANT;
Jouk Jansen40322e12004-04-05 08:50:36 +00001841 param_var->param_binding_length++;
1842 Program->Base.NumParameters++;
1843 break;
1844
1845 default:
Brian Paula088f162006-09-05 23:08:51 +00001846 program_error(ctx, Program->Position,
1847 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001848 return 1;
1849 }
1850
1851 /* Make sure we haven't blown past our parameter limits */
1852 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1853 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001854 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001855 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1856 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001857 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001858 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001859 return 1;
1860 }
1861
1862 return err;
1863}
1864
Brian Paul7aebaf32005-10-30 21:23:23 +00001865
Jouk Jansen40322e12004-04-05 08:50:36 +00001866/**
1867 * This picks out PARAM program parameter bindings.
1868 *
1869 * XXX: This needs to be stressed & tested
1870 *
1871 * \return 0 on sucess, 1 on error
1872 */
1873static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001874parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001875 struct arb_program *Program)
1876{
Brian Paulbd997cd2004-07-20 21:12:56 +00001877 GLuint found, err;
1878 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001879 struct var_cache *param_var;
1880
1881 err = 0;
1882 param_var = parse_string (inst, vc_head, Program, &found);
1883 Program->Position = parse_position (inst);
1884
1885 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001886 char *error_msg = (char *)
1887 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07001888 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00001889 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001890 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001891 _mesa_free (error_msg);
1892 return 1;
1893 }
1894
1895 specified_length = parse_integer (inst, Program);
1896
1897 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001898 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001899 return 1;
1900 }
1901
1902 param_var->type = vt_param;
1903 param_var->param_binding_length = 0;
1904
1905 /* Right now, everything is shoved into the main state register file.
1906 *
1907 * In the future, it would be nice to leave things ENV/LOCAL params
1908 * in their respective register files, if possible
1909 */
1910 param_var->param_binding_type = PROGRAM_STATE_VAR;
1911
1912 /* Remember to:
1913 * * - add each guy to the parameter list
1914 * * - increment the param_var->param_binding_len
1915 * * - store the param_var->param_binding_begin for the first one
1916 * * - compare the actual len to the specified len at the end
1917 */
1918 while (**inst != PARAM_NULL) {
1919 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1920 return 1;
1921 }
1922
1923 /* Test array length here! */
1924 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001925 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001926 program_error(ctx, Program->Position,
1927 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001928 }
1929 }
1930
1931 (*inst)++;
1932
1933 return 0;
1934}
1935
1936/**
1937 *
1938 */
1939static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001940parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001941 struct arb_program *Program, struct var_cache **new_var)
1942{
1943 struct var_cache *param_var;
1944
1945 /* First, insert a dummy entry into the var_cache */
1946 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001947 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001948 param_var->type = vt_param;
1949
1950 param_var->param_binding_length = 0;
1951 /* Don't fill in binding_begin; We use the default value of -1
1952 * to tell if its already initialized, elsewhere.
1953 *
1954 * param_var->param_binding_begin = 0;
1955 */
1956 param_var->param_binding_type = PROGRAM_STATE_VAR;
1957
1958 var_cache_append (vc_head, param_var);
1959
1960 /* Then fill it with juicy parameter goodness */
1961 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1962 return 1;
1963
1964 *new_var = param_var;
1965
1966 return 0;
1967}
1968
1969
1970/**
1971 * This handles the declaration of TEMP variables
1972 *
1973 * \return 0 on sucess, 1 on error
1974 */
1975static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001976parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001977 struct arb_program *Program)
1978{
1979 GLuint found;
1980 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001981
1982 while (**inst != 0) {
1983 temp_var = parse_string (inst, vc_head, Program, &found);
1984 Program->Position = parse_position (inst);
1985 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001986 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001987 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07001988 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00001989 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001990 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001991 _mesa_free (error_msg);
1992 return 1;
1993 }
1994
1995 temp_var->type = vt_temp;
1996
1997 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1998 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001999 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002000 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2001 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002002 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00002003 program_error(ctx, Program->Position,
2004 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002005 return 1;
2006 }
2007
2008 temp_var->temp_binding = Program->Base.NumTemporaries;
2009 Program->Base.NumTemporaries++;
2010 }
2011 (*inst)++;
2012
2013 return 0;
2014}
2015
2016/**
2017 * This handles variables of the OUTPUT variety
2018 *
2019 * \return 0 on sucess, 1 on error
2020 */
2021static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002022parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002023 struct arb_program *Program)
2024{
2025 GLuint found;
2026 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002027 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002028
2029 output_var = parse_string (inst, vc_head, Program, &found);
2030 Program->Position = parse_position (inst);
2031 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002032 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002033 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07002034 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00002035 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002036 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002037 _mesa_free (error_msg);
2038 return 1;
2039 }
2040
2041 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002042
2043 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2044 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002045}
2046
2047/**
2048 * This handles variables of the ALIAS kind
2049 *
2050 * \return 0 on sucess, 1 on error
2051 */
2052static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002053parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002054 struct arb_program *Program)
2055{
2056 GLuint found;
2057 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002058
2059 temp_var = parse_string (inst, vc_head, Program, &found);
2060 Program->Position = parse_position (inst);
2061
2062 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002063 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002064 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07002065 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00002066 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002067 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002068 _mesa_free (error_msg);
2069 return 1;
2070 }
2071
2072 temp_var->type = vt_alias;
2073 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2074 Program->Position = parse_position (inst);
2075
2076 if (!found)
2077 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002078 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002079 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2080 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2081 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002082 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002083 _mesa_free (error_msg);
2084 return 1;
2085 }
2086
2087 return 0;
2088}
2089
2090/**
2091 * This handles variables of the ADDRESS kind
2092 *
2093 * \return 0 on sucess, 1 on error
2094 */
2095static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002096parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002097 struct arb_program *Program)
2098{
2099 GLuint found;
2100 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002101
2102 while (**inst != 0) {
2103 temp_var = parse_string (inst, vc_head, Program, &found);
2104 Program->Position = parse_position (inst);
2105 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002106 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002107 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07002108 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00002109 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002110 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002111 _mesa_free (error_msg);
2112 return 1;
2113 }
2114
2115 temp_var->type = vt_address;
2116
2117 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002118 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002119 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002120 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002121 return 1;
2122 }
2123
2124 temp_var->address_binding = Program->Base.NumAddressRegs;
2125 Program->Base.NumAddressRegs++;
2126 }
2127 (*inst)++;
2128
2129 return 0;
2130}
2131
2132/**
2133 * Parse a program declaration
2134 *
2135 * \return 0 on sucess, 1 on error
2136 */
2137static GLint
Brian Paula088f162006-09-05 23:08:51 +00002138parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002139 struct arb_program *Program)
2140{
2141 GLint err = 0;
2142
2143 switch (*(*inst)++) {
2144 case ADDRESS:
2145 err = parse_address (ctx, inst, vc_head, Program);
2146 break;
2147
2148 case ALIAS:
2149 err = parse_alias (ctx, inst, vc_head, Program);
2150 break;
2151
2152 case ATTRIB:
2153 err = parse_attrib (ctx, inst, vc_head, Program);
2154 break;
2155
2156 case OUTPUT:
2157 err = parse_output (ctx, inst, vc_head, Program);
2158 break;
2159
2160 case PARAM:
2161 err = parse_param (ctx, inst, vc_head, Program);
2162 break;
2163
2164 case TEMP:
2165 err = parse_temp (ctx, inst, vc_head, Program);
2166 break;
2167 }
2168
2169 return err;
2170}
2171
2172/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002173 * Handle the parsing out of a masked destination register, either for a
2174 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002175 *
2176 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002177 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002178 * position invariant
2179 *
2180 * \param File - The register file we write to
2181 * \param Index - The register index we write to
2182 * \param WriteMask - The mask controlling which components we write (1->write)
2183 *
2184 * \return 0 on sucess, 1 on error
2185 */
2186static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002187parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002188 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002189 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002190{
Brian Paul7aebaf32005-10-30 21:23:23 +00002191 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002192 struct var_cache *dst;
2193
2194 /* We either have a result register specified, or a
2195 * variable that may or may not be writable
2196 */
2197 switch (*(*inst)++) {
2198 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002199 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002200 return 1;
2201 *File = PROGRAM_OUTPUT;
2202 break;
2203
2204 case REGISTER_ESTABLISHED_NAME:
2205 dst = parse_string (inst, vc_head, Program, &result);
2206 Program->Position = parse_position (inst);
2207
2208 /* If the name has never been added to our symbol table, we're hosed */
2209 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002210 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002211 return 1;
2212 }
2213
2214 switch (dst->type) {
2215 case vt_output:
2216 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002217 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002218 break;
2219
2220 case vt_temp:
2221 *File = PROGRAM_TEMPORARY;
2222 *Index = dst->temp_binding;
2223 break;
2224
2225 /* If the var type is not vt_output or vt_temp, no go */
2226 default:
Brian Paula088f162006-09-05 23:08:51 +00002227 program_error(ctx, Program->Position,
2228 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002229 return 1;
2230 }
2231 break;
2232
2233 default:
Brian Paula088f162006-09-05 23:08:51 +00002234 program_error(ctx, Program->Position,
2235 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002236 return 1;
2237 }
2238
2239
2240 /* Position invariance test */
2241 /* This test is done now in syntax portion - when position invariance OPTION
2242 is specified, "result.position" rule is disabled so there is no way
2243 to write the position
2244 */
2245 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2246 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002247 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002248 "Vertex program specified position invariance and wrote vertex position");
2249 }*/
2250
2251 /* And then the mask.
2252 * w,a -> bit 0
2253 * z,b -> bit 1
2254 * y,g -> bit 2
2255 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002256 *
2257 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002258 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002259 tmp = (GLint) *(*inst)++;
2260 *WriteMask = (((tmp>>3) & 0x1) |
2261 ((tmp>>1) & 0x2) |
2262 ((tmp<<1) & 0x4) |
2263 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002264
2265 return 0;
2266}
2267
2268
2269/**
2270 * Handle the parsing of a address register
2271 *
2272 * \param Index - The register index we write to
2273 *
2274 * \return 0 on sucess, 1 on error
2275 */
2276static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002277parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002278 struct var_cache **vc_head,
2279 struct arb_program *Program, GLint * Index)
2280{
2281 struct var_cache *dst;
2282 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002283
2284 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002285
2286 dst = parse_string (inst, vc_head, Program, &result);
2287 Program->Position = parse_position (inst);
2288
2289 /* If the name has never been added to our symbol table, we're hosed */
2290 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002291 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002292 return 1;
2293 }
2294
2295 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002296 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002297 return 1;
2298 }
2299
2300 return 0;
2301}
2302
Brian Paul8e8fa632005-07-01 02:03:33 +00002303#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002304/**
2305 * Handle the parsing out of a masked address register
2306 *
2307 * \param Index - The register index we write to
2308 * \param WriteMask - The mask controlling which components we write (1->write)
2309 *
2310 * \return 0 on sucess, 1 on error
2311 */
2312static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002313parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002314 struct var_cache **vc_head,
2315 struct arb_program *Program, GLint * Index,
2316 GLboolean * WriteMask)
2317{
2318 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2319 return 1;
2320
2321 /* This should be 0x8 */
2322 (*inst)++;
2323
2324 /* Writemask of .x is implied */
2325 WriteMask[0] = 1;
2326 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2327
2328 return 0;
2329}
Brian Paul8e8fa632005-07-01 02:03:33 +00002330#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002331
2332/**
2333 * Parse out a swizzle mask.
2334 *
Brian Paul32df89e2005-10-29 18:26:43 +00002335 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002336 *
2337 * The len parameter allows us to grab 4 components for a vector
2338 * swizzle, or just 1 component for a scalar src register selection
2339 */
Brian Paul32df89e2005-10-29 18:26:43 +00002340static void
Brian Paula088f162006-09-05 23:08:51 +00002341parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002342{
Brian Paul32df89e2005-10-29 18:26:43 +00002343 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002344
Brian Paul32df89e2005-10-29 18:26:43 +00002345 for (i = 0; i < 4; i++)
2346 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002347
Brian Paul32df89e2005-10-29 18:26:43 +00002348 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002349 switch (*(*inst)++) {
2350 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002351 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002352 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002353 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002354 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002355 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002356 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002357 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002358 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002359 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002360 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002361 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002362 default:
2363 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2364 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002365 }
2366 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002367}
2368
Jouk Jansen40322e12004-04-05 08:50:36 +00002369
Brian Paul32df89e2005-10-29 18:26:43 +00002370/**
2371 * Parse an extended swizzle mask which is a sequence of
2372 * four x/y/z/w/0/1 tokens.
2373 * \return swizzle four swizzle values
2374 * \return negateMask four element bitfield
2375 */
2376static void
Brian Paula088f162006-09-05 23:08:51 +00002377parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002378 GLubyte *negateMask)
2379{
2380 GLint i;
2381
2382 *negateMask = 0x0;
2383 for (i = 0; i < 4; i++) {
2384 GLubyte swz;
2385 if (parse_sign(inst) == -1)
2386 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002387
2388 swz = *(*inst)++;
2389
2390 switch (swz) {
2391 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002392 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002393 break;
2394 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002395 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002396 break;
2397 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002398 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002399 break;
2400 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002401 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002402 break;
2403 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002404 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002405 break;
2406 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002407 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002408 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002409 default:
2410 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2411 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002412 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002413 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002414}
2415
2416
2417static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002418parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2419 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002420 struct arb_program *Program,
2421 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002422 GLboolean *IsRelOffset )
2423{
2424 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002425 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002426 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002427
Keith Whitwell7c26b612005-04-21 14:46:57 +00002428 *IsRelOffset = 0;
2429
Jouk Jansen40322e12004-04-05 08:50:36 +00002430 /* And the binding for the src */
2431 switch (*(*inst)++) {
2432 case REGISTER_ATTRIB:
2433 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002434 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002435 return 1;
2436 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002437 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002438
2439 /* We need to insert a dummy variable into the var_cache so we can
2440 * catch generic vertex attrib aliasing errors
2441 */
2442 var_cache_create(&src);
2443 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002444 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002445 src->attrib_binding = binding;
2446 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002447 var_cache_append(vc_head, src);
2448 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002449 program_error(ctx, Program->Position,
2450 "Cannot use both a generic vertex attribute "
2451 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002452 return 1;
2453 }
2454 break;
2455
2456 case REGISTER_PARAM:
2457 switch (**inst) {
2458 case PARAM_ARRAY_ELEMENT:
2459 (*inst)++;
2460 src = parse_string (inst, vc_head, Program, &found);
2461 Program->Position = parse_position (inst);
2462
2463 if (!found) {
Brian7b91d872008-03-27 15:48:54 -06002464 char s[1000];
2465 sprintf(s, "Undefined variable: %s", src->name);
2466 program_error(ctx, Program->Position, s);
Jouk Jansen40322e12004-04-05 08:50:36 +00002467 return 1;
2468 }
2469
Brian Paul95801792005-12-06 15:41:43 +00002470 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002471
2472 switch (*(*inst)++) {
2473 case ARRAY_INDEX_ABSOLUTE:
2474 offset = parse_integer (inst, Program);
2475
2476 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002477 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002478 program_error(ctx, Program->Position,
2479 "Index out of range");
2480 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002481 return 1;
2482 }
2483
2484 *Index = src->param_binding_begin + offset;
2485 break;
2486
2487 case ARRAY_INDEX_RELATIVE:
2488 {
2489 GLint addr_reg_idx, rel_off;
2490
2491 /* First, grab the address regiseter */
2492 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2493 return 1;
2494
2495 /* And the .x */
2496 ((*inst)++);
2497 ((*inst)++);
2498 ((*inst)++);
2499 ((*inst)++);
2500
2501 /* Then the relative offset */
2502 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2503
2504 /* And store it properly */
2505 *Index = src->param_binding_begin + rel_off;
2506 *IsRelOffset = 1;
2507 }
2508 break;
2509 }
2510 break;
2511
2512 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002513 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2514 return 1;
2515
Brian Paul95801792005-12-06 15:41:43 +00002516 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002517 *Index = src->param_binding_begin;
2518 break;
2519 }
2520 break;
2521
2522 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002523 src = parse_string (inst, vc_head, Program, &found);
2524 Program->Position = parse_position (inst);
2525
2526 /* If the name has never been added to our symbol table, we're hosed */
2527 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002528 program_error(ctx, Program->Position,
2529 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002530 return 1;
2531 }
2532
2533 switch (src->type) {
2534 case vt_attrib:
2535 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002536 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002537 break;
2538
2539 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2540 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002541 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002542 *Index = src->param_binding_begin;
2543 break;
2544
2545 case vt_temp:
2546 *File = PROGRAM_TEMPORARY;
2547 *Index = src->temp_binding;
2548 break;
2549
2550 /* If the var type is vt_output no go */
2551 default:
Brian Paula088f162006-09-05 23:08:51 +00002552 program_error(ctx, Program->Position,
2553 "destination register is read only");
2554 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002555 return 1;
2556 }
2557 break;
2558
2559 default:
Brian Paula088f162006-09-05 23:08:51 +00002560 program_error(ctx, Program->Position,
2561 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002562 return 1;
2563 }
2564
Markus Amslerf3c49062008-03-17 08:35:37 -06002565 /* Add attributes to InputsRead only if they are used the program.
2566 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2567 if (*File == PROGRAM_INPUT)
2568 Program->Base.InputsRead |= (1 << *Index);
2569
Jouk Jansen40322e12004-04-05 08:50:36 +00002570 return 0;
2571}
2572
2573/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002574 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002575 */
2576static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002577parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002578 struct var_cache **vc_head,
2579 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002580 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002581{
Brian Paul7aebaf32005-10-30 21:23:23 +00002582 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002583 GLint index;
2584 GLboolean negate;
2585 GLubyte swizzle[4];
2586 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002587
Jouk Jansen40322e12004-04-05 08:50:36 +00002588 /* Grab the sign */
Brian Paulbedb93e2008-03-25 11:22:57 -06002589 negate = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002590
2591 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002592 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002593 return 1;
2594
2595 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002596 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002597
Brian Paul32df89e2005-10-29 18:26:43 +00002598 reg->File = file;
2599 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002600 reg->NegateBase = negate;
2601 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002602 return 0;
2603}
2604
Jouk Jansen40322e12004-04-05 08:50:36 +00002605
Brian Paulb7fc1c32006-08-30 23:38:03 +00002606/**
2607 * Parse fragment program destination register.
2608 * \return 1 if error, 0 if no error.
2609 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002610static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002611parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002612 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002613 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002614{
Brian Paul7aebaf32005-10-30 21:23:23 +00002615 GLint mask;
2616 GLuint idx;
2617 enum register_file file;
2618
Keith Whitwell7c26b612005-04-21 14:46:57 +00002619 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002620 return 1;
2621
Keith Whitwell7c26b612005-04-21 14:46:57 +00002622 reg->File = file;
2623 reg->Index = idx;
2624 reg->WriteMask = mask;
2625 return 0;
2626}
2627
2628
Brian Paul7aebaf32005-10-30 21:23:23 +00002629/**
2630 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002631 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002632 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002633static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002634parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002635 struct var_cache **vc_head,
2636 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002637 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002638{
Brian Paul7aebaf32005-10-30 21:23:23 +00002639 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002640 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002641 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002642 GLubyte Swizzle[4];
2643 GLboolean IsRelOffset;
2644
2645 /* Grab the sign */
Brian Paulbedb93e2008-03-25 11:22:57 -06002646 Negate = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002647
2648 /* And the src reg */
2649 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2650 return 1;
2651
2652 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002653 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002654
Keith Whitwell7c26b612005-04-21 14:46:57 +00002655 reg->File = File;
2656 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002657 reg->NegateBase = Negate;
2658 reg->Swizzle = (Swizzle[0] << 0);
2659
Jouk Jansen40322e12004-04-05 08:50:36 +00002660 return 0;
2661}
2662
Keith Whitwell7c26b612005-04-21 14:46:57 +00002663
Jouk Jansen40322e12004-04-05 08:50:36 +00002664/**
2665 * This is a big mother that handles getting opcodes into the instruction
2666 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002667 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002668 */
2669static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002670parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002671 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002672 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002673{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002674 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002675 GLuint texcoord;
2676 GLubyte instClass, type, code;
2677 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002678 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002679
Brian Pauld6272e02006-10-29 18:03:16 +00002680 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002681
2682 /* Record the position in the program string for debugging */
2683 fp->StringPos = Program->Position;
2684
2685 /* OP_ALU_INST or OP_TEX_INST */
2686 instClass = *(*inst)++;
2687
2688 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2689 * OP_TEX_{SAMPLE, KIL}
2690 */
2691 type = *(*inst)++;
2692
2693 /* The actual opcode name */
2694 code = *(*inst)++;
2695
2696 /* Increment the correct count */
2697 switch (instClass) {
2698 case OP_ALU_INST:
2699 Program->NumAluInstructions++;
2700 break;
2701 case OP_TEX_INST:
2702 Program->NumTexInstructions++;
2703 break;
2704 }
2705
Jouk Jansen40322e12004-04-05 08:50:36 +00002706 switch (type) {
2707 case OP_ALU_VECTOR:
2708 switch (code) {
2709 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002710 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002711 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002712 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002713 break;
2714
2715 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002716 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002717 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002718 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002719 break;
2720
2721 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002722 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002723 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002724 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002725 break;
2726
2727 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002728 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002729 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002730 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002731 break;
2732
2733 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002734 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002735 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002736 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002737 break;
2738 }
2739
Keith Whitwell7c26b612005-04-21 14:46:57 +00002740 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002741 return 1;
2742
Keith Whitwell7c26b612005-04-21 14:46:57 +00002743 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002744 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002745 break;
2746
2747 case OP_ALU_SCALAR:
2748 switch (code) {
2749 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002750 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002752 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 break;
2754
2755 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002756 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002757 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002758 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002759 break;
2760
2761 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002762 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002763 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002764 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002765 break;
2766
2767 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002768 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002769 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002770 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002771 break;
2772
2773 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002774 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002776 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002777 break;
2778
2779 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002780 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002781 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002782 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 break;
2784
2785 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002786 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 case OP_SCS:
2788
Brian Paul7e807512005-11-05 17:10:45 +00002789 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002790 break;
2791 }
2792
Keith Whitwell7c26b612005-04-21 14:46:57 +00002793 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002794 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002795
2796 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002798 break;
2799
2800 case OP_ALU_BINSC:
2801 switch (code) {
2802 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002803 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002804 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002805 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002806 break;
2807 }
2808
Keith Whitwell7c26b612005-04-21 14:46:57 +00002809 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002810 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002811
Jouk Jansen40322e12004-04-05 08:50:36 +00002812 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002813 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002814 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002815 }
2816 break;
2817
2818
2819 case OP_ALU_BIN:
2820 switch (code) {
2821 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002822 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002823 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002824 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 break;
2826
2827 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002828 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002829 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002830 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 break;
2832
2833 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002834 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002836 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002837 break;
2838
2839 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002840 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002842 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002843 break;
2844
2845 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002846 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002848 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 break;
2850
2851 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002852 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002854 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 break;
2856
2857 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002858 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002859 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002860 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002861 break;
2862
2863 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002864 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002865 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002866 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002867 break;
2868
2869 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002870 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002871 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002872 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002873 break;
2874
2875 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002876 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002877 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002878 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002879 break;
2880
2881 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002882 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002884 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 break;
2886
2887 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002888 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002890 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 break;
2892 }
2893
Keith Whitwell7c26b612005-04-21 14:46:57 +00002894 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002895 return 1;
2896 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002897 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2898 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 }
2900 break;
2901
2902 case OP_ALU_TRI:
2903 switch (code) {
2904 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002905 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002906 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002907 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 break;
2909
2910 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002911 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002912 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002913 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 break;
2915
2916 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002917 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002918 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002919 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002920 break;
2921 }
2922
Keith Whitwell7c26b612005-04-21 14:46:57 +00002923 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002924 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002925
Jouk Jansen40322e12004-04-05 08:50:36 +00002926 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002927 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2928 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002929 }
2930 break;
2931
2932 case OP_ALU_SWZ:
2933 switch (code) {
2934 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002935 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002937 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002938 break;
2939 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002940 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002941 return 1;
2942
Keith Whitwell7c26b612005-04-21 14:46:57 +00002943 {
Brian Paul32df89e2005-10-29 18:26:43 +00002944 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002945 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002946 enum register_file file;
2947 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002948
Brian Paul32df89e2005-10-29 18:26:43 +00002949 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002950 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002951 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2952 fp->SrcReg[0].File = file;
2953 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002954 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002955 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2956 swizzle[1],
2957 swizzle[2],
2958 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002959 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002960 break;
2961
2962 case OP_TEX_SAMPLE:
2963 switch (code) {
2964 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002965 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002966 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002967 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002968 break;
2969
2970 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002971 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002972 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002973 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002974 break;
2975
2976 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002977 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002978 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002979 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002980 break;
2981 }
2982
Keith Whitwell7c26b612005-04-21 14:46:57 +00002983 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002984 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002985
2986 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002987 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002988
2989 /* texImageUnit */
2990 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2991 return 1;
2992 fp->TexSrcUnit = texcoord;
2993
2994 /* texTarget */
2995 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07002996 case TEXTARGET_SHADOW1D:
2997 shadow_tex = 1 << texcoord;
2998 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00002999 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003000 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003001 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003002 case TEXTARGET_SHADOW2D:
3003 shadow_tex = 1 << texcoord;
3004 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003005 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003006 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003007 break;
3008 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003009 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003010 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003011 case TEXTARGET_SHADOWRECT:
3012 shadow_tex = 1 << texcoord;
3013 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003014 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003015 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003016 break;
3017 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003018 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003019 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003020 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003021 shadow_tex = 1 << texcoord;
3022 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003023 case TEXTARGET_1D_ARRAY:
3024 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3025 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003026 case TEXTARGET_SHADOW2D_ARRAY:
3027 shadow_tex = 1 << texcoord;
3028 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003029 case TEXTARGET_2D_ARRAY:
3030 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3031 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003032 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003033
3034 /* Don't test the first time a particular sampler is seen. Each time
3035 * after that, make sure the shadow state is the same.
3036 */
3037 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3038 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3039 program_error(ctx, Program->Position,
3040 "texture image unit used for shadow sampling and non-shadow sampling");
3041 return 1;
3042 }
3043
Brian Paulb7fc1c32006-08-30 23:38:03 +00003044 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3045 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3046 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003047 program_error(ctx, Program->Position,
3048 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003049 return 1;
3050 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003051
3052
3053 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003054 break;
3055
3056 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003057 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003058 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003059 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003060 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003061 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003062 default:
3063 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3064 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003065 }
3066
3067 return 0;
3068}
3069
Keith Whitwell7c26b612005-04-21 14:46:57 +00003070static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003071parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003072 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003073 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003074{
Brian Paul7aebaf32005-10-30 21:23:23 +00003075 GLint mask;
3076 GLuint idx;
3077 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003078
3079 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3080 return 1;
3081
3082 reg->File = file;
3083 reg->Index = idx;
3084 reg->WriteMask = mask;
3085 return 0;
3086}
3087
3088/**
3089 * Handle the parsing out of a masked address register
3090 *
3091 * \param Index - The register index we write to
3092 * \param WriteMask - The mask controlling which components we write (1->write)
3093 *
3094 * \return 0 on sucess, 1 on error
3095 */
3096static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003097parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003098 struct var_cache **vc_head,
3099 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003100 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003101{
3102 GLint idx;
3103
3104 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3105 return 1;
3106
3107 /* This should be 0x8 */
3108 (*inst)++;
3109
3110 reg->File = PROGRAM_ADDRESS;
3111 reg->Index = idx;
3112
3113 /* Writemask of .x is implied */
3114 reg->WriteMask = 0x1;
3115 return 0;
3116}
3117
3118/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003119 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003120 */
3121static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003122parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003123 struct var_cache **vc_head,
3124 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003125 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003126{
Brian Paul7aebaf32005-10-30 21:23:23 +00003127 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003128 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003129 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003130 GLubyte swizzle[4];
3131 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003132
3133 /* Grab the sign */
Brian Paulbedb93e2008-03-25 11:22:57 -06003134 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003135
3136 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003137 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003138 return 1;
3139
3140 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003141 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003142
Brian Paul32df89e2005-10-29 18:26:43 +00003143 reg->File = file;
3144 reg->Index = index;
3145 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3146 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003147 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003148 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003149 return 0;
3150}
3151
3152
3153static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003154parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003155 struct var_cache **vc_head,
3156 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003157 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003158{
Brian Paul7aebaf32005-10-30 21:23:23 +00003159 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003160 GLint Index;
Brian Paulbedb93e2008-03-25 11:22:57 -06003161 GLubyte negateMask;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003162 GLubyte Swizzle[4];
3163 GLboolean IsRelOffset;
3164
3165 /* Grab the sign */
Brian Paulbedb93e2008-03-25 11:22:57 -06003166 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003167
3168 /* And the src reg */
3169 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3170 return 1;
3171
3172 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003173 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003174
3175 reg->File = File;
3176 reg->Index = Index;
3177 reg->Swizzle = (Swizzle[0] << 0);
Brian Paulbedb93e2008-03-25 11:22:57 -06003178 reg->NegateBase = negateMask;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003179 reg->RelAddr = IsRelOffset;
3180 return 0;
3181}
3182
3183
Jouk Jansen40322e12004-04-05 08:50:36 +00003184/**
3185 * This is a big mother that handles getting opcodes into the instruction
3186 * and handling the src & dst registers for vertex program instructions
3187 */
3188static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003189parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003190 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003191 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003192{
3193 GLint a;
3194 GLubyte type, code;
3195
3196 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3197 type = *(*inst)++;
3198
3199 /* The actual opcode name */
3200 code = *(*inst)++;
3201
Brian Pauld6272e02006-10-29 18:03:16 +00003202 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003203 /* Record the position in the program string for debugging */
3204 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003205
3206 switch (type) {
3207 /* XXX: */
3208 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003209 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003210
3211 /* Remember to set SrcReg.RelAddr; */
3212
3213 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003214 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003215 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003216
Jouk Jansen40322e12004-04-05 08:50:36 +00003217 vp->DstReg.File = PROGRAM_ADDRESS;
3218
3219 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003220 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003221 return 1;
3222
3223 break;
3224
3225 case OP_ALU_VECTOR:
3226 switch (code) {
3227 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003228 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003229 break;
3230 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003231 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003232 break;
3233 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003234 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 break;
3236 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003237 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003238 break;
3239 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003240 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003241 break;
3242 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003243
3244 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003246
3247 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 return 1;
3249 break;
3250
3251 case OP_ALU_SCALAR:
3252 switch (code) {
3253 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003254 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003255 break;
3256 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003257 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003258 break;
3259 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003260 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003261 break;
3262 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003263 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003264 break;
3265 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003266 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003267 break;
3268 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003269 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003270 break;
3271 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003272 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003273 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003274
3275 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 return 1;
3277 break;
3278
3279 case OP_ALU_BINSC:
3280 switch (code) {
3281 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003282 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003283 break;
3284 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003285 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003286 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003287
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003289 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003290 return 1;
3291 }
3292 break;
3293
3294 case OP_ALU_BIN:
3295 switch (code) {
3296 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003297 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003298 break;
3299 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003300 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003301 break;
3302 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003303 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003304 break;
3305 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003306 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003307 break;
3308 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003309 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003310 break;
3311 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003312 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003313 break;
3314 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003315 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003316 break;
3317 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003318 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003319 break;
3320 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003321 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003322 break;
3323 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003324 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003325 break;
3326 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003327 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003328 break;
3329 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003330 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003331 break;
3332 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003333 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003334 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003335
Jouk Jansen40322e12004-04-05 08:50:36 +00003336 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003337 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003338 return 1;
3339 }
3340 break;
3341
3342 case OP_ALU_TRI:
3343 switch (code) {
3344 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003345 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003346 break;
3347 }
3348
Keith Whitwell7c26b612005-04-21 14:46:57 +00003349 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003350 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003351
Jouk Jansen40322e12004-04-05 08:50:36 +00003352 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003353 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003354 return 1;
3355 }
3356 break;
3357
3358 case OP_ALU_SWZ:
3359 switch (code) {
3360 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003361 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003362 break;
3363 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003364 {
Brian Paul32df89e2005-10-29 18:26:43 +00003365 GLubyte swizzle[4];
3366 GLubyte negateMask;
3367 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003368 enum register_file file;
3369 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003370
Keith Whitwell7c26b612005-04-21 14:46:57 +00003371 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3372 return 1;
3373
Brian Paul32df89e2005-10-29 18:26:43 +00003374 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003375 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003376 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3377 vp->SrcReg[0].File = file;
3378 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003379 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003380 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3381 swizzle[1],
3382 swizzle[2],
3383 swizzle[3]);
3384 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003385 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003386 break;
3387 }
3388 return 0;
3389}
3390
3391#if DEBUG_PARSING
3392
3393static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003394debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3395 struct arb_program *Program)
3396{
3397 struct var_cache *vc;
3398 GLint a, b;
3399
Brianb618ac82007-02-22 09:39:25 -07003400 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003401
3402 /* First of all, print out the contents of the var_cache */
3403 vc = vc_head;
3404 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003405 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003406 switch (vc->type) {
3407 case vt_none:
3408 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3409 break;
3410 case vt_attrib:
3411 fprintf (stderr, "ATTRIB %s\n", vc->name);
3412 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3413 break;
3414 case vt_param:
3415 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3416 vc->param_binding_begin, vc->param_binding_length);
3417 b = vc->param_binding_begin;
3418 for (a = 0; a < vc->param_binding_length; a++) {
3419 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003420 Program->Base.Parameters->Parameters[a + b].Name);
3421 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3422 const char *s;
3423 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3424 [a + b].StateIndexes);
3425 fprintf(stderr, "%s\n", s);
3426 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003427 }
3428 else
3429 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003430 Program->Base.Parameters->ParameterValues[a + b][0],
3431 Program->Base.Parameters->ParameterValues[a + b][1],
3432 Program->Base.Parameters->ParameterValues[a + b][2],
3433 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003434 }
3435 break;
3436 case vt_temp:
3437 fprintf (stderr, "TEMP %s\n", vc->name);
3438 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3439 break;
3440 case vt_output:
3441 fprintf (stderr, "OUTPUT %s\n", vc->name);
3442 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3443 break;
3444 case vt_alias:
3445 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003446 fprintf (stderr, " binding: 0x%p (%s)\n",
3447 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003448 break;
Brianb618ac82007-02-22 09:39:25 -07003449 default:
3450 /* nothing */
3451 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003452 }
3453 vc = vc->next;
3454 }
3455}
3456
Brian Paul72030e02005-11-03 03:30:34 +00003457#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003458
3459
3460/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003461 * The main loop for parsing a fragment or vertex program
3462 *
Brian Paul72030e02005-11-03 03:30:34 +00003463 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003464 */
Brian Paul72030e02005-11-03 03:30:34 +00003465static GLint
Brian Paula088f162006-09-05 23:08:51 +00003466parse_instructions(GLcontext * ctx, const GLubyte * inst,
3467 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003468{
Brian Paul72030e02005-11-03 03:30:34 +00003469 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3470 ? ctx->Const.FragmentProgram.MaxInstructions
3471 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003472 GLint err = 0;
3473
Brian Paul72030e02005-11-03 03:30:34 +00003474 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3475
Jouk Jansen40322e12004-04-05 08:50:36 +00003476 Program->MajorVersion = (GLuint) * inst++;
3477 Program->MinorVersion = (GLuint) * inst++;
3478
3479 while (*inst != END) {
3480 switch (*inst++) {
3481
3482 case OPTION:
3483 switch (*inst++) {
3484 case ARB_PRECISION_HINT_FASTEST:
3485 Program->PrecisionOption = GL_FASTEST;
3486 break;
3487
3488 case ARB_PRECISION_HINT_NICEST:
3489 Program->PrecisionOption = GL_NICEST;
3490 break;
3491
3492 case ARB_FOG_EXP:
3493 Program->FogOption = GL_EXP;
3494 break;
3495
3496 case ARB_FOG_EXP2:
3497 Program->FogOption = GL_EXP2;
3498 break;
3499
3500 case ARB_FOG_LINEAR:
3501 Program->FogOption = GL_LINEAR;
3502 break;
3503
3504 case ARB_POSITION_INVARIANT:
3505 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003506 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003507 break;
3508
3509 case ARB_FRAGMENT_PROGRAM_SHADOW:
3510 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3511 /* TODO ARB_fragment_program_shadow code */
3512 }
3513 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003514
3515 case ARB_DRAW_BUFFERS:
3516 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3517 /* do nothing for now */
3518 }
3519 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003520
3521 case MESA_TEXTURE_ARRAY:
3522 /* do nothing for now */
3523 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003524 }
3525 break;
3526
3527 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003528 /* check length */
3529 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003530 program_error(ctx, Program->Position,
3531 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003532 return 1;
3533 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003534 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003535 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003536 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003537 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003538 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003539 }
3540 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003541 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003542 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003543 }
3544
Brian Paul72030e02005-11-03 03:30:34 +00003545 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003546 Program->Base.NumInstructions++;
3547 break;
3548
3549 case DECLARATION:
3550 err = parse_declaration (ctx, &inst, vc_head, Program);
3551 break;
3552
3553 default:
3554 break;
3555 }
3556
3557 if (err)
3558 break;
3559 }
3560
3561 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003562 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003563 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003564 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003565 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003566 /* YYY Wrong Position in program, whatever, at least not random -> crash
3567 Program->Position = parse_position (&inst);
3568 */
Brian Paul8c41a142005-11-19 15:36:28 +00003569 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003570 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003571 Program->Base.NumInstructions++;
3572
Brian Paul05051032005-11-01 04:36:33 +00003573 /*
3574 * Initialize native counts to logical counts. The device driver may
3575 * change them if program is translated into a hardware program.
3576 */
3577 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3578 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3579 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3580 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3581 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003582
Jouk Jansen40322e12004-04-05 08:50:36 +00003583 return err;
3584}
3585
Brian Paul7aebaf32005-10-30 21:23:23 +00003586
Jouk Jansen40322e12004-04-05 08:50:36 +00003587/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003588LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003589#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003590;
3591
Brian Paul7aebaf32005-10-30 21:23:23 +00003592
Brian Paul8c41a142005-11-19 15:36:28 +00003593/**
3594 * Set a grammar parameter.
3595 * \param name the grammar parameter
3596 * \param value the new parameter value
3597 * \return 0 if OK, 1 if error
3598 */
3599static int
3600set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003601{
3602 char error_msg[300];
3603 GLint error_pos;
3604
Brian Paul8c41a142005-11-19 15:36:28 +00003605 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003606 return 0;
3607
3608 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3609 _mesa_set_program_error (ctx, error_pos, error_msg);
3610 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3611 return 1;
3612}
3613
Brian Paul8c41a142005-11-19 15:36:28 +00003614
3615/**
3616 * Enable support for the given language option in the parser.
3617 * \return 1 if OK, 0 if error
3618 */
3619static int
3620enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003621{
Brian Paul8c41a142005-11-19 15:36:28 +00003622 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003623}
3624
Brian Paul8c41a142005-11-19 15:36:28 +00003625
3626/**
3627 * Enable parser extensions based on which OpenGL extensions are supported
3628 * by this rendering context.
3629 *
3630 * \return GL_TRUE if OK, GL_FALSE if error.
3631 */
3632static GLboolean
3633enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003634{
Brian Paul8c41a142005-11-19 15:36:28 +00003635#if 0
3636 /* These are not supported at this time */
3637 if ((ctx->Extensions.ARB_vertex_blend ||
3638 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003639 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003640 return GL_FALSE;
3641 if (ctx->Extensions.ARB_matrix_palette
3642 && !enable_ext(ctx, id, "matrix_palette"))
3643 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003644#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003645 if (ctx->Extensions.ARB_fragment_program_shadow
3646 && !enable_ext(ctx, id, "fragment_program_shadow"))
3647 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003648 if (ctx->Extensions.EXT_point_parameters
3649 && !enable_ext(ctx, id, "point_parameters"))
3650 return GL_FALSE;
3651 if (ctx->Extensions.EXT_secondary_color
3652 && !enable_ext(ctx, id, "secondary_color"))
3653 return GL_FALSE;
3654 if (ctx->Extensions.EXT_fog_coord
3655 && !enable_ext(ctx, id, "fog_coord"))
3656 return GL_FALSE;
3657 if (ctx->Extensions.NV_texture_rectangle
3658 && !enable_ext(ctx, id, "texture_rectangle"))
3659 return GL_FALSE;
3660 if (ctx->Extensions.ARB_draw_buffers
3661 && !enable_ext(ctx, id, "draw_buffers"))
3662 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003663 if (ctx->Extensions.MESA_texture_array
3664 && !enable_ext(ctx, id, "texture_array"))
3665 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003666#if 1
3667 /* hack for Warcraft (see bug 8060) */
3668 enable_ext(ctx, id, "vertex_blend");
3669#endif
3670
Brian Paul8c41a142005-11-19 15:36:28 +00003671 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003672}
3673
Brian Paul8c41a142005-11-19 15:36:28 +00003674
Jouk Jansen40322e12004-04-05 08:50:36 +00003675/**
3676 * This kicks everything off.
3677 *
3678 * \param ctx - The GL Context
3679 * \param str - The program string
3680 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003681 * \param program - The arb_program struct to return all the parsed info in
3682 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003683 */
Brian Paul8c41a142005-11-19 15:36:28 +00003684static GLboolean
3685_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3686 const GLubyte *str, GLsizei len,
3687 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003688{
3689 GLint a, err, error_pos;
3690 char error_msg[300];
3691 GLuint parsed_len;
3692 struct var_cache *vc_head;
3693 grammar arbprogram_syn_id;
3694 GLubyte *parsed, *inst;
3695 GLubyte *strz = NULL;
3696 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3697
Brian Paul8c41a142005-11-19 15:36:28 +00003698 /* set the program target before parsing */
3699 program->Base.Target = target;
3700
Brian Paul7f76b8f2004-09-10 01:05:39 +00003701 /* Reset error state */
3702 _mesa_set_program_error(ctx, -1, NULL);
3703
Brian Paul8c41a142005-11-19 15:36:28 +00003704 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003705 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003706 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003707 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003708 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003709
3710 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3711 if (grammar_syn_id == 0) {
3712 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003713 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003714 _mesa_set_program_error (ctx, error_pos, error_msg);
3715 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003716 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003717 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 }
3719
Brian Paul8c41a142005-11-19 15:36:28 +00003720 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3721 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003722
Brian Paul49a80ca2006-04-28 15:40:11 +00003723 /* 'parsed' is unused here */
3724 _mesa_free (parsed);
3725 parsed = NULL;
3726
Brian Paul45703642005-10-29 15:52:31 +00003727 /* NOTE: we can't destroy grammar_syn_id right here because
3728 * grammar_destroy() can reset the last error
3729 */
Brian Paul8c41a142005-11-19 15:36:28 +00003730 if (err) {
3731 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003732 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3733 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003734 _mesa_error (ctx, GL_INVALID_OPERATION,
3735 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003736 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003737 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003738 }
3739
3740 grammar_destroy (grammar_syn_id);
3741
3742 arbprogram_syn_is_ok = 1;
3743 }
3744
3745 /* create the grammar object */
3746 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3747 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003748 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003749 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3750 _mesa_set_program_error (ctx, error_pos, error_msg);
3751 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003752 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003753 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003754 }
3755
3756 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003757 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003758 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3759 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003760 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003761 }
3762
Brian Paul8c41a142005-11-19 15:36:28 +00003763 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3764 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003765 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003766 }
3767
3768 /* check for NULL character occurences */
3769 {
Brian Paul8c41a142005-11-19 15:36:28 +00003770 GLint i;
3771 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003772 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003773 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003774 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003775 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003776 }
Brian Paul8c41a142005-11-19 15:36:28 +00003777 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003778 }
3779
3780 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003781 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003782 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003783 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3784 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003785 return GL_FALSE;
3786 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003787 _mesa_memcpy (strz, str, len);
3788 strz[len] = '\0';
3789
Michal Krolb80bc052004-10-21 14:09:54 +00003790 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003791 err = !grammar_fast_check(arbprogram_syn_id, strz,
3792 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003793
3794 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003795 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003796 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3797 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003798
Brian Paulb3aefd12005-09-19 20:12:32 +00003799#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003800 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003801 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003802 int line, col;
3803 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003804 fprintf(stderr, "program: %s\n", (char *) strz);
3805 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003806 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3807 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003808 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3809 } while (0)
3810#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003811
Brian Paula088f162006-09-05 23:08:51 +00003812 _mesa_free(strz);
3813 _mesa_free(parsed);
3814
Jouk Jansen40322e12004-04-05 08:50:36 +00003815 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003816 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003817 }
3818
Jouk Jansen40322e12004-04-05 08:50:36 +00003819 grammar_destroy (arbprogram_syn_id);
3820
Brian Paul8c41a142005-11-19 15:36:28 +00003821 /*
3822 * Program string is syntactically correct at this point
3823 * Parse the tokenized version of the program now, generating
3824 * vertex/fragment program instructions.
3825 */
3826
Jouk Jansen40322e12004-04-05 08:50:36 +00003827 /* Initialize the arb_program struct */
3828 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003829 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003830 program->Base.NumInstructions =
3831 program->Base.NumTemporaries =
3832 program->Base.NumParameters =
3833 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003834 program->Base.Parameters = _mesa_new_parameter_list ();
3835 program->Base.InputsRead = 0x0;
3836 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003837 program->Position = 0;
3838 program->MajorVersion = program->MinorVersion = 0;
3839 program->PrecisionOption = GL_DONT_CARE;
3840 program->FogOption = GL_NONE;
3841 program->HintPositionInvariant = GL_FALSE;
3842 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003843 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003844 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003845 program->NumAluInstructions =
3846 program->NumTexInstructions =
3847 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003848 program->UsesKill = 0;
3849
Jouk Jansen40322e12004-04-05 08:50:36 +00003850 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003851 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003852
3853 /* Start examining the tokens in the array */
3854 inst = parsed;
3855
3856 /* Check the grammer rev */
3857 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003858 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003859 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003860 }
3861 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003862 /* ignore program target */
3863 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003864 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003865 }
3866
3867 /*debug_variables(ctx, vc_head, program); */
3868
3869 /* We're done with the parsed binary array */
3870 var_cache_destroy (&vc_head);
3871
3872 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003873
Brian Paul8c41a142005-11-19 15:36:28 +00003874 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3875 * to size [ap.Base.NumInstructions].
3876 */
Brian Paula7543902006-08-24 21:58:32 +00003877 program->Base.Instructions
3878 = _mesa_realloc_instructions(program->Base.Instructions,
3879 MAX_INSTRUCTIONS,
3880 program->Base.NumInstructions);
3881
Brian Paul45703642005-10-29 15:52:31 +00003882 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003883}
Brian Paul8c41a142005-11-19 15:36:28 +00003884
3885
3886
3887void
3888_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3889 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003890 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003891{
3892 struct arb_program ap;
3893 GLuint i;
3894
3895 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003896 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003897 /* Error in the program. Just return. */
3898 return;
3899 }
3900
3901 /* Copy the relevant contents of the arb_program struct into the
3902 * fragment_program struct.
3903 */
3904 program->Base.String = ap.Base.String;
3905 program->Base.NumInstructions = ap.Base.NumInstructions;
3906 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3907 program->Base.NumParameters = ap.Base.NumParameters;
3908 program->Base.NumAttributes = ap.Base.NumAttributes;
3909 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003910 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3911 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3912 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3913 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3914 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003915 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3916 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3917 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3918 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3919 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3920 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003921 program->Base.InputsRead = ap.Base.InputsRead;
3922 program->Base.OutputsWritten = ap.Base.OutputsWritten;
Briand1284d32008-03-12 15:33:41 -06003923 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
Brianc9db2232007-01-04 17:22:19 -07003924 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Briand1284d32008-03-12 15:33:41 -06003925 if (ap.TexturesUsed[i])
3926 program->Base.SamplersUsed |= (1 << i);
3927 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003928 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003929 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003930 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003931
3932 if (program->Base.Instructions)
3933 _mesa_free(program->Base.Instructions);
3934 program->Base.Instructions = ap.Base.Instructions;
3935
3936 if (program->Base.Parameters)
3937 _mesa_free_parameter_list(program->Base.Parameters);
3938 program->Base.Parameters = ap.Base.Parameters;
3939
Brian Paulc0ef1662008-03-25 11:32:31 -06003940 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3941 * or similar. We used to leave this up to drivers, but it appears
3942 * there's no hardware that wants to do fog in a discrete stage separate
3943 * from the fragment shader.
3944 */
3945 if (program->FogOption != GL_NONE) {
3946 _mesa_append_fog_code(ctx, program);
3947 program->FogOption = GL_NONE;
3948 }
3949
Brian Paul8c41a142005-11-19 15:36:28 +00003950#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00003951 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
3952 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003953#endif
3954}
3955
3956
3957
3958/**
3959 * Parse the vertex program string. If success, update the given
3960 * vertex_program object with the new program. Else, leave the vertex_program
3961 * object unchanged.
3962 */
3963void
3964_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3965 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003966 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003967{
3968 struct arb_program ap;
3969
3970 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3971
Brian Paul95801792005-12-06 15:41:43 +00003972 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003973 /* Error in the program. Just return. */
3974 return;
3975 }
3976
3977 /* Copy the relevant contents of the arb_program struct into the
3978 * vertex_program struct.
3979 */
3980 program->Base.String = ap.Base.String;
3981 program->Base.NumInstructions = ap.Base.NumInstructions;
3982 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3983 program->Base.NumParameters = ap.Base.NumParameters;
3984 program->Base.NumAttributes = ap.Base.NumAttributes;
3985 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003986 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3987 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3988 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3989 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3990 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003991 program->Base.InputsRead = ap.Base.InputsRead;
3992 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3993 program->IsPositionInvariant = ap.HintPositionInvariant;
3994
3995 if (program->Base.Instructions)
3996 _mesa_free(program->Base.Instructions);
3997 program->Base.Instructions = ap.Base.Instructions;
3998
3999 if (program->Base.Parameters)
4000 _mesa_free_parameter_list(program->Base.Parameters);
4001 program->Base.Parameters = ap.Base.Parameters;
4002
4003#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01004004 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00004005 _mesa_print_program(&program->Base);
4006#endif
4007}