blob: 35253daa2e6563360bd0b7c0469bd877ee4e45de [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brianab31a3a2007-09-13 11:41:49 -06003 * Version: 7.1
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Brian7d2b6a02008-03-27 16:17:37 -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
Brianab31a3a2007-09-13 11:41:49 -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)) {
Michal Krol9ba52e12009-03-16 09:12:21 +0100972 *color = 0;
Brian Paula088f162006-09-05 23:08:51 +0000973 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000974 return 1;
975 }
976
977 *color = (GLuint) i;
978 return 0;
979}
980
981
982/**
Ian Romanick2549c262009-01-14 10:05:40 -0800983 * Validate the index of a texture coordinate
984 *
Jouk Jansen40322e12004-04-05 08:50:36 +0000985 * \param coord The texture unit index
986 * \return 0 on sucess, 1 on error
987 */
988static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000989parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000990 struct arb_program *Program, GLuint * coord)
991{
Brian Paulbd997cd2004-07-20 21:12:56 +0000992 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000993
Ian Romanick2549c262009-01-14 10:05:40 -0800994 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureCoordUnits)) {
995 program_error(ctx, Program->Position, "Invalid texture coordinate index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000996 return 1;
997 }
998
Brian Paulbd997cd2004-07-20 21:12:56 +0000999 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +00001000 return 0;
1001}
1002
Ian Romanick2549c262009-01-14 10:05:40 -08001003
1004/**
1005 * Validate the index of a texture image unit
1006 *
1007 * \param coord The texture unit index
1008 * \return 0 on sucess, 1 on error
1009 */
1010static GLuint
1011parse_teximage_num (GLcontext * ctx, const GLubyte ** inst,
1012 struct arb_program *Program, GLuint * coord)
1013{
1014 GLint i = parse_integer (inst, Program);
1015
1016 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureImageUnits)) {
1017 program_error(ctx, Program->Position, "Invalid texture image index");
1018 return 1;
1019 }
1020
1021 *coord = (GLuint) i;
1022 return 0;
1023}
1024
1025
Jouk Jansen40322e12004-04-05 08:50:36 +00001026/**
1027 * \param coord The weight index
1028 * \return 0 on sucess, 1 on error
1029 */
1030static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001031parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001032 GLint * coord)
1033{
1034 *coord = parse_integer (inst, Program);
1035
1036 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +00001037 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +00001038 return 1;
1039 }
1040
1041 return 0;
1042}
1043
1044/**
1045 * \param coord The clip plane index
1046 * \return 0 on sucess, 1 on error
1047 */
1048static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001049parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001050 struct arb_program *Program, GLint * coord)
1051{
1052 *coord = parse_integer (inst, Program);
1053
1054 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +00001055 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +00001056 return 1;
1057 }
1058
1059 return 0;
1060}
1061
1062
1063/**
1064 * \return 0 on front face, 1 on back face
1065 */
1066static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001067parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +00001068{
1069 switch (*(*inst)++) {
1070 case FACE_FRONT:
1071 return 0;
1072
1073 case FACE_BACK:
1074 return 1;
1075 }
1076 return 0;
1077}
1078
1079
1080/**
1081 * Given a matrix and a modifier token on the binary array, return tokens
1082 * that _mesa_fetch_state() [program.c] can understand.
1083 *
1084 * \param matrix - the matrix we are talking about
1085 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1086 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1087 * \return 0 on sucess, 1 on failure
1088 */
1089static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001090parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001091 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1092{
1093 GLubyte mat = *(*inst)++;
1094
1095 *matrix_idx = 0;
1096
1097 switch (mat) {
1098 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001099 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001100 *matrix_idx = parse_integer (inst, Program);
1101 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001102 program_error(ctx, Program->Position,
1103 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001104 return 1;
1105 }
1106 break;
1107
1108 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001109 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001110 break;
1111
1112 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001113 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001114 break;
1115
1116 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001117 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001118 *matrix_idx = parse_integer (inst, Program);
1119 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001120 program_error(ctx, Program->Position, "Invalid Texture Unit");
1121 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001122 return 1;
1123 }
1124 break;
1125
1126 /* This is not currently supported (ARB_matrix_palette) */
1127 case MATRIX_PALETTE:
1128 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001129 program_error(ctx, Program->Position,
1130 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001131 return 1;
1132 break;
1133
1134 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001135 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001136 *matrix_idx = parse_integer (inst, Program);
1137 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001138 program_error(ctx, Program->Position, "Invalid Program Matrix");
1139 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001140 return 1;
1141 }
1142 break;
1143 }
1144
1145 switch (*(*inst)++) {
1146 case MATRIX_MODIFIER_IDENTITY:
1147 *matrix_modifier = 0;
1148 break;
1149 case MATRIX_MODIFIER_INVERSE:
1150 *matrix_modifier = STATE_MATRIX_INVERSE;
1151 break;
1152 case MATRIX_MODIFIER_TRANSPOSE:
1153 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1154 break;
1155 case MATRIX_MODIFIER_INVTRANS:
1156 *matrix_modifier = STATE_MATRIX_INVTRANS;
1157 break;
1158 }
1159
1160 return 0;
1161}
1162
1163
1164/**
1165 * This parses a state string (rather, the binary version of it) into
1166 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1167 *
1168 * \param inst - the start in the binary arry to start working from
1169 * \param state_tokens - the storage for the 6-token state description
1170 * \return - 0 on sucess, 1 on error
1171 */
1172static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001173parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001174 struct arb_program *Program,
1175 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001176{
Brian Paul4ca0af12008-07-09 08:35:50 -06001177 GLubyte token = *(*inst)++;
1178
1179 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001180 case STATE_MATERIAL_PARSER:
1181 state_tokens[0] = STATE_MATERIAL;
1182 state_tokens[1] = parse_face_type (inst);
1183 switch (*(*inst)++) {
1184 case MATERIAL_AMBIENT:
1185 state_tokens[2] = STATE_AMBIENT;
1186 break;
1187 case MATERIAL_DIFFUSE:
1188 state_tokens[2] = STATE_DIFFUSE;
1189 break;
1190 case MATERIAL_SPECULAR:
1191 state_tokens[2] = STATE_SPECULAR;
1192 break;
1193 case MATERIAL_EMISSION:
1194 state_tokens[2] = STATE_EMISSION;
1195 break;
1196 case MATERIAL_SHININESS:
1197 state_tokens[2] = STATE_SHININESS;
1198 break;
1199 }
1200 break;
1201
1202 case STATE_LIGHT_PARSER:
1203 state_tokens[0] = STATE_LIGHT;
1204 state_tokens[1] = parse_integer (inst, Program);
1205
1206 /* Check the value of state_tokens[1] against the # of lights */
1207 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001208 program_error(ctx, Program->Position, "Invalid Light Number");
1209 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001210 return 1;
1211 }
1212
1213 switch (*(*inst)++) {
1214 case LIGHT_AMBIENT:
1215 state_tokens[2] = STATE_AMBIENT;
1216 break;
1217 case LIGHT_DIFFUSE:
1218 state_tokens[2] = STATE_DIFFUSE;
1219 break;
1220 case LIGHT_SPECULAR:
1221 state_tokens[2] = STATE_SPECULAR;
1222 break;
1223 case LIGHT_POSITION:
1224 state_tokens[2] = STATE_POSITION;
1225 break;
1226 case LIGHT_ATTENUATION:
1227 state_tokens[2] = STATE_ATTENUATION;
1228 break;
1229 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001230 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001231 break;
1232 case LIGHT_SPOT_DIRECTION:
1233 state_tokens[2] = STATE_SPOT_DIRECTION;
1234 break;
1235 }
1236 break;
1237
1238 case STATE_LIGHT_MODEL:
1239 switch (*(*inst)++) {
1240 case LIGHT_MODEL_AMBIENT:
1241 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1242 break;
1243 case LIGHT_MODEL_SCENECOLOR:
1244 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1245 state_tokens[1] = parse_face_type (inst);
1246 break;
1247 }
1248 break;
1249
1250 case STATE_LIGHT_PROD:
1251 state_tokens[0] = STATE_LIGHTPROD;
1252 state_tokens[1] = parse_integer (inst, Program);
1253
1254 /* Check the value of state_tokens[1] against the # of lights */
1255 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001256 program_error(ctx, Program->Position, "Invalid Light Number");
1257 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001258 return 1;
1259 }
1260
1261 state_tokens[2] = parse_face_type (inst);
1262 switch (*(*inst)++) {
1263 case LIGHT_PROD_AMBIENT:
1264 state_tokens[3] = STATE_AMBIENT;
1265 break;
1266 case LIGHT_PROD_DIFFUSE:
1267 state_tokens[3] = STATE_DIFFUSE;
1268 break;
1269 case LIGHT_PROD_SPECULAR:
1270 state_tokens[3] = STATE_SPECULAR;
1271 break;
1272 }
1273 break;
1274
1275
1276 case STATE_FOG:
1277 switch (*(*inst)++) {
1278 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001279 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001280 break;
1281 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001282 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001283 break;
1284 }
1285 break;
1286
1287 case STATE_TEX_ENV:
1288 state_tokens[1] = parse_integer (inst, Program);
1289 switch (*(*inst)++) {
1290 case TEX_ENV_COLOR:
1291 state_tokens[0] = STATE_TEXENV_COLOR;
1292 break;
1293 }
1294 break;
1295
1296 case STATE_TEX_GEN:
1297 {
1298 GLuint type, coord;
1299
1300 state_tokens[0] = STATE_TEXGEN;
1301 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1302
1303 if (parse_texcoord_num (ctx, inst, Program, &coord))
1304 return 1;
1305 state_tokens[1] = coord;
1306
1307 /* EYE or OBJECT */
Briand799b7a2007-09-13 11:29:00 -06001308 type = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001309
1310 /* 0 - s, 1 - t, 2 - r, 3 - q */
Briand799b7a2007-09-13 11:29:00 -06001311 coord = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001312
1313 if (type == TEX_GEN_EYE) {
1314 switch (coord) {
1315 case COMPONENT_X:
1316 state_tokens[2] = STATE_TEXGEN_EYE_S;
1317 break;
1318 case COMPONENT_Y:
1319 state_tokens[2] = STATE_TEXGEN_EYE_T;
1320 break;
1321 case COMPONENT_Z:
1322 state_tokens[2] = STATE_TEXGEN_EYE_R;
1323 break;
1324 case COMPONENT_W:
1325 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1326 break;
Briand799b7a2007-09-13 11:29:00 -06001327 default:
1328 _mesa_problem(ctx, "bad texgen component in "
1329 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001330 }
1331 }
1332 else {
1333 switch (coord) {
1334 case COMPONENT_X:
1335 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1336 break;
1337 case COMPONENT_Y:
1338 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1339 break;
1340 case COMPONENT_Z:
1341 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1342 break;
1343 case COMPONENT_W:
1344 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1345 break;
Briand799b7a2007-09-13 11:29:00 -06001346 default:
1347 _mesa_problem(ctx, "bad texgen component in "
1348 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001349 }
1350 }
1351 }
1352 break;
1353
1354 case STATE_DEPTH:
1355 switch (*(*inst)++) {
1356 case DEPTH_RANGE:
1357 state_tokens[0] = STATE_DEPTH_RANGE;
1358 break;
1359 }
1360 break;
1361
1362 case STATE_CLIP_PLANE:
1363 state_tokens[0] = STATE_CLIPPLANE;
Brianaa9d22a2007-02-23 11:21:03 -07001364 if (parse_clipplane_num (ctx, inst, Program,
1365 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001366 return 1;
1367 break;
1368
1369 case STATE_POINT:
Briand799b7a2007-09-13 11:29:00 -06001370 switch (*(*inst)++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001371 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001372 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001373 break;
1374
1375 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001376 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001377 break;
1378 }
1379 break;
1380
1381 /* XXX: I think this is the correct format for a matrix row */
1382 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001383 if (parse_matrix(ctx, inst, Program,
1384 (GLint *) &state_tokens[0],
1385 (GLint *) &state_tokens[1],
1386 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001387 return 1;
1388
Brian65319522007-02-21 11:08:21 -07001389 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001390
1391 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001392 state_tokens[3] = parse_integer (inst, Program);
1393 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001394 program_error(ctx, Program->Position,
1395 "Second matrix index less than the first");
1396 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001397 return 1;
1398 }
1399 }
1400 else {
Brian65319522007-02-21 11:08:21 -07001401 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001402 (*inst)++;
1403 }
1404 break;
1405 }
1406
1407 return 0;
1408}
1409
1410/**
1411 * This parses a state string (rather, the binary version of it) into
1412 * a 6-token similar for the state fetching code in program.c
1413 *
1414 * One might ask, why fetch these parameters into just like you fetch
1415 * state when they are already stored in other places?
1416 *
1417 * Because of array offsets -> We can stick env/local parameters in the
1418 * middle of a parameter array and then index someplace into the array
1419 * when we execute.
1420 *
1421 * One optimization might be to only do this for the cases where the
1422 * env/local parameters end up inside of an array, and leave the
1423 * single parameters (or arrays of pure env/local pareameters) in their
1424 * respective register files.
1425 *
1426 * For ENV parameters, the format is:
1427 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1428 * state_tokens[1] = STATE_ENV
1429 * state_tokens[2] = the parameter index
1430 *
1431 * for LOCAL parameters, the format is:
1432 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1433 * state_tokens[1] = STATE_LOCAL
1434 * state_tokens[2] = the parameter index
1435 *
1436 * \param inst - the start in the binary arry to start working from
1437 * \param state_tokens - the storage for the 6-token state description
1438 * \return - 0 on sucess, 1 on failure
1439 */
1440static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001441parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001442 struct arb_program *Program,
1443 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001444{
1445 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1446 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1447 else
1448 state_tokens[0] = STATE_VERTEX_PROGRAM;
1449
1450
1451 switch (*(*inst)++) {
1452 case PROGRAM_PARAM_ENV:
1453 state_tokens[1] = STATE_ENV;
1454 state_tokens[2] = parse_integer (inst, Program);
1455
1456 /* Check state_tokens[2] against the number of ENV parameters available */
1457 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001458 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001459 ||
1460 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001461 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001462 program_error(ctx, Program->Position,
1463 "Invalid Program Env Parameter");
1464 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001465 return 1;
1466 }
1467
1468 break;
1469
1470 case PROGRAM_PARAM_LOCAL:
1471 state_tokens[1] = STATE_LOCAL;
1472 state_tokens[2] = parse_integer (inst, Program);
1473
1474 /* Check state_tokens[2] against the number of LOCAL parameters available */
1475 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001476 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001477 ||
1478 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001479 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001480 program_error(ctx, Program->Position,
1481 "Invalid Program Local Parameter");
1482 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001483 return 1;
1484 }
1485 break;
1486 }
1487
1488 return 0;
1489}
1490
1491/**
1492 * For ARB_vertex_program, programs are not allowed to use both an explicit
1493 * vertex attribute and a generic vertex attribute corresponding to the same
1494 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1495 *
1496 * This will walk our var_cache and make sure that nobody does anything fishy.
1497 *
1498 * \return 0 on sucess, 1 on error
1499 */
1500static GLuint
1501generic_attrib_check(struct var_cache *vc_head)
1502{
1503 int a;
1504 struct var_cache *curr;
1505 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1506 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1507
1508 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1509 explicitAttrib[a] = GL_FALSE;
1510 genericAttrib[a] = GL_FALSE;
1511 }
1512
1513 curr = vc_head;
1514 while (curr) {
1515 if (curr->type == vt_attrib) {
1516 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001517 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001518 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001519 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001520 }
1521
1522 curr = curr->next;
1523 }
1524
1525 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1526 if ((explicitAttrib[a]) && (genericAttrib[a]))
1527 return 1;
1528 }
1529
1530 return 0;
1531}
1532
1533/**
1534 * This will handle the binding side of an ATTRIB var declaration
1535 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001536 * \param inputReg returns the input register index, one of the
1537 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001538 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001539 */
1540static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001541parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001542 struct arb_program *Program,
1543 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001544{
Jouk Jansen40322e12004-04-05 08:50:36 +00001545 GLint err = 0;
1546
1547 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001548
Jouk Jansen40322e12004-04-05 08:50:36 +00001549 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1550 switch (*(*inst)++) {
1551 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001552 {
1553 GLint coord;
1554 err = parse_color_type (ctx, inst, Program, &coord);
1555 *inputReg = FRAG_ATTRIB_COL0 + coord;
1556 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001557 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001558 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001559 {
Brian8fa6f732007-02-01 09:24:41 -07001560 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001561 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1562 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1563 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001564 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001565 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001566 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001567 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001568 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001569 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001570 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001571 default:
1572 err = 1;
1573 break;
1574 }
1575 }
1576 else {
1577 switch (*(*inst)++) {
1578 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001579 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001580 break;
1581
1582 case VERTEX_ATTRIB_WEIGHT:
1583 {
1584 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001585 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001586 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001587#if 1
1588 /* hack for Warcraft (see bug 8060) */
1589 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001590 break;
Brian Paul3a557502006-09-05 23:15:29 +00001591#else
Brian Paula088f162006-09-05 23:08:51 +00001592 program_error(ctx, Program->Position,
1593 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001594 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001595#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001596 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001597
1598 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001599 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001600 break;
1601
1602 case VERTEX_ATTRIB_COLOR:
1603 {
1604 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001605 err = parse_color_type (ctx, inst, Program, &color);
1606 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001607 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001608 }
1609 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001610 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001611 }
1612 }
1613 break;
1614
1615 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001616 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 break;
1618
1619 case VERTEX_ATTRIB_TEXCOORD:
1620 {
Brian8fa6f732007-02-01 09:24:41 -07001621 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001622 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001623 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 }
1625 break;
1626
Jouk Jansen40322e12004-04-05 08:50:36 +00001627 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001628 /* Not supported at this time */
1629 {
1630 const char *msg = "ARB_palette_matrix not supported";
1631 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001632 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001633 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001634 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001635
1636 case VERTEX_ATTRIB_GENERIC:
1637 {
1638 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001639 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001640 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001642 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1643 * attributes do not alias the conventional vertex
1644 * attributes.
1645 */
1646 if (attrib > 0)
1647 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001648 else
1649 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001650 }
1651 }
1652 break;
1653
1654 default:
1655 err = 1;
1656 break;
1657 }
1658 }
1659
Jouk Jansen40322e12004-04-05 08:50:36 +00001660 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001661 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001662 }
1663
Jouk Jansen40322e12004-04-05 08:50:36 +00001664 return err;
1665}
1666
Brian Paul7aebaf32005-10-30 21:23:23 +00001667
Jouk Jansen40322e12004-04-05 08:50:36 +00001668/**
1669 * This translates between a binary token for an output variable type
1670 * and the mesa token for the same thing.
1671 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001672 * \param inst The parsed tokens
1673 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001674 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001675 */
1676static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001677parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001678 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001679{
Brian Paul7aebaf32005-10-30 21:23:23 +00001680 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001681
Brian Paul7aebaf32005-10-30 21:23:23 +00001682 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001683 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001684 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001685 GLuint out_color;
1686
Brian Paulbe76b7f2004-10-04 14:40:05 +00001687 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001688 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001689 */
1690 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001691 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul8d475822009-02-28 11:49:46 -07001692 *outputReg = FRAG_RESULT_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001693 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001694 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001695 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1696 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001697 }
1698 break;
1699
1700 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001701 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001702 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul8d475822009-02-28 11:49:46 -07001703 *outputReg = FRAG_RESULT_DEPTH;
Jouk Jansen40322e12004-04-05 08:50:36 +00001704 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001705 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001706 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001707 GLint color_type;
1708 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001709 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1710 if (err)
1711 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001712
Jouk Jansen40322e12004-04-05 08:50:36 +00001713 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001714 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001715 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001716 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001717 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001718 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001719 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001720 }
1721 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001722 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001723 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001724 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001725 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001726 }
1727 /* primary color */
1728 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001729 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001730 }
1731 }
1732 }
1733 break;
1734
1735 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001736 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001737 break;
1738
1739 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001740 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001741 break;
1742
1743 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001744 {
1745 GLuint unit;
1746 if (parse_texcoord_num (ctx, inst, Program, &unit))
1747 return 1;
1748 *outputReg = VERT_RESULT_TEX0 + unit;
1749 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001750 break;
1751 }
1752
Brian Paulde997602005-11-12 17:53:14 +00001753 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001754
1755 return 0;
1756}
1757
Brian Paul7aebaf32005-10-30 21:23:23 +00001758
Jouk Jansen40322e12004-04-05 08:50:36 +00001759/**
1760 * This handles the declaration of ATTRIB variables
1761 *
1762 * XXX: Still needs
1763 * parse_vert_attrib_binding(), or something like that
1764 *
1765 * \return 0 on sucess, 1 on error
1766 */
1767static GLint
Brian Paula088f162006-09-05 23:08:51 +00001768parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001769 struct arb_program *Program)
1770{
1771 GLuint found;
Jouk Jansen40322e12004-04-05 08:50:36 +00001772 struct var_cache *attrib_var;
1773
1774 attrib_var = parse_string (inst, vc_head, Program, &found);
1775 Program->Position = parse_position (inst);
1776 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06001777 program_error2(ctx, Program->Position,
1778 "Duplicate variable declaration",
1779 (char *) attrib_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001780 return 1;
1781 }
1782
1783 attrib_var->type = vt_attrib;
1784
Brian Paul7aebaf32005-10-30 21:23:23 +00001785 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001786 &attrib_var->attrib_is_generic))
1787 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001788
Brian Paul7aebaf32005-10-30 21:23:23 +00001789 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001790 program_error(ctx, Program->Position,
1791 "Cannot use both a generic vertex attribute "
1792 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001793 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001794 }
1795
1796 Program->Base.NumAttributes++;
1797 return 0;
1798}
1799
1800/**
1801 * \param use -- TRUE if we're called when declaring implicit parameters,
1802 * FALSE if we're declaraing variables. This has to do with
1803 * if we get a signed or unsigned float for scalar constants
1804 */
1805static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001806parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001807 struct var_cache *param_var,
1808 struct arb_program *Program, GLboolean use)
1809{
1810 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001811 GLuint err = 0;
Roland Scheidegger8dc18842007-11-29 03:08:18 +01001812 gl_state_index state_tokens[STATE_LENGTH] = {0, 0, 0, 0, 0};
Jouk Jansen40322e12004-04-05 08:50:36 +00001813 GLfloat const_values[4];
1814
Brian Paul4ca0af12008-07-09 08:35:50 -06001815 GLubyte token = *(*inst)++;
1816
1817 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001818 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001819 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1820 return 1;
1821
1822 /* If we adding STATE_MATRIX that has multiple rows, we need to
1823 * unroll it and call _mesa_add_state_reference() for each row
1824 */
Brian65319522007-02-21 11:08:21 -07001825 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1826 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1827 state_tokens[0] == STATE_MVP_MATRIX ||
1828 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1829 state_tokens[0] == STATE_PROGRAM_MATRIX)
1830 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001831 GLint row;
Brian65319522007-02-21 11:08:21 -07001832 const GLint first_row = state_tokens[2];
1833 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001834
1835 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001836 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001837
Brian Paulde997602005-11-12 17:53:14 +00001838 idx = _mesa_add_state_reference(Program->Base.Parameters,
1839 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001840 if (param_var->param_binding_begin == ~0U)
1841 param_var->param_binding_begin = idx;
1842 param_var->param_binding_length++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001843 }
1844 }
1845 else {
Brian Paulde997602005-11-12 17:53:14 +00001846 idx = _mesa_add_state_reference(Program->Base.Parameters,
1847 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001848 if (param_var->param_binding_begin == ~0U)
1849 param_var->param_binding_begin = idx;
1850 param_var->param_binding_length++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001851 }
1852 break;
1853
1854 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001855 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1856 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001857 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001858 if (param_var->param_binding_begin == ~0U)
1859 param_var->param_binding_begin = idx;
1860 param_var->param_binding_length++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001861
1862 /* Check if there is more: 0 -> we're done, else its an integer */
1863 if (**inst) {
1864 GLuint out_of_range, new_idx;
1865 GLuint start_idx = state_tokens[2] + 1;
1866 GLuint end_idx = parse_integer (inst, Program);
1867
1868 out_of_range = 0;
1869 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1870 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001871 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001872 || ((state_tokens[1] == STATE_LOCAL)
1873 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001874 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001875 out_of_range = 1;
1876 }
1877 else {
1878 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001879 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001880 || ((state_tokens[1] == STATE_LOCAL)
1881 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001882 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001883 out_of_range = 1;
1884 }
1885 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001886 program_error(ctx, Program->Position,
1887 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001888 return 1;
1889 }
1890
1891 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1892 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001893 idx = _mesa_add_state_reference(Program->Base.Parameters,
1894 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001895 param_var->param_binding_length++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001896 }
1897 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001898 else {
1899 (*inst)++;
1900 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001901 break;
1902
1903 case PARAM_CONSTANT:
Brian978145a2008-03-27 16:00:15 -06001904 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
Jouk Jansen40322e12004-04-05 08:50:36 +00001905 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001906 idx = _mesa_add_named_constant(Program->Base.Parameters,
1907 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001908 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001909 if (param_var->param_binding_begin == ~0U)
1910 param_var->param_binding_begin = idx;
Brian Paul0576e832008-09-19 13:04:52 +02001911 param_var->param_binding_type = PROGRAM_STATE_VAR;
1912 /* Note: when we reference this parameter in an instruction later,
1913 * we'll check if it's really a constant/immediate and set the
1914 * instruction register type appropriately.
1915 */
Jouk Jansen40322e12004-04-05 08:50:36 +00001916 param_var->param_binding_length++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001917 break;
1918
1919 default:
Brian Paula088f162006-09-05 23:08:51 +00001920 program_error(ctx, Program->Position,
1921 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001922 return 1;
1923 }
1924
Brian Paul866bdd02009-04-03 15:41:10 -06001925 Program->Base.NumParameters = Program->Base.Parameters->NumParameters;
1926
Jouk Jansen40322e12004-04-05 08:50:36 +00001927 /* Make sure we haven't blown past our parameter limits */
1928 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paula4173952009-04-03 13:48:43 -06001929 (Program->Base.NumParameters >
Brian Paul05051032005-11-01 04:36:33 +00001930 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001931 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
Brian Paula4173952009-04-03 13:48:43 -06001932 && (Program->Base.NumParameters >
Brian Paul05051032005-11-01 04:36:33 +00001933 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001934 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001935 return 1;
1936 }
1937
1938 return err;
1939}
1940
Brian Paul7aebaf32005-10-30 21:23:23 +00001941
Jouk Jansen40322e12004-04-05 08:50:36 +00001942/**
1943 * This picks out PARAM program parameter bindings.
1944 *
1945 * XXX: This needs to be stressed & tested
1946 *
1947 * \return 0 on sucess, 1 on error
1948 */
1949static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001950parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001951 struct arb_program *Program)
1952{
Brian Paulbd997cd2004-07-20 21:12:56 +00001953 GLuint found, err;
1954 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001955 struct var_cache *param_var;
1956
1957 err = 0;
1958 param_var = parse_string (inst, vc_head, Program, &found);
1959 Program->Position = parse_position (inst);
1960
1961 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06001962 program_error2(ctx, Program->Position,
1963 "Duplicate variable declaration",
1964 (char *) param_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001965 return 1;
1966 }
1967
1968 specified_length = parse_integer (inst, Program);
1969
1970 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001971 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001972 return 1;
1973 }
1974
1975 param_var->type = vt_param;
1976 param_var->param_binding_length = 0;
1977
1978 /* Right now, everything is shoved into the main state register file.
1979 *
1980 * In the future, it would be nice to leave things ENV/LOCAL params
1981 * in their respective register files, if possible
1982 */
1983 param_var->param_binding_type = PROGRAM_STATE_VAR;
1984
1985 /* Remember to:
1986 * * - add each guy to the parameter list
1987 * * - increment the param_var->param_binding_len
1988 * * - store the param_var->param_binding_begin for the first one
1989 * * - compare the actual len to the specified len at the end
1990 */
1991 while (**inst != PARAM_NULL) {
1992 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1993 return 1;
1994 }
1995
1996 /* Test array length here! */
1997 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001998 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001999 program_error(ctx, Program->Position,
2000 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00002001 }
2002 }
2003
2004 (*inst)++;
2005
2006 return 0;
2007}
2008
2009/**
2010 *
2011 */
2012static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002013parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002014 struct arb_program *Program, struct var_cache **new_var)
2015{
2016 struct var_cache *param_var;
2017
2018 /* First, insert a dummy entry into the var_cache */
2019 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00002020 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00002021 param_var->type = vt_param;
2022
2023 param_var->param_binding_length = 0;
2024 /* Don't fill in binding_begin; We use the default value of -1
2025 * to tell if its already initialized, elsewhere.
2026 *
2027 * param_var->param_binding_begin = 0;
2028 */
2029 param_var->param_binding_type = PROGRAM_STATE_VAR;
2030
2031 var_cache_append (vc_head, param_var);
2032
2033 /* Then fill it with juicy parameter goodness */
2034 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
2035 return 1;
2036
2037 *new_var = param_var;
2038
2039 return 0;
2040}
2041
2042
2043/**
2044 * This handles the declaration of TEMP variables
2045 *
2046 * \return 0 on sucess, 1 on error
2047 */
2048static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002049parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002050 struct arb_program *Program)
2051{
2052 GLuint found;
2053 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002054
2055 while (**inst != 0) {
2056 temp_var = parse_string (inst, vc_head, Program, &found);
2057 Program->Position = parse_position (inst);
2058 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002059 program_error2(ctx, Program->Position,
2060 "Duplicate variable declaration",
2061 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002062 return 1;
2063 }
2064
2065 temp_var->type = vt_temp;
2066
2067 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2068 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002069 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002070 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2071 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002072 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00002073 program_error(ctx, Program->Position,
2074 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002075 return 1;
2076 }
2077
2078 temp_var->temp_binding = Program->Base.NumTemporaries;
2079 Program->Base.NumTemporaries++;
2080 }
2081 (*inst)++;
2082
2083 return 0;
2084}
2085
2086/**
2087 * This handles variables of the OUTPUT variety
2088 *
2089 * \return 0 on sucess, 1 on error
2090 */
2091static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002092parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002093 struct arb_program *Program)
2094{
2095 GLuint found;
2096 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002097 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002098
2099 output_var = parse_string (inst, vc_head, Program, &found);
2100 Program->Position = parse_position (inst);
2101 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002102 program_error2(ctx, Program->Position,
2103 "Duplicate variable declaration",
2104 (char *) output_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002105 return 1;
2106 }
2107
2108 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002109
2110 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2111 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002112}
2113
2114/**
2115 * This handles variables of the ALIAS kind
2116 *
2117 * \return 0 on sucess, 1 on error
2118 */
2119static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002120parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002121 struct arb_program *Program)
2122{
2123 GLuint found;
2124 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002125
2126 temp_var = parse_string (inst, vc_head, Program, &found);
2127 Program->Position = parse_position (inst);
2128
2129 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002130 program_error2(ctx, Program->Position,
2131 "Duplicate variable declaration",
2132 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002133 return 1;
2134 }
2135
2136 temp_var->type = vt_alias;
2137 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2138 Program->Position = parse_position (inst);
2139
2140 if (!found)
2141 {
Brianab31a3a2007-09-13 11:41:49 -06002142 program_error2(ctx, Program->Position,
2143 "Undefined alias value",
2144 (char *) temp_var->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002145 return 1;
2146 }
2147
2148 return 0;
2149}
2150
2151/**
2152 * This handles variables of the ADDRESS kind
2153 *
2154 * \return 0 on sucess, 1 on error
2155 */
2156static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002157parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002158 struct arb_program *Program)
2159{
2160 GLuint found;
2161 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002162
2163 while (**inst != 0) {
2164 temp_var = parse_string (inst, vc_head, Program, &found);
2165 Program->Position = parse_position (inst);
2166 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002167 program_error2(ctx, Program->Position,
2168 "Duplicate variable declaration",
2169 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002170 return 1;
2171 }
2172
2173 temp_var->type = vt_address;
2174
2175 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002176 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002177 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002178 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002179 return 1;
2180 }
2181
2182 temp_var->address_binding = Program->Base.NumAddressRegs;
2183 Program->Base.NumAddressRegs++;
2184 }
2185 (*inst)++;
2186
2187 return 0;
2188}
2189
2190/**
2191 * Parse a program declaration
2192 *
2193 * \return 0 on sucess, 1 on error
2194 */
2195static GLint
Brian Paula088f162006-09-05 23:08:51 +00002196parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002197 struct arb_program *Program)
2198{
2199 GLint err = 0;
2200
2201 switch (*(*inst)++) {
2202 case ADDRESS:
2203 err = parse_address (ctx, inst, vc_head, Program);
2204 break;
2205
2206 case ALIAS:
2207 err = parse_alias (ctx, inst, vc_head, Program);
2208 break;
2209
2210 case ATTRIB:
2211 err = parse_attrib (ctx, inst, vc_head, Program);
2212 break;
2213
2214 case OUTPUT:
2215 err = parse_output (ctx, inst, vc_head, Program);
2216 break;
2217
2218 case PARAM:
2219 err = parse_param (ctx, inst, vc_head, Program);
2220 break;
2221
2222 case TEMP:
2223 err = parse_temp (ctx, inst, vc_head, Program);
2224 break;
2225 }
2226
2227 return err;
2228}
2229
2230/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002231 * Handle the parsing out of a masked destination register, either for a
2232 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002233 *
2234 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002235 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002236 * position invariant
2237 *
2238 * \param File - The register file we write to
2239 * \param Index - The register index we write to
2240 * \param WriteMask - The mask controlling which components we write (1->write)
2241 *
2242 * \return 0 on sucess, 1 on error
2243 */
2244static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002245parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002246 struct var_cache **vc_head, struct arb_program *Program,
Brian Paulb4026d92009-03-07 12:02:52 -07002247 gl_register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002248{
Brian Paul7aebaf32005-10-30 21:23:23 +00002249 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002250 struct var_cache *dst;
2251
2252 /* We either have a result register specified, or a
2253 * variable that may or may not be writable
2254 */
2255 switch (*(*inst)++) {
2256 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002257 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002258 return 1;
2259 *File = PROGRAM_OUTPUT;
2260 break;
2261
2262 case REGISTER_ESTABLISHED_NAME:
2263 dst = parse_string (inst, vc_head, Program, &result);
2264 Program->Position = parse_position (inst);
2265
2266 /* If the name has never been added to our symbol table, we're hosed */
2267 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002268 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002269 return 1;
2270 }
2271
2272 switch (dst->type) {
2273 case vt_output:
2274 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002275 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002276 break;
2277
2278 case vt_temp:
2279 *File = PROGRAM_TEMPORARY;
2280 *Index = dst->temp_binding;
2281 break;
2282
2283 /* If the var type is not vt_output or vt_temp, no go */
2284 default:
Brian Paula088f162006-09-05 23:08:51 +00002285 program_error(ctx, Program->Position,
2286 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002287 return 1;
2288 }
2289 break;
2290
2291 default:
Brian Paula088f162006-09-05 23:08:51 +00002292 program_error(ctx, Program->Position,
2293 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002294 return 1;
2295 }
2296
2297
2298 /* Position invariance test */
2299 /* This test is done now in syntax portion - when position invariance OPTION
2300 is specified, "result.position" rule is disabled so there is no way
2301 to write the position
2302 */
2303 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2304 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002305 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002306 "Vertex program specified position invariance and wrote vertex position");
2307 }*/
2308
2309 /* And then the mask.
2310 * w,a -> bit 0
2311 * z,b -> bit 1
2312 * y,g -> bit 2
2313 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002314 *
2315 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002316 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002317 tmp = (GLint) *(*inst)++;
2318 *WriteMask = (((tmp>>3) & 0x1) |
2319 ((tmp>>1) & 0x2) |
2320 ((tmp<<1) & 0x4) |
2321 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002322
2323 return 0;
2324}
2325
2326
2327/**
2328 * Handle the parsing of a address register
2329 *
2330 * \param Index - The register index we write to
2331 *
2332 * \return 0 on sucess, 1 on error
2333 */
2334static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002335parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002336 struct var_cache **vc_head,
2337 struct arb_program *Program, GLint * Index)
2338{
2339 struct var_cache *dst;
2340 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002341
2342 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002343
2344 dst = parse_string (inst, vc_head, Program, &result);
2345 Program->Position = parse_position (inst);
2346
2347 /* If the name has never been added to our symbol table, we're hosed */
2348 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002349 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002350 return 1;
2351 }
2352
2353 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002354 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002355 return 1;
2356 }
2357
2358 return 0;
2359}
2360
Brian Paul8e8fa632005-07-01 02:03:33 +00002361#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002362/**
2363 * Handle the parsing out of a masked address register
2364 *
2365 * \param Index - The register index we write to
2366 * \param WriteMask - The mask controlling which components we write (1->write)
2367 *
2368 * \return 0 on sucess, 1 on error
2369 */
2370static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002371parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002372 struct var_cache **vc_head,
2373 struct arb_program *Program, GLint * Index,
2374 GLboolean * WriteMask)
2375{
2376 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2377 return 1;
2378
2379 /* This should be 0x8 */
2380 (*inst)++;
2381
2382 /* Writemask of .x is implied */
2383 WriteMask[0] = 1;
2384 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2385
2386 return 0;
2387}
Brian Paul8e8fa632005-07-01 02:03:33 +00002388#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002389
2390/**
2391 * Parse out a swizzle mask.
2392 *
Brian Paul32df89e2005-10-29 18:26:43 +00002393 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002394 *
2395 * The len parameter allows us to grab 4 components for a vector
2396 * swizzle, or just 1 component for a scalar src register selection
2397 */
Brian Paul32df89e2005-10-29 18:26:43 +00002398static void
Brian Paula088f162006-09-05 23:08:51 +00002399parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002400{
Brian Paul32df89e2005-10-29 18:26:43 +00002401 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002402
Brian Paul32df89e2005-10-29 18:26:43 +00002403 for (i = 0; i < 4; i++)
2404 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002405
Brian Paul32df89e2005-10-29 18:26:43 +00002406 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002407 switch (*(*inst)++) {
2408 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002409 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002410 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002411 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002412 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002413 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002414 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002415 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002416 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002417 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002418 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002419 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002420 default:
2421 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2422 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002423 }
2424 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002425}
2426
Jouk Jansen40322e12004-04-05 08:50:36 +00002427
Brian Paul32df89e2005-10-29 18:26:43 +00002428/**
2429 * Parse an extended swizzle mask which is a sequence of
2430 * four x/y/z/w/0/1 tokens.
2431 * \return swizzle four swizzle values
2432 * \return negateMask four element bitfield
2433 */
2434static void
Brian Paula088f162006-09-05 23:08:51 +00002435parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002436 GLubyte *negateMask)
2437{
2438 GLint i;
2439
2440 *negateMask = 0x0;
2441 for (i = 0; i < 4; i++) {
2442 GLubyte swz;
2443 if (parse_sign(inst) == -1)
2444 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002445
2446 swz = *(*inst)++;
2447
2448 switch (swz) {
2449 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002450 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002451 break;
2452 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002453 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002454 break;
2455 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002456 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002457 break;
2458 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002459 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002460 break;
2461 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002462 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002463 break;
2464 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002465 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002466 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002467 default:
2468 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2469 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002470 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002471 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002472}
2473
2474
2475static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002476parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2477 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002478 struct arb_program *Program,
Brian Paulb4026d92009-03-07 12:02:52 -07002479 gl_register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002480 GLboolean *IsRelOffset )
2481{
2482 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002483 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002484 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002485
Keith Whitwell7c26b612005-04-21 14:46:57 +00002486 *IsRelOffset = 0;
2487
Jouk Jansen40322e12004-04-05 08:50:36 +00002488 /* And the binding for the src */
2489 switch (*(*inst)++) {
2490 case REGISTER_ATTRIB:
2491 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002492 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002493 return 1;
2494 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002495 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002496
2497 /* We need to insert a dummy variable into the var_cache so we can
2498 * catch generic vertex attrib aliasing errors
2499 */
2500 var_cache_create(&src);
2501 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002502 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002503 src->attrib_binding = binding;
2504 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002505 var_cache_append(vc_head, src);
2506 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002507 program_error(ctx, Program->Position,
2508 "Cannot use both a generic vertex attribute "
2509 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002510 return 1;
2511 }
2512 break;
2513
2514 case REGISTER_PARAM:
2515 switch (**inst) {
2516 case PARAM_ARRAY_ELEMENT:
2517 (*inst)++;
2518 src = parse_string (inst, vc_head, Program, &found);
2519 Program->Position = parse_position (inst);
2520
2521 if (!found) {
Brianab31a3a2007-09-13 11:41:49 -06002522 program_error2(ctx, Program->Position,
2523 "Undefined variable",
2524 (char *) src->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002525 return 1;
2526 }
2527
Brian Paulb4026d92009-03-07 12:02:52 -07002528 *File = (gl_register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002529
2530 switch (*(*inst)++) {
2531 case ARRAY_INDEX_ABSOLUTE:
2532 offset = parse_integer (inst, Program);
2533
2534 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002535 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002536 program_error(ctx, Program->Position,
2537 "Index out of range");
2538 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002539 return 1;
2540 }
2541
2542 *Index = src->param_binding_begin + offset;
2543 break;
2544
2545 case ARRAY_INDEX_RELATIVE:
2546 {
2547 GLint addr_reg_idx, rel_off;
2548
2549 /* First, grab the address regiseter */
2550 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2551 return 1;
2552
2553 /* And the .x */
2554 ((*inst)++);
2555 ((*inst)++);
2556 ((*inst)++);
2557 ((*inst)++);
2558
2559 /* Then the relative offset */
2560 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2561
2562 /* And store it properly */
2563 *Index = src->param_binding_begin + rel_off;
2564 *IsRelOffset = 1;
2565 }
2566 break;
2567 }
2568 break;
2569
2570 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002571 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2572 return 1;
2573
Brian Paulb4026d92009-03-07 12:02:52 -07002574 *File = (gl_register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002575 *Index = src->param_binding_begin;
2576 break;
2577 }
2578 break;
2579
2580 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002581 src = parse_string (inst, vc_head, Program, &found);
2582 Program->Position = parse_position (inst);
2583
2584 /* If the name has never been added to our symbol table, we're hosed */
2585 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002586 program_error(ctx, Program->Position,
2587 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002588 return 1;
2589 }
2590
2591 switch (src->type) {
2592 case vt_attrib:
2593 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002594 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002595 break;
2596
2597 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2598 case vt_param:
Brian Paulb4026d92009-03-07 12:02:52 -07002599 *File = (gl_register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002600 *Index = src->param_binding_begin;
2601 break;
2602
2603 case vt_temp:
2604 *File = PROGRAM_TEMPORARY;
2605 *Index = src->temp_binding;
2606 break;
2607
2608 /* If the var type is vt_output no go */
2609 default:
Brian Paula088f162006-09-05 23:08:51 +00002610 program_error(ctx, Program->Position,
2611 "destination register is read only");
2612 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002613 return 1;
2614 }
2615 break;
2616
2617 default:
Brian Paula088f162006-09-05 23:08:51 +00002618 program_error(ctx, Program->Position,
2619 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002620 return 1;
2621 }
2622
Brian Paul0576e832008-09-19 13:04:52 +02002623 if (*File == PROGRAM_STATE_VAR) {
Brian Paulb4026d92009-03-07 12:02:52 -07002624 gl_register_file file;
Michal Krol8701e5f2008-09-19 19:11:37 +02002625
Brian Paul0576e832008-09-19 13:04:52 +02002626 /* If we're referencing the Program->Parameters[] array, check if the
2627 * parameter is really a constant/literal. If so, set File to CONSTANT.
2628 */
Michal Krola77976d2008-10-01 19:36:04 +02002629 assert(*Index < (GLint) Program->Base.Parameters->NumParameters);
Michal Krol8701e5f2008-09-19 19:11:37 +02002630 file = Program->Base.Parameters->Parameters[*Index].Type;
Brian Paul0576e832008-09-19 13:04:52 +02002631 if (file == PROGRAM_CONSTANT)
2632 *File = PROGRAM_CONSTANT;
2633 }
2634
Markus Amslerf2b91422008-03-17 08:35:27 -06002635 /* Add attributes to InputsRead only if they are used the program.
2636 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2637 if (*File == PROGRAM_INPUT)
2638 Program->Base.InputsRead |= (1 << *Index);
2639
Jouk Jansen40322e12004-04-05 08:50:36 +00002640 return 0;
2641}
2642
Brian7d2b6a02008-03-27 16:17:37 -06002643
Jouk Jansen40322e12004-04-05 08:50:36 +00002644/**
Brian7d2b6a02008-03-27 16:17:37 -06002645 * Parse vertex/fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002646 */
2647static GLuint
Brian7d2b6a02008-03-27 16:17:37 -06002648parse_vector_src_reg(GLcontext *ctx, const GLubyte **inst,
2649 struct var_cache **vc_head,
2650 struct arb_program *program,
2651 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002652{
Brian Paulb4026d92009-03-07 12:02:52 -07002653 gl_register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002654 GLint index;
Brian7d2b6a02008-03-27 16:17:37 -06002655 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002656 GLubyte swizzle[4];
2657 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002658
Jouk Jansen40322e12004-04-05 08:50:36 +00002659 /* Grab the sign */
Brian7d2b6a02008-03-27 16:17:37 -06002660 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002661
2662 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002663 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002664 return 1;
2665
2666 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002667 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002668
Brian Paul32df89e2005-10-29 18:26:43 +00002669 reg->File = file;
2670 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002671 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Brian7d2b6a02008-03-27 16:17:37 -06002672 reg->NegateBase = negateMask;
2673 reg->RelAddr = isRelOffset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002674 return 0;
2675}
2676
Jouk Jansen40322e12004-04-05 08:50:36 +00002677
Brian Paulb7fc1c32006-08-30 23:38:03 +00002678/**
Brian7d2b6a02008-03-27 16:17:37 -06002679 * Parse vertex/fragment program scalar source register.
2680 */
2681static GLuint
2682parse_scalar_src_reg(GLcontext *ctx, const GLubyte **inst,
2683 struct var_cache **vc_head,
2684 struct arb_program *program,
2685 struct prog_src_register *reg)
2686{
Brian Paulb4026d92009-03-07 12:02:52 -07002687 gl_register_file file;
Brian7d2b6a02008-03-27 16:17:37 -06002688 GLint index;
2689 GLubyte negateMask;
2690 GLubyte swizzle[4];
2691 GLboolean isRelOffset;
2692
2693 /* Grab the sign */
2694 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
2695
2696 /* And the src reg */
2697 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
2698 return 1;
2699
2700 /* finally, the swizzle */
2701 parse_swizzle_mask(inst, swizzle, 1);
2702
2703 reg->File = file;
2704 reg->Index = index;
2705 reg->Swizzle = (swizzle[0] << 0);
2706 reg->NegateBase = negateMask;
2707 reg->RelAddr = isRelOffset;
2708 return 0;
2709}
2710
2711
2712/**
2713 * Parse vertex/fragment program destination register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002714 * \return 1 if error, 0 if no error.
2715 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002716static GLuint
Brian7d2b6a02008-03-27 16:17:37 -06002717parse_dst_reg(GLcontext * ctx, const GLubyte ** inst,
2718 struct var_cache **vc_head, struct arb_program *program,
2719 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002720{
Brian Paul7aebaf32005-10-30 21:23:23 +00002721 GLint mask;
2722 GLuint idx;
Brian Paulb4026d92009-03-07 12:02:52 -07002723 gl_register_file file;
Brian Paul7aebaf32005-10-30 21:23:23 +00002724
Brian7d2b6a02008-03-27 16:17:37 -06002725 if (parse_masked_dst_reg (ctx, inst, vc_head, program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002726 return 1;
2727
Keith Whitwell7c26b612005-04-21 14:46:57 +00002728 reg->File = file;
2729 reg->Index = idx;
2730 reg->WriteMask = mask;
2731 return 0;
2732}
2733
2734
Brian Paul7aebaf32005-10-30 21:23:23 +00002735/**
Jouk Jansen40322e12004-04-05 08:50:36 +00002736 * This is a big mother that handles getting opcodes into the instruction
2737 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002738 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002739 */
2740static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002741parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002742 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002743 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002744{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002745 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002746 GLuint texcoord;
2747 GLubyte instClass, type, code;
2748 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002749 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002750
Brian Pauld6272e02006-10-29 18:03:16 +00002751 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002752
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 /* OP_ALU_INST or OP_TEX_INST */
2754 instClass = *(*inst)++;
2755
2756 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2757 * OP_TEX_{SAMPLE, KIL}
2758 */
2759 type = *(*inst)++;
2760
2761 /* The actual opcode name */
2762 code = *(*inst)++;
2763
2764 /* Increment the correct count */
2765 switch (instClass) {
2766 case OP_ALU_INST:
2767 Program->NumAluInstructions++;
2768 break;
2769 case OP_TEX_INST:
2770 Program->NumTexInstructions++;
2771 break;
2772 }
2773
Jouk Jansen40322e12004-04-05 08:50:36 +00002774 switch (type) {
2775 case OP_ALU_VECTOR:
2776 switch (code) {
2777 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002778 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002779 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002780 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002781 break;
2782
2783 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002784 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002786 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 break;
2788
2789 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002790 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002792 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 break;
2794
2795 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002796 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002798 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002799 break;
2800
2801 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002802 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002803 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002804 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002805 break;
2806 }
2807
Brian7d2b6a02008-03-27 16:17:37 -06002808 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002809 return 1;
2810
Brian7d2b6a02008-03-27 16:17:37 -06002811 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002812 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 break;
2814
2815 case OP_ALU_SCALAR:
2816 switch (code) {
2817 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002818 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002820 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002821 break;
2822
2823 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002824 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002826 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002827 break;
2828
2829 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002830 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002832 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002833 break;
2834
2835 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002836 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002837 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002838 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002839 break;
2840
2841 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002842 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002843 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002844 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 break;
2846
2847 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002848 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002850 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002851 break;
2852
2853 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002854 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 case OP_SCS:
2856
Brian Paul7e807512005-11-05 17:10:45 +00002857 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002858 break;
2859 }
2860
Brian7d2b6a02008-03-27 16:17:37 -06002861 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002862 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002863
Brian7d2b6a02008-03-27 16:17:37 -06002864 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002865 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002866 break;
2867
2868 case OP_ALU_BINSC:
2869 switch (code) {
2870 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002871 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002872 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002873 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002874 break;
2875 }
2876
Brian7d2b6a02008-03-27 16:17:37 -06002877 if (parse_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002878 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002879
Jouk Jansen40322e12004-04-05 08:50:36 +00002880 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002881 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002882 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 }
2884 break;
2885
2886
2887 case OP_ALU_BIN:
2888 switch (code) {
2889 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002890 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002891 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002892 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 break;
2894
2895 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002896 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002898 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 break;
2900
2901 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002902 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002903 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002904 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002905 break;
2906
2907 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002908 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002909 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002910 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002911 break;
2912
2913 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002914 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002915 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002916 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002917 break;
2918
2919 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002920 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002921 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002922 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002923 break;
2924
2925 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002926 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002927 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002928 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002929 break;
2930
2931 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002932 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002933 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002934 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002935 break;
2936
2937 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002938 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002939 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002940 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002941 break;
2942
2943 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002944 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002945 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002946 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002947 break;
2948
2949 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002950 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002951 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002952 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002953 break;
2954
2955 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002956 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002957 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002958 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002959 break;
2960 }
2961
Brian7d2b6a02008-03-27 16:17:37 -06002962 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002963 return 1;
2964 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002965 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002966 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002967 }
2968 break;
2969
2970 case OP_ALU_TRI:
2971 switch (code) {
2972 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002973 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002974 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002975 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002976 break;
2977
2978 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002979 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002980 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002981 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002982 break;
2983
2984 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002985 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002986 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002987 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002988 break;
2989 }
2990
Brian7d2b6a02008-03-27 16:17:37 -06002991 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002992 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002993
Jouk Jansen40322e12004-04-05 08:50:36 +00002994 for (a = 0; a < 3; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002995 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002996 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002997 }
2998 break;
2999
3000 case OP_ALU_SWZ:
3001 switch (code) {
3002 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003003 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003004 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003005 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003006 break;
3007 }
Brian7d2b6a02008-03-27 16:17:37 -06003008 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 return 1;
3010
Keith Whitwell7c26b612005-04-21 14:46:57 +00003011 {
Brian Paul32df89e2005-10-29 18:26:43 +00003012 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00003013 GLubyte negateMask;
Brian Paulb4026d92009-03-07 12:02:52 -07003014 gl_register_file file;
Brian Paul7aebaf32005-10-30 21:23:23 +00003015 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003016
Brian Paul32df89e2005-10-29 18:26:43 +00003017 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003018 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003019 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
3020 fp->SrcReg[0].File = file;
3021 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00003022 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003023 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3024 swizzle[1],
3025 swizzle[2],
3026 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003027 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003028 break;
3029
3030 case OP_TEX_SAMPLE:
3031 switch (code) {
3032 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003033 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003034 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00003035 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003036 break;
3037
3038 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003039 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003040 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003041 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003042 break;
3043
3044 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003045 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003046 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003047 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003048 break;
3049 }
3050
Brian7d2b6a02008-03-27 16:17:37 -06003051 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003052 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003053
Brian7d2b6a02008-03-27 16:17:37 -06003054 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003055 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003056
3057 /* texImageUnit */
Ian Romanick2549c262009-01-14 10:05:40 -08003058 if (parse_teximage_num (ctx, inst, Program, &texcoord))
Jouk Jansen40322e12004-04-05 08:50:36 +00003059 return 1;
3060 fp->TexSrcUnit = texcoord;
3061
3062 /* texTarget */
3063 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07003064 case TEXTARGET_SHADOW1D:
3065 shadow_tex = 1 << texcoord;
3066 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003067 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003068 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003069 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003070 case TEXTARGET_SHADOW2D:
3071 shadow_tex = 1 << texcoord;
3072 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003073 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003074 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003075 break;
3076 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003077 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003078 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003079 case TEXTARGET_SHADOWRECT:
3080 shadow_tex = 1 << texcoord;
3081 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003082 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003083 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003084 break;
3085 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003086 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003087 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003088 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003089 shadow_tex = 1 << texcoord;
3090 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003091 case TEXTARGET_1D_ARRAY:
3092 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3093 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003094 case TEXTARGET_SHADOW2D_ARRAY:
3095 shadow_tex = 1 << texcoord;
3096 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003097 case TEXTARGET_2D_ARRAY:
3098 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3099 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003100 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003101
Brian Paul44e018c2009-02-20 13:42:08 -07003102 if (shadow_tex)
3103 fp->TexShadow = 1;
3104
Ian Romanick7b559a92007-06-07 13:58:50 -07003105 /* Don't test the first time a particular sampler is seen. Each time
3106 * after that, make sure the shadow state is the same.
3107 */
3108 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3109 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3110 program_error(ctx, Program->Position,
3111 "texture image unit used for shadow sampling and non-shadow sampling");
3112 return 1;
3113 }
3114
Brian Paulb7fc1c32006-08-30 23:38:03 +00003115 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3116 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3117 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003118 program_error(ctx, Program->Position,
3119 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003120 return 1;
3121 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003122
3123
3124 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003125 break;
3126
3127 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003128 Program->UsesKill = 1;
Brian7d2b6a02008-03-27 16:17:37 -06003129 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003130 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003131 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003132 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003133 default:
3134 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3135 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003136 }
3137
3138 return 0;
3139}
3140
Keith Whitwell7c26b612005-04-21 14:46:57 +00003141
3142/**
3143 * Handle the parsing out of a masked address register
3144 *
3145 * \param Index - The register index we write to
3146 * \param WriteMask - The mask controlling which components we write (1->write)
3147 *
3148 * \return 0 on sucess, 1 on error
3149 */
3150static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003151parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003152 struct var_cache **vc_head,
3153 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003154 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003155{
3156 GLint idx;
3157
3158 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3159 return 1;
3160
3161 /* This should be 0x8 */
3162 (*inst)++;
3163
3164 reg->File = PROGRAM_ADDRESS;
3165 reg->Index = idx;
3166
3167 /* Writemask of .x is implied */
3168 reg->WriteMask = 0x1;
3169 return 0;
3170}
3171
Keith Whitwell7c26b612005-04-21 14:46:57 +00003172
Jouk Jansen40322e12004-04-05 08:50:36 +00003173/**
3174 * This is a big mother that handles getting opcodes into the instruction
3175 * and handling the src & dst registers for vertex program instructions
3176 */
3177static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003178parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003179 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003180 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003181{
3182 GLint a;
3183 GLubyte type, code;
3184
3185 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3186 type = *(*inst)++;
3187
3188 /* The actual opcode name */
3189 code = *(*inst)++;
3190
Brian Pauld6272e02006-10-29 18:03:16 +00003191 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003192
3193 switch (type) {
3194 /* XXX: */
3195 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003196 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003197
3198 /* Remember to set SrcReg.RelAddr; */
3199
3200 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003201 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003202 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003203
Jouk Jansen40322e12004-04-05 08:50:36 +00003204 vp->DstReg.File = PROGRAM_ADDRESS;
3205
3206 /* Get a scalar src register */
Brian7d2b6a02008-03-27 16:17:37 -06003207 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003208 return 1;
3209
3210 break;
3211
3212 case OP_ALU_VECTOR:
3213 switch (code) {
3214 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003215 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003216 break;
3217 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003218 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003219 break;
3220 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003221 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003222 break;
3223 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003224 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003225 break;
3226 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003227 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 break;
3229 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003230
Brian7d2b6a02008-03-27 16:17:37 -06003231 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003232 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003233
Brian7d2b6a02008-03-27 16:17:37 -06003234 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 return 1;
3236 break;
3237
3238 case OP_ALU_SCALAR:
3239 switch (code) {
3240 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003241 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003242 break;
3243 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003244 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 break;
3246 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003247 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 break;
3249 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003250 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 break;
3252 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003253 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 break;
3255 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003256 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003257 break;
3258 }
Brian7d2b6a02008-03-27 16:17:37 -06003259 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003260 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003261
Brian7d2b6a02008-03-27 16:17:37 -06003262 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003263 return 1;
3264 break;
3265
3266 case OP_ALU_BINSC:
3267 switch (code) {
3268 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003269 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003270 break;
3271 }
Brian7d2b6a02008-03-27 16:17:37 -06003272 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003273 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003274
Jouk Jansen40322e12004-04-05 08:50:36 +00003275 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003276 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003277 return 1;
3278 }
3279 break;
3280
3281 case OP_ALU_BIN:
3282 switch (code) {
3283 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003284 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003285 break;
3286 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003287 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 break;
3289 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003290 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003291 break;
3292 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003293 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 break;
3295 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003296 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 break;
3298 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003299 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 break;
3301 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003302 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003303 break;
3304 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003305 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003306 break;
3307 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003308 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 break;
3310 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003311 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003312 break;
3313 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003314 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003315 break;
3316 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003317 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003318 break;
3319 }
Brian7d2b6a02008-03-27 16:17:37 -06003320 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003321 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003322
Jouk Jansen40322e12004-04-05 08:50:36 +00003323 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003324 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003325 return 1;
3326 }
3327 break;
3328
3329 case OP_ALU_TRI:
3330 switch (code) {
3331 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003332 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003333 break;
3334 }
3335
Brian7d2b6a02008-03-27 16:17:37 -06003336 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003337 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003338
Jouk Jansen40322e12004-04-05 08:50:36 +00003339 for (a = 0; a < 3; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003340 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003341 return 1;
3342 }
3343 break;
3344
3345 case OP_ALU_SWZ:
3346 switch (code) {
3347 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003348 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003349 break;
3350 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003351 {
Brian Paul32df89e2005-10-29 18:26:43 +00003352 GLubyte swizzle[4];
3353 GLubyte negateMask;
3354 GLboolean relAddr;
Brian Paulb4026d92009-03-07 12:02:52 -07003355 gl_register_file file;
Brian Paul7aebaf32005-10-30 21:23:23 +00003356 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003357
Brian7d2b6a02008-03-27 16:17:37 -06003358 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003359 return 1;
3360
Brian Paul32df89e2005-10-29 18:26:43 +00003361 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003362 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003363 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3364 vp->SrcReg[0].File = file;
3365 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003366 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003367 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3368 swizzle[1],
3369 swizzle[2],
3370 swizzle[3]);
3371 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003372 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003373 break;
3374 }
3375 return 0;
3376}
3377
3378#if DEBUG_PARSING
3379
3380static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003381debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3382 struct arb_program *Program)
3383{
3384 struct var_cache *vc;
3385 GLint a, b;
3386
Brianb618ac82007-02-22 09:39:25 -07003387 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003388
3389 /* First of all, print out the contents of the var_cache */
3390 vc = vc_head;
3391 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003392 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003393 switch (vc->type) {
3394 case vt_none:
3395 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3396 break;
3397 case vt_attrib:
3398 fprintf (stderr, "ATTRIB %s\n", vc->name);
3399 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3400 break;
3401 case vt_param:
3402 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3403 vc->param_binding_begin, vc->param_binding_length);
3404 b = vc->param_binding_begin;
3405 for (a = 0; a < vc->param_binding_length; a++) {
3406 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003407 Program->Base.Parameters->Parameters[a + b].Name);
3408 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
Michal Krolce3cf632008-09-05 12:25:50 +02003409 char *s;
Brianb618ac82007-02-22 09:39:25 -07003410 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3411 [a + b].StateIndexes);
3412 fprintf(stderr, "%s\n", s);
Michal Krolce3cf632008-09-05 12:25:50 +02003413 _mesa_free(s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003414 }
3415 else
3416 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003417 Program->Base.Parameters->ParameterValues[a + b][0],
3418 Program->Base.Parameters->ParameterValues[a + b][1],
3419 Program->Base.Parameters->ParameterValues[a + b][2],
3420 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003421 }
3422 break;
3423 case vt_temp:
3424 fprintf (stderr, "TEMP %s\n", vc->name);
3425 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3426 break;
3427 case vt_output:
3428 fprintf (stderr, "OUTPUT %s\n", vc->name);
3429 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3430 break;
3431 case vt_alias:
3432 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003433 fprintf (stderr, " binding: 0x%p (%s)\n",
3434 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003435 break;
Brianb618ac82007-02-22 09:39:25 -07003436 default:
3437 /* nothing */
3438 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003439 }
3440 vc = vc->next;
3441 }
3442}
3443
Brian Paul72030e02005-11-03 03:30:34 +00003444#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003445
3446
3447/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003448 * The main loop for parsing a fragment or vertex program
3449 *
Brian Paul72030e02005-11-03 03:30:34 +00003450 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003451 */
Brian Paul72030e02005-11-03 03:30:34 +00003452static GLint
Brian Paula088f162006-09-05 23:08:51 +00003453parse_instructions(GLcontext * ctx, const GLubyte * inst,
3454 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003455{
Brian Paul72030e02005-11-03 03:30:34 +00003456 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3457 ? ctx->Const.FragmentProgram.MaxInstructions
3458 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003459 GLint err = 0;
3460
Jonathan Whitee3378792008-09-30 15:38:38 -06003461 ASSERT(MAX_PROGRAM_INSTRUCTIONS >= maxInst);
Brian Paul72030e02005-11-03 03:30:34 +00003462
Jouk Jansen40322e12004-04-05 08:50:36 +00003463 Program->MajorVersion = (GLuint) * inst++;
3464 Program->MinorVersion = (GLuint) * inst++;
3465
3466 while (*inst != END) {
3467 switch (*inst++) {
3468
3469 case OPTION:
3470 switch (*inst++) {
3471 case ARB_PRECISION_HINT_FASTEST:
3472 Program->PrecisionOption = GL_FASTEST;
3473 break;
3474
3475 case ARB_PRECISION_HINT_NICEST:
3476 Program->PrecisionOption = GL_NICEST;
3477 break;
3478
3479 case ARB_FOG_EXP:
3480 Program->FogOption = GL_EXP;
3481 break;
3482
3483 case ARB_FOG_EXP2:
3484 Program->FogOption = GL_EXP2;
3485 break;
3486
3487 case ARB_FOG_LINEAR:
3488 Program->FogOption = GL_LINEAR;
3489 break;
3490
3491 case ARB_POSITION_INVARIANT:
3492 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003493 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003494 break;
3495
3496 case ARB_FRAGMENT_PROGRAM_SHADOW:
3497 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3498 /* TODO ARB_fragment_program_shadow code */
3499 }
3500 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003501
3502 case ARB_DRAW_BUFFERS:
3503 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3504 /* do nothing for now */
3505 }
3506 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003507
3508 case MESA_TEXTURE_ARRAY:
3509 /* do nothing for now */
3510 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003511 }
3512 break;
3513
3514 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003515 /* check length */
3516 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003517 program_error(ctx, Program->Position,
3518 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003519 return 1;
3520 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003521 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003522 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003523 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003524 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003525 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003526 }
3527 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003528 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003529 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003530 }
3531
Brian Paul72030e02005-11-03 03:30:34 +00003532 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003533 Program->Base.NumInstructions++;
3534 break;
3535
3536 case DECLARATION:
3537 err = parse_declaration (ctx, &inst, vc_head, Program);
3538 break;
3539
3540 default:
3541 break;
3542 }
3543
3544 if (err)
3545 break;
3546 }
3547
3548 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003549 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003550 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003551 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003552 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003553 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003554 Program->Base.NumInstructions++;
3555
Brian Paul05051032005-11-01 04:36:33 +00003556 /*
3557 * Initialize native counts to logical counts. The device driver may
3558 * change them if program is translated into a hardware program.
3559 */
3560 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3561 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3562 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3563 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3564 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003565
Jouk Jansen40322e12004-04-05 08:50:36 +00003566 return err;
3567}
3568
Brian Paul7aebaf32005-10-30 21:23:23 +00003569
Jouk Jansen40322e12004-04-05 08:50:36 +00003570/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003571LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003572#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003573;
3574
Brian Paul7aebaf32005-10-30 21:23:23 +00003575
Brian Paul8c41a142005-11-19 15:36:28 +00003576/**
3577 * Set a grammar parameter.
3578 * \param name the grammar parameter
3579 * \param value the new parameter value
3580 * \return 0 if OK, 1 if error
3581 */
3582static int
3583set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003584{
3585 char error_msg[300];
3586 GLint error_pos;
3587
Brian Paul8c41a142005-11-19 15:36:28 +00003588 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003589 return 0;
3590
3591 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3592 _mesa_set_program_error (ctx, error_pos, error_msg);
3593 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3594 return 1;
3595}
3596
Brian Paul8c41a142005-11-19 15:36:28 +00003597
3598/**
3599 * Enable support for the given language option in the parser.
3600 * \return 1 if OK, 0 if error
3601 */
3602static int
3603enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003604{
Brian Paul8c41a142005-11-19 15:36:28 +00003605 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003606}
3607
Brian Paul8c41a142005-11-19 15:36:28 +00003608
3609/**
3610 * Enable parser extensions based on which OpenGL extensions are supported
3611 * by this rendering context.
3612 *
3613 * \return GL_TRUE if OK, GL_FALSE if error.
3614 */
3615static GLboolean
3616enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003617{
Brian Paul8c41a142005-11-19 15:36:28 +00003618#if 0
3619 /* These are not supported at this time */
3620 if ((ctx->Extensions.ARB_vertex_blend ||
3621 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003622 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003623 return GL_FALSE;
3624 if (ctx->Extensions.ARB_matrix_palette
3625 && !enable_ext(ctx, id, "matrix_palette"))
3626 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003627#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003628 if (ctx->Extensions.ARB_fragment_program_shadow
3629 && !enable_ext(ctx, id, "fragment_program_shadow"))
3630 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003631 if (ctx->Extensions.EXT_point_parameters
3632 && !enable_ext(ctx, id, "point_parameters"))
3633 return GL_FALSE;
3634 if (ctx->Extensions.EXT_secondary_color
3635 && !enable_ext(ctx, id, "secondary_color"))
3636 return GL_FALSE;
3637 if (ctx->Extensions.EXT_fog_coord
3638 && !enable_ext(ctx, id, "fog_coord"))
3639 return GL_FALSE;
3640 if (ctx->Extensions.NV_texture_rectangle
3641 && !enable_ext(ctx, id, "texture_rectangle"))
3642 return GL_FALSE;
Ian Romanick8aa209c2009-01-27 19:10:43 -08003643 if (!enable_ext(ctx, id, "draw_buffers"))
Brian Paul8c41a142005-11-19 15:36:28 +00003644 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003645 if (ctx->Extensions.MESA_texture_array
3646 && !enable_ext(ctx, id, "texture_array"))
3647 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003648#if 1
3649 /* hack for Warcraft (see bug 8060) */
3650 enable_ext(ctx, id, "vertex_blend");
3651#endif
3652
Brian Paul8c41a142005-11-19 15:36:28 +00003653 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003654}
3655
Brian Paul8c41a142005-11-19 15:36:28 +00003656
Jouk Jansen40322e12004-04-05 08:50:36 +00003657/**
3658 * This kicks everything off.
3659 *
3660 * \param ctx - The GL Context
3661 * \param str - The program string
3662 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003663 * \param program - The arb_program struct to return all the parsed info in
3664 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003665 */
Brian Paul8c41a142005-11-19 15:36:28 +00003666static GLboolean
3667_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3668 const GLubyte *str, GLsizei len,
3669 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003670{
3671 GLint a, err, error_pos;
3672 char error_msg[300];
3673 GLuint parsed_len;
3674 struct var_cache *vc_head;
3675 grammar arbprogram_syn_id;
3676 GLubyte *parsed, *inst;
3677 GLubyte *strz = NULL;
3678 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3679
Brian Paul8c41a142005-11-19 15:36:28 +00003680 /* set the program target before parsing */
3681 program->Base.Target = target;
3682
Brian Paul7f76b8f2004-09-10 01:05:39 +00003683 /* Reset error state */
3684 _mesa_set_program_error(ctx, -1, NULL);
3685
Brian Paul8c41a142005-11-19 15:36:28 +00003686 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003687 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003688 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003689 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003690 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003691
3692 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3693 if (grammar_syn_id == 0) {
3694 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003695 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003696 _mesa_set_program_error (ctx, error_pos, error_msg);
3697 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003698 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003699 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003700 }
3701
Brian Paul8c41a142005-11-19 15:36:28 +00003702 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3703 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003704
Brian Paul49a80ca2006-04-28 15:40:11 +00003705 /* 'parsed' is unused here */
3706 _mesa_free (parsed);
3707 parsed = NULL;
3708
Brian Paul45703642005-10-29 15:52:31 +00003709 /* NOTE: we can't destroy grammar_syn_id right here because
3710 * grammar_destroy() can reset the last error
3711 */
Brian Paul8c41a142005-11-19 15:36:28 +00003712 if (err) {
3713 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003714 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3715 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003716 _mesa_error (ctx, GL_INVALID_OPERATION,
3717 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003719 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003720 }
3721
3722 grammar_destroy (grammar_syn_id);
3723
3724 arbprogram_syn_is_ok = 1;
3725 }
3726
3727 /* create the grammar object */
3728 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3729 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003730 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003731 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3732 _mesa_set_program_error (ctx, error_pos, error_msg);
3733 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003734 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003735 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003736 }
3737
3738 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003739 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003740 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3741 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003742 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003743 }
3744
Brian Paul8c41a142005-11-19 15:36:28 +00003745 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3746 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003747 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003748 }
3749
3750 /* check for NULL character occurences */
3751 {
Brian Paul8c41a142005-11-19 15:36:28 +00003752 GLint i;
3753 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003754 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003755 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003756 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003757 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003758 }
Brian Paul8c41a142005-11-19 15:36:28 +00003759 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003760 }
3761
3762 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003763 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003764 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003765 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3766 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003767 return GL_FALSE;
3768 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003769 _mesa_memcpy (strz, str, len);
3770 strz[len] = '\0';
3771
Michal Krolb80bc052004-10-21 14:09:54 +00003772 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003773 err = !grammar_fast_check(arbprogram_syn_id, strz,
3774 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003775
3776 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003777 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003778 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3779 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003780
Brian Paulb3aefd12005-09-19 20:12:32 +00003781#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003782 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003783 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003784 int line, col;
3785 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003786 fprintf(stderr, "program: %s\n", (char *) strz);
Eric Anholtb039b782008-01-15 15:09:21 -08003787 fprintf(stderr, "Error Pos: %d\n", ctx->Program.ErrorPos);
3788 s = (char *) _mesa_find_line_column(strz, strz+ctx->Program.ErrorPos,
Brian Paula088f162006-09-05 23:08:51 +00003789 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003790 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
Eric Anholtb039b782008-01-15 15:09:21 -08003791 } while (0);
Brian Paulb3aefd12005-09-19 20:12:32 +00003792#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003793
Brian Paula088f162006-09-05 23:08:51 +00003794 _mesa_free(strz);
3795 _mesa_free(parsed);
3796
Jouk Jansen40322e12004-04-05 08:50:36 +00003797 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003798 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003799 }
3800
Jouk Jansen40322e12004-04-05 08:50:36 +00003801 grammar_destroy (arbprogram_syn_id);
3802
Brian Paul8c41a142005-11-19 15:36:28 +00003803 /*
3804 * Program string is syntactically correct at this point
3805 * Parse the tokenized version of the program now, generating
3806 * vertex/fragment program instructions.
3807 */
3808
Jouk Jansen40322e12004-04-05 08:50:36 +00003809 /* Initialize the arb_program struct */
3810 program->Base.String = strz;
Jonathan Whitee3378792008-09-30 15:38:38 -06003811 program->Base.Instructions = _mesa_alloc_instructions(MAX_PROGRAM_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003812 program->Base.NumInstructions =
3813 program->Base.NumTemporaries =
3814 program->Base.NumParameters =
3815 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003816 program->Base.Parameters = _mesa_new_parameter_list ();
3817 program->Base.InputsRead = 0x0;
3818 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003819 program->Position = 0;
3820 program->MajorVersion = program->MinorVersion = 0;
3821 program->PrecisionOption = GL_DONT_CARE;
3822 program->FogOption = GL_NONE;
3823 program->HintPositionInvariant = GL_FALSE;
3824 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003825 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003826 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003827 program->NumAluInstructions =
3828 program->NumTexInstructions =
3829 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003830 program->UsesKill = 0;
3831
Jouk Jansen40322e12004-04-05 08:50:36 +00003832 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003833 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003834
3835 /* Start examining the tokens in the array */
3836 inst = parsed;
3837
3838 /* Check the grammer rev */
3839 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003840 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003841 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003842 }
3843 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003844 /* ignore program target */
3845 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003846 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003847 }
3848
3849 /*debug_variables(ctx, vc_head, program); */
3850
3851 /* We're done with the parsed binary array */
3852 var_cache_destroy (&vc_head);
3853
3854 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003855
Jonathan Whitee3378792008-09-30 15:38:38 -06003856 /* Reallocate the instruction array from size [MAX_PROGRAM_INSTRUCTIONS]
Brian Paul8c41a142005-11-19 15:36:28 +00003857 * to size [ap.Base.NumInstructions].
3858 */
Brian Paula7543902006-08-24 21:58:32 +00003859 program->Base.Instructions
3860 = _mesa_realloc_instructions(program->Base.Instructions,
Jonathan Whitee3378792008-09-30 15:38:38 -06003861 MAX_PROGRAM_INSTRUCTIONS,
Brian Paula7543902006-08-24 21:58:32 +00003862 program->Base.NumInstructions);
3863
Brian Paul45703642005-10-29 15:52:31 +00003864 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003865}
Brian Paul8c41a142005-11-19 15:36:28 +00003866
3867
3868
3869void
3870_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3871 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003872 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003873{
3874 struct arb_program ap;
3875 GLuint i;
3876
3877 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003878 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003879 /* Error in the program. Just return. */
3880 return;
3881 }
3882
3883 /* Copy the relevant contents of the arb_program struct into the
3884 * fragment_program struct.
3885 */
3886 program->Base.String = ap.Base.String;
3887 program->Base.NumInstructions = ap.Base.NumInstructions;
3888 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3889 program->Base.NumParameters = ap.Base.NumParameters;
3890 program->Base.NumAttributes = ap.Base.NumAttributes;
3891 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003892 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3893 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3894 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3895 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3896 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003897 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3898 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3899 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3900 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3901 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3902 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003903 program->Base.InputsRead = ap.Base.InputsRead;
3904 program->Base.OutputsWritten = ap.Base.OutputsWritten;
Brian Paul1af2b142008-05-14 16:44:48 -06003905 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
Brianc9db2232007-01-04 17:22:19 -07003906 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Brian Paul1af2b142008-05-14 16:44:48 -06003907 if (ap.TexturesUsed[i])
3908 program->Base.SamplersUsed |= (1 << i);
3909 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003910 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003911 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003912 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003913
Brian Paulf8baad22008-10-06 12:29:29 -06003914 if (program->FogOption)
3915 program->Base.InputsRead |= FRAG_BIT_FOGC;
3916
Brian Paul8c41a142005-11-19 15:36:28 +00003917 if (program->Base.Instructions)
3918 _mesa_free(program->Base.Instructions);
3919 program->Base.Instructions = ap.Base.Instructions;
3920
3921 if (program->Base.Parameters)
3922 _mesa_free_parameter_list(program->Base.Parameters);
3923 program->Base.Parameters = ap.Base.Parameters;
3924
Brian Paulc0ef1662008-03-25 11:32:31 -06003925 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3926 * or similar. We used to leave this up to drivers, but it appears
3927 * there's no hardware that wants to do fog in a discrete stage separate
3928 * from the fragment shader.
3929 */
3930 if (program->FogOption != GL_NONE) {
3931 _mesa_append_fog_code(ctx, program);
3932 program->FogOption = GL_NONE;
3933 }
3934
Brian Paul8c41a142005-11-19 15:36:28 +00003935#if DEBUG_FP
Zack Rusin74964ff2008-06-10 16:59:44 -04003936 _mesa_printf("____________Fragment program %u ________\n", program->Base.Id);
Brian Paul11a54c32006-11-15 19:54:25 +00003937 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003938#endif
3939}
3940
3941
3942
3943/**
3944 * Parse the vertex program string. If success, update the given
3945 * vertex_program object with the new program. Else, leave the vertex_program
3946 * object unchanged.
3947 */
3948void
3949_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3950 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003951 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003952{
3953 struct arb_program ap;
3954
3955 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3956
Brian Paul95801792005-12-06 15:41:43 +00003957 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian3075f262008-02-20 08:53:41 -07003958 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
Brian Paul8c41a142005-11-19 15:36:28 +00003959 return;
3960 }
3961
3962 /* Copy the relevant contents of the arb_program struct into the
3963 * vertex_program struct.
3964 */
3965 program->Base.String = ap.Base.String;
3966 program->Base.NumInstructions = ap.Base.NumInstructions;
3967 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3968 program->Base.NumParameters = ap.Base.NumParameters;
3969 program->Base.NumAttributes = ap.Base.NumAttributes;
3970 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003971 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3972 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3973 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3974 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3975 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003976 program->Base.InputsRead = ap.Base.InputsRead;
3977 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3978 program->IsPositionInvariant = ap.HintPositionInvariant;
3979
3980 if (program->Base.Instructions)
3981 _mesa_free(program->Base.Instructions);
3982 program->Base.Instructions = ap.Base.Instructions;
3983
3984 if (program->Base.Parameters)
3985 _mesa_free_parameter_list(program->Base.Parameters);
3986 program->Base.Parameters = ap.Base.Parameters;
3987
3988#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01003989 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00003990 _mesa_print_program(&program->Base);
3991#endif
3992}