blob: 38e174f3adc9a8706ce1511377e8ad6c39c2de35 [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian50a20d42008-03-27 15:51:07 -06003 * Version: 7.1
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Brianb7978af2007-01-09 19:17:17 -07005 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Jouk Jansen40322e12004-04-05 08:50:36 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#define DEBUG_PARSING 0
26
27/**
28 * \file arbprogparse.c
29 * ARB_*_program parser core
30 * \author Karl Rasche
31 */
32
Brianc223c6b2007-07-04 13:15:20 -060033#include "main/glheader.h"
34#include "main/imports.h"
35#include "shader/grammar/grammar_mesa.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000036#include "arbprogparse.h"
Brian Paul32df89e2005-10-29 18:26:43 +000037#include "program.h"
Brian Paulc0ef1662008-03-25 11:32:31 -060038#include "programopt.h"
Brianc0551f02006-12-14 15:02:37 -070039#include "prog_parameter.h"
40#include "prog_statevars.h"
Brian Paul8c41a142005-11-19 15:36:28 +000041#include "context.h"
Brian Paul6211a142006-08-24 23:37:13 +000042#include "macros.h"
Brian Paul8c41a142005-11-19 15:36:28 +000043#include "mtypes.h"
Brianc0551f02006-12-14 15:02:37 -070044#include "prog_instruction.h"
Brian Paul8c41a142005-11-19 15:36:28 +000045
46
Brian Paul6211a142006-08-24 23:37:13 +000047/* For ARB programs, use the NV instruction limits */
48#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
49 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000050
51
52/**
53 * This is basically a union of the vertex_program and fragment_program
54 * structs that we can use to parse the program into
55 *
56 * XXX we can probably get rid of this entirely someday.
57 */
58struct arb_program
59{
Brian Paul122629f2006-07-20 16:49:57 +000060 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000061
62 GLuint Position; /* Just used for error reporting while parsing */
63 GLuint MajorVersion;
64 GLuint MinorVersion;
65
66 /* ARB_vertex_progmra options */
67 GLboolean HintPositionInvariant;
68
69 /* ARB_fragment_progmra options */
70 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
71 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
72
73 /* ARB_fragment_program specifics */
74 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
Ian Romanick7b559a92007-06-07 13:58:50 -070075 GLbitfield ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +000076 GLuint NumAluInstructions;
77 GLuint NumTexInstructions;
78 GLuint NumTexIndirections;
79
80 GLboolean UsesKill;
81};
82
Ian Romanick9bdfee32005-07-18 12:31:24 +000083
Brian Paula6c423d2004-08-25 15:59:48 +000084
Jouk Jansen40322e12004-04-05 08:50:36 +000085/* TODO:
86 * Fragment Program Stuff:
87 * -----------------------------------------------------
88 *
89 * - things from Michal's email
90 * + overflow on atoi
91 * + not-overflowing floats (don't use parse_integer..)
92 * + can remove range checking in arbparse.c
93 *
94 * - check all limits of number of various variables
95 * + parameters
96 *
97 * - test! test! test!
98 *
99 * Vertex Program Stuff:
100 * -----------------------------------------------------
101 * - Optimize param array usage and count limits correctly, see spec,
102 * section 2.14.3.7
103 * + Record if an array is reference absolutly or relatively (or both)
104 * + For absolute arrays, store a bitmap of accesses
105 * + For single parameters, store an access flag
106 * + After parsing, make a parameter cleanup and merging pass, where
107 * relative arrays are layed out first, followed by abs arrays, and
108 * finally single state.
109 * + Remap offsets for param src and dst registers
110 * + Now we can properly count parameter usage
111 *
112 * - Multiple state binding errors in param arrays (see spec, just before
113 * section 2.14.3.3)
114 * - grep for XXX
115 *
116 * Mesa Stuff
117 * -----------------------------------------------------
118 * - User clipping planes vs. PositionInvariant
119 * - Is it sufficient to just multiply by the mvp to transform in the
120 * PositionInvariant case? Or do we need something more involved?
121 *
122 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
123 * - fetch state listed in program_parameters list
124 * + WTF should this go???
125 * + currently in nvvertexec.c and s_nvfragprog.c
126 *
127 * - allow for multiple address registers (and fetch address regs properly)
128 *
129 * Cosmetic Stuff
130 * -----------------------------------------------------
131 * - remove any leftover unused grammer.c stuff (dict_ ?)
132 * - fix grammer.c error handling so its not static
133 * - #ifdef around stuff pertaining to extentions
134 *
135 * Outstanding Questions:
136 * -----------------------------------------------------
137 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
138 * what gets hacked off because of this:
139 * + VERTEX_ATTRIB_MATRIXINDEX
140 * + VERTEX_ATTRIB_WEIGHT
141 * + MATRIX_MODELVIEW
142 * + MATRIX_PALETTE
143 *
144 * - When can we fetch env/local params from their own register files, and
145 * when to we have to fetch them into the main state register file?
146 * (think arrays)
147 *
148 * Grammar Changes:
149 * -----------------------------------------------------
150 */
151
152/* Changes since moving the file to shader directory
153
1542004-III-4 ------------------------------------------------------------
155- added #include "grammar_mesa.h"
156- removed grammar specific code part (it resides now in grammar.c)
157- added GL_ARB_fragment_program_shadow tokens
158- modified #include "arbparse_syn.h"
159- major changes inside _mesa_parse_arb_program()
160- check the program string for '\0' characters
161- copy the program string to a one-byte-longer location to have
162 it null-terminated
163- position invariance test (not writing to result.position) moved
164 to syntax part
165*/
166
167typedef GLubyte *production;
168
Brian Paul11a54c32006-11-15 19:54:25 +0000169
Jouk Jansen40322e12004-04-05 08:50:36 +0000170/**
171 * This is the text describing the rules to parse the grammar
172 */
Brian Paul11a54c32006-11-15 19:54:25 +0000173LONGSTRING static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000174#include "arbprogram_syn.h"
175;
176
177/**
178 * These should match up with the values defined in arbprogram.syn
179 */
180
181/*
182 Changes:
183 - changed and merged V_* and F_* opcode values to OP_*.
184 - added GL_ARB_fragment_program_shadow specific tokens (michal)
185*/
Ian Romanickbb372f12007-05-16 15:34:22 -0700186#define REVISION 0x0a
Jouk Jansen40322e12004-04-05 08:50:36 +0000187
188/* program type */
189#define FRAGMENT_PROGRAM 0x01
190#define VERTEX_PROGRAM 0x02
191
192/* program section */
193#define OPTION 0x01
194#define INSTRUCTION 0x02
195#define DECLARATION 0x03
196#define END 0x04
197
Michal Krolb80bc052004-10-21 14:09:54 +0000198/* GL_ARB_fragment_program option */
199#define ARB_PRECISION_HINT_FASTEST 0x00
200#define ARB_PRECISION_HINT_NICEST 0x01
201#define ARB_FOG_EXP 0x02
202#define ARB_FOG_EXP2 0x03
203#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000204
Michal Krolb80bc052004-10-21 14:09:54 +0000205/* GL_ARB_vertex_program option */
206#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000207
Michal Krolb80bc052004-10-21 14:09:54 +0000208/* GL_ARB_fragment_program_shadow option */
209#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000210
Michal Krolb80bc052004-10-21 14:09:54 +0000211/* GL_ARB_draw_buffers option */
212#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000213
Ian Romanickbb372f12007-05-16 15:34:22 -0700214/* GL_MESA_texture_array option */
215#define MESA_TEXTURE_ARRAY 0x08
216
Jouk Jansen40322e12004-04-05 08:50:36 +0000217/* GL_ARB_fragment_program instruction class */
218#define OP_ALU_INST 0x00
219#define OP_TEX_INST 0x01
220
221/* GL_ARB_vertex_program instruction class */
222/* OP_ALU_INST */
223
224/* GL_ARB_fragment_program instruction type */
225#define OP_ALU_VECTOR 0x00
226#define OP_ALU_SCALAR 0x01
227#define OP_ALU_BINSC 0x02
228#define OP_ALU_BIN 0x03
229#define OP_ALU_TRI 0x04
230#define OP_ALU_SWZ 0x05
231#define OP_TEX_SAMPLE 0x06
232#define OP_TEX_KIL 0x07
233
234/* GL_ARB_vertex_program instruction type */
235#define OP_ALU_ARL 0x08
236/* OP_ALU_VECTOR */
237/* OP_ALU_SCALAR */
238/* OP_ALU_BINSC */
239/* OP_ALU_BIN */
240/* OP_ALU_TRI */
241/* OP_ALU_SWZ */
242
243/* GL_ARB_fragment_program instruction code */
244#define OP_ABS 0x00
245#define OP_ABS_SAT 0x1B
246#define OP_FLR 0x09
247#define OP_FLR_SAT 0x26
248#define OP_FRC 0x0A
249#define OP_FRC_SAT 0x27
250#define OP_LIT 0x0C
251#define OP_LIT_SAT 0x2A
252#define OP_MOV 0x11
253#define OP_MOV_SAT 0x30
254#define OP_COS 0x1F
255#define OP_COS_SAT 0x20
256#define OP_EX2 0x07
257#define OP_EX2_SAT 0x25
258#define OP_LG2 0x0B
259#define OP_LG2_SAT 0x29
260#define OP_RCP 0x14
261#define OP_RCP_SAT 0x33
262#define OP_RSQ 0x15
263#define OP_RSQ_SAT 0x34
264#define OP_SIN 0x38
265#define OP_SIN_SAT 0x39
266#define OP_SCS 0x35
267#define OP_SCS_SAT 0x36
268#define OP_POW 0x13
269#define OP_POW_SAT 0x32
270#define OP_ADD 0x01
271#define OP_ADD_SAT 0x1C
272#define OP_DP3 0x03
273#define OP_DP3_SAT 0x21
274#define OP_DP4 0x04
275#define OP_DP4_SAT 0x22
276#define OP_DPH 0x05
277#define OP_DPH_SAT 0x23
278#define OP_DST 0x06
279#define OP_DST_SAT 0x24
280#define OP_MAX 0x0F
281#define OP_MAX_SAT 0x2E
282#define OP_MIN 0x10
283#define OP_MIN_SAT 0x2F
284#define OP_MUL 0x12
285#define OP_MUL_SAT 0x31
286#define OP_SGE 0x16
287#define OP_SGE_SAT 0x37
288#define OP_SLT 0x17
289#define OP_SLT_SAT 0x3A
290#define OP_SUB 0x18
291#define OP_SUB_SAT 0x3B
292#define OP_XPD 0x1A
293#define OP_XPD_SAT 0x43
294#define OP_CMP 0x1D
295#define OP_CMP_SAT 0x1E
296#define OP_LRP 0x2B
297#define OP_LRP_SAT 0x2C
298#define OP_MAD 0x0E
299#define OP_MAD_SAT 0x2D
300#define OP_SWZ 0x19
301#define OP_SWZ_SAT 0x3C
302#define OP_TEX 0x3D
303#define OP_TEX_SAT 0x3E
304#define OP_TXB 0x3F
305#define OP_TXB_SAT 0x40
306#define OP_TXP 0x41
307#define OP_TXP_SAT 0x42
308#define OP_KIL 0x28
309
310/* GL_ARB_vertex_program instruction code */
311#define OP_ARL 0x02
312/* OP_ABS */
313/* OP_FLR */
314/* OP_FRC */
315/* OP_LIT */
316/* OP_MOV */
317/* OP_EX2 */
318#define OP_EXP 0x08
319/* OP_LG2 */
320#define OP_LOG 0x0D
321/* OP_RCP */
322/* OP_RSQ */
323/* OP_POW */
324/* OP_ADD */
325/* OP_DP3 */
326/* OP_DP4 */
327/* OP_DPH */
328/* OP_DST */
329/* OP_MAX */
330/* OP_MIN */
331/* OP_MUL */
332/* OP_SGE */
333/* OP_SLT */
334/* OP_SUB */
335/* OP_XPD */
336/* OP_MAD */
337/* OP_SWZ */
338
339/* fragment attribute binding */
340#define FRAGMENT_ATTRIB_COLOR 0x01
341#define FRAGMENT_ATTRIB_TEXCOORD 0x02
342#define FRAGMENT_ATTRIB_FOGCOORD 0x03
343#define FRAGMENT_ATTRIB_POSITION 0x04
344
345/* vertex attribute binding */
346#define VERTEX_ATTRIB_POSITION 0x01
347#define VERTEX_ATTRIB_WEIGHT 0x02
348#define VERTEX_ATTRIB_NORMAL 0x03
349#define VERTEX_ATTRIB_COLOR 0x04
350#define VERTEX_ATTRIB_FOGCOORD 0x05
351#define VERTEX_ATTRIB_TEXCOORD 0x06
352#define VERTEX_ATTRIB_MATRIXINDEX 0x07
353#define VERTEX_ATTRIB_GENERIC 0x08
354
355/* fragment result binding */
356#define FRAGMENT_RESULT_COLOR 0x01
357#define FRAGMENT_RESULT_DEPTH 0x02
358
359/* vertex result binding */
360#define VERTEX_RESULT_POSITION 0x01
361#define VERTEX_RESULT_COLOR 0x02
362#define VERTEX_RESULT_FOGCOORD 0x03
363#define VERTEX_RESULT_POINTSIZE 0x04
364#define VERTEX_RESULT_TEXCOORD 0x05
365
366/* texture target */
367#define TEXTARGET_1D 0x01
368#define TEXTARGET_2D 0x02
369#define TEXTARGET_3D 0x03
370#define TEXTARGET_RECT 0x04
371#define TEXTARGET_CUBE 0x05
372/* GL_ARB_fragment_program_shadow */
373#define TEXTARGET_SHADOW1D 0x06
374#define TEXTARGET_SHADOW2D 0x07
375#define TEXTARGET_SHADOWRECT 0x08
Ian Romanickbb372f12007-05-16 15:34:22 -0700376/* GL_MESA_texture_array */
377#define TEXTARGET_1D_ARRAY 0x09
378#define TEXTARGET_2D_ARRAY 0x0a
Ian Romanick69358e72007-06-05 09:24:27 -0700379#define TEXTARGET_SHADOW1D_ARRAY 0x0b
380#define TEXTARGET_SHADOW2D_ARRAY 0x0c
Jouk Jansen40322e12004-04-05 08:50:36 +0000381
382/* face type */
383#define FACE_FRONT 0x00
384#define FACE_BACK 0x01
385
386/* color type */
387#define COLOR_PRIMARY 0x00
388#define COLOR_SECONDARY 0x01
389
390/* component */
391#define COMPONENT_X 0x00
392#define COMPONENT_Y 0x01
393#define COMPONENT_Z 0x02
394#define COMPONENT_W 0x03
395#define COMPONENT_0 0x04
396#define COMPONENT_1 0x05
397
398/* array index type */
399#define ARRAY_INDEX_ABSOLUTE 0x00
400#define ARRAY_INDEX_RELATIVE 0x01
401
402/* matrix name */
403#define MATRIX_MODELVIEW 0x01
404#define MATRIX_PROJECTION 0x02
405#define MATRIX_MVP 0x03
406#define MATRIX_TEXTURE 0x04
407#define MATRIX_PALETTE 0x05
408#define MATRIX_PROGRAM 0x06
409
410/* matrix modifier */
411#define MATRIX_MODIFIER_IDENTITY 0x00
412#define MATRIX_MODIFIER_INVERSE 0x01
413#define MATRIX_MODIFIER_TRANSPOSE 0x02
414#define MATRIX_MODIFIER_INVTRANS 0x03
415
416/* constant type */
417#define CONSTANT_SCALAR 0x01
418#define CONSTANT_VECTOR 0x02
419
420/* program param type */
421#define PROGRAM_PARAM_ENV 0x01
422#define PROGRAM_PARAM_LOCAL 0x02
423
424/* register type */
425#define REGISTER_ATTRIB 0x01
426#define REGISTER_PARAM 0x02
427#define REGISTER_RESULT 0x03
428#define REGISTER_ESTABLISHED_NAME 0x04
429
430/* param binding */
431#define PARAM_NULL 0x00
432#define PARAM_ARRAY_ELEMENT 0x01
433#define PARAM_STATE_ELEMENT 0x02
434#define PARAM_PROGRAM_ELEMENT 0x03
435#define PARAM_PROGRAM_ELEMENTS 0x04
436#define PARAM_CONSTANT 0x05
437
438/* param state property */
439#define STATE_MATERIAL_PARSER 0x01
440#define STATE_LIGHT_PARSER 0x02
441#define STATE_LIGHT_MODEL 0x03
442#define STATE_LIGHT_PROD 0x04
443#define STATE_FOG 0x05
444#define STATE_MATRIX_ROWS 0x06
445/* GL_ARB_fragment_program */
446#define STATE_TEX_ENV 0x07
447#define STATE_DEPTH 0x08
448/* GL_ARB_vertex_program */
449#define STATE_TEX_GEN 0x09
450#define STATE_CLIP_PLANE 0x0A
451#define STATE_POINT 0x0B
452
453/* state material property */
454#define MATERIAL_AMBIENT 0x01
455#define MATERIAL_DIFFUSE 0x02
456#define MATERIAL_SPECULAR 0x03
457#define MATERIAL_EMISSION 0x04
458#define MATERIAL_SHININESS 0x05
459
460/* state light property */
461#define LIGHT_AMBIENT 0x01
462#define LIGHT_DIFFUSE 0x02
463#define LIGHT_SPECULAR 0x03
464#define LIGHT_POSITION 0x04
465#define LIGHT_ATTENUATION 0x05
466#define LIGHT_HALF 0x06
467#define LIGHT_SPOT_DIRECTION 0x07
468
469/* state light model property */
470#define LIGHT_MODEL_AMBIENT 0x01
471#define LIGHT_MODEL_SCENECOLOR 0x02
472
473/* state light product property */
474#define LIGHT_PROD_AMBIENT 0x01
475#define LIGHT_PROD_DIFFUSE 0x02
476#define LIGHT_PROD_SPECULAR 0x03
477
478/* state texture environment property */
479#define TEX_ENV_COLOR 0x01
480
481/* state texture generation coord property */
482#define TEX_GEN_EYE 0x01
483#define TEX_GEN_OBJECT 0x02
484
485/* state fog property */
486#define FOG_COLOR 0x01
487#define FOG_PARAMS 0x02
488
489/* state depth property */
490#define DEPTH_RANGE 0x01
491
492/* state point parameters property */
493#define POINT_SIZE 0x01
494#define POINT_ATTENUATION 0x02
495
496/* declaration */
497#define ATTRIB 0x01
498#define PARAM 0x02
499#define TEMP 0x03
500#define OUTPUT 0x04
501#define ALIAS 0x05
502/* GL_ARB_vertex_program */
503#define ADDRESS 0x06
504
505/*-----------------------------------------------------------------------
506 * From here on down is the semantic checking portion
507 *
508 */
509
510/**
511 * Variable Table Handling functions
512 */
513typedef enum
514{
515 vt_none,
516 vt_address,
517 vt_attrib,
518 vt_param,
519 vt_temp,
520 vt_output,
521 vt_alias
522} var_type;
523
524
Brian Paul7aebaf32005-10-30 21:23:23 +0000525/**
526 * Setting an explicit field for each of the binding properties is a bit
527 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000528 */
529struct var_cache
530{
Brian Paul6a769d92006-04-28 15:42:15 +0000531 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000532 var_type type;
533 GLuint address_binding; /* The index of the address register we should
534 * be using */
535 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000536 GLuint attrib_is_generic; /* If the attrib was specified through a generic
537 * vertex attrib */
538 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000539 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000540 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
541 * that this is aliased to */
542 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
543 * PROGRAM_ENV_PARAM} */
544 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
545 * the tokens representing our bound state (or constants)
546 * start */
547 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
548 * we take up with our state tokens or constants. Note that
549 * this is _not_ the same as the number of param registers
550 * we eventually use */
551 struct var_cache *next;
552};
553
554static GLvoid
555var_cache_create (struct var_cache **va)
556{
557 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
558 if (*va) {
559 (**va).name = NULL;
560 (**va).type = vt_none;
561 (**va).attrib_binding = ~0;
562 (**va).attrib_is_generic = 0;
563 (**va).temp_binding = ~0;
564 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000565 (**va).param_binding_type = ~0;
566 (**va).param_binding_begin = ~0;
567 (**va).param_binding_length = ~0;
568 (**va).alias_binding = NULL;
569 (**va).next = NULL;
570 }
571}
572
573static GLvoid
574var_cache_destroy (struct var_cache **va)
575{
576 if (*va) {
577 var_cache_destroy (&(**va).next);
578 _mesa_free (*va);
579 *va = NULL;
580 }
581}
582
583static GLvoid
584var_cache_append (struct var_cache **va, struct var_cache *nv)
585{
586 if (*va)
587 var_cache_append (&(**va).next, nv);
588 else
589 *va = nv;
590}
591
592static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000593var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000594{
Brian Paul0a360cf2005-01-17 01:21:03 +0000595 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000596
597 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000598 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000599 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000600 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000601 return va;
602 }
603
604 va = va->next;
605 }
606
607 return NULL;
608}
609
Brian Paula088f162006-09-05 23:08:51 +0000610
611
612/**
613 * Called when an error is detected while parsing/compiling a program.
614 * Sets the ctx->Program.ErrorString field to descript and records a
615 * GL_INVALID_OPERATION error.
616 * \param position position of error in program string
617 * \param descrip verbose error description
618 */
619static void
620program_error(GLcontext *ctx, GLint position, const char *descrip)
621{
622 if (descrip) {
623 const char *prefix = "glProgramString(", *suffix = ")";
624 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
625 _mesa_strlen(prefix) +
626 _mesa_strlen(suffix) + 1);
627 if (str) {
628 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
629 _mesa_error(ctx, GL_INVALID_OPERATION, str);
630 _mesa_free(str);
631 }
632 }
633 _mesa_set_program_error(ctx, position, descrip);
634}
635
636
Brian50a20d42008-03-27 15:51:07 -0600637/**
638 * As above, but with an extra string parameter for more info.
639 */
640static void
641program_error2(GLcontext *ctx, GLint position, const char *descrip,
642 const char *var)
643{
644 if (descrip) {
645 const char *prefix = "glProgramString(", *suffix = ")";
646 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
647 _mesa_strlen(": ") +
648 _mesa_strlen(var) +
649 _mesa_strlen(prefix) +
650 _mesa_strlen(suffix) + 1);
651 if (str) {
652 _mesa_sprintf(str, "%s%s: %s%s", prefix, descrip, var, suffix);
653 _mesa_error(ctx, GL_INVALID_OPERATION, str);
654 _mesa_free(str);
655 }
656 }
657 {
658 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
659 _mesa_strlen(": ") +
660 _mesa_strlen(var) + 1);
661 if (str) {
662 _mesa_sprintf(str, "%s: %s", descrip, var);
663 }
664 _mesa_set_program_error(ctx, position, str);
665 if (str) {
666 _mesa_free(str);
667 }
668 }
669}
670
671
Brian Paula088f162006-09-05 23:08:51 +0000672
Jouk Jansen40322e12004-04-05 08:50:36 +0000673/**
674 * constructs an integer from 4 GLubytes in LE format
675 */
676static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000677parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000678{
679 GLuint value;
680
681 value = (GLuint) (*(*inst)++);
682 value += (GLuint) (*(*inst)++) * 0x100;
683 value += (GLuint) (*(*inst)++) * 0x10000;
684 value += (GLuint) (*(*inst)++) * 0x1000000;
685
686 return value;
687}
688
689/**
690 * This will, given a string, lookup the string as a variable name in the
691 * var cache. If the name is found, the var cache node corresponding to the
692 * var name is returned. If it is not found, a new entry is allocated
693 *
694 * \param I Points into the binary array where the string identifier begins
695 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
696 * \return The location on the var_cache corresponding the the string starting at I
697 */
698static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000699parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000700 struct arb_program *Program, GLuint * found)
701{
Brian Paula088f162006-09-05 23:08:51 +0000702 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000703 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000704 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000705
706 *inst += _mesa_strlen ((char *) i) + 1;
707
708 va = var_cache_find (*vc_head, i);
709
710 if (va) {
711 *found = 1;
712 return va;
713 }
714
715 *found = 0;
716 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000717 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000718
719 var_cache_append (vc_head, va);
720
721 return va;
722}
723
724static char *
Brian Paula088f162006-09-05 23:08:51 +0000725parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000726{
Brian Paula088f162006-09-05 23:08:51 +0000727 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000728 (void) Program;
729
Jouk Jansen40322e12004-04-05 08:50:36 +0000730 *inst += _mesa_strlen ((char *) i) + 1;
731
732 return (char *) i;
733}
734
735/**
Brian Paul05908952004-06-08 15:20:23 +0000736 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000737 */
Brian Paul05908952004-06-08 15:20:23 +0000738static GLint
Brian Paula088f162006-09-05 23:08:51 +0000739parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000740{
741 /*return *(*inst)++ != '+'; */
742
743 if (**inst == '-') {
744 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000745 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000746 }
747 else if (**inst == '+') {
748 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000749 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000750 }
751
Brian Paul05908952004-06-08 15:20:23 +0000752 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000753}
754
755/**
756 * parses and returns signed integer
757 */
758static GLint
Brian Paula088f162006-09-05 23:08:51 +0000759parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000760{
761 GLint sign;
762 GLint value;
763
764 /* check if *inst points to '+' or '-'
765 * if yes, grab the sign and increment *inst
766 */
767 sign = parse_sign (inst);
768
769 /* now check if *inst points to 0
770 * if yes, increment the *inst and return the default value
771 */
772 if (**inst == 0) {
773 (*inst)++;
774 return 0;
775 }
776
777 /* parse the integer as you normally would do it */
778 value = _mesa_atoi (parse_string_without_adding (inst, Program));
779
780 /* now, after terminating 0 there is a position
781 * to parse it - parse_position()
782 */
783 Program->Position = parse_position (inst);
784
Brian Paul05908952004-06-08 15:20:23 +0000785 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000786}
787
788/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000789 Accumulate this string of digits, and return them as
790 a large integer represented in floating point (for range).
791 If scale is not NULL, also accumulates a power-of-ten
792 integer scale factor that represents the number of digits
793 in the string.
794*/
795static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000796parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000797{
798 GLdouble value = 0.0;
799 GLdouble oscale = 1.0;
800
801 if (**inst == 0) { /* this string of digits is empty-- do nothing */
802 (*inst)++;
803 }
804 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000805 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000806 GLubyte digit = *((*inst)++);
807 value = value * 10.0 + (GLint) (digit - '0');
808 oscale *= 10.0;
809 }
810 assert(**inst == 0); /* integer string should end with 0 */
811 (*inst)++; /* skip over terminating 0 */
812 Program->Position = parse_position(inst); /* skip position (from integer) */
813 }
814 if (scale)
815 *scale = oscale;
816 return value;
817}
818
819/**
820 Parse an unsigned floating-point number from this stream of tokenized
821 characters. Example floating-point formats supported:
822 12.34
823 12
824 0.34
825 .34
826 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000827 */
828static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000829parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000830{
Brian Paul1ff8f502005-02-16 15:08:29 +0000831 GLint exponent;
832 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000833
Brian Paul1ff8f502005-02-16 15:08:29 +0000834 whole = parse_float_string(inst, Program, 0);
835 fraction = parse_float_string(inst, Program, &fracScale);
836
837 /* Parse signed exponent */
838 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000839
Brian Paul1ff8f502005-02-16 15:08:29 +0000840 /* Assemble parts of floating-point number: */
841 return (GLfloat) ((whole + fraction / fracScale) *
842 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000843}
844
845
846/**
847 */
848static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000849parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000850{
Brian Paul05908952004-06-08 15:20:23 +0000851 GLint sign = parse_sign (inst);
852 GLfloat value = parse_float (inst, Program);
853 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000854}
855
856/**
857 * This picks out a constant value from the parsed array. The constant vector is r
858 * returned in the *values array, which should be of length 4.
859 *
860 * \param values - The 4 component vector with the constant value in it
861 */
862static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000863parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000864 GLboolean use)
865{
866 GLuint components, i;
867
868
869 switch (*(*inst)++) {
870 case CONSTANT_SCALAR:
871 if (use == GL_TRUE) {
872 values[0] =
873 values[1] =
874 values[2] = values[3] = parse_float (inst, Program);
875 }
876 else {
877 values[0] =
878 values[1] =
879 values[2] = values[3] = parse_signed_float (inst, Program);
880 }
881
882 break;
883 case CONSTANT_VECTOR:
884 values[0] = values[1] = values[2] = 0;
885 values[3] = 1;
886 components = *(*inst)++;
887 for (i = 0; i < components; i++) {
888 values[i] = parse_signed_float (inst, Program);
889 }
890 break;
891 }
892}
893
894/**
895 * \param offset The offset from the address register that we should
896 * address
897 *
898 * \return 0 on sucess, 1 on error
899 */
900static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000901parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
902 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000903{
Brian Paul6a769d92006-04-28 15:42:15 +0000904 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000905 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000906 return 0;
907}
908
909/**
910 * \param color 0 if color type is primary, 1 if color type is secondary
911 * \return 0 on sucess, 1 on error
912 */
913static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000914parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000915 GLint * color)
916{
Brian Paula6c423d2004-08-25 15:59:48 +0000917 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000918 *color = *(*inst)++ != COLOR_PRIMARY;
919 return 0;
920}
921
922/**
923 * Get an integer corresponding to a generic vertex attribute.
924 *
925 * \return 0 on sucess, 1 on error
926 */
927static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000928parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000929 struct arb_program *Program, GLuint *attrib)
930{
Brian Paulbd997cd2004-07-20 21:12:56 +0000931 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000932
Michal Krolbb38cad2006-04-11 11:41:11 +0000933 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000934 {
Brian Paula088f162006-09-05 23:08:51 +0000935 program_error(ctx, Program->Position,
936 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000937 return 1;
938 }
939
Brian Paulbd997cd2004-07-20 21:12:56 +0000940 *attrib = (GLuint) i;
941
Jouk Jansen40322e12004-04-05 08:50:36 +0000942 return 0;
943}
944
945
946/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000947 * \param color The index of the color buffer to write into
948 * \return 0 on sucess, 1 on error
949 */
950static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000951parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000952 struct arb_program *Program, GLuint * color)
953{
954 GLint i = parse_integer (inst, Program);
955
956 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000957 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000958 return 1;
959 }
960
961 *color = (GLuint) i;
962 return 0;
963}
964
965
966/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000967 * \param coord The texture unit index
968 * \return 0 on sucess, 1 on error
969 */
970static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000971parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000972 struct arb_program *Program, GLuint * coord)
973{
Brian Paulbd997cd2004-07-20 21:12:56 +0000974 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000975
Brian Paula6c423d2004-08-25 15:59:48 +0000976 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000977 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000978 return 1;
979 }
980
Brian Paulbd997cd2004-07-20 21:12:56 +0000981 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000982 return 0;
983}
984
985/**
986 * \param coord The weight index
987 * \return 0 on sucess, 1 on error
988 */
989static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000990parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000991 GLint * coord)
992{
993 *coord = parse_integer (inst, Program);
994
995 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000996 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000997 return 1;
998 }
999
1000 return 0;
1001}
1002
1003/**
1004 * \param coord The clip plane index
1005 * \return 0 on sucess, 1 on error
1006 */
1007static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001008parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001009 struct arb_program *Program, GLint * coord)
1010{
1011 *coord = parse_integer (inst, Program);
1012
1013 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +00001014 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +00001015 return 1;
1016 }
1017
1018 return 0;
1019}
1020
1021
1022/**
1023 * \return 0 on front face, 1 on back face
1024 */
1025static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001026parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +00001027{
1028 switch (*(*inst)++) {
1029 case FACE_FRONT:
1030 return 0;
1031
1032 case FACE_BACK:
1033 return 1;
1034 }
1035 return 0;
1036}
1037
1038
1039/**
1040 * Given a matrix and a modifier token on the binary array, return tokens
1041 * that _mesa_fetch_state() [program.c] can understand.
1042 *
1043 * \param matrix - the matrix we are talking about
1044 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1045 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1046 * \return 0 on sucess, 1 on failure
1047 */
1048static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001049parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001050 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1051{
1052 GLubyte mat = *(*inst)++;
1053
1054 *matrix_idx = 0;
1055
1056 switch (mat) {
1057 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001058 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001059 *matrix_idx = parse_integer (inst, Program);
1060 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001061 program_error(ctx, Program->Position,
1062 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001063 return 1;
1064 }
1065 break;
1066
1067 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001068 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001069 break;
1070
1071 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001072 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001073 break;
1074
1075 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001076 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001077 *matrix_idx = parse_integer (inst, Program);
1078 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001079 program_error(ctx, Program->Position, "Invalid Texture Unit");
1080 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001081 return 1;
1082 }
1083 break;
1084
1085 /* This is not currently supported (ARB_matrix_palette) */
1086 case MATRIX_PALETTE:
1087 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001088 program_error(ctx, Program->Position,
1089 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001090 return 1;
1091 break;
1092
1093 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001094 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001095 *matrix_idx = parse_integer (inst, Program);
1096 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001097 program_error(ctx, Program->Position, "Invalid Program Matrix");
1098 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001099 return 1;
1100 }
1101 break;
1102 }
1103
1104 switch (*(*inst)++) {
1105 case MATRIX_MODIFIER_IDENTITY:
1106 *matrix_modifier = 0;
1107 break;
1108 case MATRIX_MODIFIER_INVERSE:
1109 *matrix_modifier = STATE_MATRIX_INVERSE;
1110 break;
1111 case MATRIX_MODIFIER_TRANSPOSE:
1112 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1113 break;
1114 case MATRIX_MODIFIER_INVTRANS:
1115 *matrix_modifier = STATE_MATRIX_INVTRANS;
1116 break;
1117 }
1118
1119 return 0;
1120}
1121
1122
1123/**
1124 * This parses a state string (rather, the binary version of it) into
1125 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1126 *
1127 * \param inst - the start in the binary arry to start working from
1128 * \param state_tokens - the storage for the 6-token state description
1129 * \return - 0 on sucess, 1 on error
1130 */
1131static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001132parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001133 struct arb_program *Program,
1134 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001135{
1136 switch (*(*inst)++) {
1137 case STATE_MATERIAL_PARSER:
1138 state_tokens[0] = STATE_MATERIAL;
1139 state_tokens[1] = parse_face_type (inst);
1140 switch (*(*inst)++) {
1141 case MATERIAL_AMBIENT:
1142 state_tokens[2] = STATE_AMBIENT;
1143 break;
1144 case MATERIAL_DIFFUSE:
1145 state_tokens[2] = STATE_DIFFUSE;
1146 break;
1147 case MATERIAL_SPECULAR:
1148 state_tokens[2] = STATE_SPECULAR;
1149 break;
1150 case MATERIAL_EMISSION:
1151 state_tokens[2] = STATE_EMISSION;
1152 break;
1153 case MATERIAL_SHININESS:
1154 state_tokens[2] = STATE_SHININESS;
1155 break;
1156 }
1157 break;
1158
1159 case STATE_LIGHT_PARSER:
1160 state_tokens[0] = STATE_LIGHT;
1161 state_tokens[1] = parse_integer (inst, Program);
1162
1163 /* Check the value of state_tokens[1] against the # of lights */
1164 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001165 program_error(ctx, Program->Position, "Invalid Light Number");
1166 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001167 return 1;
1168 }
1169
1170 switch (*(*inst)++) {
1171 case LIGHT_AMBIENT:
1172 state_tokens[2] = STATE_AMBIENT;
1173 break;
1174 case LIGHT_DIFFUSE:
1175 state_tokens[2] = STATE_DIFFUSE;
1176 break;
1177 case LIGHT_SPECULAR:
1178 state_tokens[2] = STATE_SPECULAR;
1179 break;
1180 case LIGHT_POSITION:
1181 state_tokens[2] = STATE_POSITION;
1182 break;
1183 case LIGHT_ATTENUATION:
1184 state_tokens[2] = STATE_ATTENUATION;
1185 break;
1186 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001187 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001188 break;
1189 case LIGHT_SPOT_DIRECTION:
1190 state_tokens[2] = STATE_SPOT_DIRECTION;
1191 break;
1192 }
1193 break;
1194
1195 case STATE_LIGHT_MODEL:
1196 switch (*(*inst)++) {
1197 case LIGHT_MODEL_AMBIENT:
1198 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1199 break;
1200 case LIGHT_MODEL_SCENECOLOR:
1201 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1202 state_tokens[1] = parse_face_type (inst);
1203 break;
1204 }
1205 break;
1206
1207 case STATE_LIGHT_PROD:
1208 state_tokens[0] = STATE_LIGHTPROD;
1209 state_tokens[1] = parse_integer (inst, Program);
1210
1211 /* Check the value of state_tokens[1] against the # of lights */
1212 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001213 program_error(ctx, Program->Position, "Invalid Light Number");
1214 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001215 return 1;
1216 }
1217
1218 state_tokens[2] = parse_face_type (inst);
1219 switch (*(*inst)++) {
1220 case LIGHT_PROD_AMBIENT:
1221 state_tokens[3] = STATE_AMBIENT;
1222 break;
1223 case LIGHT_PROD_DIFFUSE:
1224 state_tokens[3] = STATE_DIFFUSE;
1225 break;
1226 case LIGHT_PROD_SPECULAR:
1227 state_tokens[3] = STATE_SPECULAR;
1228 break;
1229 }
1230 break;
1231
1232
1233 case STATE_FOG:
1234 switch (*(*inst)++) {
1235 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001236 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001237 break;
1238 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001239 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001240 break;
1241 }
1242 break;
1243
1244 case STATE_TEX_ENV:
1245 state_tokens[1] = parse_integer (inst, Program);
1246 switch (*(*inst)++) {
1247 case TEX_ENV_COLOR:
1248 state_tokens[0] = STATE_TEXENV_COLOR;
1249 break;
1250 }
1251 break;
1252
1253 case STATE_TEX_GEN:
1254 {
1255 GLuint type, coord;
1256
1257 state_tokens[0] = STATE_TEXGEN;
1258 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1259
1260 if (parse_texcoord_num (ctx, inst, Program, &coord))
1261 return 1;
1262 state_tokens[1] = coord;
1263
1264 /* EYE or OBJECT */
Brian7b91d872008-03-27 15:48:54 -06001265 type = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001266
1267 /* 0 - s, 1 - t, 2 - r, 3 - q */
Brian7b91d872008-03-27 15:48:54 -06001268 coord = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001269
1270 if (type == TEX_GEN_EYE) {
1271 switch (coord) {
1272 case COMPONENT_X:
1273 state_tokens[2] = STATE_TEXGEN_EYE_S;
1274 break;
1275 case COMPONENT_Y:
1276 state_tokens[2] = STATE_TEXGEN_EYE_T;
1277 break;
1278 case COMPONENT_Z:
1279 state_tokens[2] = STATE_TEXGEN_EYE_R;
1280 break;
1281 case COMPONENT_W:
1282 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1283 break;
Brian7b91d872008-03-27 15:48:54 -06001284 default:
1285 _mesa_problem(ctx, "bad texgen component in "
1286 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001287 }
1288 }
1289 else {
1290 switch (coord) {
1291 case COMPONENT_X:
1292 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1293 break;
1294 case COMPONENT_Y:
1295 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1296 break;
1297 case COMPONENT_Z:
1298 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1299 break;
1300 case COMPONENT_W:
1301 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1302 break;
Brian7b91d872008-03-27 15:48:54 -06001303 default:
1304 _mesa_problem(ctx, "bad texgen component in "
1305 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001306 }
1307 }
1308 }
1309 break;
1310
1311 case STATE_DEPTH:
1312 switch (*(*inst)++) {
1313 case DEPTH_RANGE:
1314 state_tokens[0] = STATE_DEPTH_RANGE;
1315 break;
1316 }
1317 break;
1318
1319 case STATE_CLIP_PLANE:
1320 state_tokens[0] = STATE_CLIPPLANE;
1321 state_tokens[1] = parse_integer (inst, Program);
Brianaa9d22a2007-02-23 11:21:03 -07001322 if (parse_clipplane_num (ctx, inst, Program,
1323 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001324 return 1;
1325 break;
1326
1327 case STATE_POINT:
Brian7b91d872008-03-27 15:48:54 -06001328 switch (*(*inst)++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001329 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001330 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001331 break;
1332
1333 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001334 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001335 break;
1336 }
1337 break;
1338
1339 /* XXX: I think this is the correct format for a matrix row */
1340 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001341 if (parse_matrix(ctx, inst, Program,
1342 (GLint *) &state_tokens[0],
1343 (GLint *) &state_tokens[1],
1344 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001345 return 1;
1346
Brian65319522007-02-21 11:08:21 -07001347 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001348
1349 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001350 state_tokens[3] = parse_integer (inst, Program);
1351 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001352 program_error(ctx, Program->Position,
1353 "Second matrix index less than the first");
1354 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001355 return 1;
1356 }
1357 }
1358 else {
Brian65319522007-02-21 11:08:21 -07001359 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001360 (*inst)++;
1361 }
1362 break;
1363 }
1364
1365 return 0;
1366}
1367
1368/**
1369 * This parses a state string (rather, the binary version of it) into
1370 * a 6-token similar for the state fetching code in program.c
1371 *
1372 * One might ask, why fetch these parameters into just like you fetch
1373 * state when they are already stored in other places?
1374 *
1375 * Because of array offsets -> We can stick env/local parameters in the
1376 * middle of a parameter array and then index someplace into the array
1377 * when we execute.
1378 *
1379 * One optimization might be to only do this for the cases where the
1380 * env/local parameters end up inside of an array, and leave the
1381 * single parameters (or arrays of pure env/local pareameters) in their
1382 * respective register files.
1383 *
1384 * For ENV parameters, the format is:
1385 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1386 * state_tokens[1] = STATE_ENV
1387 * state_tokens[2] = the parameter index
1388 *
1389 * for LOCAL parameters, the format is:
1390 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1391 * state_tokens[1] = STATE_LOCAL
1392 * state_tokens[2] = the parameter index
1393 *
1394 * \param inst - the start in the binary arry to start working from
1395 * \param state_tokens - the storage for the 6-token state description
1396 * \return - 0 on sucess, 1 on failure
1397 */
1398static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001399parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001400 struct arb_program *Program,
1401 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001402{
1403 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1404 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1405 else
1406 state_tokens[0] = STATE_VERTEX_PROGRAM;
1407
1408
1409 switch (*(*inst)++) {
1410 case PROGRAM_PARAM_ENV:
1411 state_tokens[1] = STATE_ENV;
1412 state_tokens[2] = parse_integer (inst, Program);
1413
1414 /* Check state_tokens[2] against the number of ENV parameters available */
1415 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001416 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001417 ||
1418 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001419 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001420 program_error(ctx, Program->Position,
1421 "Invalid Program Env Parameter");
1422 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001423 return 1;
1424 }
1425
1426 break;
1427
1428 case PROGRAM_PARAM_LOCAL:
1429 state_tokens[1] = STATE_LOCAL;
1430 state_tokens[2] = parse_integer (inst, Program);
1431
1432 /* Check state_tokens[2] against the number of LOCAL parameters available */
1433 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001434 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001435 ||
1436 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001437 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001438 program_error(ctx, Program->Position,
1439 "Invalid Program Local Parameter");
1440 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001441 return 1;
1442 }
1443 break;
1444 }
1445
1446 return 0;
1447}
1448
1449/**
1450 * For ARB_vertex_program, programs are not allowed to use both an explicit
1451 * vertex attribute and a generic vertex attribute corresponding to the same
1452 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1453 *
1454 * This will walk our var_cache and make sure that nobody does anything fishy.
1455 *
1456 * \return 0 on sucess, 1 on error
1457 */
1458static GLuint
1459generic_attrib_check(struct var_cache *vc_head)
1460{
1461 int a;
1462 struct var_cache *curr;
1463 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1464 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1465
1466 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1467 explicitAttrib[a] = GL_FALSE;
1468 genericAttrib[a] = GL_FALSE;
1469 }
1470
1471 curr = vc_head;
1472 while (curr) {
1473 if (curr->type == vt_attrib) {
1474 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001475 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001476 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001477 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001478 }
1479
1480 curr = curr->next;
1481 }
1482
1483 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1484 if ((explicitAttrib[a]) && (genericAttrib[a]))
1485 return 1;
1486 }
1487
1488 return 0;
1489}
1490
1491/**
1492 * This will handle the binding side of an ATTRIB var declaration
1493 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001494 * \param inputReg returns the input register index, one of the
1495 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001496 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001497 */
1498static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001499parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001500 struct arb_program *Program,
1501 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001502{
Jouk Jansen40322e12004-04-05 08:50:36 +00001503 GLint err = 0;
1504
1505 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001506
Jouk Jansen40322e12004-04-05 08:50:36 +00001507 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1508 switch (*(*inst)++) {
1509 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001510 {
1511 GLint coord;
1512 err = parse_color_type (ctx, inst, Program, &coord);
1513 *inputReg = FRAG_ATTRIB_COL0 + coord;
1514 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001515 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001516 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001517 {
Brian8fa6f732007-02-01 09:24:41 -07001518 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001519 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1520 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1521 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001522 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001523 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001524 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001525 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001527 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001528 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001529 default:
1530 err = 1;
1531 break;
1532 }
1533 }
1534 else {
1535 switch (*(*inst)++) {
1536 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001537 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001538 break;
1539
1540 case VERTEX_ATTRIB_WEIGHT:
1541 {
1542 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001543 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001544 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001545#if 1
1546 /* hack for Warcraft (see bug 8060) */
1547 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001548 break;
Brian Paul3a557502006-09-05 23:15:29 +00001549#else
Brian Paula088f162006-09-05 23:08:51 +00001550 program_error(ctx, Program->Position,
1551 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001552 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001553#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001554 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001555
1556 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001557 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001558 break;
1559
1560 case VERTEX_ATTRIB_COLOR:
1561 {
1562 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001563 err = parse_color_type (ctx, inst, Program, &color);
1564 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001565 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001566 }
1567 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001568 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001569 }
1570 }
1571 break;
1572
1573 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001574 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001575 break;
1576
1577 case VERTEX_ATTRIB_TEXCOORD:
1578 {
Brian8fa6f732007-02-01 09:24:41 -07001579 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001580 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001581 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001582 }
1583 break;
1584
Jouk Jansen40322e12004-04-05 08:50:36 +00001585 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001586 /* Not supported at this time */
1587 {
1588 const char *msg = "ARB_palette_matrix not supported";
1589 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001590 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001591 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001592 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001593
1594 case VERTEX_ATTRIB_GENERIC:
1595 {
1596 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001597 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001598 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001599 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001600 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1601 * attributes do not alias the conventional vertex
1602 * attributes.
1603 */
1604 if (attrib > 0)
1605 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001606 else
1607 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001608 }
1609 }
1610 break;
1611
1612 default:
1613 err = 1;
1614 break;
1615 }
1616 }
1617
Jouk Jansen40322e12004-04-05 08:50:36 +00001618 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001619 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001620 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001621
1622 return err;
1623}
1624
Brian Paul7aebaf32005-10-30 21:23:23 +00001625
Jouk Jansen40322e12004-04-05 08:50:36 +00001626/**
1627 * This translates between a binary token for an output variable type
1628 * and the mesa token for the same thing.
1629 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001630 * \param inst The parsed tokens
1631 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001632 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001633 */
1634static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001635parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001636 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001637{
Brian Paul7aebaf32005-10-30 21:23:23 +00001638 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001639
Brian Paul7aebaf32005-10-30 21:23:23 +00001640 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001642 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001643 GLuint out_color;
1644
Brian Paulbe76b7f2004-10-04 14:40:05 +00001645 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001646 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001647 */
1648 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001649 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001650 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001651 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001652 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001653 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1654 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001655 }
1656 break;
1657
1658 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001659 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001660 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001661 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001662 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001663 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001664 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001665 GLint color_type;
1666 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001667 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1668 if (err)
1669 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001670
Jouk Jansen40322e12004-04-05 08:50:36 +00001671 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001672 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001673 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001674 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001675 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001676 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001677 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001678 }
1679 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001680 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001681 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001682 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001683 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001684 }
1685 /* primary color */
1686 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001687 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001688 }
1689 }
1690 }
1691 break;
1692
1693 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001694 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001695 break;
1696
1697 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001698 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001699 break;
1700
1701 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001702 {
1703 GLuint unit;
1704 if (parse_texcoord_num (ctx, inst, Program, &unit))
1705 return 1;
1706 *outputReg = VERT_RESULT_TEX0 + unit;
1707 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001708 break;
1709 }
1710
Brian Paulde997602005-11-12 17:53:14 +00001711 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001712
1713 return 0;
1714}
1715
Brian Paul7aebaf32005-10-30 21:23:23 +00001716
Jouk Jansen40322e12004-04-05 08:50:36 +00001717/**
1718 * This handles the declaration of ATTRIB variables
1719 *
1720 * XXX: Still needs
1721 * parse_vert_attrib_binding(), or something like that
1722 *
1723 * \return 0 on sucess, 1 on error
1724 */
1725static GLint
Brian Paula088f162006-09-05 23:08:51 +00001726parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001727 struct arb_program *Program)
1728{
1729 GLuint found;
Jouk Jansen40322e12004-04-05 08:50:36 +00001730 struct var_cache *attrib_var;
1731
1732 attrib_var = parse_string (inst, vc_head, Program, &found);
1733 Program->Position = parse_position (inst);
1734 if (found) {
Brian50a20d42008-03-27 15:51:07 -06001735 program_error2(ctx, Program->Position,
1736 "Duplicate variable declaration",
1737 (char *) attrib_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001738 return 1;
1739 }
1740
1741 attrib_var->type = vt_attrib;
1742
Brian Paul7aebaf32005-10-30 21:23:23 +00001743 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001744 &attrib_var->attrib_is_generic))
1745 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001746
Brian Paul7aebaf32005-10-30 21:23:23 +00001747 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001748 program_error(ctx, Program->Position,
1749 "Cannot use both a generic vertex attribute "
1750 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001751 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001752 }
1753
1754 Program->Base.NumAttributes++;
1755 return 0;
1756}
1757
1758/**
1759 * \param use -- TRUE if we're called when declaring implicit parameters,
1760 * FALSE if we're declaraing variables. This has to do with
1761 * if we get a signed or unsigned float for scalar constants
1762 */
1763static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001764parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001765 struct var_cache *param_var,
1766 struct arb_program *Program, GLboolean use)
1767{
1768 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001769 GLuint err = 0;
Brianaa9d22a2007-02-23 11:21:03 -07001770 gl_state_index state_tokens[STATE_LENGTH];
Jouk Jansen40322e12004-04-05 08:50:36 +00001771 GLfloat const_values[4];
1772
Jouk Jansen40322e12004-04-05 08:50:36 +00001773 switch (*(*inst)++) {
1774 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001775 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1776 return 1;
1777
1778 /* If we adding STATE_MATRIX that has multiple rows, we need to
1779 * unroll it and call _mesa_add_state_reference() for each row
1780 */
Brian65319522007-02-21 11:08:21 -07001781 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1782 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1783 state_tokens[0] == STATE_MVP_MATRIX ||
1784 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1785 state_tokens[0] == STATE_PROGRAM_MATRIX)
1786 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001787 GLint row;
Brian65319522007-02-21 11:08:21 -07001788 const GLint first_row = state_tokens[2];
1789 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001790
1791 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001792 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001793
Brian Paulde997602005-11-12 17:53:14 +00001794 idx = _mesa_add_state_reference(Program->Base.Parameters,
1795 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001796 if (param_var->param_binding_begin == ~0U)
1797 param_var->param_binding_begin = idx;
1798 param_var->param_binding_length++;
1799 Program->Base.NumParameters++;
1800 }
1801 }
1802 else {
Brian Paulde997602005-11-12 17:53:14 +00001803 idx = _mesa_add_state_reference(Program->Base.Parameters,
1804 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001805 if (param_var->param_binding_begin == ~0U)
1806 param_var->param_binding_begin = idx;
1807 param_var->param_binding_length++;
1808 Program->Base.NumParameters++;
1809 }
1810 break;
1811
1812 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001813 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1814 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001815 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001816 if (param_var->param_binding_begin == ~0U)
1817 param_var->param_binding_begin = idx;
1818 param_var->param_binding_length++;
1819 Program->Base.NumParameters++;
1820
1821 /* Check if there is more: 0 -> we're done, else its an integer */
1822 if (**inst) {
1823 GLuint out_of_range, new_idx;
1824 GLuint start_idx = state_tokens[2] + 1;
1825 GLuint end_idx = parse_integer (inst, Program);
1826
1827 out_of_range = 0;
1828 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1829 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001830 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001831 || ((state_tokens[1] == STATE_LOCAL)
1832 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001833 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001834 out_of_range = 1;
1835 }
1836 else {
1837 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001838 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001839 || ((state_tokens[1] == STATE_LOCAL)
1840 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001841 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001842 out_of_range = 1;
1843 }
1844 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001845 program_error(ctx, Program->Position,
1846 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001847 return 1;
1848 }
1849
1850 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1851 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001852 idx = _mesa_add_state_reference(Program->Base.Parameters,
1853 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001854 param_var->param_binding_length++;
1855 Program->Base.NumParameters++;
1856 }
1857 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001858 else {
1859 (*inst)++;
1860 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001861 break;
1862
1863 case PARAM_CONSTANT:
Brian7cbfe8c2008-01-18 12:45:55 -07001864 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
Jouk Jansen40322e12004-04-05 08:50:36 +00001865 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001866 idx = _mesa_add_named_constant(Program->Base.Parameters,
1867 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001868 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001869 if (param_var->param_binding_begin == ~0U)
1870 param_var->param_binding_begin = idx;
Brian7cbfe8c2008-01-18 12:45:55 -07001871 param_var->param_binding_type = PROGRAM_CONSTANT;
Jouk Jansen40322e12004-04-05 08:50:36 +00001872 param_var->param_binding_length++;
1873 Program->Base.NumParameters++;
1874 break;
1875
1876 default:
Brian Paula088f162006-09-05 23:08:51 +00001877 program_error(ctx, Program->Position,
1878 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001879 return 1;
1880 }
1881
1882 /* Make sure we haven't blown past our parameter limits */
1883 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1884 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001885 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001886 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1887 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001888 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001889 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001890 return 1;
1891 }
1892
1893 return err;
1894}
1895
Brian Paul7aebaf32005-10-30 21:23:23 +00001896
Jouk Jansen40322e12004-04-05 08:50:36 +00001897/**
1898 * This picks out PARAM program parameter bindings.
1899 *
1900 * XXX: This needs to be stressed & tested
1901 *
1902 * \return 0 on sucess, 1 on error
1903 */
1904static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001905parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001906 struct arb_program *Program)
1907{
Brian Paulbd997cd2004-07-20 21:12:56 +00001908 GLuint found, err;
1909 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001910 struct var_cache *param_var;
1911
1912 err = 0;
1913 param_var = parse_string (inst, vc_head, Program, &found);
1914 Program->Position = parse_position (inst);
1915
1916 if (found) {
Brian50a20d42008-03-27 15:51:07 -06001917 program_error2(ctx, Program->Position,
1918 "Duplicate variable declaration",
1919 (char *) param_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001920 return 1;
1921 }
1922
1923 specified_length = parse_integer (inst, Program);
1924
1925 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001926 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001927 return 1;
1928 }
1929
1930 param_var->type = vt_param;
1931 param_var->param_binding_length = 0;
1932
1933 /* Right now, everything is shoved into the main state register file.
1934 *
1935 * In the future, it would be nice to leave things ENV/LOCAL params
1936 * in their respective register files, if possible
1937 */
1938 param_var->param_binding_type = PROGRAM_STATE_VAR;
1939
1940 /* Remember to:
1941 * * - add each guy to the parameter list
1942 * * - increment the param_var->param_binding_len
1943 * * - store the param_var->param_binding_begin for the first one
1944 * * - compare the actual len to the specified len at the end
1945 */
1946 while (**inst != PARAM_NULL) {
1947 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1948 return 1;
1949 }
1950
1951 /* Test array length here! */
1952 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001953 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001954 program_error(ctx, Program->Position,
1955 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001956 }
1957 }
1958
1959 (*inst)++;
1960
1961 return 0;
1962}
1963
1964/**
1965 *
1966 */
1967static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001968parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001969 struct arb_program *Program, struct var_cache **new_var)
1970{
1971 struct var_cache *param_var;
1972
1973 /* First, insert a dummy entry into the var_cache */
1974 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001975 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001976 param_var->type = vt_param;
1977
1978 param_var->param_binding_length = 0;
1979 /* Don't fill in binding_begin; We use the default value of -1
1980 * to tell if its already initialized, elsewhere.
1981 *
1982 * param_var->param_binding_begin = 0;
1983 */
1984 param_var->param_binding_type = PROGRAM_STATE_VAR;
1985
1986 var_cache_append (vc_head, param_var);
1987
1988 /* Then fill it with juicy parameter goodness */
1989 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1990 return 1;
1991
1992 *new_var = param_var;
1993
1994 return 0;
1995}
1996
1997
1998/**
1999 * This handles the declaration of TEMP variables
2000 *
2001 * \return 0 on sucess, 1 on error
2002 */
2003static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002004parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002005 struct arb_program *Program)
2006{
2007 GLuint found;
2008 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002009
2010 while (**inst != 0) {
2011 temp_var = parse_string (inst, vc_head, Program, &found);
2012 Program->Position = parse_position (inst);
2013 if (found) {
Brian50a20d42008-03-27 15:51:07 -06002014 program_error2(ctx, Program->Position,
2015 "Duplicate variable declaration",
2016 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002017 return 1;
2018 }
2019
2020 temp_var->type = vt_temp;
2021
2022 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2023 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002024 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002025 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2026 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002027 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00002028 program_error(ctx, Program->Position,
2029 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002030 return 1;
2031 }
2032
2033 temp_var->temp_binding = Program->Base.NumTemporaries;
2034 Program->Base.NumTemporaries++;
2035 }
2036 (*inst)++;
2037
2038 return 0;
2039}
2040
2041/**
2042 * This handles variables of the OUTPUT variety
2043 *
2044 * \return 0 on sucess, 1 on error
2045 */
2046static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002047parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002048 struct arb_program *Program)
2049{
2050 GLuint found;
2051 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002052 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002053
2054 output_var = parse_string (inst, vc_head, Program, &found);
2055 Program->Position = parse_position (inst);
2056 if (found) {
Brian50a20d42008-03-27 15:51:07 -06002057 program_error2(ctx, Program->Position,
2058 "Duplicate variable declaration",
2059 (char *) output_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002060 return 1;
2061 }
2062
2063 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002064
2065 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2066 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002067}
2068
2069/**
2070 * This handles variables of the ALIAS kind
2071 *
2072 * \return 0 on sucess, 1 on error
2073 */
2074static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002075parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002076 struct arb_program *Program)
2077{
2078 GLuint found;
2079 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002080
2081 temp_var = parse_string (inst, vc_head, Program, &found);
2082 Program->Position = parse_position (inst);
2083
2084 if (found) {
Brian50a20d42008-03-27 15:51:07 -06002085 program_error2(ctx, Program->Position,
2086 "Duplicate variable declaration",
2087 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002088 return 1;
2089 }
2090
2091 temp_var->type = vt_alias;
2092 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2093 Program->Position = parse_position (inst);
2094
2095 if (!found)
2096 {
Brian50a20d42008-03-27 15:51:07 -06002097 program_error2(ctx, Program->Position,
2098 "Undefined alias value",
2099 (char *) temp_var->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002100 return 1;
2101 }
2102
2103 return 0;
2104}
2105
2106/**
2107 * This handles variables of the ADDRESS kind
2108 *
2109 * \return 0 on sucess, 1 on error
2110 */
2111static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002112parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002113 struct arb_program *Program)
2114{
2115 GLuint found;
2116 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002117
2118 while (**inst != 0) {
2119 temp_var = parse_string (inst, vc_head, Program, &found);
2120 Program->Position = parse_position (inst);
2121 if (found) {
Brian50a20d42008-03-27 15:51:07 -06002122 program_error2(ctx, Program->Position,
2123 "Duplicate variable declaration",
2124 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002125 return 1;
2126 }
2127
2128 temp_var->type = vt_address;
2129
2130 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002131 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002132 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002133 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002134 return 1;
2135 }
2136
2137 temp_var->address_binding = Program->Base.NumAddressRegs;
2138 Program->Base.NumAddressRegs++;
2139 }
2140 (*inst)++;
2141
2142 return 0;
2143}
2144
2145/**
2146 * Parse a program declaration
2147 *
2148 * \return 0 on sucess, 1 on error
2149 */
2150static GLint
Brian Paula088f162006-09-05 23:08:51 +00002151parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002152 struct arb_program *Program)
2153{
2154 GLint err = 0;
2155
2156 switch (*(*inst)++) {
2157 case ADDRESS:
2158 err = parse_address (ctx, inst, vc_head, Program);
2159 break;
2160
2161 case ALIAS:
2162 err = parse_alias (ctx, inst, vc_head, Program);
2163 break;
2164
2165 case ATTRIB:
2166 err = parse_attrib (ctx, inst, vc_head, Program);
2167 break;
2168
2169 case OUTPUT:
2170 err = parse_output (ctx, inst, vc_head, Program);
2171 break;
2172
2173 case PARAM:
2174 err = parse_param (ctx, inst, vc_head, Program);
2175 break;
2176
2177 case TEMP:
2178 err = parse_temp (ctx, inst, vc_head, Program);
2179 break;
2180 }
2181
2182 return err;
2183}
2184
2185/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002186 * Handle the parsing out of a masked destination register, either for a
2187 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002188 *
2189 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002190 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002191 * position invariant
2192 *
2193 * \param File - The register file we write to
2194 * \param Index - The register index we write to
2195 * \param WriteMask - The mask controlling which components we write (1->write)
2196 *
2197 * \return 0 on sucess, 1 on error
2198 */
2199static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002200parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002201 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002202 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002203{
Brian Paul7aebaf32005-10-30 21:23:23 +00002204 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002205 struct var_cache *dst;
2206
2207 /* We either have a result register specified, or a
2208 * variable that may or may not be writable
2209 */
2210 switch (*(*inst)++) {
2211 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002212 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002213 return 1;
2214 *File = PROGRAM_OUTPUT;
2215 break;
2216
2217 case REGISTER_ESTABLISHED_NAME:
2218 dst = parse_string (inst, vc_head, Program, &result);
2219 Program->Position = parse_position (inst);
2220
2221 /* If the name has never been added to our symbol table, we're hosed */
2222 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002223 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002224 return 1;
2225 }
2226
2227 switch (dst->type) {
2228 case vt_output:
2229 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002230 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002231 break;
2232
2233 case vt_temp:
2234 *File = PROGRAM_TEMPORARY;
2235 *Index = dst->temp_binding;
2236 break;
2237
2238 /* If the var type is not vt_output or vt_temp, no go */
2239 default:
Brian Paula088f162006-09-05 23:08:51 +00002240 program_error(ctx, Program->Position,
2241 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002242 return 1;
2243 }
2244 break;
2245
2246 default:
Brian Paula088f162006-09-05 23:08:51 +00002247 program_error(ctx, Program->Position,
2248 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002249 return 1;
2250 }
2251
2252
2253 /* Position invariance test */
2254 /* This test is done now in syntax portion - when position invariance OPTION
2255 is specified, "result.position" rule is disabled so there is no way
2256 to write the position
2257 */
2258 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2259 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002260 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002261 "Vertex program specified position invariance and wrote vertex position");
2262 }*/
2263
2264 /* And then the mask.
2265 * w,a -> bit 0
2266 * z,b -> bit 1
2267 * y,g -> bit 2
2268 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002269 *
2270 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002271 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002272 tmp = (GLint) *(*inst)++;
2273 *WriteMask = (((tmp>>3) & 0x1) |
2274 ((tmp>>1) & 0x2) |
2275 ((tmp<<1) & 0x4) |
2276 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002277
2278 return 0;
2279}
2280
2281
2282/**
2283 * Handle the parsing of a address register
2284 *
2285 * \param Index - The register index we write to
2286 *
2287 * \return 0 on sucess, 1 on error
2288 */
2289static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002290parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002291 struct var_cache **vc_head,
2292 struct arb_program *Program, GLint * Index)
2293{
2294 struct var_cache *dst;
2295 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002296
2297 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002298
2299 dst = parse_string (inst, vc_head, Program, &result);
2300 Program->Position = parse_position (inst);
2301
2302 /* If the name has never been added to our symbol table, we're hosed */
2303 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002304 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002305 return 1;
2306 }
2307
2308 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002309 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002310 return 1;
2311 }
2312
2313 return 0;
2314}
2315
Brian Paul8e8fa632005-07-01 02:03:33 +00002316#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002317/**
2318 * Handle the parsing out of a masked address register
2319 *
2320 * \param Index - The register index we write to
2321 * \param WriteMask - The mask controlling which components we write (1->write)
2322 *
2323 * \return 0 on sucess, 1 on error
2324 */
2325static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002326parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002327 struct var_cache **vc_head,
2328 struct arb_program *Program, GLint * Index,
2329 GLboolean * WriteMask)
2330{
2331 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2332 return 1;
2333
2334 /* This should be 0x8 */
2335 (*inst)++;
2336
2337 /* Writemask of .x is implied */
2338 WriteMask[0] = 1;
2339 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2340
2341 return 0;
2342}
Brian Paul8e8fa632005-07-01 02:03:33 +00002343#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002344
2345/**
2346 * Parse out a swizzle mask.
2347 *
Brian Paul32df89e2005-10-29 18:26:43 +00002348 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002349 *
2350 * The len parameter allows us to grab 4 components for a vector
2351 * swizzle, or just 1 component for a scalar src register selection
2352 */
Brian Paul32df89e2005-10-29 18:26:43 +00002353static void
Brian Paula088f162006-09-05 23:08:51 +00002354parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002355{
Brian Paul32df89e2005-10-29 18:26:43 +00002356 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002357
Brian Paul32df89e2005-10-29 18:26:43 +00002358 for (i = 0; i < 4; i++)
2359 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002360
Brian Paul32df89e2005-10-29 18:26:43 +00002361 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002362 switch (*(*inst)++) {
2363 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002364 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002365 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002366 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002367 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002368 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002369 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002370 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002371 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002372 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002373 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002374 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002375 default:
2376 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2377 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002378 }
2379 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002380}
2381
Jouk Jansen40322e12004-04-05 08:50:36 +00002382
Brian Paul32df89e2005-10-29 18:26:43 +00002383/**
2384 * Parse an extended swizzle mask which is a sequence of
2385 * four x/y/z/w/0/1 tokens.
2386 * \return swizzle four swizzle values
2387 * \return negateMask four element bitfield
2388 */
2389static void
Brian Paula088f162006-09-05 23:08:51 +00002390parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002391 GLubyte *negateMask)
2392{
2393 GLint i;
2394
2395 *negateMask = 0x0;
2396 for (i = 0; i < 4; i++) {
2397 GLubyte swz;
2398 if (parse_sign(inst) == -1)
2399 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002400
2401 swz = *(*inst)++;
2402
2403 switch (swz) {
2404 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002405 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002406 break;
2407 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002408 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002409 break;
2410 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002411 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002412 break;
2413 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002414 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002415 break;
2416 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002417 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002418 break;
2419 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002420 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002421 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002422 default:
2423 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2424 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002425 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002426 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002427}
2428
2429
2430static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002431parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2432 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002433 struct arb_program *Program,
2434 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002435 GLboolean *IsRelOffset )
2436{
2437 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002438 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002439 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002440
Keith Whitwell7c26b612005-04-21 14:46:57 +00002441 *IsRelOffset = 0;
2442
Jouk Jansen40322e12004-04-05 08:50:36 +00002443 /* And the binding for the src */
2444 switch (*(*inst)++) {
2445 case REGISTER_ATTRIB:
2446 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002447 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002448 return 1;
2449 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002450 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002451
2452 /* We need to insert a dummy variable into the var_cache so we can
2453 * catch generic vertex attrib aliasing errors
2454 */
2455 var_cache_create(&src);
2456 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002457 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002458 src->attrib_binding = binding;
2459 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002460 var_cache_append(vc_head, src);
2461 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002462 program_error(ctx, Program->Position,
2463 "Cannot use both a generic vertex attribute "
2464 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002465 return 1;
2466 }
2467 break;
2468
2469 case REGISTER_PARAM:
2470 switch (**inst) {
2471 case PARAM_ARRAY_ELEMENT:
2472 (*inst)++;
2473 src = parse_string (inst, vc_head, Program, &found);
2474 Program->Position = parse_position (inst);
2475
2476 if (!found) {
Brian50a20d42008-03-27 15:51:07 -06002477 program_error2(ctx, Program->Position,
2478 "Undefined variable",
2479 (char *) src->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002480 return 1;
2481 }
2482
Brian Paul95801792005-12-06 15:41:43 +00002483 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002484
2485 switch (*(*inst)++) {
2486 case ARRAY_INDEX_ABSOLUTE:
2487 offset = parse_integer (inst, Program);
2488
2489 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002490 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002491 program_error(ctx, Program->Position,
2492 "Index out of range");
2493 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002494 return 1;
2495 }
2496
2497 *Index = src->param_binding_begin + offset;
2498 break;
2499
2500 case ARRAY_INDEX_RELATIVE:
2501 {
2502 GLint addr_reg_idx, rel_off;
2503
2504 /* First, grab the address regiseter */
2505 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2506 return 1;
2507
2508 /* And the .x */
2509 ((*inst)++);
2510 ((*inst)++);
2511 ((*inst)++);
2512 ((*inst)++);
2513
2514 /* Then the relative offset */
2515 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2516
2517 /* And store it properly */
2518 *Index = src->param_binding_begin + rel_off;
2519 *IsRelOffset = 1;
2520 }
2521 break;
2522 }
2523 break;
2524
2525 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002526 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2527 return 1;
2528
Brian Paul95801792005-12-06 15:41:43 +00002529 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002530 *Index = src->param_binding_begin;
2531 break;
2532 }
2533 break;
2534
2535 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002536 src = parse_string (inst, vc_head, Program, &found);
2537 Program->Position = parse_position (inst);
2538
2539 /* If the name has never been added to our symbol table, we're hosed */
2540 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002541 program_error(ctx, Program->Position,
2542 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002543 return 1;
2544 }
2545
2546 switch (src->type) {
2547 case vt_attrib:
2548 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002549 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002550 break;
2551
2552 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2553 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002554 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002555 *Index = src->param_binding_begin;
2556 break;
2557
2558 case vt_temp:
2559 *File = PROGRAM_TEMPORARY;
2560 *Index = src->temp_binding;
2561 break;
2562
2563 /* If the var type is vt_output no go */
2564 default:
Brian Paula088f162006-09-05 23:08:51 +00002565 program_error(ctx, Program->Position,
2566 "destination register is read only");
2567 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002568 return 1;
2569 }
2570 break;
2571
2572 default:
Brian Paula088f162006-09-05 23:08:51 +00002573 program_error(ctx, Program->Position,
2574 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002575 return 1;
2576 }
2577
Markus Amslerf3c49062008-03-17 08:35:37 -06002578 /* Add attributes to InputsRead only if they are used the program.
2579 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2580 if (*File == PROGRAM_INPUT)
2581 Program->Base.InputsRead |= (1 << *Index);
2582
Jouk Jansen40322e12004-04-05 08:50:36 +00002583 return 0;
2584}
2585
2586/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002587 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002588 */
2589static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002590parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002591 struct var_cache **vc_head,
2592 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002593 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002594{
Brian Paul7aebaf32005-10-30 21:23:23 +00002595 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002596 GLint index;
2597 GLboolean negate;
2598 GLubyte swizzle[4];
2599 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002600
Jouk Jansen40322e12004-04-05 08:50:36 +00002601 /* Grab the sign */
Brian Paulbedb93e2008-03-25 11:22:57 -06002602 negate = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002603
2604 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002605 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002606 return 1;
2607
2608 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002609 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002610
Brian Paul32df89e2005-10-29 18:26:43 +00002611 reg->File = file;
2612 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002613 reg->NegateBase = negate;
2614 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002615 return 0;
2616}
2617
Jouk Jansen40322e12004-04-05 08:50:36 +00002618
Brian Paulb7fc1c32006-08-30 23:38:03 +00002619/**
2620 * Parse fragment program destination register.
2621 * \return 1 if error, 0 if no error.
2622 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002623static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002624parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002625 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002626 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002627{
Brian Paul7aebaf32005-10-30 21:23:23 +00002628 GLint mask;
2629 GLuint idx;
2630 enum register_file file;
2631
Keith Whitwell7c26b612005-04-21 14:46:57 +00002632 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002633 return 1;
2634
Keith Whitwell7c26b612005-04-21 14:46:57 +00002635 reg->File = file;
2636 reg->Index = idx;
2637 reg->WriteMask = mask;
2638 return 0;
2639}
2640
2641
Brian Paul7aebaf32005-10-30 21:23:23 +00002642/**
2643 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002644 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002645 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002646static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002647parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002648 struct var_cache **vc_head,
2649 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002650 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002651{
Brian Paul7aebaf32005-10-30 21:23:23 +00002652 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002653 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002654 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002655 GLubyte Swizzle[4];
2656 GLboolean IsRelOffset;
2657
2658 /* Grab the sign */
Brian Paulbedb93e2008-03-25 11:22:57 -06002659 Negate = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002660
2661 /* And the src reg */
2662 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2663 return 1;
2664
2665 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002666 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002667
Keith Whitwell7c26b612005-04-21 14:46:57 +00002668 reg->File = File;
2669 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002670 reg->NegateBase = Negate;
2671 reg->Swizzle = (Swizzle[0] << 0);
2672
Jouk Jansen40322e12004-04-05 08:50:36 +00002673 return 0;
2674}
2675
Keith Whitwell7c26b612005-04-21 14:46:57 +00002676
Jouk Jansen40322e12004-04-05 08:50:36 +00002677/**
2678 * This is a big mother that handles getting opcodes into the instruction
2679 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002680 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002681 */
2682static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002683parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002684 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002685 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002686{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002687 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002688 GLuint texcoord;
2689 GLubyte instClass, type, code;
2690 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002691 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002692
Brian Pauld6272e02006-10-29 18:03:16 +00002693 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002694
2695 /* Record the position in the program string for debugging */
2696 fp->StringPos = Program->Position;
2697
2698 /* OP_ALU_INST or OP_TEX_INST */
2699 instClass = *(*inst)++;
2700
2701 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2702 * OP_TEX_{SAMPLE, KIL}
2703 */
2704 type = *(*inst)++;
2705
2706 /* The actual opcode name */
2707 code = *(*inst)++;
2708
2709 /* Increment the correct count */
2710 switch (instClass) {
2711 case OP_ALU_INST:
2712 Program->NumAluInstructions++;
2713 break;
2714 case OP_TEX_INST:
2715 Program->NumTexInstructions++;
2716 break;
2717 }
2718
Jouk Jansen40322e12004-04-05 08:50:36 +00002719 switch (type) {
2720 case OP_ALU_VECTOR:
2721 switch (code) {
2722 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002723 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002724 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002725 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002726 break;
2727
2728 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002729 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002730 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002731 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002732 break;
2733
2734 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002735 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002736 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002737 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002738 break;
2739
2740 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002741 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002742 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002743 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002744 break;
2745
2746 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002747 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002748 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002749 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002750 break;
2751 }
2752
Keith Whitwell7c26b612005-04-21 14:46:57 +00002753 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 return 1;
2755
Keith Whitwell7c26b612005-04-21 14:46:57 +00002756 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002757 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002758 break;
2759
2760 case OP_ALU_SCALAR:
2761 switch (code) {
2762 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002763 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002764 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002765 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002766 break;
2767
2768 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002769 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002770 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002771 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002772 break;
2773
2774 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002775 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002776 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002777 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002778 break;
2779
2780 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002781 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002783 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002784 break;
2785
2786 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002787 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002788 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002789 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002790 break;
2791
2792 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002793 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002794 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002795 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002796 break;
2797
2798 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002799 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 case OP_SCS:
2801
Brian Paul7e807512005-11-05 17:10:45 +00002802 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002803 break;
2804 }
2805
Keith Whitwell7c26b612005-04-21 14:46:57 +00002806 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002808
2809 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002810 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002811 break;
2812
2813 case OP_ALU_BINSC:
2814 switch (code) {
2815 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002816 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002817 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002818 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 break;
2820 }
2821
Keith Whitwell7c26b612005-04-21 14:46:57 +00002822 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002823 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002824
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002826 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002827 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002828 }
2829 break;
2830
2831
2832 case OP_ALU_BIN:
2833 switch (code) {
2834 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002835 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002836 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002837 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002838 break;
2839
2840 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002841 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002842 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002843 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 break;
2845
2846 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002847 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002848 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002849 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002850 break;
2851
2852 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002853 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002854 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002855 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002856 break;
2857
2858 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002859 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002860 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002861 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002862 break;
2863
2864 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002865 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002866 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002867 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002868 break;
2869
2870 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002871 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002872 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002873 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002874 break;
2875
2876 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002877 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002878 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002879 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002880 break;
2881
2882 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002883 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002884 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002885 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002886 break;
2887
2888 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002889 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002890 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002891 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002892 break;
2893
2894 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002895 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002896 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002897 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002898 break;
2899
2900 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002901 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002902 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002903 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002904 break;
2905 }
2906
Keith Whitwell7c26b612005-04-21 14:46:57 +00002907 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 return 1;
2909 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002910 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2911 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002912 }
2913 break;
2914
2915 case OP_ALU_TRI:
2916 switch (code) {
2917 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002918 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002919 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002920 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002921 break;
2922
2923 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002924 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002925 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002926 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002927 break;
2928
2929 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002930 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002931 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002932 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002933 break;
2934 }
2935
Keith Whitwell7c26b612005-04-21 14:46:57 +00002936 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002937 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002938
Jouk Jansen40322e12004-04-05 08:50:36 +00002939 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002940 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2941 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002942 }
2943 break;
2944
2945 case OP_ALU_SWZ:
2946 switch (code) {
2947 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002948 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002949 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002950 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002951 break;
2952 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002953 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002954 return 1;
2955
Keith Whitwell7c26b612005-04-21 14:46:57 +00002956 {
Brian Paul32df89e2005-10-29 18:26:43 +00002957 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002958 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002959 enum register_file file;
2960 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002961
Brian Paul32df89e2005-10-29 18:26:43 +00002962 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002963 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002964 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2965 fp->SrcReg[0].File = file;
2966 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002967 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002968 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2969 swizzle[1],
2970 swizzle[2],
2971 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002972 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002973 break;
2974
2975 case OP_TEX_SAMPLE:
2976 switch (code) {
2977 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002978 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002979 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002980 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002981 break;
2982
2983 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002984 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002985 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002986 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002987 break;
2988
2989 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002990 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002991 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002992 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002993 break;
2994 }
2995
Keith Whitwell7c26b612005-04-21 14:46:57 +00002996 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002997 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002998
2999 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003000 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003001
3002 /* texImageUnit */
3003 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3004 return 1;
3005 fp->TexSrcUnit = texcoord;
3006
3007 /* texTarget */
3008 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07003009 case TEXTARGET_SHADOW1D:
3010 shadow_tex = 1 << texcoord;
3011 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003012 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003013 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003014 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003015 case TEXTARGET_SHADOW2D:
3016 shadow_tex = 1 << texcoord;
3017 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003018 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003019 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003020 break;
3021 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003022 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003023 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003024 case TEXTARGET_SHADOWRECT:
3025 shadow_tex = 1 << texcoord;
3026 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003027 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003028 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003029 break;
3030 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003031 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003032 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003033 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003034 shadow_tex = 1 << texcoord;
3035 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003036 case TEXTARGET_1D_ARRAY:
3037 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3038 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003039 case TEXTARGET_SHADOW2D_ARRAY:
3040 shadow_tex = 1 << texcoord;
3041 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003042 case TEXTARGET_2D_ARRAY:
3043 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3044 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003045 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003046
3047 /* Don't test the first time a particular sampler is seen. Each time
3048 * after that, make sure the shadow state is the same.
3049 */
3050 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3051 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3052 program_error(ctx, Program->Position,
3053 "texture image unit used for shadow sampling and non-shadow sampling");
3054 return 1;
3055 }
3056
Brian Paulb7fc1c32006-08-30 23:38:03 +00003057 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3058 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3059 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003060 program_error(ctx, Program->Position,
3061 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003062 return 1;
3063 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003064
3065
3066 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003067 break;
3068
3069 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003070 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003071 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003072 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003073 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003074 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003075 default:
3076 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3077 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003078 }
3079
3080 return 0;
3081}
3082
Keith Whitwell7c26b612005-04-21 14:46:57 +00003083static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003084parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003085 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003086 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003087{
Brian Paul7aebaf32005-10-30 21:23:23 +00003088 GLint mask;
3089 GLuint idx;
3090 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003091
3092 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3093 return 1;
3094
3095 reg->File = file;
3096 reg->Index = idx;
3097 reg->WriteMask = mask;
3098 return 0;
3099}
3100
3101/**
3102 * Handle the parsing out of a masked address register
3103 *
3104 * \param Index - The register index we write to
3105 * \param WriteMask - The mask controlling which components we write (1->write)
3106 *
3107 * \return 0 on sucess, 1 on error
3108 */
3109static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003110parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003111 struct var_cache **vc_head,
3112 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003113 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003114{
3115 GLint idx;
3116
3117 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3118 return 1;
3119
3120 /* This should be 0x8 */
3121 (*inst)++;
3122
3123 reg->File = PROGRAM_ADDRESS;
3124 reg->Index = idx;
3125
3126 /* Writemask of .x is implied */
3127 reg->WriteMask = 0x1;
3128 return 0;
3129}
3130
3131/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003132 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003133 */
3134static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003135parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003136 struct var_cache **vc_head,
3137 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003138 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003139{
Brian Paul7aebaf32005-10-30 21:23:23 +00003140 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003141 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003142 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003143 GLubyte swizzle[4];
3144 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003145
3146 /* Grab the sign */
Brian Paulbedb93e2008-03-25 11:22:57 -06003147 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003148
3149 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003150 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003151 return 1;
3152
3153 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003154 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003155
Brian Paul32df89e2005-10-29 18:26:43 +00003156 reg->File = file;
3157 reg->Index = index;
3158 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3159 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003160 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003161 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003162 return 0;
3163}
3164
3165
3166static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003167parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003168 struct var_cache **vc_head,
3169 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003170 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003171{
Brian Paul7aebaf32005-10-30 21:23:23 +00003172 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003173 GLint Index;
Brian Paulbedb93e2008-03-25 11:22:57 -06003174 GLubyte negateMask;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003175 GLubyte Swizzle[4];
3176 GLboolean IsRelOffset;
3177
3178 /* Grab the sign */
Brian Paulbedb93e2008-03-25 11:22:57 -06003179 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003180
3181 /* And the src reg */
3182 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3183 return 1;
3184
3185 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003186 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003187
3188 reg->File = File;
3189 reg->Index = Index;
3190 reg->Swizzle = (Swizzle[0] << 0);
Brian Paulbedb93e2008-03-25 11:22:57 -06003191 reg->NegateBase = negateMask;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003192 reg->RelAddr = IsRelOffset;
3193 return 0;
3194}
3195
3196
Jouk Jansen40322e12004-04-05 08:50:36 +00003197/**
3198 * This is a big mother that handles getting opcodes into the instruction
3199 * and handling the src & dst registers for vertex program instructions
3200 */
3201static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003202parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003203 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003204 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003205{
3206 GLint a;
3207 GLubyte type, code;
3208
3209 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3210 type = *(*inst)++;
3211
3212 /* The actual opcode name */
3213 code = *(*inst)++;
3214
Brian Pauld6272e02006-10-29 18:03:16 +00003215 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003216 /* Record the position in the program string for debugging */
3217 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003218
3219 switch (type) {
3220 /* XXX: */
3221 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003222 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003223
3224 /* Remember to set SrcReg.RelAddr; */
3225
3226 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003227 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003229
Jouk Jansen40322e12004-04-05 08:50:36 +00003230 vp->DstReg.File = PROGRAM_ADDRESS;
3231
3232 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003233 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003234 return 1;
3235
3236 break;
3237
3238 case OP_ALU_VECTOR:
3239 switch (code) {
3240 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003241 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003242 break;
3243 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003244 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 break;
3246 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003247 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 break;
3249 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003250 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 break;
3252 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003253 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 break;
3255 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003256
3257 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003258 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003259
3260 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003261 return 1;
3262 break;
3263
3264 case OP_ALU_SCALAR:
3265 switch (code) {
3266 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003267 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003268 break;
3269 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003270 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003271 break;
3272 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003273 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003274 break;
3275 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003276 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003277 break;
3278 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003279 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003280 break;
3281 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003282 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003283 break;
3284 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003285 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003286 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003287
3288 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003289 return 1;
3290 break;
3291
3292 case OP_ALU_BINSC:
3293 switch (code) {
3294 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003295 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003296 break;
3297 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003298 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003299 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003300
Jouk Jansen40322e12004-04-05 08:50:36 +00003301 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003302 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003303 return 1;
3304 }
3305 break;
3306
3307 case OP_ALU_BIN:
3308 switch (code) {
3309 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003310 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003311 break;
3312 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003313 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003314 break;
3315 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003316 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003317 break;
3318 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003319 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003320 break;
3321 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003322 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003323 break;
3324 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003325 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003326 break;
3327 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003328 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003329 break;
3330 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003331 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003332 break;
3333 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003334 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003335 break;
3336 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003337 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003338 break;
3339 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003340 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003341 break;
3342 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003343 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003344 break;
3345 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003346 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003347 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003348
Jouk Jansen40322e12004-04-05 08:50:36 +00003349 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003350 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003351 return 1;
3352 }
3353 break;
3354
3355 case OP_ALU_TRI:
3356 switch (code) {
3357 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003358 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003359 break;
3360 }
3361
Keith Whitwell7c26b612005-04-21 14:46:57 +00003362 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003363 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003364
Jouk Jansen40322e12004-04-05 08:50:36 +00003365 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003366 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003367 return 1;
3368 }
3369 break;
3370
3371 case OP_ALU_SWZ:
3372 switch (code) {
3373 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003374 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003375 break;
3376 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003377 {
Brian Paul32df89e2005-10-29 18:26:43 +00003378 GLubyte swizzle[4];
3379 GLubyte negateMask;
3380 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003381 enum register_file file;
3382 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003383
Keith Whitwell7c26b612005-04-21 14:46:57 +00003384 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3385 return 1;
3386
Brian Paul32df89e2005-10-29 18:26:43 +00003387 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003388 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003389 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3390 vp->SrcReg[0].File = file;
3391 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003392 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003393 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3394 swizzle[1],
3395 swizzle[2],
3396 swizzle[3]);
3397 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003398 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003399 break;
3400 }
3401 return 0;
3402}
3403
3404#if DEBUG_PARSING
3405
3406static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003407debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3408 struct arb_program *Program)
3409{
3410 struct var_cache *vc;
3411 GLint a, b;
3412
Brianb618ac82007-02-22 09:39:25 -07003413 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003414
3415 /* First of all, print out the contents of the var_cache */
3416 vc = vc_head;
3417 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003418 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003419 switch (vc->type) {
3420 case vt_none:
3421 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3422 break;
3423 case vt_attrib:
3424 fprintf (stderr, "ATTRIB %s\n", vc->name);
3425 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3426 break;
3427 case vt_param:
3428 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3429 vc->param_binding_begin, vc->param_binding_length);
3430 b = vc->param_binding_begin;
3431 for (a = 0; a < vc->param_binding_length; a++) {
3432 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003433 Program->Base.Parameters->Parameters[a + b].Name);
3434 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3435 const char *s;
3436 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3437 [a + b].StateIndexes);
3438 fprintf(stderr, "%s\n", s);
3439 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003440 }
3441 else
3442 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003443 Program->Base.Parameters->ParameterValues[a + b][0],
3444 Program->Base.Parameters->ParameterValues[a + b][1],
3445 Program->Base.Parameters->ParameterValues[a + b][2],
3446 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003447 }
3448 break;
3449 case vt_temp:
3450 fprintf (stderr, "TEMP %s\n", vc->name);
3451 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3452 break;
3453 case vt_output:
3454 fprintf (stderr, "OUTPUT %s\n", vc->name);
3455 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3456 break;
3457 case vt_alias:
3458 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003459 fprintf (stderr, " binding: 0x%p (%s)\n",
3460 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003461 break;
Brianb618ac82007-02-22 09:39:25 -07003462 default:
3463 /* nothing */
3464 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003465 }
3466 vc = vc->next;
3467 }
3468}
3469
Brian Paul72030e02005-11-03 03:30:34 +00003470#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003471
3472
3473/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003474 * The main loop for parsing a fragment or vertex program
3475 *
Brian Paul72030e02005-11-03 03:30:34 +00003476 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003477 */
Brian Paul72030e02005-11-03 03:30:34 +00003478static GLint
Brian Paula088f162006-09-05 23:08:51 +00003479parse_instructions(GLcontext * ctx, const GLubyte * inst,
3480 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003481{
Brian Paul72030e02005-11-03 03:30:34 +00003482 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3483 ? ctx->Const.FragmentProgram.MaxInstructions
3484 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003485 GLint err = 0;
3486
Brian Paul72030e02005-11-03 03:30:34 +00003487 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3488
Jouk Jansen40322e12004-04-05 08:50:36 +00003489 Program->MajorVersion = (GLuint) * inst++;
3490 Program->MinorVersion = (GLuint) * inst++;
3491
3492 while (*inst != END) {
3493 switch (*inst++) {
3494
3495 case OPTION:
3496 switch (*inst++) {
3497 case ARB_PRECISION_HINT_FASTEST:
3498 Program->PrecisionOption = GL_FASTEST;
3499 break;
3500
3501 case ARB_PRECISION_HINT_NICEST:
3502 Program->PrecisionOption = GL_NICEST;
3503 break;
3504
3505 case ARB_FOG_EXP:
3506 Program->FogOption = GL_EXP;
3507 break;
3508
3509 case ARB_FOG_EXP2:
3510 Program->FogOption = GL_EXP2;
3511 break;
3512
3513 case ARB_FOG_LINEAR:
3514 Program->FogOption = GL_LINEAR;
3515 break;
3516
3517 case ARB_POSITION_INVARIANT:
3518 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003519 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003520 break;
3521
3522 case ARB_FRAGMENT_PROGRAM_SHADOW:
3523 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3524 /* TODO ARB_fragment_program_shadow code */
3525 }
3526 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003527
3528 case ARB_DRAW_BUFFERS:
3529 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3530 /* do nothing for now */
3531 }
3532 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003533
3534 case MESA_TEXTURE_ARRAY:
3535 /* do nothing for now */
3536 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003537 }
3538 break;
3539
3540 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003541 /* check length */
3542 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003543 program_error(ctx, Program->Position,
3544 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003545 return 1;
3546 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003547 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003548 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003549 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003550 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003551 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003552 }
3553 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003554 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003555 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003556 }
3557
Brian Paul72030e02005-11-03 03:30:34 +00003558 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003559 Program->Base.NumInstructions++;
3560 break;
3561
3562 case DECLARATION:
3563 err = parse_declaration (ctx, &inst, vc_head, Program);
3564 break;
3565
3566 default:
3567 break;
3568 }
3569
3570 if (err)
3571 break;
3572 }
3573
3574 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003575 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003576 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003577 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003578 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003579 /* YYY Wrong Position in program, whatever, at least not random -> crash
3580 Program->Position = parse_position (&inst);
3581 */
Brian Paul8c41a142005-11-19 15:36:28 +00003582 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003583 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003584 Program->Base.NumInstructions++;
3585
Brian Paul05051032005-11-01 04:36:33 +00003586 /*
3587 * Initialize native counts to logical counts. The device driver may
3588 * change them if program is translated into a hardware program.
3589 */
3590 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3591 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3592 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3593 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3594 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003595
Jouk Jansen40322e12004-04-05 08:50:36 +00003596 return err;
3597}
3598
Brian Paul7aebaf32005-10-30 21:23:23 +00003599
Jouk Jansen40322e12004-04-05 08:50:36 +00003600/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003601LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003602#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003603;
3604
Brian Paul7aebaf32005-10-30 21:23:23 +00003605
Brian Paul8c41a142005-11-19 15:36:28 +00003606/**
3607 * Set a grammar parameter.
3608 * \param name the grammar parameter
3609 * \param value the new parameter value
3610 * \return 0 if OK, 1 if error
3611 */
3612static int
3613set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003614{
3615 char error_msg[300];
3616 GLint error_pos;
3617
Brian Paul8c41a142005-11-19 15:36:28 +00003618 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003619 return 0;
3620
3621 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3622 _mesa_set_program_error (ctx, error_pos, error_msg);
3623 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3624 return 1;
3625}
3626
Brian Paul8c41a142005-11-19 15:36:28 +00003627
3628/**
3629 * Enable support for the given language option in the parser.
3630 * \return 1 if OK, 0 if error
3631 */
3632static int
3633enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003634{
Brian Paul8c41a142005-11-19 15:36:28 +00003635 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003636}
3637
Brian Paul8c41a142005-11-19 15:36:28 +00003638
3639/**
3640 * Enable parser extensions based on which OpenGL extensions are supported
3641 * by this rendering context.
3642 *
3643 * \return GL_TRUE if OK, GL_FALSE if error.
3644 */
3645static GLboolean
3646enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003647{
Brian Paul8c41a142005-11-19 15:36:28 +00003648#if 0
3649 /* These are not supported at this time */
3650 if ((ctx->Extensions.ARB_vertex_blend ||
3651 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003652 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003653 return GL_FALSE;
3654 if (ctx->Extensions.ARB_matrix_palette
3655 && !enable_ext(ctx, id, "matrix_palette"))
3656 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003657#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003658 if (ctx->Extensions.ARB_fragment_program_shadow
3659 && !enable_ext(ctx, id, "fragment_program_shadow"))
3660 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003661 if (ctx->Extensions.EXT_point_parameters
3662 && !enable_ext(ctx, id, "point_parameters"))
3663 return GL_FALSE;
3664 if (ctx->Extensions.EXT_secondary_color
3665 && !enable_ext(ctx, id, "secondary_color"))
3666 return GL_FALSE;
3667 if (ctx->Extensions.EXT_fog_coord
3668 && !enable_ext(ctx, id, "fog_coord"))
3669 return GL_FALSE;
3670 if (ctx->Extensions.NV_texture_rectangle
3671 && !enable_ext(ctx, id, "texture_rectangle"))
3672 return GL_FALSE;
3673 if (ctx->Extensions.ARB_draw_buffers
3674 && !enable_ext(ctx, id, "draw_buffers"))
3675 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003676 if (ctx->Extensions.MESA_texture_array
3677 && !enable_ext(ctx, id, "texture_array"))
3678 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003679#if 1
3680 /* hack for Warcraft (see bug 8060) */
3681 enable_ext(ctx, id, "vertex_blend");
3682#endif
3683
Brian Paul8c41a142005-11-19 15:36:28 +00003684 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003685}
3686
Brian Paul8c41a142005-11-19 15:36:28 +00003687
Jouk Jansen40322e12004-04-05 08:50:36 +00003688/**
3689 * This kicks everything off.
3690 *
3691 * \param ctx - The GL Context
3692 * \param str - The program string
3693 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003694 * \param program - The arb_program struct to return all the parsed info in
3695 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003696 */
Brian Paul8c41a142005-11-19 15:36:28 +00003697static GLboolean
3698_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3699 const GLubyte *str, GLsizei len,
3700 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003701{
3702 GLint a, err, error_pos;
3703 char error_msg[300];
3704 GLuint parsed_len;
3705 struct var_cache *vc_head;
3706 grammar arbprogram_syn_id;
3707 GLubyte *parsed, *inst;
3708 GLubyte *strz = NULL;
3709 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3710
Brian Paul8c41a142005-11-19 15:36:28 +00003711 /* set the program target before parsing */
3712 program->Base.Target = target;
3713
Brian Paul7f76b8f2004-09-10 01:05:39 +00003714 /* Reset error state */
3715 _mesa_set_program_error(ctx, -1, NULL);
3716
Brian Paul8c41a142005-11-19 15:36:28 +00003717 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003719 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003720 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003721 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003722
3723 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3724 if (grammar_syn_id == 0) {
3725 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003726 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003727 _mesa_set_program_error (ctx, error_pos, error_msg);
3728 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003729 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003730 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003731 }
3732
Brian Paul8c41a142005-11-19 15:36:28 +00003733 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3734 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003735
Brian Paul49a80ca2006-04-28 15:40:11 +00003736 /* 'parsed' is unused here */
3737 _mesa_free (parsed);
3738 parsed = NULL;
3739
Brian Paul45703642005-10-29 15:52:31 +00003740 /* NOTE: we can't destroy grammar_syn_id right here because
3741 * grammar_destroy() can reset the last error
3742 */
Brian Paul8c41a142005-11-19 15:36:28 +00003743 if (err) {
3744 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003745 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3746 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003747 _mesa_error (ctx, GL_INVALID_OPERATION,
3748 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003749 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003750 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003751 }
3752
3753 grammar_destroy (grammar_syn_id);
3754
3755 arbprogram_syn_is_ok = 1;
3756 }
3757
3758 /* create the grammar object */
3759 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3760 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003761 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003762 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3763 _mesa_set_program_error (ctx, error_pos, error_msg);
3764 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003765 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003766 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003767 }
3768
3769 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003770 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003771 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3772 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003773 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003774 }
3775
Brian Paul8c41a142005-11-19 15:36:28 +00003776 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3777 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003778 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003779 }
3780
3781 /* check for NULL character occurences */
3782 {
Brian Paul8c41a142005-11-19 15:36:28 +00003783 GLint i;
3784 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003785 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003786 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003787 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003788 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003789 }
Brian Paul8c41a142005-11-19 15:36:28 +00003790 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003791 }
3792
3793 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003794 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003795 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003796 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3797 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003798 return GL_FALSE;
3799 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003800 _mesa_memcpy (strz, str, len);
3801 strz[len] = '\0';
3802
Michal Krolb80bc052004-10-21 14:09:54 +00003803 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003804 err = !grammar_fast_check(arbprogram_syn_id, strz,
3805 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003806
3807 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003808 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003809 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3810 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003811
Brian Paulb3aefd12005-09-19 20:12:32 +00003812#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003813 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003814 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003815 int line, col;
3816 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003817 fprintf(stderr, "program: %s\n", (char *) strz);
3818 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003819 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3820 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003821 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3822 } while (0)
3823#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003824
Brian Paula088f162006-09-05 23:08:51 +00003825 _mesa_free(strz);
3826 _mesa_free(parsed);
3827
Jouk Jansen40322e12004-04-05 08:50:36 +00003828 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003829 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003830 }
3831
Jouk Jansen40322e12004-04-05 08:50:36 +00003832 grammar_destroy (arbprogram_syn_id);
3833
Brian Paul8c41a142005-11-19 15:36:28 +00003834 /*
3835 * Program string is syntactically correct at this point
3836 * Parse the tokenized version of the program now, generating
3837 * vertex/fragment program instructions.
3838 */
3839
Jouk Jansen40322e12004-04-05 08:50:36 +00003840 /* Initialize the arb_program struct */
3841 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003842 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003843 program->Base.NumInstructions =
3844 program->Base.NumTemporaries =
3845 program->Base.NumParameters =
3846 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003847 program->Base.Parameters = _mesa_new_parameter_list ();
3848 program->Base.InputsRead = 0x0;
3849 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003850 program->Position = 0;
3851 program->MajorVersion = program->MinorVersion = 0;
3852 program->PrecisionOption = GL_DONT_CARE;
3853 program->FogOption = GL_NONE;
3854 program->HintPositionInvariant = GL_FALSE;
3855 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003856 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003857 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003858 program->NumAluInstructions =
3859 program->NumTexInstructions =
3860 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003861 program->UsesKill = 0;
3862
Jouk Jansen40322e12004-04-05 08:50:36 +00003863 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003864 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003865
3866 /* Start examining the tokens in the array */
3867 inst = parsed;
3868
3869 /* Check the grammer rev */
3870 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003871 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003872 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003873 }
3874 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003875 /* ignore program target */
3876 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003877 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003878 }
3879
3880 /*debug_variables(ctx, vc_head, program); */
3881
3882 /* We're done with the parsed binary array */
3883 var_cache_destroy (&vc_head);
3884
3885 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003886
Brian Paul8c41a142005-11-19 15:36:28 +00003887 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3888 * to size [ap.Base.NumInstructions].
3889 */
Brian Paula7543902006-08-24 21:58:32 +00003890 program->Base.Instructions
3891 = _mesa_realloc_instructions(program->Base.Instructions,
3892 MAX_INSTRUCTIONS,
3893 program->Base.NumInstructions);
3894
Brian Paul45703642005-10-29 15:52:31 +00003895 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003896}
Brian Paul8c41a142005-11-19 15:36:28 +00003897
3898
3899
3900void
3901_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3902 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003903 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003904{
3905 struct arb_program ap;
3906 GLuint i;
3907
3908 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003909 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003910 /* Error in the program. Just return. */
3911 return;
3912 }
3913
3914 /* Copy the relevant contents of the arb_program struct into the
3915 * fragment_program struct.
3916 */
3917 program->Base.String = ap.Base.String;
3918 program->Base.NumInstructions = ap.Base.NumInstructions;
3919 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3920 program->Base.NumParameters = ap.Base.NumParameters;
3921 program->Base.NumAttributes = ap.Base.NumAttributes;
3922 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003923 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3924 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3925 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3926 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3927 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003928 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3929 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3930 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3931 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3932 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3933 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003934 program->Base.InputsRead = ap.Base.InputsRead;
3935 program->Base.OutputsWritten = ap.Base.OutputsWritten;
Briand1284d32008-03-12 15:33:41 -06003936 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
Brianc9db2232007-01-04 17:22:19 -07003937 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Briand1284d32008-03-12 15:33:41 -06003938 if (ap.TexturesUsed[i])
3939 program->Base.SamplersUsed |= (1 << i);
3940 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003941 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003942 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003943 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003944
3945 if (program->Base.Instructions)
3946 _mesa_free(program->Base.Instructions);
3947 program->Base.Instructions = ap.Base.Instructions;
3948
3949 if (program->Base.Parameters)
3950 _mesa_free_parameter_list(program->Base.Parameters);
3951 program->Base.Parameters = ap.Base.Parameters;
3952
Brian Paulc0ef1662008-03-25 11:32:31 -06003953 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3954 * or similar. We used to leave this up to drivers, but it appears
3955 * there's no hardware that wants to do fog in a discrete stage separate
3956 * from the fragment shader.
3957 */
3958 if (program->FogOption != GL_NONE) {
3959 _mesa_append_fog_code(ctx, program);
3960 program->FogOption = GL_NONE;
3961 }
3962
Brian Paul8c41a142005-11-19 15:36:28 +00003963#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00003964 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
3965 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003966#endif
3967}
3968
3969
3970
3971/**
3972 * Parse the vertex program string. If success, update the given
3973 * vertex_program object with the new program. Else, leave the vertex_program
3974 * object unchanged.
3975 */
3976void
3977_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3978 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003979 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003980{
3981 struct arb_program ap;
3982
3983 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3984
Brian Paul95801792005-12-06 15:41:43 +00003985 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003986 /* Error in the program. Just return. */
3987 return;
3988 }
3989
3990 /* Copy the relevant contents of the arb_program struct into the
3991 * vertex_program struct.
3992 */
3993 program->Base.String = ap.Base.String;
3994 program->Base.NumInstructions = ap.Base.NumInstructions;
3995 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3996 program->Base.NumParameters = ap.Base.NumParameters;
3997 program->Base.NumAttributes = ap.Base.NumAttributes;
3998 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003999 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4000 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4001 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4002 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4003 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004004 program->Base.InputsRead = ap.Base.InputsRead;
4005 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4006 program->IsPositionInvariant = ap.HintPositionInvariant;
4007
4008 if (program->Base.Instructions)
4009 _mesa_free(program->Base.Instructions);
4010 program->Base.Instructions = ap.Base.Instructions;
4011
4012 if (program->Base.Parameters)
4013 _mesa_free_parameter_list(program->Base.Parameters);
4014 program->Base.Parameters = ap.Base.Parameters;
4015
4016#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01004017 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00004018 _mesa_print_program(&program->Base);
4019#endif
4020}