blob: 448251b223dc59e12fbe1a0c140ceba3bb6d5664 [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian50a20d42008-03-27 15:51:07 -06003 * Version: 7.1
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Brianbdf279b2008-03-27 16:19:46 -06005 * Copyright (C) 1999-2008 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
Brian Paul0576e832008-09-19 13:04:52 +020033/**
34Notes on program parameters, etc.
35
36The instructions we emit will use six kinds of source registers:
37
38 PROGRAM_INPUT - input registers
39 PROGRAM_TEMPORARY - temp registers
40 PROGRAM_ADDRESS - address/indirect register
41 PROGRAM_SAMPLER - texture sampler
42 PROGRAM_CONSTANT - indexes into program->Parameters, a known constant/literal
43 PROGRAM_STATE_VAR - indexes into program->Parameters, and may actually be:
44 + a state variable, like "state.fog.color", or
45 + a pointer to a "program.local[k]" parameter, or
46 + a pointer to a "program.env[k]" parameter
47
48Basically, all the program.local[] and program.env[] values will get mapped
49into the unified gl_program->Parameters array. This solves the problem of
50having three separate program parameter arrays.
51*/
52
53
Brianc223c6b2007-07-04 13:15:20 -060054#include "main/glheader.h"
55#include "main/imports.h"
Brian Paulbbd28712008-09-18 12:26:54 -060056#include "main/context.h"
57#include "main/macros.h"
58#include "main/mtypes.h"
Brianc223c6b2007-07-04 13:15:20 -060059#include "shader/grammar/grammar_mesa.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000060#include "arbprogparse.h"
Brian Paul32df89e2005-10-29 18:26:43 +000061#include "program.h"
Brian Paulc0ef1662008-03-25 11:32:31 -060062#include "programopt.h"
Brianc0551f02006-12-14 15:02:37 -070063#include "prog_parameter.h"
64#include "prog_statevars.h"
Brianc0551f02006-12-14 15:02:37 -070065#include "prog_instruction.h"
Brian Paul8c41a142005-11-19 15:36:28 +000066
Brian Paul8c41a142005-11-19 15:36:28 +000067/**
68 * This is basically a union of the vertex_program and fragment_program
69 * structs that we can use to parse the program into
70 *
71 * XXX we can probably get rid of this entirely someday.
72 */
73struct arb_program
74{
Brian Paul122629f2006-07-20 16:49:57 +000075 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000076
77 GLuint Position; /* Just used for error reporting while parsing */
78 GLuint MajorVersion;
79 GLuint MinorVersion;
80
81 /* ARB_vertex_progmra options */
82 GLboolean HintPositionInvariant;
83
84 /* ARB_fragment_progmra options */
85 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
86 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
87
88 /* ARB_fragment_program specifics */
89 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
Ian Romanick7b559a92007-06-07 13:58:50 -070090 GLbitfield ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +000091 GLuint NumAluInstructions;
92 GLuint NumTexInstructions;
93 GLuint NumTexIndirections;
94
95 GLboolean UsesKill;
96};
97
Ian Romanick9bdfee32005-07-18 12:31:24 +000098
Brian Paula6c423d2004-08-25 15:59:48 +000099
Jouk Jansen40322e12004-04-05 08:50:36 +0000100/* TODO:
101 * Fragment Program Stuff:
102 * -----------------------------------------------------
103 *
104 * - things from Michal's email
105 * + overflow on atoi
106 * + not-overflowing floats (don't use parse_integer..)
107 * + can remove range checking in arbparse.c
108 *
109 * - check all limits of number of various variables
110 * + parameters
111 *
112 * - test! test! test!
113 *
114 * Vertex Program Stuff:
115 * -----------------------------------------------------
116 * - Optimize param array usage and count limits correctly, see spec,
117 * section 2.14.3.7
118 * + Record if an array is reference absolutly or relatively (or both)
119 * + For absolute arrays, store a bitmap of accesses
120 * + For single parameters, store an access flag
121 * + After parsing, make a parameter cleanup and merging pass, where
122 * relative arrays are layed out first, followed by abs arrays, and
123 * finally single state.
124 * + Remap offsets for param src and dst registers
125 * + Now we can properly count parameter usage
126 *
127 * - Multiple state binding errors in param arrays (see spec, just before
128 * section 2.14.3.3)
129 * - grep for XXX
130 *
131 * Mesa Stuff
132 * -----------------------------------------------------
133 * - User clipping planes vs. PositionInvariant
134 * - Is it sufficient to just multiply by the mvp to transform in the
135 * PositionInvariant case? Or do we need something more involved?
136 *
137 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
138 * - fetch state listed in program_parameters list
139 * + WTF should this go???
140 * + currently in nvvertexec.c and s_nvfragprog.c
141 *
142 * - allow for multiple address registers (and fetch address regs properly)
143 *
144 * Cosmetic Stuff
145 * -----------------------------------------------------
146 * - remove any leftover unused grammer.c stuff (dict_ ?)
147 * - fix grammer.c error handling so its not static
148 * - #ifdef around stuff pertaining to extentions
149 *
150 * Outstanding Questions:
151 * -----------------------------------------------------
152 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
153 * what gets hacked off because of this:
154 * + VERTEX_ATTRIB_MATRIXINDEX
155 * + VERTEX_ATTRIB_WEIGHT
156 * + MATRIX_MODELVIEW
157 * + MATRIX_PALETTE
158 *
159 * - When can we fetch env/local params from their own register files, and
160 * when to we have to fetch them into the main state register file?
161 * (think arrays)
162 *
163 * Grammar Changes:
164 * -----------------------------------------------------
165 */
166
167/* Changes since moving the file to shader directory
168
1692004-III-4 ------------------------------------------------------------
170- added #include "grammar_mesa.h"
171- removed grammar specific code part (it resides now in grammar.c)
172- added GL_ARB_fragment_program_shadow tokens
173- modified #include "arbparse_syn.h"
174- major changes inside _mesa_parse_arb_program()
175- check the program string for '\0' characters
176- copy the program string to a one-byte-longer location to have
177 it null-terminated
178- position invariance test (not writing to result.position) moved
179 to syntax part
180*/
181
182typedef GLubyte *production;
183
Brian Paul11a54c32006-11-15 19:54:25 +0000184
Jouk Jansen40322e12004-04-05 08:50:36 +0000185/**
186 * This is the text describing the rules to parse the grammar
187 */
Brian Paul11a54c32006-11-15 19:54:25 +0000188LONGSTRING static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000189#include "arbprogram_syn.h"
190;
191
192/**
193 * These should match up with the values defined in arbprogram.syn
194 */
195
196/*
197 Changes:
198 - changed and merged V_* and F_* opcode values to OP_*.
199 - added GL_ARB_fragment_program_shadow specific tokens (michal)
200*/
Ian Romanickbb372f12007-05-16 15:34:22 -0700201#define REVISION 0x0a
Jouk Jansen40322e12004-04-05 08:50:36 +0000202
203/* program type */
204#define FRAGMENT_PROGRAM 0x01
205#define VERTEX_PROGRAM 0x02
206
207/* program section */
208#define OPTION 0x01
209#define INSTRUCTION 0x02
210#define DECLARATION 0x03
211#define END 0x04
212
Michal Krolb80bc052004-10-21 14:09:54 +0000213/* GL_ARB_fragment_program option */
214#define ARB_PRECISION_HINT_FASTEST 0x00
215#define ARB_PRECISION_HINT_NICEST 0x01
216#define ARB_FOG_EXP 0x02
217#define ARB_FOG_EXP2 0x03
218#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000219
Michal Krolb80bc052004-10-21 14:09:54 +0000220/* GL_ARB_vertex_program option */
221#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000222
Michal Krolb80bc052004-10-21 14:09:54 +0000223/* GL_ARB_fragment_program_shadow option */
224#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000225
Michal Krolb80bc052004-10-21 14:09:54 +0000226/* GL_ARB_draw_buffers option */
227#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000228
Ian Romanickbb372f12007-05-16 15:34:22 -0700229/* GL_MESA_texture_array option */
230#define MESA_TEXTURE_ARRAY 0x08
231
Jouk Jansen40322e12004-04-05 08:50:36 +0000232/* GL_ARB_fragment_program instruction class */
233#define OP_ALU_INST 0x00
234#define OP_TEX_INST 0x01
235
236/* GL_ARB_vertex_program instruction class */
237/* OP_ALU_INST */
238
239/* GL_ARB_fragment_program instruction type */
240#define OP_ALU_VECTOR 0x00
241#define OP_ALU_SCALAR 0x01
242#define OP_ALU_BINSC 0x02
243#define OP_ALU_BIN 0x03
244#define OP_ALU_TRI 0x04
245#define OP_ALU_SWZ 0x05
246#define OP_TEX_SAMPLE 0x06
247#define OP_TEX_KIL 0x07
248
249/* GL_ARB_vertex_program instruction type */
250#define OP_ALU_ARL 0x08
251/* OP_ALU_VECTOR */
252/* OP_ALU_SCALAR */
253/* OP_ALU_BINSC */
254/* OP_ALU_BIN */
255/* OP_ALU_TRI */
256/* OP_ALU_SWZ */
257
258/* GL_ARB_fragment_program instruction code */
259#define OP_ABS 0x00
260#define OP_ABS_SAT 0x1B
261#define OP_FLR 0x09
262#define OP_FLR_SAT 0x26
263#define OP_FRC 0x0A
264#define OP_FRC_SAT 0x27
265#define OP_LIT 0x0C
266#define OP_LIT_SAT 0x2A
267#define OP_MOV 0x11
268#define OP_MOV_SAT 0x30
269#define OP_COS 0x1F
270#define OP_COS_SAT 0x20
271#define OP_EX2 0x07
272#define OP_EX2_SAT 0x25
273#define OP_LG2 0x0B
274#define OP_LG2_SAT 0x29
275#define OP_RCP 0x14
276#define OP_RCP_SAT 0x33
277#define OP_RSQ 0x15
278#define OP_RSQ_SAT 0x34
279#define OP_SIN 0x38
280#define OP_SIN_SAT 0x39
281#define OP_SCS 0x35
282#define OP_SCS_SAT 0x36
283#define OP_POW 0x13
284#define OP_POW_SAT 0x32
285#define OP_ADD 0x01
286#define OP_ADD_SAT 0x1C
287#define OP_DP3 0x03
288#define OP_DP3_SAT 0x21
289#define OP_DP4 0x04
290#define OP_DP4_SAT 0x22
291#define OP_DPH 0x05
292#define OP_DPH_SAT 0x23
293#define OP_DST 0x06
294#define OP_DST_SAT 0x24
295#define OP_MAX 0x0F
296#define OP_MAX_SAT 0x2E
297#define OP_MIN 0x10
298#define OP_MIN_SAT 0x2F
299#define OP_MUL 0x12
300#define OP_MUL_SAT 0x31
301#define OP_SGE 0x16
302#define OP_SGE_SAT 0x37
303#define OP_SLT 0x17
304#define OP_SLT_SAT 0x3A
305#define OP_SUB 0x18
306#define OP_SUB_SAT 0x3B
307#define OP_XPD 0x1A
308#define OP_XPD_SAT 0x43
309#define OP_CMP 0x1D
310#define OP_CMP_SAT 0x1E
311#define OP_LRP 0x2B
312#define OP_LRP_SAT 0x2C
313#define OP_MAD 0x0E
314#define OP_MAD_SAT 0x2D
315#define OP_SWZ 0x19
316#define OP_SWZ_SAT 0x3C
317#define OP_TEX 0x3D
318#define OP_TEX_SAT 0x3E
319#define OP_TXB 0x3F
320#define OP_TXB_SAT 0x40
321#define OP_TXP 0x41
322#define OP_TXP_SAT 0x42
323#define OP_KIL 0x28
324
325/* GL_ARB_vertex_program instruction code */
326#define OP_ARL 0x02
327/* OP_ABS */
328/* OP_FLR */
329/* OP_FRC */
330/* OP_LIT */
331/* OP_MOV */
332/* OP_EX2 */
333#define OP_EXP 0x08
334/* OP_LG2 */
335#define OP_LOG 0x0D
336/* OP_RCP */
337/* OP_RSQ */
338/* OP_POW */
339/* OP_ADD */
340/* OP_DP3 */
341/* OP_DP4 */
342/* OP_DPH */
343/* OP_DST */
344/* OP_MAX */
345/* OP_MIN */
346/* OP_MUL */
347/* OP_SGE */
348/* OP_SLT */
349/* OP_SUB */
350/* OP_XPD */
351/* OP_MAD */
352/* OP_SWZ */
353
354/* fragment attribute binding */
355#define FRAGMENT_ATTRIB_COLOR 0x01
356#define FRAGMENT_ATTRIB_TEXCOORD 0x02
357#define FRAGMENT_ATTRIB_FOGCOORD 0x03
358#define FRAGMENT_ATTRIB_POSITION 0x04
359
360/* vertex attribute binding */
361#define VERTEX_ATTRIB_POSITION 0x01
362#define VERTEX_ATTRIB_WEIGHT 0x02
363#define VERTEX_ATTRIB_NORMAL 0x03
364#define VERTEX_ATTRIB_COLOR 0x04
365#define VERTEX_ATTRIB_FOGCOORD 0x05
366#define VERTEX_ATTRIB_TEXCOORD 0x06
367#define VERTEX_ATTRIB_MATRIXINDEX 0x07
368#define VERTEX_ATTRIB_GENERIC 0x08
369
370/* fragment result binding */
371#define FRAGMENT_RESULT_COLOR 0x01
372#define FRAGMENT_RESULT_DEPTH 0x02
373
374/* vertex result binding */
375#define VERTEX_RESULT_POSITION 0x01
376#define VERTEX_RESULT_COLOR 0x02
377#define VERTEX_RESULT_FOGCOORD 0x03
378#define VERTEX_RESULT_POINTSIZE 0x04
379#define VERTEX_RESULT_TEXCOORD 0x05
380
381/* texture target */
382#define TEXTARGET_1D 0x01
383#define TEXTARGET_2D 0x02
384#define TEXTARGET_3D 0x03
385#define TEXTARGET_RECT 0x04
386#define TEXTARGET_CUBE 0x05
387/* GL_ARB_fragment_program_shadow */
388#define TEXTARGET_SHADOW1D 0x06
389#define TEXTARGET_SHADOW2D 0x07
390#define TEXTARGET_SHADOWRECT 0x08
Ian Romanickbb372f12007-05-16 15:34:22 -0700391/* GL_MESA_texture_array */
392#define TEXTARGET_1D_ARRAY 0x09
393#define TEXTARGET_2D_ARRAY 0x0a
Ian Romanick69358e72007-06-05 09:24:27 -0700394#define TEXTARGET_SHADOW1D_ARRAY 0x0b
395#define TEXTARGET_SHADOW2D_ARRAY 0x0c
Jouk Jansen40322e12004-04-05 08:50:36 +0000396
397/* face type */
398#define FACE_FRONT 0x00
399#define FACE_BACK 0x01
400
401/* color type */
402#define COLOR_PRIMARY 0x00
403#define COLOR_SECONDARY 0x01
404
405/* component */
406#define COMPONENT_X 0x00
407#define COMPONENT_Y 0x01
408#define COMPONENT_Z 0x02
409#define COMPONENT_W 0x03
410#define COMPONENT_0 0x04
411#define COMPONENT_1 0x05
412
413/* array index type */
414#define ARRAY_INDEX_ABSOLUTE 0x00
415#define ARRAY_INDEX_RELATIVE 0x01
416
417/* matrix name */
418#define MATRIX_MODELVIEW 0x01
419#define MATRIX_PROJECTION 0x02
420#define MATRIX_MVP 0x03
421#define MATRIX_TEXTURE 0x04
422#define MATRIX_PALETTE 0x05
423#define MATRIX_PROGRAM 0x06
424
425/* matrix modifier */
426#define MATRIX_MODIFIER_IDENTITY 0x00
427#define MATRIX_MODIFIER_INVERSE 0x01
428#define MATRIX_MODIFIER_TRANSPOSE 0x02
429#define MATRIX_MODIFIER_INVTRANS 0x03
430
431/* constant type */
432#define CONSTANT_SCALAR 0x01
433#define CONSTANT_VECTOR 0x02
434
435/* program param type */
436#define PROGRAM_PARAM_ENV 0x01
437#define PROGRAM_PARAM_LOCAL 0x02
438
439/* register type */
440#define REGISTER_ATTRIB 0x01
441#define REGISTER_PARAM 0x02
442#define REGISTER_RESULT 0x03
443#define REGISTER_ESTABLISHED_NAME 0x04
444
445/* param binding */
446#define PARAM_NULL 0x00
447#define PARAM_ARRAY_ELEMENT 0x01
448#define PARAM_STATE_ELEMENT 0x02
449#define PARAM_PROGRAM_ELEMENT 0x03
450#define PARAM_PROGRAM_ELEMENTS 0x04
451#define PARAM_CONSTANT 0x05
452
453/* param state property */
454#define STATE_MATERIAL_PARSER 0x01
455#define STATE_LIGHT_PARSER 0x02
456#define STATE_LIGHT_MODEL 0x03
457#define STATE_LIGHT_PROD 0x04
458#define STATE_FOG 0x05
459#define STATE_MATRIX_ROWS 0x06
460/* GL_ARB_fragment_program */
461#define STATE_TEX_ENV 0x07
462#define STATE_DEPTH 0x08
463/* GL_ARB_vertex_program */
464#define STATE_TEX_GEN 0x09
465#define STATE_CLIP_PLANE 0x0A
466#define STATE_POINT 0x0B
467
468/* state material property */
469#define MATERIAL_AMBIENT 0x01
470#define MATERIAL_DIFFUSE 0x02
471#define MATERIAL_SPECULAR 0x03
472#define MATERIAL_EMISSION 0x04
473#define MATERIAL_SHININESS 0x05
474
475/* state light property */
476#define LIGHT_AMBIENT 0x01
477#define LIGHT_DIFFUSE 0x02
478#define LIGHT_SPECULAR 0x03
479#define LIGHT_POSITION 0x04
480#define LIGHT_ATTENUATION 0x05
481#define LIGHT_HALF 0x06
482#define LIGHT_SPOT_DIRECTION 0x07
483
484/* state light model property */
485#define LIGHT_MODEL_AMBIENT 0x01
486#define LIGHT_MODEL_SCENECOLOR 0x02
487
488/* state light product property */
489#define LIGHT_PROD_AMBIENT 0x01
490#define LIGHT_PROD_DIFFUSE 0x02
491#define LIGHT_PROD_SPECULAR 0x03
492
493/* state texture environment property */
494#define TEX_ENV_COLOR 0x01
495
496/* state texture generation coord property */
497#define TEX_GEN_EYE 0x01
498#define TEX_GEN_OBJECT 0x02
499
500/* state fog property */
501#define FOG_COLOR 0x01
502#define FOG_PARAMS 0x02
503
504/* state depth property */
505#define DEPTH_RANGE 0x01
506
507/* state point parameters property */
508#define POINT_SIZE 0x01
509#define POINT_ATTENUATION 0x02
510
511/* declaration */
512#define ATTRIB 0x01
513#define PARAM 0x02
514#define TEMP 0x03
515#define OUTPUT 0x04
516#define ALIAS 0x05
517/* GL_ARB_vertex_program */
518#define ADDRESS 0x06
519
520/*-----------------------------------------------------------------------
521 * From here on down is the semantic checking portion
522 *
523 */
524
525/**
526 * Variable Table Handling functions
527 */
528typedef enum
529{
530 vt_none,
531 vt_address,
532 vt_attrib,
533 vt_param,
534 vt_temp,
535 vt_output,
536 vt_alias
537} var_type;
538
539
Brian Paul7aebaf32005-10-30 21:23:23 +0000540/**
541 * Setting an explicit field for each of the binding properties is a bit
542 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000543 */
544struct var_cache
545{
Brian Paul6a769d92006-04-28 15:42:15 +0000546 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000547 var_type type;
548 GLuint address_binding; /* The index of the address register we should
549 * be using */
550 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000551 GLuint attrib_is_generic; /* If the attrib was specified through a generic
552 * vertex attrib */
553 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000554 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000555 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
556 * that this is aliased to */
557 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
558 * PROGRAM_ENV_PARAM} */
559 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
560 * the tokens representing our bound state (or constants)
561 * start */
562 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
563 * we take up with our state tokens or constants. Note that
564 * this is _not_ the same as the number of param registers
565 * we eventually use */
566 struct var_cache *next;
567};
568
569static GLvoid
570var_cache_create (struct var_cache **va)
571{
572 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
573 if (*va) {
574 (**va).name = NULL;
575 (**va).type = vt_none;
576 (**va).attrib_binding = ~0;
577 (**va).attrib_is_generic = 0;
578 (**va).temp_binding = ~0;
579 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000580 (**va).param_binding_type = ~0;
581 (**va).param_binding_begin = ~0;
582 (**va).param_binding_length = ~0;
583 (**va).alias_binding = NULL;
584 (**va).next = NULL;
585 }
586}
587
588static GLvoid
589var_cache_destroy (struct var_cache **va)
590{
591 if (*va) {
592 var_cache_destroy (&(**va).next);
593 _mesa_free (*va);
594 *va = NULL;
595 }
596}
597
598static GLvoid
599var_cache_append (struct var_cache **va, struct var_cache *nv)
600{
601 if (*va)
602 var_cache_append (&(**va).next, nv);
603 else
604 *va = nv;
605}
606
607static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000608var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000609{
Brian Paul0a360cf2005-01-17 01:21:03 +0000610 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000611
612 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000613 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000614 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000615 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000616 return va;
617 }
618
619 va = va->next;
620 }
621
622 return NULL;
623}
624
Brian Paula088f162006-09-05 23:08:51 +0000625
626
627/**
628 * Called when an error is detected while parsing/compiling a program.
629 * Sets the ctx->Program.ErrorString field to descript and records a
630 * GL_INVALID_OPERATION error.
631 * \param position position of error in program string
632 * \param descrip verbose error description
633 */
634static void
635program_error(GLcontext *ctx, GLint position, const char *descrip)
636{
637 if (descrip) {
638 const char *prefix = "glProgramString(", *suffix = ")";
639 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
640 _mesa_strlen(prefix) +
641 _mesa_strlen(suffix) + 1);
642 if (str) {
643 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
644 _mesa_error(ctx, GL_INVALID_OPERATION, str);
645 _mesa_free(str);
646 }
647 }
648 _mesa_set_program_error(ctx, position, descrip);
649}
650
651
Brian50a20d42008-03-27 15:51:07 -0600652/**
653 * As above, but with an extra string parameter for more info.
654 */
655static void
656program_error2(GLcontext *ctx, GLint position, const char *descrip,
657 const char *var)
658{
659 if (descrip) {
660 const char *prefix = "glProgramString(", *suffix = ")";
661 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
662 _mesa_strlen(": ") +
663 _mesa_strlen(var) +
664 _mesa_strlen(prefix) +
665 _mesa_strlen(suffix) + 1);
666 if (str) {
667 _mesa_sprintf(str, "%s%s: %s%s", prefix, descrip, var, suffix);
668 _mesa_error(ctx, GL_INVALID_OPERATION, str);
669 _mesa_free(str);
670 }
671 }
672 {
673 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
674 _mesa_strlen(": ") +
675 _mesa_strlen(var) + 1);
676 if (str) {
677 _mesa_sprintf(str, "%s: %s", descrip, var);
678 }
679 _mesa_set_program_error(ctx, position, str);
680 if (str) {
681 _mesa_free(str);
682 }
683 }
684}
685
686
Brian Paula088f162006-09-05 23:08:51 +0000687
Jouk Jansen40322e12004-04-05 08:50:36 +0000688/**
689 * constructs an integer from 4 GLubytes in LE format
690 */
691static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000692parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000693{
694 GLuint value;
695
696 value = (GLuint) (*(*inst)++);
697 value += (GLuint) (*(*inst)++) * 0x100;
698 value += (GLuint) (*(*inst)++) * 0x10000;
699 value += (GLuint) (*(*inst)++) * 0x1000000;
700
701 return value;
702}
703
704/**
705 * This will, given a string, lookup the string as a variable name in the
706 * var cache. If the name is found, the var cache node corresponding to the
707 * var name is returned. If it is not found, a new entry is allocated
708 *
709 * \param I Points into the binary array where the string identifier begins
710 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
711 * \return The location on the var_cache corresponding the the string starting at I
712 */
713static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000714parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000715 struct arb_program *Program, GLuint * found)
716{
Brian Paula088f162006-09-05 23:08:51 +0000717 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000718 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000719 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000720
721 *inst += _mesa_strlen ((char *) i) + 1;
722
723 va = var_cache_find (*vc_head, i);
724
725 if (va) {
726 *found = 1;
727 return va;
728 }
729
730 *found = 0;
731 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000732 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000733
734 var_cache_append (vc_head, va);
735
736 return va;
737}
738
739static char *
Brian Paula088f162006-09-05 23:08:51 +0000740parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000741{
Brian Paula088f162006-09-05 23:08:51 +0000742 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000743 (void) Program;
744
Jouk Jansen40322e12004-04-05 08:50:36 +0000745 *inst += _mesa_strlen ((char *) i) + 1;
746
747 return (char *) i;
748}
749
750/**
Brian Paul05908952004-06-08 15:20:23 +0000751 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000752 */
Brian Paul05908952004-06-08 15:20:23 +0000753static GLint
Brian Paula088f162006-09-05 23:08:51 +0000754parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000755{
756 /*return *(*inst)++ != '+'; */
757
758 if (**inst == '-') {
759 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000760 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000761 }
762 else if (**inst == '+') {
763 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000764 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000765 }
766
Brian Paul05908952004-06-08 15:20:23 +0000767 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000768}
769
770/**
771 * parses and returns signed integer
772 */
773static GLint
Brian Paula088f162006-09-05 23:08:51 +0000774parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000775{
776 GLint sign;
777 GLint value;
778
779 /* check if *inst points to '+' or '-'
780 * if yes, grab the sign and increment *inst
781 */
782 sign = parse_sign (inst);
783
784 /* now check if *inst points to 0
785 * if yes, increment the *inst and return the default value
786 */
787 if (**inst == 0) {
788 (*inst)++;
789 return 0;
790 }
791
792 /* parse the integer as you normally would do it */
793 value = _mesa_atoi (parse_string_without_adding (inst, Program));
794
795 /* now, after terminating 0 there is a position
796 * to parse it - parse_position()
797 */
798 Program->Position = parse_position (inst);
799
Brian Paul05908952004-06-08 15:20:23 +0000800 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000801}
802
803/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000804 Accumulate this string of digits, and return them as
805 a large integer represented in floating point (for range).
806 If scale is not NULL, also accumulates a power-of-ten
807 integer scale factor that represents the number of digits
808 in the string.
809*/
810static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000811parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000812{
813 GLdouble value = 0.0;
814 GLdouble oscale = 1.0;
815
816 if (**inst == 0) { /* this string of digits is empty-- do nothing */
817 (*inst)++;
818 }
819 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000820 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000821 GLubyte digit = *((*inst)++);
822 value = value * 10.0 + (GLint) (digit - '0');
823 oscale *= 10.0;
824 }
825 assert(**inst == 0); /* integer string should end with 0 */
826 (*inst)++; /* skip over terminating 0 */
827 Program->Position = parse_position(inst); /* skip position (from integer) */
828 }
829 if (scale)
830 *scale = oscale;
831 return value;
832}
833
834/**
835 Parse an unsigned floating-point number from this stream of tokenized
836 characters. Example floating-point formats supported:
837 12.34
838 12
839 0.34
840 .34
841 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000842 */
843static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000844parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000845{
Brian Paul1ff8f502005-02-16 15:08:29 +0000846 GLint exponent;
847 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000848
Brian Paul1ff8f502005-02-16 15:08:29 +0000849 whole = parse_float_string(inst, Program, 0);
850 fraction = parse_float_string(inst, Program, &fracScale);
851
852 /* Parse signed exponent */
853 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000854
Brian Paul1ff8f502005-02-16 15:08:29 +0000855 /* Assemble parts of floating-point number: */
856 return (GLfloat) ((whole + fraction / fracScale) *
857 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000858}
859
860
861/**
862 */
863static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000864parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000865{
Brian Paul05908952004-06-08 15:20:23 +0000866 GLint sign = parse_sign (inst);
867 GLfloat value = parse_float (inst, Program);
868 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000869}
870
871/**
872 * This picks out a constant value from the parsed array. The constant vector is r
873 * returned in the *values array, which should be of length 4.
874 *
875 * \param values - The 4 component vector with the constant value in it
876 */
877static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000878parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000879 GLboolean use)
880{
881 GLuint components, i;
882
883
884 switch (*(*inst)++) {
885 case CONSTANT_SCALAR:
886 if (use == GL_TRUE) {
887 values[0] =
888 values[1] =
889 values[2] = values[3] = parse_float (inst, Program);
890 }
891 else {
892 values[0] =
893 values[1] =
894 values[2] = values[3] = parse_signed_float (inst, Program);
895 }
896
897 break;
898 case CONSTANT_VECTOR:
899 values[0] = values[1] = values[2] = 0;
900 values[3] = 1;
901 components = *(*inst)++;
902 for (i = 0; i < components; i++) {
903 values[i] = parse_signed_float (inst, Program);
904 }
905 break;
906 }
907}
908
909/**
910 * \param offset The offset from the address register that we should
911 * address
912 *
913 * \return 0 on sucess, 1 on error
914 */
915static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000916parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
917 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000918{
Brian Paul6a769d92006-04-28 15:42:15 +0000919 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000920 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000921 return 0;
922}
923
924/**
925 * \param color 0 if color type is primary, 1 if color type is secondary
926 * \return 0 on sucess, 1 on error
927 */
928static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000929parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000930 GLint * color)
931{
Brian Paula6c423d2004-08-25 15:59:48 +0000932 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000933 *color = *(*inst)++ != COLOR_PRIMARY;
934 return 0;
935}
936
937/**
938 * Get an integer corresponding to a generic vertex attribute.
939 *
940 * \return 0 on sucess, 1 on error
941 */
942static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000943parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000944 struct arb_program *Program, GLuint *attrib)
945{
Brian Paulbd997cd2004-07-20 21:12:56 +0000946 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000947
Michal Krolbb38cad2006-04-11 11:41:11 +0000948 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000949 {
Brian Paula088f162006-09-05 23:08:51 +0000950 program_error(ctx, Program->Position,
951 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000952 return 1;
953 }
954
Brian Paulbd997cd2004-07-20 21:12:56 +0000955 *attrib = (GLuint) i;
956
Jouk Jansen40322e12004-04-05 08:50:36 +0000957 return 0;
958}
959
960
961/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000962 * \param color The index of the color buffer to write into
963 * \return 0 on sucess, 1 on error
964 */
965static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000966parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000967 struct arb_program *Program, GLuint * color)
968{
969 GLint i = parse_integer (inst, Program);
970
971 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000972 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000973 return 1;
974 }
975
976 *color = (GLuint) i;
977 return 0;
978}
979
980
981/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000982 * \param coord The texture unit index
983 * \return 0 on sucess, 1 on error
984 */
985static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000986parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000987 struct arb_program *Program, GLuint * coord)
988{
Brian Paulbd997cd2004-07-20 21:12:56 +0000989 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000990
Brian Paula6c423d2004-08-25 15:59:48 +0000991 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000992 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000993 return 1;
994 }
995
Brian Paulbd997cd2004-07-20 21:12:56 +0000996 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000997 return 0;
998}
999
1000/**
1001 * \param coord The weight index
1002 * \return 0 on sucess, 1 on error
1003 */
1004static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001005parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001006 GLint * coord)
1007{
1008 *coord = parse_integer (inst, Program);
1009
1010 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +00001011 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +00001012 return 1;
1013 }
1014
1015 return 0;
1016}
1017
1018/**
1019 * \param coord The clip plane index
1020 * \return 0 on sucess, 1 on error
1021 */
1022static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001023parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001024 struct arb_program *Program, GLint * coord)
1025{
1026 *coord = parse_integer (inst, Program);
1027
1028 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +00001029 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +00001030 return 1;
1031 }
1032
1033 return 0;
1034}
1035
1036
1037/**
1038 * \return 0 on front face, 1 on back face
1039 */
1040static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001041parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +00001042{
1043 switch (*(*inst)++) {
1044 case FACE_FRONT:
1045 return 0;
1046
1047 case FACE_BACK:
1048 return 1;
1049 }
1050 return 0;
1051}
1052
1053
1054/**
1055 * Given a matrix and a modifier token on the binary array, return tokens
1056 * that _mesa_fetch_state() [program.c] can understand.
1057 *
1058 * \param matrix - the matrix we are talking about
1059 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1060 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1061 * \return 0 on sucess, 1 on failure
1062 */
1063static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001064parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001065 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1066{
1067 GLubyte mat = *(*inst)++;
1068
1069 *matrix_idx = 0;
1070
1071 switch (mat) {
1072 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001073 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001074 *matrix_idx = parse_integer (inst, Program);
1075 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001076 program_error(ctx, Program->Position,
1077 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001078 return 1;
1079 }
1080 break;
1081
1082 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001083 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001084 break;
1085
1086 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001087 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001088 break;
1089
1090 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001091 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001092 *matrix_idx = parse_integer (inst, Program);
1093 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001094 program_error(ctx, Program->Position, "Invalid Texture Unit");
1095 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001096 return 1;
1097 }
1098 break;
1099
1100 /* This is not currently supported (ARB_matrix_palette) */
1101 case MATRIX_PALETTE:
1102 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001103 program_error(ctx, Program->Position,
1104 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001105 return 1;
1106 break;
1107
1108 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001109 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001110 *matrix_idx = parse_integer (inst, Program);
1111 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001112 program_error(ctx, Program->Position, "Invalid Program Matrix");
1113 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001114 return 1;
1115 }
1116 break;
1117 }
1118
1119 switch (*(*inst)++) {
1120 case MATRIX_MODIFIER_IDENTITY:
1121 *matrix_modifier = 0;
1122 break;
1123 case MATRIX_MODIFIER_INVERSE:
1124 *matrix_modifier = STATE_MATRIX_INVERSE;
1125 break;
1126 case MATRIX_MODIFIER_TRANSPOSE:
1127 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1128 break;
1129 case MATRIX_MODIFIER_INVTRANS:
1130 *matrix_modifier = STATE_MATRIX_INVTRANS;
1131 break;
1132 }
1133
1134 return 0;
1135}
1136
1137
1138/**
1139 * This parses a state string (rather, the binary version of it) into
1140 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1141 *
1142 * \param inst - the start in the binary arry to start working from
1143 * \param state_tokens - the storage for the 6-token state description
1144 * \return - 0 on sucess, 1 on error
1145 */
1146static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001147parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001148 struct arb_program *Program,
1149 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001150{
Brian Paul57f2d072008-07-09 08:47:23 -06001151 GLubyte token = *(*inst)++;
1152
1153 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001154 case STATE_MATERIAL_PARSER:
1155 state_tokens[0] = STATE_MATERIAL;
1156 state_tokens[1] = parse_face_type (inst);
1157 switch (*(*inst)++) {
1158 case MATERIAL_AMBIENT:
1159 state_tokens[2] = STATE_AMBIENT;
1160 break;
1161 case MATERIAL_DIFFUSE:
1162 state_tokens[2] = STATE_DIFFUSE;
1163 break;
1164 case MATERIAL_SPECULAR:
1165 state_tokens[2] = STATE_SPECULAR;
1166 break;
1167 case MATERIAL_EMISSION:
1168 state_tokens[2] = STATE_EMISSION;
1169 break;
1170 case MATERIAL_SHININESS:
1171 state_tokens[2] = STATE_SHININESS;
1172 break;
1173 }
1174 break;
1175
1176 case STATE_LIGHT_PARSER:
1177 state_tokens[0] = STATE_LIGHT;
1178 state_tokens[1] = parse_integer (inst, Program);
1179
1180 /* Check the value of state_tokens[1] against the # of lights */
1181 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001182 program_error(ctx, Program->Position, "Invalid Light Number");
1183 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001184 return 1;
1185 }
1186
1187 switch (*(*inst)++) {
1188 case LIGHT_AMBIENT:
1189 state_tokens[2] = STATE_AMBIENT;
1190 break;
1191 case LIGHT_DIFFUSE:
1192 state_tokens[2] = STATE_DIFFUSE;
1193 break;
1194 case LIGHT_SPECULAR:
1195 state_tokens[2] = STATE_SPECULAR;
1196 break;
1197 case LIGHT_POSITION:
1198 state_tokens[2] = STATE_POSITION;
1199 break;
1200 case LIGHT_ATTENUATION:
1201 state_tokens[2] = STATE_ATTENUATION;
1202 break;
1203 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001204 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001205 break;
1206 case LIGHT_SPOT_DIRECTION:
1207 state_tokens[2] = STATE_SPOT_DIRECTION;
1208 break;
1209 }
1210 break;
1211
1212 case STATE_LIGHT_MODEL:
1213 switch (*(*inst)++) {
1214 case LIGHT_MODEL_AMBIENT:
1215 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1216 break;
1217 case LIGHT_MODEL_SCENECOLOR:
1218 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1219 state_tokens[1] = parse_face_type (inst);
1220 break;
1221 }
1222 break;
1223
1224 case STATE_LIGHT_PROD:
1225 state_tokens[0] = STATE_LIGHTPROD;
1226 state_tokens[1] = parse_integer (inst, Program);
1227
1228 /* Check the value of state_tokens[1] against the # of lights */
1229 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001230 program_error(ctx, Program->Position, "Invalid Light Number");
1231 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001232 return 1;
1233 }
1234
1235 state_tokens[2] = parse_face_type (inst);
1236 switch (*(*inst)++) {
1237 case LIGHT_PROD_AMBIENT:
1238 state_tokens[3] = STATE_AMBIENT;
1239 break;
1240 case LIGHT_PROD_DIFFUSE:
1241 state_tokens[3] = STATE_DIFFUSE;
1242 break;
1243 case LIGHT_PROD_SPECULAR:
1244 state_tokens[3] = STATE_SPECULAR;
1245 break;
1246 }
1247 break;
1248
1249
1250 case STATE_FOG:
1251 switch (*(*inst)++) {
1252 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001253 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001254 break;
1255 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001256 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001257 break;
1258 }
1259 break;
1260
1261 case STATE_TEX_ENV:
1262 state_tokens[1] = parse_integer (inst, Program);
1263 switch (*(*inst)++) {
1264 case TEX_ENV_COLOR:
1265 state_tokens[0] = STATE_TEXENV_COLOR;
1266 break;
1267 }
1268 break;
1269
1270 case STATE_TEX_GEN:
1271 {
1272 GLuint type, coord;
1273
1274 state_tokens[0] = STATE_TEXGEN;
1275 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1276
1277 if (parse_texcoord_num (ctx, inst, Program, &coord))
1278 return 1;
1279 state_tokens[1] = coord;
1280
1281 /* EYE or OBJECT */
Brian7b91d872008-03-27 15:48:54 -06001282 type = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001283
1284 /* 0 - s, 1 - t, 2 - r, 3 - q */
Brian7b91d872008-03-27 15:48:54 -06001285 coord = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001286
1287 if (type == TEX_GEN_EYE) {
1288 switch (coord) {
1289 case COMPONENT_X:
1290 state_tokens[2] = STATE_TEXGEN_EYE_S;
1291 break;
1292 case COMPONENT_Y:
1293 state_tokens[2] = STATE_TEXGEN_EYE_T;
1294 break;
1295 case COMPONENT_Z:
1296 state_tokens[2] = STATE_TEXGEN_EYE_R;
1297 break;
1298 case COMPONENT_W:
1299 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1300 break;
Brian7b91d872008-03-27 15:48:54 -06001301 default:
1302 _mesa_problem(ctx, "bad texgen component in "
1303 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001304 }
1305 }
1306 else {
1307 switch (coord) {
1308 case COMPONENT_X:
1309 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1310 break;
1311 case COMPONENT_Y:
1312 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1313 break;
1314 case COMPONENT_Z:
1315 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1316 break;
1317 case COMPONENT_W:
1318 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1319 break;
Brian7b91d872008-03-27 15:48:54 -06001320 default:
1321 _mesa_problem(ctx, "bad texgen component in "
1322 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001323 }
1324 }
1325 }
1326 break;
1327
1328 case STATE_DEPTH:
1329 switch (*(*inst)++) {
1330 case DEPTH_RANGE:
1331 state_tokens[0] = STATE_DEPTH_RANGE;
1332 break;
1333 }
1334 break;
1335
1336 case STATE_CLIP_PLANE:
1337 state_tokens[0] = STATE_CLIPPLANE;
Brianaa9d22a2007-02-23 11:21:03 -07001338 if (parse_clipplane_num (ctx, inst, Program,
1339 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001340 return 1;
1341 break;
1342
1343 case STATE_POINT:
Brian7b91d872008-03-27 15:48:54 -06001344 switch (*(*inst)++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001345 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001346 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001347 break;
1348
1349 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001350 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001351 break;
1352 }
1353 break;
1354
1355 /* XXX: I think this is the correct format for a matrix row */
1356 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001357 if (parse_matrix(ctx, inst, Program,
1358 (GLint *) &state_tokens[0],
1359 (GLint *) &state_tokens[1],
1360 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001361 return 1;
1362
Brian65319522007-02-21 11:08:21 -07001363 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001364
1365 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001366 state_tokens[3] = parse_integer (inst, Program);
1367 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001368 program_error(ctx, Program->Position,
1369 "Second matrix index less than the first");
1370 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001371 return 1;
1372 }
1373 }
1374 else {
Brian65319522007-02-21 11:08:21 -07001375 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001376 (*inst)++;
1377 }
1378 break;
1379 }
1380
1381 return 0;
1382}
1383
1384/**
1385 * This parses a state string (rather, the binary version of it) into
1386 * a 6-token similar for the state fetching code in program.c
1387 *
1388 * One might ask, why fetch these parameters into just like you fetch
1389 * state when they are already stored in other places?
1390 *
1391 * Because of array offsets -> We can stick env/local parameters in the
1392 * middle of a parameter array and then index someplace into the array
1393 * when we execute.
1394 *
1395 * One optimization might be to only do this for the cases where the
1396 * env/local parameters end up inside of an array, and leave the
1397 * single parameters (or arrays of pure env/local pareameters) in their
1398 * respective register files.
1399 *
1400 * For ENV parameters, the format is:
1401 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1402 * state_tokens[1] = STATE_ENV
1403 * state_tokens[2] = the parameter index
1404 *
1405 * for LOCAL parameters, the format is:
1406 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1407 * state_tokens[1] = STATE_LOCAL
1408 * state_tokens[2] = the parameter index
1409 *
1410 * \param inst - the start in the binary arry to start working from
1411 * \param state_tokens - the storage for the 6-token state description
1412 * \return - 0 on sucess, 1 on failure
1413 */
1414static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001415parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001416 struct arb_program *Program,
1417 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001418{
1419 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1420 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1421 else
1422 state_tokens[0] = STATE_VERTEX_PROGRAM;
1423
1424
1425 switch (*(*inst)++) {
1426 case PROGRAM_PARAM_ENV:
1427 state_tokens[1] = STATE_ENV;
1428 state_tokens[2] = parse_integer (inst, Program);
1429
1430 /* Check state_tokens[2] against the number of ENV parameters available */
1431 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001432 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001433 ||
1434 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001435 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001436 program_error(ctx, Program->Position,
1437 "Invalid Program Env Parameter");
1438 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001439 return 1;
1440 }
1441
1442 break;
1443
1444 case PROGRAM_PARAM_LOCAL:
1445 state_tokens[1] = STATE_LOCAL;
1446 state_tokens[2] = parse_integer (inst, Program);
1447
1448 /* Check state_tokens[2] against the number of LOCAL parameters available */
1449 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001450 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001451 ||
1452 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001453 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001454 program_error(ctx, Program->Position,
1455 "Invalid Program Local Parameter");
1456 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001457 return 1;
1458 }
1459 break;
1460 }
1461
1462 return 0;
1463}
1464
1465/**
1466 * For ARB_vertex_program, programs are not allowed to use both an explicit
1467 * vertex attribute and a generic vertex attribute corresponding to the same
1468 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1469 *
1470 * This will walk our var_cache and make sure that nobody does anything fishy.
1471 *
1472 * \return 0 on sucess, 1 on error
1473 */
1474static GLuint
1475generic_attrib_check(struct var_cache *vc_head)
1476{
1477 int a;
1478 struct var_cache *curr;
1479 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1480 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1481
1482 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1483 explicitAttrib[a] = GL_FALSE;
1484 genericAttrib[a] = GL_FALSE;
1485 }
1486
1487 curr = vc_head;
1488 while (curr) {
1489 if (curr->type == vt_attrib) {
1490 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001491 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001492 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001493 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001494 }
1495
1496 curr = curr->next;
1497 }
1498
1499 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1500 if ((explicitAttrib[a]) && (genericAttrib[a]))
1501 return 1;
1502 }
1503
1504 return 0;
1505}
1506
1507/**
1508 * This will handle the binding side of an ATTRIB var declaration
1509 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001510 * \param inputReg returns the input register index, one of the
1511 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001512 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001513 */
1514static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001515parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001516 struct arb_program *Program,
1517 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001518{
Jouk Jansen40322e12004-04-05 08:50:36 +00001519 GLint err = 0;
1520
1521 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001522
Jouk Jansen40322e12004-04-05 08:50:36 +00001523 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1524 switch (*(*inst)++) {
1525 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001526 {
1527 GLint coord;
1528 err = parse_color_type (ctx, inst, Program, &coord);
1529 *inputReg = FRAG_ATTRIB_COL0 + coord;
1530 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001531 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001532 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001533 {
Brian8fa6f732007-02-01 09:24:41 -07001534 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001535 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1536 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1537 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001538 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001539 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001540 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001541 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001542 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001543 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001544 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001545 default:
1546 err = 1;
1547 break;
1548 }
1549 }
1550 else {
1551 switch (*(*inst)++) {
1552 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001553 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001554 break;
1555
1556 case VERTEX_ATTRIB_WEIGHT:
1557 {
1558 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001559 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001560 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001561#if 1
1562 /* hack for Warcraft (see bug 8060) */
1563 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001564 break;
Brian Paul3a557502006-09-05 23:15:29 +00001565#else
Brian Paula088f162006-09-05 23:08:51 +00001566 program_error(ctx, Program->Position,
1567 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001568 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001569#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001570 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001571
1572 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001573 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001574 break;
1575
1576 case VERTEX_ATTRIB_COLOR:
1577 {
1578 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001579 err = parse_color_type (ctx, inst, Program, &color);
1580 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001581 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001582 }
1583 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001584 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001585 }
1586 }
1587 break;
1588
1589 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001590 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001591 break;
1592
1593 case VERTEX_ATTRIB_TEXCOORD:
1594 {
Brian8fa6f732007-02-01 09:24:41 -07001595 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001596 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001597 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001598 }
1599 break;
1600
Jouk Jansen40322e12004-04-05 08:50:36 +00001601 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001602 /* Not supported at this time */
1603 {
1604 const char *msg = "ARB_palette_matrix not supported";
1605 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001606 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001607 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001608 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001609
1610 case VERTEX_ATTRIB_GENERIC:
1611 {
1612 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001613 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001614 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001615 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001616 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1617 * attributes do not alias the conventional vertex
1618 * attributes.
1619 */
1620 if (attrib > 0)
1621 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001622 else
1623 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 }
1625 }
1626 break;
1627
1628 default:
1629 err = 1;
1630 break;
1631 }
1632 }
1633
Jouk Jansen40322e12004-04-05 08:50:36 +00001634 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001635 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001636 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001637
1638 return err;
1639}
1640
Brian Paul7aebaf32005-10-30 21:23:23 +00001641
Jouk Jansen40322e12004-04-05 08:50:36 +00001642/**
1643 * This translates between a binary token for an output variable type
1644 * and the mesa token for the same thing.
1645 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001646 * \param inst The parsed tokens
1647 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001648 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001649 */
1650static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001651parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001652 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001653{
Brian Paul7aebaf32005-10-30 21:23:23 +00001654 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001655
Brian Paul7aebaf32005-10-30 21:23:23 +00001656 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001657 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001658 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001659 GLuint out_color;
1660
Brian Paulbe76b7f2004-10-04 14:40:05 +00001661 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001662 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001663 */
1664 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001665 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001666 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001667 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001668 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001669 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1670 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001671 }
1672 break;
1673
1674 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001675 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001676 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001677 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001678 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001679 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001680 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001681 GLint color_type;
1682 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001683 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1684 if (err)
1685 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001686
Jouk Jansen40322e12004-04-05 08:50:36 +00001687 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001688 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001689 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001690 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001691 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001692 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001693 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001694 }
1695 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001696 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001697 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001698 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001699 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001700 }
1701 /* primary color */
1702 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001703 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001704 }
1705 }
1706 }
1707 break;
1708
1709 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001710 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001711 break;
1712
1713 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001714 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001715 break;
1716
1717 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001718 {
1719 GLuint unit;
1720 if (parse_texcoord_num (ctx, inst, Program, &unit))
1721 return 1;
1722 *outputReg = VERT_RESULT_TEX0 + unit;
1723 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001724 break;
1725 }
1726
Brian Paulde997602005-11-12 17:53:14 +00001727 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001728
1729 return 0;
1730}
1731
Brian Paul7aebaf32005-10-30 21:23:23 +00001732
Jouk Jansen40322e12004-04-05 08:50:36 +00001733/**
1734 * This handles the declaration of ATTRIB variables
1735 *
1736 * XXX: Still needs
1737 * parse_vert_attrib_binding(), or something like that
1738 *
1739 * \return 0 on sucess, 1 on error
1740 */
1741static GLint
Brian Paula088f162006-09-05 23:08:51 +00001742parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001743 struct arb_program *Program)
1744{
1745 GLuint found;
Jouk Jansen40322e12004-04-05 08:50:36 +00001746 struct var_cache *attrib_var;
1747
1748 attrib_var = parse_string (inst, vc_head, Program, &found);
1749 Program->Position = parse_position (inst);
1750 if (found) {
Brian50a20d42008-03-27 15:51:07 -06001751 program_error2(ctx, Program->Position,
1752 "Duplicate variable declaration",
1753 (char *) attrib_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001754 return 1;
1755 }
1756
1757 attrib_var->type = vt_attrib;
1758
Brian Paul7aebaf32005-10-30 21:23:23 +00001759 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001760 &attrib_var->attrib_is_generic))
1761 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001762
Brian Paul7aebaf32005-10-30 21:23:23 +00001763 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001764 program_error(ctx, Program->Position,
1765 "Cannot use both a generic vertex attribute "
1766 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001767 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001768 }
1769
1770 Program->Base.NumAttributes++;
1771 return 0;
1772}
1773
1774/**
1775 * \param use -- TRUE if we're called when declaring implicit parameters,
1776 * FALSE if we're declaraing variables. This has to do with
1777 * if we get a signed or unsigned float for scalar constants
1778 */
1779static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001780parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001781 struct var_cache *param_var,
1782 struct arb_program *Program, GLboolean use)
1783{
1784 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001785 GLuint err = 0;
Briana0720942008-03-27 15:52:00 -06001786 gl_state_index state_tokens[STATE_LENGTH] = {0, 0, 0, 0, 0};
Jouk Jansen40322e12004-04-05 08:50:36 +00001787 GLfloat const_values[4];
1788
Brian Paul57f2d072008-07-09 08:47:23 -06001789 GLubyte token = *(*inst)++;
1790
1791 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001792 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001793 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1794 return 1;
1795
1796 /* If we adding STATE_MATRIX that has multiple rows, we need to
1797 * unroll it and call _mesa_add_state_reference() for each row
1798 */
Brian65319522007-02-21 11:08:21 -07001799 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1800 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1801 state_tokens[0] == STATE_MVP_MATRIX ||
1802 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1803 state_tokens[0] == STATE_PROGRAM_MATRIX)
1804 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001805 GLint row;
Brian65319522007-02-21 11:08:21 -07001806 const GLint first_row = state_tokens[2];
1807 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001808
1809 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001810 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001811
Brian Paulde997602005-11-12 17:53:14 +00001812 idx = _mesa_add_state_reference(Program->Base.Parameters,
1813 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001814 if (param_var->param_binding_begin == ~0U)
1815 param_var->param_binding_begin = idx;
1816 param_var->param_binding_length++;
1817 Program->Base.NumParameters++;
1818 }
1819 }
1820 else {
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 if (param_var->param_binding_begin == ~0U)
1824 param_var->param_binding_begin = idx;
1825 param_var->param_binding_length++;
1826 Program->Base.NumParameters++;
1827 }
1828 break;
1829
1830 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001831 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1832 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001833 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001834 if (param_var->param_binding_begin == ~0U)
1835 param_var->param_binding_begin = idx;
1836 param_var->param_binding_length++;
1837 Program->Base.NumParameters++;
1838
1839 /* Check if there is more: 0 -> we're done, else its an integer */
1840 if (**inst) {
1841 GLuint out_of_range, new_idx;
1842 GLuint start_idx = state_tokens[2] + 1;
1843 GLuint end_idx = parse_integer (inst, Program);
1844
1845 out_of_range = 0;
1846 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1847 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001848 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001849 || ((state_tokens[1] == STATE_LOCAL)
1850 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001851 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001852 out_of_range = 1;
1853 }
1854 else {
1855 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001856 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001857 || ((state_tokens[1] == STATE_LOCAL)
1858 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001859 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001860 out_of_range = 1;
1861 }
1862 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001863 program_error(ctx, Program->Position,
1864 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001865 return 1;
1866 }
1867
1868 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1869 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001870 idx = _mesa_add_state_reference(Program->Base.Parameters,
1871 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001872 param_var->param_binding_length++;
1873 Program->Base.NumParameters++;
1874 }
1875 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001876 else {
1877 (*inst)++;
1878 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001879 break;
1880
1881 case PARAM_CONSTANT:
Brian7cbfe8c2008-01-18 12:45:55 -07001882 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
Jouk Jansen40322e12004-04-05 08:50:36 +00001883 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001884 idx = _mesa_add_named_constant(Program->Base.Parameters,
1885 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001886 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001887 if (param_var->param_binding_begin == ~0U)
1888 param_var->param_binding_begin = idx;
Brian Paul0576e832008-09-19 13:04:52 +02001889 param_var->param_binding_type = PROGRAM_STATE_VAR;
1890 /* Note: when we reference this parameter in an instruction later,
1891 * we'll check if it's really a constant/immediate and set the
1892 * instruction register type appropriately.
1893 */
Jouk Jansen40322e12004-04-05 08:50:36 +00001894 param_var->param_binding_length++;
1895 Program->Base.NumParameters++;
1896 break;
1897
1898 default:
Brian Paula088f162006-09-05 23:08:51 +00001899 program_error(ctx, Program->Position,
1900 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001901 return 1;
1902 }
1903
1904 /* Make sure we haven't blown past our parameter limits */
1905 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1906 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001907 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001908 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1909 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001910 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001911 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001912 return 1;
1913 }
1914
1915 return err;
1916}
1917
Brian Paul7aebaf32005-10-30 21:23:23 +00001918
Jouk Jansen40322e12004-04-05 08:50:36 +00001919/**
1920 * This picks out PARAM program parameter bindings.
1921 *
1922 * XXX: This needs to be stressed & tested
1923 *
1924 * \return 0 on sucess, 1 on error
1925 */
1926static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001927parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001928 struct arb_program *Program)
1929{
Brian Paulbd997cd2004-07-20 21:12:56 +00001930 GLuint found, err;
1931 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001932 struct var_cache *param_var;
1933
1934 err = 0;
1935 param_var = parse_string (inst, vc_head, Program, &found);
1936 Program->Position = parse_position (inst);
1937
1938 if (found) {
Brian50a20d42008-03-27 15:51:07 -06001939 program_error2(ctx, Program->Position,
1940 "Duplicate variable declaration",
1941 (char *) param_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001942 return 1;
1943 }
1944
1945 specified_length = parse_integer (inst, Program);
1946
1947 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001948 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001949 return 1;
1950 }
1951
1952 param_var->type = vt_param;
1953 param_var->param_binding_length = 0;
1954
1955 /* Right now, everything is shoved into the main state register file.
1956 *
1957 * In the future, it would be nice to leave things ENV/LOCAL params
1958 * in their respective register files, if possible
1959 */
1960 param_var->param_binding_type = PROGRAM_STATE_VAR;
1961
1962 /* Remember to:
1963 * * - add each guy to the parameter list
1964 * * - increment the param_var->param_binding_len
1965 * * - store the param_var->param_binding_begin for the first one
1966 * * - compare the actual len to the specified len at the end
1967 */
1968 while (**inst != PARAM_NULL) {
1969 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1970 return 1;
1971 }
1972
1973 /* Test array length here! */
1974 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001975 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001976 program_error(ctx, Program->Position,
1977 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001978 }
1979 }
1980
1981 (*inst)++;
1982
1983 return 0;
1984}
1985
1986/**
1987 *
1988 */
1989static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001990parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001991 struct arb_program *Program, struct var_cache **new_var)
1992{
1993 struct var_cache *param_var;
1994
1995 /* First, insert a dummy entry into the var_cache */
1996 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001997 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001998 param_var->type = vt_param;
1999
2000 param_var->param_binding_length = 0;
2001 /* Don't fill in binding_begin; We use the default value of -1
2002 * to tell if its already initialized, elsewhere.
2003 *
2004 * param_var->param_binding_begin = 0;
2005 */
2006 param_var->param_binding_type = PROGRAM_STATE_VAR;
2007
2008 var_cache_append (vc_head, param_var);
2009
2010 /* Then fill it with juicy parameter goodness */
2011 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
2012 return 1;
2013
2014 *new_var = param_var;
2015
2016 return 0;
2017}
2018
2019
2020/**
2021 * This handles the declaration of TEMP variables
2022 *
2023 * \return 0 on sucess, 1 on error
2024 */
2025static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002026parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002027 struct arb_program *Program)
2028{
2029 GLuint found;
2030 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002031
2032 while (**inst != 0) {
2033 temp_var = parse_string (inst, vc_head, Program, &found);
2034 Program->Position = parse_position (inst);
2035 if (found) {
Brian50a20d42008-03-27 15:51:07 -06002036 program_error2(ctx, Program->Position,
2037 "Duplicate variable declaration",
2038 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002039 return 1;
2040 }
2041
2042 temp_var->type = vt_temp;
2043
2044 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2045 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002046 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002047 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2048 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002049 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00002050 program_error(ctx, Program->Position,
2051 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002052 return 1;
2053 }
2054
2055 temp_var->temp_binding = Program->Base.NumTemporaries;
2056 Program->Base.NumTemporaries++;
2057 }
2058 (*inst)++;
2059
2060 return 0;
2061}
2062
2063/**
2064 * This handles variables of the OUTPUT variety
2065 *
2066 * \return 0 on sucess, 1 on error
2067 */
2068static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002069parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002070 struct arb_program *Program)
2071{
2072 GLuint found;
2073 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002074 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002075
2076 output_var = parse_string (inst, vc_head, Program, &found);
2077 Program->Position = parse_position (inst);
2078 if (found) {
Brian50a20d42008-03-27 15:51:07 -06002079 program_error2(ctx, Program->Position,
2080 "Duplicate variable declaration",
2081 (char *) output_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002082 return 1;
2083 }
2084
2085 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002086
2087 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2088 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002089}
2090
2091/**
2092 * This handles variables of the ALIAS kind
2093 *
2094 * \return 0 on sucess, 1 on error
2095 */
2096static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002097parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002098 struct arb_program *Program)
2099{
2100 GLuint found;
2101 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002102
2103 temp_var = parse_string (inst, vc_head, Program, &found);
2104 Program->Position = parse_position (inst);
2105
2106 if (found) {
Brian50a20d42008-03-27 15:51:07 -06002107 program_error2(ctx, Program->Position,
2108 "Duplicate variable declaration",
2109 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002110 return 1;
2111 }
2112
2113 temp_var->type = vt_alias;
2114 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2115 Program->Position = parse_position (inst);
2116
2117 if (!found)
2118 {
Brian50a20d42008-03-27 15:51:07 -06002119 program_error2(ctx, Program->Position,
2120 "Undefined alias value",
2121 (char *) temp_var->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002122 return 1;
2123 }
2124
2125 return 0;
2126}
2127
2128/**
2129 * This handles variables of the ADDRESS kind
2130 *
2131 * \return 0 on sucess, 1 on error
2132 */
2133static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002134parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002135 struct arb_program *Program)
2136{
2137 GLuint found;
2138 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002139
2140 while (**inst != 0) {
2141 temp_var = parse_string (inst, vc_head, Program, &found);
2142 Program->Position = parse_position (inst);
2143 if (found) {
Brian50a20d42008-03-27 15:51:07 -06002144 program_error2(ctx, Program->Position,
2145 "Duplicate variable declaration",
2146 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002147 return 1;
2148 }
2149
2150 temp_var->type = vt_address;
2151
2152 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002153 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002154 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002155 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002156 return 1;
2157 }
2158
2159 temp_var->address_binding = Program->Base.NumAddressRegs;
2160 Program->Base.NumAddressRegs++;
2161 }
2162 (*inst)++;
2163
2164 return 0;
2165}
2166
2167/**
2168 * Parse a program declaration
2169 *
2170 * \return 0 on sucess, 1 on error
2171 */
2172static GLint
Brian Paula088f162006-09-05 23:08:51 +00002173parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002174 struct arb_program *Program)
2175{
2176 GLint err = 0;
2177
2178 switch (*(*inst)++) {
2179 case ADDRESS:
2180 err = parse_address (ctx, inst, vc_head, Program);
2181 break;
2182
2183 case ALIAS:
2184 err = parse_alias (ctx, inst, vc_head, Program);
2185 break;
2186
2187 case ATTRIB:
2188 err = parse_attrib (ctx, inst, vc_head, Program);
2189 break;
2190
2191 case OUTPUT:
2192 err = parse_output (ctx, inst, vc_head, Program);
2193 break;
2194
2195 case PARAM:
2196 err = parse_param (ctx, inst, vc_head, Program);
2197 break;
2198
2199 case TEMP:
2200 err = parse_temp (ctx, inst, vc_head, Program);
2201 break;
2202 }
2203
2204 return err;
2205}
2206
2207/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002208 * Handle the parsing out of a masked destination register, either for a
2209 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002210 *
2211 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002212 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002213 * position invariant
2214 *
2215 * \param File - The register file we write to
2216 * \param Index - The register index we write to
2217 * \param WriteMask - The mask controlling which components we write (1->write)
2218 *
2219 * \return 0 on sucess, 1 on error
2220 */
2221static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002222parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002223 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002224 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002225{
Brian Paul7aebaf32005-10-30 21:23:23 +00002226 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002227 struct var_cache *dst;
2228
2229 /* We either have a result register specified, or a
2230 * variable that may or may not be writable
2231 */
2232 switch (*(*inst)++) {
2233 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002234 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002235 return 1;
2236 *File = PROGRAM_OUTPUT;
2237 break;
2238
2239 case REGISTER_ESTABLISHED_NAME:
2240 dst = parse_string (inst, vc_head, Program, &result);
2241 Program->Position = parse_position (inst);
2242
2243 /* If the name has never been added to our symbol table, we're hosed */
2244 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002245 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002246 return 1;
2247 }
2248
2249 switch (dst->type) {
2250 case vt_output:
2251 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002252 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002253 break;
2254
2255 case vt_temp:
2256 *File = PROGRAM_TEMPORARY;
2257 *Index = dst->temp_binding;
2258 break;
2259
2260 /* If the var type is not vt_output or vt_temp, no go */
2261 default:
Brian Paula088f162006-09-05 23:08:51 +00002262 program_error(ctx, Program->Position,
2263 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002264 return 1;
2265 }
2266 break;
2267
2268 default:
Brian Paula088f162006-09-05 23:08:51 +00002269 program_error(ctx, Program->Position,
2270 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002271 return 1;
2272 }
2273
2274
2275 /* Position invariance test */
2276 /* This test is done now in syntax portion - when position invariance OPTION
2277 is specified, "result.position" rule is disabled so there is no way
2278 to write the position
2279 */
2280 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2281 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002282 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002283 "Vertex program specified position invariance and wrote vertex position");
2284 }*/
2285
2286 /* And then the mask.
2287 * w,a -> bit 0
2288 * z,b -> bit 1
2289 * y,g -> bit 2
2290 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002291 *
2292 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002293 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002294 tmp = (GLint) *(*inst)++;
2295 *WriteMask = (((tmp>>3) & 0x1) |
2296 ((tmp>>1) & 0x2) |
2297 ((tmp<<1) & 0x4) |
2298 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002299
2300 return 0;
2301}
2302
2303
2304/**
2305 * Handle the parsing of a address register
2306 *
2307 * \param Index - The register index we write to
2308 *
2309 * \return 0 on sucess, 1 on error
2310 */
2311static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002312parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002313 struct var_cache **vc_head,
2314 struct arb_program *Program, GLint * Index)
2315{
2316 struct var_cache *dst;
2317 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002318
2319 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002320
2321 dst = parse_string (inst, vc_head, Program, &result);
2322 Program->Position = parse_position (inst);
2323
2324 /* If the name has never been added to our symbol table, we're hosed */
2325 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002326 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002327 return 1;
2328 }
2329
2330 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002331 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002332 return 1;
2333 }
2334
2335 return 0;
2336}
2337
Brian Paul8e8fa632005-07-01 02:03:33 +00002338#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002339/**
2340 * Handle the parsing out of a masked address register
2341 *
2342 * \param Index - The register index we write to
2343 * \param WriteMask - The mask controlling which components we write (1->write)
2344 *
2345 * \return 0 on sucess, 1 on error
2346 */
2347static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002348parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002349 struct var_cache **vc_head,
2350 struct arb_program *Program, GLint * Index,
2351 GLboolean * WriteMask)
2352{
2353 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2354 return 1;
2355
2356 /* This should be 0x8 */
2357 (*inst)++;
2358
2359 /* Writemask of .x is implied */
2360 WriteMask[0] = 1;
2361 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2362
2363 return 0;
2364}
Brian Paul8e8fa632005-07-01 02:03:33 +00002365#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002366
2367/**
2368 * Parse out a swizzle mask.
2369 *
Brian Paul32df89e2005-10-29 18:26:43 +00002370 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002371 *
2372 * The len parameter allows us to grab 4 components for a vector
2373 * swizzle, or just 1 component for a scalar src register selection
2374 */
Brian Paul32df89e2005-10-29 18:26:43 +00002375static void
Brian Paula088f162006-09-05 23:08:51 +00002376parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002377{
Brian Paul32df89e2005-10-29 18:26:43 +00002378 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002379
Brian Paul32df89e2005-10-29 18:26:43 +00002380 for (i = 0; i < 4; i++)
2381 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002382
Brian Paul32df89e2005-10-29 18:26:43 +00002383 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002384 switch (*(*inst)++) {
2385 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002386 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002388 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002389 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002390 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002391 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002392 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002393 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002394 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002395 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002396 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002397 default:
2398 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2399 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002400 }
2401 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002402}
2403
Jouk Jansen40322e12004-04-05 08:50:36 +00002404
Brian Paul32df89e2005-10-29 18:26:43 +00002405/**
2406 * Parse an extended swizzle mask which is a sequence of
2407 * four x/y/z/w/0/1 tokens.
2408 * \return swizzle four swizzle values
2409 * \return negateMask four element bitfield
2410 */
2411static void
Brian Paula088f162006-09-05 23:08:51 +00002412parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002413 GLubyte *negateMask)
2414{
2415 GLint i;
2416
2417 *negateMask = 0x0;
2418 for (i = 0; i < 4; i++) {
2419 GLubyte swz;
2420 if (parse_sign(inst) == -1)
2421 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002422
2423 swz = *(*inst)++;
2424
2425 switch (swz) {
2426 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002427 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002428 break;
2429 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002430 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002431 break;
2432 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002433 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002434 break;
2435 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002436 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002437 break;
2438 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002439 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002440 break;
2441 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002442 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002443 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002444 default:
2445 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2446 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002447 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002448 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002449}
2450
2451
2452static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002453parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2454 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002455 struct arb_program *Program,
2456 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002457 GLboolean *IsRelOffset )
2458{
2459 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002460 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002461 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002462
Keith Whitwell7c26b612005-04-21 14:46:57 +00002463 *IsRelOffset = 0;
2464
Jouk Jansen40322e12004-04-05 08:50:36 +00002465 /* And the binding for the src */
2466 switch (*(*inst)++) {
2467 case REGISTER_ATTRIB:
2468 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002469 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002470 return 1;
2471 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002472 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002473
2474 /* We need to insert a dummy variable into the var_cache so we can
2475 * catch generic vertex attrib aliasing errors
2476 */
2477 var_cache_create(&src);
2478 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002479 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002480 src->attrib_binding = binding;
2481 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002482 var_cache_append(vc_head, src);
2483 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002484 program_error(ctx, Program->Position,
2485 "Cannot use both a generic vertex attribute "
2486 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002487 return 1;
2488 }
2489 break;
2490
2491 case REGISTER_PARAM:
2492 switch (**inst) {
2493 case PARAM_ARRAY_ELEMENT:
2494 (*inst)++;
2495 src = parse_string (inst, vc_head, Program, &found);
2496 Program->Position = parse_position (inst);
2497
2498 if (!found) {
Brian50a20d42008-03-27 15:51:07 -06002499 program_error2(ctx, Program->Position,
2500 "Undefined variable",
2501 (char *) src->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002502 return 1;
2503 }
2504
Brian Paul95801792005-12-06 15:41:43 +00002505 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002506
2507 switch (*(*inst)++) {
2508 case ARRAY_INDEX_ABSOLUTE:
2509 offset = parse_integer (inst, Program);
2510
2511 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002512 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002513 program_error(ctx, Program->Position,
2514 "Index out of range");
2515 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002516 return 1;
2517 }
2518
2519 *Index = src->param_binding_begin + offset;
2520 break;
2521
2522 case ARRAY_INDEX_RELATIVE:
2523 {
2524 GLint addr_reg_idx, rel_off;
2525
2526 /* First, grab the address regiseter */
2527 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2528 return 1;
2529
2530 /* And the .x */
2531 ((*inst)++);
2532 ((*inst)++);
2533 ((*inst)++);
2534 ((*inst)++);
2535
2536 /* Then the relative offset */
2537 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2538
2539 /* And store it properly */
2540 *Index = src->param_binding_begin + rel_off;
2541 *IsRelOffset = 1;
2542 }
2543 break;
2544 }
2545 break;
2546
2547 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002548 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2549 return 1;
2550
Brian Paul95801792005-12-06 15:41:43 +00002551 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002552 *Index = src->param_binding_begin;
2553 break;
2554 }
2555 break;
2556
2557 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002558 src = parse_string (inst, vc_head, Program, &found);
2559 Program->Position = parse_position (inst);
2560
2561 /* If the name has never been added to our symbol table, we're hosed */
2562 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002563 program_error(ctx, Program->Position,
2564 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002565 return 1;
2566 }
2567
2568 switch (src->type) {
2569 case vt_attrib:
2570 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002571 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002572 break;
2573
2574 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2575 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002576 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002577 *Index = src->param_binding_begin;
2578 break;
2579
2580 case vt_temp:
2581 *File = PROGRAM_TEMPORARY;
2582 *Index = src->temp_binding;
2583 break;
2584
2585 /* If the var type is vt_output no go */
2586 default:
Brian Paula088f162006-09-05 23:08:51 +00002587 program_error(ctx, Program->Position,
2588 "destination register is read only");
2589 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002590 return 1;
2591 }
2592 break;
2593
2594 default:
Brian Paula088f162006-09-05 23:08:51 +00002595 program_error(ctx, Program->Position,
2596 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002597 return 1;
2598 }
2599
Brian Paul0576e832008-09-19 13:04:52 +02002600 if (*File == PROGRAM_STATE_VAR) {
Michal Krol8701e5f2008-09-19 19:11:37 +02002601 enum register_file file;
2602
Brian Paul0576e832008-09-19 13:04:52 +02002603 /* If we're referencing the Program->Parameters[] array, check if the
2604 * parameter is really a constant/literal. If so, set File to CONSTANT.
2605 */
Michal Krola77976d2008-10-01 19:36:04 +02002606 assert(*Index < (GLint) Program->Base.Parameters->NumParameters);
Michal Krol8701e5f2008-09-19 19:11:37 +02002607 file = Program->Base.Parameters->Parameters[*Index].Type;
Brian Paul0576e832008-09-19 13:04:52 +02002608 if (file == PROGRAM_CONSTANT)
2609 *File = PROGRAM_CONSTANT;
2610 }
2611
Markus Amslerf3c49062008-03-17 08:35:37 -06002612 /* Add attributes to InputsRead only if they are used the program.
2613 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2614 if (*File == PROGRAM_INPUT)
2615 Program->Base.InputsRead |= (1 << *Index);
2616
Jouk Jansen40322e12004-04-05 08:50:36 +00002617 return 0;
2618}
2619
Brianbdf279b2008-03-27 16:19:46 -06002620
Jouk Jansen40322e12004-04-05 08:50:36 +00002621/**
Brianbdf279b2008-03-27 16:19:46 -06002622 * Parse vertex/fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002623 */
2624static GLuint
Brianbdf279b2008-03-27 16:19:46 -06002625parse_vector_src_reg(GLcontext *ctx, const GLubyte **inst,
2626 struct var_cache **vc_head,
2627 struct arb_program *program,
2628 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002629{
Brian Paul7aebaf32005-10-30 21:23:23 +00002630 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002631 GLint index;
Brianbdf279b2008-03-27 16:19:46 -06002632 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002633 GLubyte swizzle[4];
2634 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002635
Jouk Jansen40322e12004-04-05 08:50:36 +00002636 /* Grab the sign */
Brianbdf279b2008-03-27 16:19:46 -06002637 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002638
2639 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002640 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002641 return 1;
2642
2643 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002644 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002645
Brian Paul32df89e2005-10-29 18:26:43 +00002646 reg->File = file;
2647 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002648 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Brianbdf279b2008-03-27 16:19:46 -06002649 reg->NegateBase = negateMask;
2650 reg->RelAddr = isRelOffset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002651 return 0;
2652}
2653
Jouk Jansen40322e12004-04-05 08:50:36 +00002654
Brian Paulb7fc1c32006-08-30 23:38:03 +00002655/**
Brianbdf279b2008-03-27 16:19:46 -06002656 * Parse vertex/fragment program scalar source register.
2657 */
2658static GLuint
2659parse_scalar_src_reg(GLcontext *ctx, const GLubyte **inst,
2660 struct var_cache **vc_head,
2661 struct arb_program *program,
2662 struct prog_src_register *reg)
2663{
2664 enum register_file file;
2665 GLint index;
2666 GLubyte negateMask;
2667 GLubyte swizzle[4];
2668 GLboolean isRelOffset;
2669
2670 /* Grab the sign */
2671 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
2672
2673 /* And the src reg */
2674 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
2675 return 1;
2676
2677 /* finally, the swizzle */
2678 parse_swizzle_mask(inst, swizzle, 1);
2679
2680 reg->File = file;
2681 reg->Index = index;
2682 reg->Swizzle = (swizzle[0] << 0);
2683 reg->NegateBase = negateMask;
2684 reg->RelAddr = isRelOffset;
2685 return 0;
2686}
2687
2688
2689/**
2690 * Parse vertex/fragment program destination register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002691 * \return 1 if error, 0 if no error.
2692 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002693static GLuint
Brianbdf279b2008-03-27 16:19:46 -06002694parse_dst_reg(GLcontext * ctx, const GLubyte ** inst,
2695 struct var_cache **vc_head, struct arb_program *program,
2696 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002697{
Brian Paul7aebaf32005-10-30 21:23:23 +00002698 GLint mask;
2699 GLuint idx;
2700 enum register_file file;
2701
Brianbdf279b2008-03-27 16:19:46 -06002702 if (parse_masked_dst_reg (ctx, inst, vc_head, program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002703 return 1;
2704
Keith Whitwell7c26b612005-04-21 14:46:57 +00002705 reg->File = file;
2706 reg->Index = idx;
2707 reg->WriteMask = mask;
2708 return 0;
2709}
2710
2711
Brian Paul7aebaf32005-10-30 21:23:23 +00002712/**
Jouk Jansen40322e12004-04-05 08:50:36 +00002713 * This is a big mother that handles getting opcodes into the instruction
2714 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002715 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002716 */
2717static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002718parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002719 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002720 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002721{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002722 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002723 GLuint texcoord;
2724 GLubyte instClass, type, code;
2725 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002726 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002727
Brian Pauld6272e02006-10-29 18:03:16 +00002728 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002729
2730 /* Record the position in the program string for debugging */
2731 fp->StringPos = Program->Position;
2732
2733 /* OP_ALU_INST or OP_TEX_INST */
2734 instClass = *(*inst)++;
2735
2736 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2737 * OP_TEX_{SAMPLE, KIL}
2738 */
2739 type = *(*inst)++;
2740
2741 /* The actual opcode name */
2742 code = *(*inst)++;
2743
2744 /* Increment the correct count */
2745 switch (instClass) {
2746 case OP_ALU_INST:
2747 Program->NumAluInstructions++;
2748 break;
2749 case OP_TEX_INST:
2750 Program->NumTexInstructions++;
2751 break;
2752 }
2753
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 switch (type) {
2755 case OP_ALU_VECTOR:
2756 switch (code) {
2757 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002758 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002759 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002760 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002761 break;
2762
2763 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002764 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002765 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002766 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 break;
2768
2769 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002770 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002771 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002772 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002773 break;
2774
2775 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002776 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002777 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002778 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002779 break;
2780
2781 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002782 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002784 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 break;
2786 }
2787
Brianbdf279b2008-03-27 16:19:46 -06002788 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002789 return 1;
2790
Brianbdf279b2008-03-27 16:19:46 -06002791 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002792 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 break;
2794
2795 case OP_ALU_SCALAR:
2796 switch (code) {
2797 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002798 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002799 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002800 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002801 break;
2802
2803 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002804 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002805 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002806 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 break;
2808
2809 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002810 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002811 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002812 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 break;
2814
2815 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002816 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002817 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002818 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 break;
2820
2821 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002822 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002823 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002824 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 break;
2826
2827 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002828 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002829 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002830 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 break;
2832
2833 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002834 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 case OP_SCS:
2836
Brian Paul7e807512005-11-05 17:10:45 +00002837 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002838 break;
2839 }
2840
Brianbdf279b2008-03-27 16:19:46 -06002841 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002842 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002843
Brianbdf279b2008-03-27 16:19:46 -06002844 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002846 break;
2847
2848 case OP_ALU_BINSC:
2849 switch (code) {
2850 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002851 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002852 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002853 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002854 break;
2855 }
2856
Brianbdf279b2008-03-27 16:19:46 -06002857 if (parse_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002858 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002859
Jouk Jansen40322e12004-04-05 08:50:36 +00002860 for (a = 0; a < 2; a++) {
Brianbdf279b2008-03-27 16:19:46 -06002861 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002862 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 }
2864 break;
2865
2866
2867 case OP_ALU_BIN:
2868 switch (code) {
2869 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002870 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002871 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002872 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002873 break;
2874
2875 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002876 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002877 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002878 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002879 break;
2880
2881 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002882 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002884 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 break;
2886
2887 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002888 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002890 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 break;
2892
2893 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002894 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002895 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002896 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 break;
2898
2899 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002900 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002901 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002902 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002903 break;
2904
2905 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002906 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002907 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002908 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002909 break;
2910
2911 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002912 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002913 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002914 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002915 break;
2916
2917 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002918 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002919 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002920 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002921 break;
2922
2923 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002924 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002925 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002926 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002927 break;
2928
2929 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002930 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002931 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002932 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002933 break;
2934
2935 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002936 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002937 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002938 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002939 break;
2940 }
2941
Brianbdf279b2008-03-27 16:19:46 -06002942 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002943 return 1;
2944 for (a = 0; a < 2; a++) {
Brianbdf279b2008-03-27 16:19:46 -06002945 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002946 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002947 }
2948 break;
2949
2950 case OP_ALU_TRI:
2951 switch (code) {
2952 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002953 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002954 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002955 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002956 break;
2957
2958 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002959 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002960 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002961 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002962 break;
2963
2964 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002965 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002966 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002967 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002968 break;
2969 }
2970
Brianbdf279b2008-03-27 16:19:46 -06002971 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002972 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002973
Jouk Jansen40322e12004-04-05 08:50:36 +00002974 for (a = 0; a < 3; a++) {
Brianbdf279b2008-03-27 16:19:46 -06002975 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002976 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002977 }
2978 break;
2979
2980 case OP_ALU_SWZ:
2981 switch (code) {
2982 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002983 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002984 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002985 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002986 break;
2987 }
Brianbdf279b2008-03-27 16:19:46 -06002988 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002989 return 1;
2990
Keith Whitwell7c26b612005-04-21 14:46:57 +00002991 {
Brian Paul32df89e2005-10-29 18:26:43 +00002992 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002993 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002994 enum register_file file;
2995 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002996
Brian Paul32df89e2005-10-29 18:26:43 +00002997 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002998 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002999 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
3000 fp->SrcReg[0].File = file;
3001 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00003002 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003003 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3004 swizzle[1],
3005 swizzle[2],
3006 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003007 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003008 break;
3009
3010 case OP_TEX_SAMPLE:
3011 switch (code) {
3012 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003013 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003014 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00003015 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003016 break;
3017
3018 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003019 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003020 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003021 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003022 break;
3023
3024 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003025 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003026 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003027 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003028 break;
3029 }
3030
Brianbdf279b2008-03-27 16:19:46 -06003031 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003032 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003033
Brianbdf279b2008-03-27 16:19:46 -06003034 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003035 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003036
3037 /* texImageUnit */
3038 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3039 return 1;
3040 fp->TexSrcUnit = texcoord;
3041
3042 /* texTarget */
3043 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07003044 case TEXTARGET_SHADOW1D:
3045 shadow_tex = 1 << texcoord;
3046 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003047 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003048 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003049 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003050 case TEXTARGET_SHADOW2D:
3051 shadow_tex = 1 << texcoord;
3052 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003053 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003054 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003055 break;
3056 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003057 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003058 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003059 case TEXTARGET_SHADOWRECT:
3060 shadow_tex = 1 << texcoord;
3061 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003062 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003063 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003064 break;
3065 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003066 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003067 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003068 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003069 shadow_tex = 1 << texcoord;
3070 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003071 case TEXTARGET_1D_ARRAY:
3072 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3073 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003074 case TEXTARGET_SHADOW2D_ARRAY:
3075 shadow_tex = 1 << texcoord;
3076 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003077 case TEXTARGET_2D_ARRAY:
3078 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3079 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003080 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003081
3082 /* Don't test the first time a particular sampler is seen. Each time
3083 * after that, make sure the shadow state is the same.
3084 */
3085 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3086 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3087 program_error(ctx, Program->Position,
3088 "texture image unit used for shadow sampling and non-shadow sampling");
3089 return 1;
3090 }
3091
Brian Paulb7fc1c32006-08-30 23:38:03 +00003092 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3093 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3094 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003095 program_error(ctx, Program->Position,
3096 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003097 return 1;
3098 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003099
3100
3101 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003102 break;
3103
3104 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003105 Program->UsesKill = 1;
Brianbdf279b2008-03-27 16:19:46 -06003106 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003107 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003108 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003109 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003110 default:
3111 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3112 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003113 }
3114
3115 return 0;
3116}
3117
Keith Whitwell7c26b612005-04-21 14:46:57 +00003118
3119/**
3120 * Handle the parsing out of a masked address register
3121 *
3122 * \param Index - The register index we write to
3123 * \param WriteMask - The mask controlling which components we write (1->write)
3124 *
3125 * \return 0 on sucess, 1 on error
3126 */
3127static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003128parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003129 struct var_cache **vc_head,
3130 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003131 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003132{
3133 GLint idx;
3134
3135 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3136 return 1;
3137
3138 /* This should be 0x8 */
3139 (*inst)++;
3140
3141 reg->File = PROGRAM_ADDRESS;
3142 reg->Index = idx;
3143
3144 /* Writemask of .x is implied */
3145 reg->WriteMask = 0x1;
3146 return 0;
3147}
3148
Keith Whitwell7c26b612005-04-21 14:46:57 +00003149
Jouk Jansen40322e12004-04-05 08:50:36 +00003150/**
3151 * This is a big mother that handles getting opcodes into the instruction
3152 * and handling the src & dst registers for vertex program instructions
3153 */
3154static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003155parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003156 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003157 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003158{
3159 GLint a;
3160 GLubyte type, code;
3161
3162 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3163 type = *(*inst)++;
3164
3165 /* The actual opcode name */
3166 code = *(*inst)++;
3167
Brian Pauld6272e02006-10-29 18:03:16 +00003168 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003169 /* Record the position in the program string for debugging */
3170 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003171
3172 switch (type) {
3173 /* XXX: */
3174 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003175 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003176
3177 /* Remember to set SrcReg.RelAddr; */
3178
3179 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003180 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003181 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003182
Jouk Jansen40322e12004-04-05 08:50:36 +00003183 vp->DstReg.File = PROGRAM_ADDRESS;
3184
3185 /* Get a scalar src register */
Brianbdf279b2008-03-27 16:19:46 -06003186 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003187 return 1;
3188
3189 break;
3190
3191 case OP_ALU_VECTOR:
3192 switch (code) {
3193 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003194 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003195 break;
3196 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003197 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003198 break;
3199 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003200 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003201 break;
3202 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003203 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003204 break;
3205 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003206 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003207 break;
3208 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003209
Brianbdf279b2008-03-27 16:19:46 -06003210 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003211 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003212
Brianbdf279b2008-03-27 16:19:46 -06003213 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003214 return 1;
3215 break;
3216
3217 case OP_ALU_SCALAR:
3218 switch (code) {
3219 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003220 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003221 break;
3222 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003223 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003224 break;
3225 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003226 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003227 break;
3228 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003229 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003230 break;
3231 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003232 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003233 break;
3234 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003235 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003236 break;
3237 }
Brianbdf279b2008-03-27 16:19:46 -06003238 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003239 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003240
Brianbdf279b2008-03-27 16:19:46 -06003241 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003242 return 1;
3243 break;
3244
3245 case OP_ALU_BINSC:
3246 switch (code) {
3247 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003248 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003249 break;
3250 }
Brianbdf279b2008-03-27 16:19:46 -06003251 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003252 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003253
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 for (a = 0; a < 2; a++) {
Brianbdf279b2008-03-27 16:19:46 -06003255 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003256 return 1;
3257 }
3258 break;
3259
3260 case OP_ALU_BIN:
3261 switch (code) {
3262 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003263 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003264 break;
3265 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003266 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003267 break;
3268 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003269 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003270 break;
3271 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003272 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003273 break;
3274 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003275 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 break;
3277 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003278 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003279 break;
3280 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003281 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003282 break;
3283 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003284 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003285 break;
3286 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003287 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 break;
3289 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003290 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003291 break;
3292 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003293 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 break;
3295 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003296 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 break;
3298 }
Brianbdf279b2008-03-27 16:19:46 -06003299 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003301
Jouk Jansen40322e12004-04-05 08:50:36 +00003302 for (a = 0; a < 2; a++) {
Brianbdf279b2008-03-27 16:19:46 -06003303 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003304 return 1;
3305 }
3306 break;
3307
3308 case OP_ALU_TRI:
3309 switch (code) {
3310 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003311 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003312 break;
3313 }
3314
Brianbdf279b2008-03-27 16:19:46 -06003315 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003316 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003317
Jouk Jansen40322e12004-04-05 08:50:36 +00003318 for (a = 0; a < 3; a++) {
Brianbdf279b2008-03-27 16:19:46 -06003319 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003320 return 1;
3321 }
3322 break;
3323
3324 case OP_ALU_SWZ:
3325 switch (code) {
3326 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003327 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003328 break;
3329 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003330 {
Brian Paul32df89e2005-10-29 18:26:43 +00003331 GLubyte swizzle[4];
3332 GLubyte negateMask;
3333 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003334 enum register_file file;
3335 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003336
Brianbdf279b2008-03-27 16:19:46 -06003337 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003338 return 1;
3339
Brian Paul32df89e2005-10-29 18:26:43 +00003340 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003341 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003342 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3343 vp->SrcReg[0].File = file;
3344 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003345 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003346 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3347 swizzle[1],
3348 swizzle[2],
3349 swizzle[3]);
3350 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003351 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003352 break;
3353 }
3354 return 0;
3355}
3356
3357#if DEBUG_PARSING
3358
3359static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003360debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3361 struct arb_program *Program)
3362{
3363 struct var_cache *vc;
3364 GLint a, b;
3365
Brianb618ac82007-02-22 09:39:25 -07003366 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003367
3368 /* First of all, print out the contents of the var_cache */
3369 vc = vc_head;
3370 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003371 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003372 switch (vc->type) {
3373 case vt_none:
3374 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3375 break;
3376 case vt_attrib:
3377 fprintf (stderr, "ATTRIB %s\n", vc->name);
3378 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3379 break;
3380 case vt_param:
3381 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3382 vc->param_binding_begin, vc->param_binding_length);
3383 b = vc->param_binding_begin;
3384 for (a = 0; a < vc->param_binding_length; a++) {
3385 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003386 Program->Base.Parameters->Parameters[a + b].Name);
3387 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
Michal Krolce3cf632008-09-05 12:25:50 +02003388 char *s;
Brianb618ac82007-02-22 09:39:25 -07003389 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3390 [a + b].StateIndexes);
3391 fprintf(stderr, "%s\n", s);
Michal Krolce3cf632008-09-05 12:25:50 +02003392 _mesa_free(s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003393 }
3394 else
3395 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003396 Program->Base.Parameters->ParameterValues[a + b][0],
3397 Program->Base.Parameters->ParameterValues[a + b][1],
3398 Program->Base.Parameters->ParameterValues[a + b][2],
3399 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003400 }
3401 break;
3402 case vt_temp:
3403 fprintf (stderr, "TEMP %s\n", vc->name);
3404 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3405 break;
3406 case vt_output:
3407 fprintf (stderr, "OUTPUT %s\n", vc->name);
3408 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3409 break;
3410 case vt_alias:
3411 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003412 fprintf (stderr, " binding: 0x%p (%s)\n",
3413 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003414 break;
Brianb618ac82007-02-22 09:39:25 -07003415 default:
3416 /* nothing */
3417 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003418 }
3419 vc = vc->next;
3420 }
3421}
3422
Brian Paul72030e02005-11-03 03:30:34 +00003423#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003424
3425
3426/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003427 * The main loop for parsing a fragment or vertex program
3428 *
Brian Paul72030e02005-11-03 03:30:34 +00003429 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003430 */
Brian Paul72030e02005-11-03 03:30:34 +00003431static GLint
Brian Paula088f162006-09-05 23:08:51 +00003432parse_instructions(GLcontext * ctx, const GLubyte * inst,
3433 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003434{
Brian Paul72030e02005-11-03 03:30:34 +00003435 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3436 ? ctx->Const.FragmentProgram.MaxInstructions
3437 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003438 GLint err = 0;
3439
Jonathan Whitee3378792008-09-30 15:38:38 -06003440 ASSERT(MAX_PROGRAM_INSTRUCTIONS >= maxInst);
Brian Paul72030e02005-11-03 03:30:34 +00003441
Jouk Jansen40322e12004-04-05 08:50:36 +00003442 Program->MajorVersion = (GLuint) * inst++;
3443 Program->MinorVersion = (GLuint) * inst++;
3444
3445 while (*inst != END) {
3446 switch (*inst++) {
3447
3448 case OPTION:
3449 switch (*inst++) {
3450 case ARB_PRECISION_HINT_FASTEST:
3451 Program->PrecisionOption = GL_FASTEST;
3452 break;
3453
3454 case ARB_PRECISION_HINT_NICEST:
3455 Program->PrecisionOption = GL_NICEST;
3456 break;
3457
3458 case ARB_FOG_EXP:
3459 Program->FogOption = GL_EXP;
3460 break;
3461
3462 case ARB_FOG_EXP2:
3463 Program->FogOption = GL_EXP2;
3464 break;
3465
3466 case ARB_FOG_LINEAR:
3467 Program->FogOption = GL_LINEAR;
3468 break;
3469
3470 case ARB_POSITION_INVARIANT:
3471 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003472 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003473 break;
3474
3475 case ARB_FRAGMENT_PROGRAM_SHADOW:
3476 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3477 /* TODO ARB_fragment_program_shadow code */
3478 }
3479 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003480
3481 case ARB_DRAW_BUFFERS:
3482 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3483 /* do nothing for now */
3484 }
3485 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003486
3487 case MESA_TEXTURE_ARRAY:
3488 /* do nothing for now */
3489 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003490 }
3491 break;
3492
3493 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003494 /* check length */
3495 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003496 program_error(ctx, Program->Position,
3497 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003498 return 1;
3499 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003500 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003501 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003502 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003503 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003504 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003505 }
3506 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003507 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003508 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003509 }
3510
Brian Paul72030e02005-11-03 03:30:34 +00003511 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003512 Program->Base.NumInstructions++;
3513 break;
3514
3515 case DECLARATION:
3516 err = parse_declaration (ctx, &inst, vc_head, Program);
3517 break;
3518
3519 default:
3520 break;
3521 }
3522
3523 if (err)
3524 break;
3525 }
3526
3527 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003528 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003529 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003530 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003531 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003532 /* YYY Wrong Position in program, whatever, at least not random -> crash
3533 Program->Position = parse_position (&inst);
3534 */
Brian Paul8c41a142005-11-19 15:36:28 +00003535 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003536 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003537 Program->Base.NumInstructions++;
3538
Brian Paul05051032005-11-01 04:36:33 +00003539 /*
3540 * Initialize native counts to logical counts. The device driver may
3541 * change them if program is translated into a hardware program.
3542 */
3543 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3544 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3545 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3546 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3547 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003548
Jouk Jansen40322e12004-04-05 08:50:36 +00003549 return err;
3550}
3551
Brian Paul7aebaf32005-10-30 21:23:23 +00003552
Jouk Jansen40322e12004-04-05 08:50:36 +00003553/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003554LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003555#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003556;
3557
Brian Paul7aebaf32005-10-30 21:23:23 +00003558
Brian Paul8c41a142005-11-19 15:36:28 +00003559/**
3560 * Set a grammar parameter.
3561 * \param name the grammar parameter
3562 * \param value the new parameter value
3563 * \return 0 if OK, 1 if error
3564 */
3565static int
3566set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003567{
3568 char error_msg[300];
3569 GLint error_pos;
3570
Brian Paul8c41a142005-11-19 15:36:28 +00003571 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003572 return 0;
3573
3574 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3575 _mesa_set_program_error (ctx, error_pos, error_msg);
3576 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3577 return 1;
3578}
3579
Brian Paul8c41a142005-11-19 15:36:28 +00003580
3581/**
3582 * Enable support for the given language option in the parser.
3583 * \return 1 if OK, 0 if error
3584 */
3585static int
3586enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003587{
Brian Paul8c41a142005-11-19 15:36:28 +00003588 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003589}
3590
Brian Paul8c41a142005-11-19 15:36:28 +00003591
3592/**
3593 * Enable parser extensions based on which OpenGL extensions are supported
3594 * by this rendering context.
3595 *
3596 * \return GL_TRUE if OK, GL_FALSE if error.
3597 */
3598static GLboolean
3599enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003600{
Brian Paul8c41a142005-11-19 15:36:28 +00003601#if 0
3602 /* These are not supported at this time */
3603 if ((ctx->Extensions.ARB_vertex_blend ||
3604 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003605 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003606 return GL_FALSE;
3607 if (ctx->Extensions.ARB_matrix_palette
3608 && !enable_ext(ctx, id, "matrix_palette"))
3609 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003610#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003611 if (ctx->Extensions.ARB_fragment_program_shadow
3612 && !enable_ext(ctx, id, "fragment_program_shadow"))
3613 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003614 if (ctx->Extensions.EXT_point_parameters
3615 && !enable_ext(ctx, id, "point_parameters"))
3616 return GL_FALSE;
3617 if (ctx->Extensions.EXT_secondary_color
3618 && !enable_ext(ctx, id, "secondary_color"))
3619 return GL_FALSE;
3620 if (ctx->Extensions.EXT_fog_coord
3621 && !enable_ext(ctx, id, "fog_coord"))
3622 return GL_FALSE;
3623 if (ctx->Extensions.NV_texture_rectangle
3624 && !enable_ext(ctx, id, "texture_rectangle"))
3625 return GL_FALSE;
3626 if (ctx->Extensions.ARB_draw_buffers
3627 && !enable_ext(ctx, id, "draw_buffers"))
3628 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003629 if (ctx->Extensions.MESA_texture_array
3630 && !enable_ext(ctx, id, "texture_array"))
3631 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003632#if 1
3633 /* hack for Warcraft (see bug 8060) */
3634 enable_ext(ctx, id, "vertex_blend");
3635#endif
3636
Brian Paul8c41a142005-11-19 15:36:28 +00003637 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003638}
3639
Brian Paul8c41a142005-11-19 15:36:28 +00003640
Jouk Jansen40322e12004-04-05 08:50:36 +00003641/**
3642 * This kicks everything off.
3643 *
3644 * \param ctx - The GL Context
3645 * \param str - The program string
3646 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003647 * \param program - The arb_program struct to return all the parsed info in
3648 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003649 */
Brian Paul8c41a142005-11-19 15:36:28 +00003650static GLboolean
3651_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3652 const GLubyte *str, GLsizei len,
3653 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003654{
3655 GLint a, err, error_pos;
3656 char error_msg[300];
3657 GLuint parsed_len;
3658 struct var_cache *vc_head;
3659 grammar arbprogram_syn_id;
3660 GLubyte *parsed, *inst;
3661 GLubyte *strz = NULL;
3662 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3663
Brian Paul8c41a142005-11-19 15:36:28 +00003664 /* set the program target before parsing */
3665 program->Base.Target = target;
3666
Brian Paul7f76b8f2004-09-10 01:05:39 +00003667 /* Reset error state */
3668 _mesa_set_program_error(ctx, -1, NULL);
3669
Brian Paul8c41a142005-11-19 15:36:28 +00003670 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003671 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003672 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003673 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003674 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003675
3676 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3677 if (grammar_syn_id == 0) {
3678 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003679 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003680 _mesa_set_program_error (ctx, error_pos, error_msg);
3681 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003682 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003683 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003684 }
3685
Brian Paul8c41a142005-11-19 15:36:28 +00003686 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3687 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003688
Brian Paul49a80ca2006-04-28 15:40:11 +00003689 /* 'parsed' is unused here */
3690 _mesa_free (parsed);
3691 parsed = NULL;
3692
Brian Paul45703642005-10-29 15:52:31 +00003693 /* NOTE: we can't destroy grammar_syn_id right here because
3694 * grammar_destroy() can reset the last error
3695 */
Brian Paul8c41a142005-11-19 15:36:28 +00003696 if (err) {
3697 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003698 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3699 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003700 _mesa_error (ctx, GL_INVALID_OPERATION,
3701 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003702 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003703 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003704 }
3705
3706 grammar_destroy (grammar_syn_id);
3707
3708 arbprogram_syn_is_ok = 1;
3709 }
3710
3711 /* create the grammar object */
3712 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3713 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003714 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003715 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3716 _mesa_set_program_error (ctx, error_pos, error_msg);
3717 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003718 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003719 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003720 }
3721
3722 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003723 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003724 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3725 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003726 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003727 }
3728
Brian Paul8c41a142005-11-19 15:36:28 +00003729 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3730 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003731 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003732 }
3733
3734 /* check for NULL character occurences */
3735 {
Brian Paul8c41a142005-11-19 15:36:28 +00003736 GLint i;
3737 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003738 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003739 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003740 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003741 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003742 }
Brian Paul8c41a142005-11-19 15:36:28 +00003743 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003744 }
3745
3746 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003747 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003748 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003749 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3750 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003751 return GL_FALSE;
3752 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003753 _mesa_memcpy (strz, str, len);
3754 strz[len] = '\0';
3755
Michal Krolb80bc052004-10-21 14:09:54 +00003756 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003757 err = !grammar_fast_check(arbprogram_syn_id, strz,
3758 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003759
3760 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003761 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003762 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3763 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003764
Brian Paulb3aefd12005-09-19 20:12:32 +00003765#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003766 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003767 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003768 int line, col;
3769 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003770 fprintf(stderr, "program: %s\n", (char *) strz);
Brian5052dab2008-03-27 16:05:40 -06003771 fprintf(stderr, "Error Pos: %d\n", ctx->Program.ErrorPos);
3772 s = (char *) _mesa_find_line_column(strz, strz+ctx->Program.ErrorPos,
Brian Paula088f162006-09-05 23:08:51 +00003773 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003774 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
Brian5052dab2008-03-27 16:05:40 -06003775 } while (0);
Brian Paulb3aefd12005-09-19 20:12:32 +00003776#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003777
Brian Paula088f162006-09-05 23:08:51 +00003778 _mesa_free(strz);
3779 _mesa_free(parsed);
3780
Jouk Jansen40322e12004-04-05 08:50:36 +00003781 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003782 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003783 }
3784
Jouk Jansen40322e12004-04-05 08:50:36 +00003785 grammar_destroy (arbprogram_syn_id);
3786
Brian Paul8c41a142005-11-19 15:36:28 +00003787 /*
3788 * Program string is syntactically correct at this point
3789 * Parse the tokenized version of the program now, generating
3790 * vertex/fragment program instructions.
3791 */
3792
Jouk Jansen40322e12004-04-05 08:50:36 +00003793 /* Initialize the arb_program struct */
3794 program->Base.String = strz;
Jonathan Whitee3378792008-09-30 15:38:38 -06003795 program->Base.Instructions = _mesa_alloc_instructions(MAX_PROGRAM_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003796 program->Base.NumInstructions =
3797 program->Base.NumTemporaries =
3798 program->Base.NumParameters =
3799 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003800 program->Base.Parameters = _mesa_new_parameter_list ();
3801 program->Base.InputsRead = 0x0;
3802 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003803 program->Position = 0;
3804 program->MajorVersion = program->MinorVersion = 0;
3805 program->PrecisionOption = GL_DONT_CARE;
3806 program->FogOption = GL_NONE;
3807 program->HintPositionInvariant = GL_FALSE;
3808 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003809 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003810 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003811 program->NumAluInstructions =
3812 program->NumTexInstructions =
3813 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003814 program->UsesKill = 0;
3815
Jouk Jansen40322e12004-04-05 08:50:36 +00003816 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003817 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003818
3819 /* Start examining the tokens in the array */
3820 inst = parsed;
3821
3822 /* Check the grammer rev */
3823 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003824 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003825 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003826 }
3827 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003828 /* ignore program target */
3829 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003830 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003831 }
3832
3833 /*debug_variables(ctx, vc_head, program); */
3834
3835 /* We're done with the parsed binary array */
3836 var_cache_destroy (&vc_head);
3837
3838 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003839
Jonathan Whitee3378792008-09-30 15:38:38 -06003840 /* Reallocate the instruction array from size [MAX_PROGRAM_INSTRUCTIONS]
Brian Paul8c41a142005-11-19 15:36:28 +00003841 * to size [ap.Base.NumInstructions].
3842 */
Brian Paula7543902006-08-24 21:58:32 +00003843 program->Base.Instructions
3844 = _mesa_realloc_instructions(program->Base.Instructions,
Jonathan Whitee3378792008-09-30 15:38:38 -06003845 MAX_PROGRAM_INSTRUCTIONS,
Brian Paula7543902006-08-24 21:58:32 +00003846 program->Base.NumInstructions);
3847
Brian Paul45703642005-10-29 15:52:31 +00003848 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003849}
Brian Paul8c41a142005-11-19 15:36:28 +00003850
3851
3852
3853void
3854_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3855 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003856 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003857{
3858 struct arb_program ap;
3859 GLuint i;
3860
3861 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003862 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003863 /* Error in the program. Just return. */
3864 return;
3865 }
3866
3867 /* Copy the relevant contents of the arb_program struct into the
3868 * fragment_program struct.
3869 */
3870 program->Base.String = ap.Base.String;
3871 program->Base.NumInstructions = ap.Base.NumInstructions;
3872 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3873 program->Base.NumParameters = ap.Base.NumParameters;
3874 program->Base.NumAttributes = ap.Base.NumAttributes;
3875 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003876 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3877 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3878 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3879 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3880 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003881 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3882 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3883 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3884 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3885 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3886 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003887 program->Base.InputsRead = ap.Base.InputsRead;
3888 program->Base.OutputsWritten = ap.Base.OutputsWritten;
Briand1284d32008-03-12 15:33:41 -06003889 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
Brianc9db2232007-01-04 17:22:19 -07003890 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Briand1284d32008-03-12 15:33:41 -06003891 if (ap.TexturesUsed[i])
3892 program->Base.SamplersUsed |= (1 << i);
3893 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003894 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003895 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003896 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003897
3898 if (program->Base.Instructions)
3899 _mesa_free(program->Base.Instructions);
3900 program->Base.Instructions = ap.Base.Instructions;
3901
3902 if (program->Base.Parameters)
3903 _mesa_free_parameter_list(program->Base.Parameters);
3904 program->Base.Parameters = ap.Base.Parameters;
3905
Brian Paulc0ef1662008-03-25 11:32:31 -06003906 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3907 * or similar. We used to leave this up to drivers, but it appears
3908 * there's no hardware that wants to do fog in a discrete stage separate
3909 * from the fragment shader.
3910 */
3911 if (program->FogOption != GL_NONE) {
3912 _mesa_append_fog_code(ctx, program);
3913 program->FogOption = GL_NONE;
3914 }
3915
Brian Paul8c41a142005-11-19 15:36:28 +00003916#if DEBUG_FP
Zack Rusin68ef8e82008-06-10 16:59:44 -04003917 _mesa_printf("____________Fragment program %u ________\n", program->Base.Id);
Brian Paul11a54c32006-11-15 19:54:25 +00003918 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003919#endif
3920}
3921
3922
3923
3924/**
3925 * Parse the vertex program string. If success, update the given
3926 * vertex_program object with the new program. Else, leave the vertex_program
3927 * object unchanged.
3928 */
3929void
3930_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3931 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003932 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003933{
3934 struct arb_program ap;
3935
3936 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3937
Brian Paul95801792005-12-06 15:41:43 +00003938 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Briand46ac952008-03-27 16:04:20 -06003939 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
Brian Paul8c41a142005-11-19 15:36:28 +00003940 return;
3941 }
3942
3943 /* Copy the relevant contents of the arb_program struct into the
3944 * vertex_program struct.
3945 */
3946 program->Base.String = ap.Base.String;
3947 program->Base.NumInstructions = ap.Base.NumInstructions;
3948 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3949 program->Base.NumParameters = ap.Base.NumParameters;
3950 program->Base.NumAttributes = ap.Base.NumAttributes;
3951 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003952 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3953 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3954 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3955 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3956 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003957 program->Base.InputsRead = ap.Base.InputsRead;
3958 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3959 program->IsPositionInvariant = ap.HintPositionInvariant;
3960
3961 if (program->Base.Instructions)
3962 _mesa_free(program->Base.Instructions);
3963 program->Base.Instructions = ap.Base.Instructions;
3964
3965 if (program->Base.Parameters)
3966 _mesa_free_parameter_list(program->Base.Parameters);
3967 program->Base.Parameters = ap.Base.Parameters;
3968
3969#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01003970 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00003971 _mesa_print_program(&program->Base);
3972#endif
3973}