blob: f10233e5fedc24314230171d0adf15197eae5f91 [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brianab31a3a2007-09-13 11:41:49 -06003 * Version: 7.1
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Brian7d2b6a02008-03-27 16:17:37 -06005 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
Jouk Jansen40322e12004-04-05 08:50:36 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#define DEBUG_PARSING 0
26
27/**
28 * \file arbprogparse.c
29 * ARB_*_program parser core
30 * \author Karl Rasche
31 */
32
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"
Brianc0551f02006-12-14 15:02:37 -070038#include "prog_parameter.h"
39#include "prog_statevars.h"
Brian Paul8c41a142005-11-19 15:36:28 +000040#include "context.h"
Brian Paul6211a142006-08-24 23:37:13 +000041#include "macros.h"
Brian Paul8c41a142005-11-19 15:36:28 +000042#include "mtypes.h"
Brianc0551f02006-12-14 15:02:37 -070043#include "prog_instruction.h"
Brian Paul8c41a142005-11-19 15:36:28 +000044
45
Brian Paul6211a142006-08-24 23:37:13 +000046/* For ARB programs, use the NV instruction limits */
47#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
48 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000049
50
51/**
52 * This is basically a union of the vertex_program and fragment_program
53 * structs that we can use to parse the program into
54 *
55 * XXX we can probably get rid of this entirely someday.
56 */
57struct arb_program
58{
Brian Paul122629f2006-07-20 16:49:57 +000059 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000060
61 GLuint Position; /* Just used for error reporting while parsing */
62 GLuint MajorVersion;
63 GLuint MinorVersion;
64
65 /* ARB_vertex_progmra options */
66 GLboolean HintPositionInvariant;
67
68 /* ARB_fragment_progmra options */
69 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
70 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
71
72 /* ARB_fragment_program specifics */
73 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
Ian Romanick7b559a92007-06-07 13:58:50 -070074 GLbitfield ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +000075 GLuint NumAluInstructions;
76 GLuint NumTexInstructions;
77 GLuint NumTexIndirections;
78
79 GLboolean UsesKill;
80};
81
Ian Romanick9bdfee32005-07-18 12:31:24 +000082
Brian Paula6c423d2004-08-25 15:59:48 +000083
Jouk Jansen40322e12004-04-05 08:50:36 +000084/* TODO:
85 * Fragment Program Stuff:
86 * -----------------------------------------------------
87 *
88 * - things from Michal's email
89 * + overflow on atoi
90 * + not-overflowing floats (don't use parse_integer..)
91 * + can remove range checking in arbparse.c
92 *
93 * - check all limits of number of various variables
94 * + parameters
95 *
96 * - test! test! test!
97 *
98 * Vertex Program Stuff:
99 * -----------------------------------------------------
100 * - Optimize param array usage and count limits correctly, see spec,
101 * section 2.14.3.7
102 * + Record if an array is reference absolutly or relatively (or both)
103 * + For absolute arrays, store a bitmap of accesses
104 * + For single parameters, store an access flag
105 * + After parsing, make a parameter cleanup and merging pass, where
106 * relative arrays are layed out first, followed by abs arrays, and
107 * finally single state.
108 * + Remap offsets for param src and dst registers
109 * + Now we can properly count parameter usage
110 *
111 * - Multiple state binding errors in param arrays (see spec, just before
112 * section 2.14.3.3)
113 * - grep for XXX
114 *
115 * Mesa Stuff
116 * -----------------------------------------------------
117 * - User clipping planes vs. PositionInvariant
118 * - Is it sufficient to just multiply by the mvp to transform in the
119 * PositionInvariant case? Or do we need something more involved?
120 *
121 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
122 * - fetch state listed in program_parameters list
123 * + WTF should this go???
124 * + currently in nvvertexec.c and s_nvfragprog.c
125 *
126 * - allow for multiple address registers (and fetch address regs properly)
127 *
128 * Cosmetic Stuff
129 * -----------------------------------------------------
130 * - remove any leftover unused grammer.c stuff (dict_ ?)
131 * - fix grammer.c error handling so its not static
132 * - #ifdef around stuff pertaining to extentions
133 *
134 * Outstanding Questions:
135 * -----------------------------------------------------
136 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
137 * what gets hacked off because of this:
138 * + VERTEX_ATTRIB_MATRIXINDEX
139 * + VERTEX_ATTRIB_WEIGHT
140 * + MATRIX_MODELVIEW
141 * + MATRIX_PALETTE
142 *
143 * - When can we fetch env/local params from their own register files, and
144 * when to we have to fetch them into the main state register file?
145 * (think arrays)
146 *
147 * Grammar Changes:
148 * -----------------------------------------------------
149 */
150
151/* Changes since moving the file to shader directory
152
1532004-III-4 ------------------------------------------------------------
154- added #include "grammar_mesa.h"
155- removed grammar specific code part (it resides now in grammar.c)
156- added GL_ARB_fragment_program_shadow tokens
157- modified #include "arbparse_syn.h"
158- major changes inside _mesa_parse_arb_program()
159- check the program string for '\0' characters
160- copy the program string to a one-byte-longer location to have
161 it null-terminated
162- position invariance test (not writing to result.position) moved
163 to syntax part
164*/
165
166typedef GLubyte *production;
167
Brian Paul11a54c32006-11-15 19:54:25 +0000168
Jouk Jansen40322e12004-04-05 08:50:36 +0000169/**
170 * This is the text describing the rules to parse the grammar
171 */
Brian Paul11a54c32006-11-15 19:54:25 +0000172LONGSTRING static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000173#include "arbprogram_syn.h"
174;
175
176/**
177 * These should match up with the values defined in arbprogram.syn
178 */
179
180/*
181 Changes:
182 - changed and merged V_* and F_* opcode values to OP_*.
183 - added GL_ARB_fragment_program_shadow specific tokens (michal)
184*/
Ian Romanickbb372f12007-05-16 15:34:22 -0700185#define REVISION 0x0a
Jouk Jansen40322e12004-04-05 08:50:36 +0000186
187/* program type */
188#define FRAGMENT_PROGRAM 0x01
189#define VERTEX_PROGRAM 0x02
190
191/* program section */
192#define OPTION 0x01
193#define INSTRUCTION 0x02
194#define DECLARATION 0x03
195#define END 0x04
196
Michal Krolb80bc052004-10-21 14:09:54 +0000197/* GL_ARB_fragment_program option */
198#define ARB_PRECISION_HINT_FASTEST 0x00
199#define ARB_PRECISION_HINT_NICEST 0x01
200#define ARB_FOG_EXP 0x02
201#define ARB_FOG_EXP2 0x03
202#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000203
Michal Krolb80bc052004-10-21 14:09:54 +0000204/* GL_ARB_vertex_program option */
205#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000206
Michal Krolb80bc052004-10-21 14:09:54 +0000207/* GL_ARB_fragment_program_shadow option */
208#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000209
Michal Krolb80bc052004-10-21 14:09:54 +0000210/* GL_ARB_draw_buffers option */
211#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000212
Ian Romanickbb372f12007-05-16 15:34:22 -0700213/* GL_MESA_texture_array option */
214#define MESA_TEXTURE_ARRAY 0x08
215
Jouk Jansen40322e12004-04-05 08:50:36 +0000216/* GL_ARB_fragment_program instruction class */
217#define OP_ALU_INST 0x00
218#define OP_TEX_INST 0x01
219
220/* GL_ARB_vertex_program instruction class */
221/* OP_ALU_INST */
222
223/* GL_ARB_fragment_program instruction type */
224#define OP_ALU_VECTOR 0x00
225#define OP_ALU_SCALAR 0x01
226#define OP_ALU_BINSC 0x02
227#define OP_ALU_BIN 0x03
228#define OP_ALU_TRI 0x04
229#define OP_ALU_SWZ 0x05
230#define OP_TEX_SAMPLE 0x06
231#define OP_TEX_KIL 0x07
232
233/* GL_ARB_vertex_program instruction type */
234#define OP_ALU_ARL 0x08
235/* OP_ALU_VECTOR */
236/* OP_ALU_SCALAR */
237/* OP_ALU_BINSC */
238/* OP_ALU_BIN */
239/* OP_ALU_TRI */
240/* OP_ALU_SWZ */
241
242/* GL_ARB_fragment_program instruction code */
243#define OP_ABS 0x00
244#define OP_ABS_SAT 0x1B
245#define OP_FLR 0x09
246#define OP_FLR_SAT 0x26
247#define OP_FRC 0x0A
248#define OP_FRC_SAT 0x27
249#define OP_LIT 0x0C
250#define OP_LIT_SAT 0x2A
251#define OP_MOV 0x11
252#define OP_MOV_SAT 0x30
253#define OP_COS 0x1F
254#define OP_COS_SAT 0x20
255#define OP_EX2 0x07
256#define OP_EX2_SAT 0x25
257#define OP_LG2 0x0B
258#define OP_LG2_SAT 0x29
259#define OP_RCP 0x14
260#define OP_RCP_SAT 0x33
261#define OP_RSQ 0x15
262#define OP_RSQ_SAT 0x34
263#define OP_SIN 0x38
264#define OP_SIN_SAT 0x39
265#define OP_SCS 0x35
266#define OP_SCS_SAT 0x36
267#define OP_POW 0x13
268#define OP_POW_SAT 0x32
269#define OP_ADD 0x01
270#define OP_ADD_SAT 0x1C
271#define OP_DP3 0x03
272#define OP_DP3_SAT 0x21
273#define OP_DP4 0x04
274#define OP_DP4_SAT 0x22
275#define OP_DPH 0x05
276#define OP_DPH_SAT 0x23
277#define OP_DST 0x06
278#define OP_DST_SAT 0x24
279#define OP_MAX 0x0F
280#define OP_MAX_SAT 0x2E
281#define OP_MIN 0x10
282#define OP_MIN_SAT 0x2F
283#define OP_MUL 0x12
284#define OP_MUL_SAT 0x31
285#define OP_SGE 0x16
286#define OP_SGE_SAT 0x37
287#define OP_SLT 0x17
288#define OP_SLT_SAT 0x3A
289#define OP_SUB 0x18
290#define OP_SUB_SAT 0x3B
291#define OP_XPD 0x1A
292#define OP_XPD_SAT 0x43
293#define OP_CMP 0x1D
294#define OP_CMP_SAT 0x1E
295#define OP_LRP 0x2B
296#define OP_LRP_SAT 0x2C
297#define OP_MAD 0x0E
298#define OP_MAD_SAT 0x2D
299#define OP_SWZ 0x19
300#define OP_SWZ_SAT 0x3C
301#define OP_TEX 0x3D
302#define OP_TEX_SAT 0x3E
303#define OP_TXB 0x3F
304#define OP_TXB_SAT 0x40
305#define OP_TXP 0x41
306#define OP_TXP_SAT 0x42
307#define OP_KIL 0x28
308
309/* GL_ARB_vertex_program instruction code */
310#define OP_ARL 0x02
311/* OP_ABS */
312/* OP_FLR */
313/* OP_FRC */
314/* OP_LIT */
315/* OP_MOV */
316/* OP_EX2 */
317#define OP_EXP 0x08
318/* OP_LG2 */
319#define OP_LOG 0x0D
320/* OP_RCP */
321/* OP_RSQ */
322/* OP_POW */
323/* OP_ADD */
324/* OP_DP3 */
325/* OP_DP4 */
326/* OP_DPH */
327/* OP_DST */
328/* OP_MAX */
329/* OP_MIN */
330/* OP_MUL */
331/* OP_SGE */
332/* OP_SLT */
333/* OP_SUB */
334/* OP_XPD */
335/* OP_MAD */
336/* OP_SWZ */
337
338/* fragment attribute binding */
339#define FRAGMENT_ATTRIB_COLOR 0x01
340#define FRAGMENT_ATTRIB_TEXCOORD 0x02
341#define FRAGMENT_ATTRIB_FOGCOORD 0x03
342#define FRAGMENT_ATTRIB_POSITION 0x04
343
344/* vertex attribute binding */
345#define VERTEX_ATTRIB_POSITION 0x01
346#define VERTEX_ATTRIB_WEIGHT 0x02
347#define VERTEX_ATTRIB_NORMAL 0x03
348#define VERTEX_ATTRIB_COLOR 0x04
349#define VERTEX_ATTRIB_FOGCOORD 0x05
350#define VERTEX_ATTRIB_TEXCOORD 0x06
351#define VERTEX_ATTRIB_MATRIXINDEX 0x07
352#define VERTEX_ATTRIB_GENERIC 0x08
353
354/* fragment result binding */
355#define FRAGMENT_RESULT_COLOR 0x01
356#define FRAGMENT_RESULT_DEPTH 0x02
357
358/* vertex result binding */
359#define VERTEX_RESULT_POSITION 0x01
360#define VERTEX_RESULT_COLOR 0x02
361#define VERTEX_RESULT_FOGCOORD 0x03
362#define VERTEX_RESULT_POINTSIZE 0x04
363#define VERTEX_RESULT_TEXCOORD 0x05
364
365/* texture target */
366#define TEXTARGET_1D 0x01
367#define TEXTARGET_2D 0x02
368#define TEXTARGET_3D 0x03
369#define TEXTARGET_RECT 0x04
370#define TEXTARGET_CUBE 0x05
371/* GL_ARB_fragment_program_shadow */
372#define TEXTARGET_SHADOW1D 0x06
373#define TEXTARGET_SHADOW2D 0x07
374#define TEXTARGET_SHADOWRECT 0x08
Ian Romanickbb372f12007-05-16 15:34:22 -0700375/* GL_MESA_texture_array */
376#define TEXTARGET_1D_ARRAY 0x09
377#define TEXTARGET_2D_ARRAY 0x0a
Ian Romanick69358e72007-06-05 09:24:27 -0700378#define TEXTARGET_SHADOW1D_ARRAY 0x0b
379#define TEXTARGET_SHADOW2D_ARRAY 0x0c
Jouk Jansen40322e12004-04-05 08:50:36 +0000380
381/* face type */
382#define FACE_FRONT 0x00
383#define FACE_BACK 0x01
384
385/* color type */
386#define COLOR_PRIMARY 0x00
387#define COLOR_SECONDARY 0x01
388
389/* component */
390#define COMPONENT_X 0x00
391#define COMPONENT_Y 0x01
392#define COMPONENT_Z 0x02
393#define COMPONENT_W 0x03
394#define COMPONENT_0 0x04
395#define COMPONENT_1 0x05
396
397/* array index type */
398#define ARRAY_INDEX_ABSOLUTE 0x00
399#define ARRAY_INDEX_RELATIVE 0x01
400
401/* matrix name */
402#define MATRIX_MODELVIEW 0x01
403#define MATRIX_PROJECTION 0x02
404#define MATRIX_MVP 0x03
405#define MATRIX_TEXTURE 0x04
406#define MATRIX_PALETTE 0x05
407#define MATRIX_PROGRAM 0x06
408
409/* matrix modifier */
410#define MATRIX_MODIFIER_IDENTITY 0x00
411#define MATRIX_MODIFIER_INVERSE 0x01
412#define MATRIX_MODIFIER_TRANSPOSE 0x02
413#define MATRIX_MODIFIER_INVTRANS 0x03
414
415/* constant type */
416#define CONSTANT_SCALAR 0x01
417#define CONSTANT_VECTOR 0x02
418
419/* program param type */
420#define PROGRAM_PARAM_ENV 0x01
421#define PROGRAM_PARAM_LOCAL 0x02
422
423/* register type */
424#define REGISTER_ATTRIB 0x01
425#define REGISTER_PARAM 0x02
426#define REGISTER_RESULT 0x03
427#define REGISTER_ESTABLISHED_NAME 0x04
428
429/* param binding */
430#define PARAM_NULL 0x00
431#define PARAM_ARRAY_ELEMENT 0x01
432#define PARAM_STATE_ELEMENT 0x02
433#define PARAM_PROGRAM_ELEMENT 0x03
434#define PARAM_PROGRAM_ELEMENTS 0x04
435#define PARAM_CONSTANT 0x05
436
437/* param state property */
438#define STATE_MATERIAL_PARSER 0x01
439#define STATE_LIGHT_PARSER 0x02
440#define STATE_LIGHT_MODEL 0x03
441#define STATE_LIGHT_PROD 0x04
442#define STATE_FOG 0x05
443#define STATE_MATRIX_ROWS 0x06
444/* GL_ARB_fragment_program */
445#define STATE_TEX_ENV 0x07
446#define STATE_DEPTH 0x08
447/* GL_ARB_vertex_program */
448#define STATE_TEX_GEN 0x09
449#define STATE_CLIP_PLANE 0x0A
450#define STATE_POINT 0x0B
451
452/* state material property */
453#define MATERIAL_AMBIENT 0x01
454#define MATERIAL_DIFFUSE 0x02
455#define MATERIAL_SPECULAR 0x03
456#define MATERIAL_EMISSION 0x04
457#define MATERIAL_SHININESS 0x05
458
459/* state light property */
460#define LIGHT_AMBIENT 0x01
461#define LIGHT_DIFFUSE 0x02
462#define LIGHT_SPECULAR 0x03
463#define LIGHT_POSITION 0x04
464#define LIGHT_ATTENUATION 0x05
465#define LIGHT_HALF 0x06
466#define LIGHT_SPOT_DIRECTION 0x07
467
468/* state light model property */
469#define LIGHT_MODEL_AMBIENT 0x01
470#define LIGHT_MODEL_SCENECOLOR 0x02
471
472/* state light product property */
473#define LIGHT_PROD_AMBIENT 0x01
474#define LIGHT_PROD_DIFFUSE 0x02
475#define LIGHT_PROD_SPECULAR 0x03
476
477/* state texture environment property */
478#define TEX_ENV_COLOR 0x01
479
480/* state texture generation coord property */
481#define TEX_GEN_EYE 0x01
482#define TEX_GEN_OBJECT 0x02
483
484/* state fog property */
485#define FOG_COLOR 0x01
486#define FOG_PARAMS 0x02
487
488/* state depth property */
489#define DEPTH_RANGE 0x01
490
491/* state point parameters property */
492#define POINT_SIZE 0x01
493#define POINT_ATTENUATION 0x02
494
495/* declaration */
496#define ATTRIB 0x01
497#define PARAM 0x02
498#define TEMP 0x03
499#define OUTPUT 0x04
500#define ALIAS 0x05
501/* GL_ARB_vertex_program */
502#define ADDRESS 0x06
503
504/*-----------------------------------------------------------------------
505 * From here on down is the semantic checking portion
506 *
507 */
508
509/**
510 * Variable Table Handling functions
511 */
512typedef enum
513{
514 vt_none,
515 vt_address,
516 vt_attrib,
517 vt_param,
518 vt_temp,
519 vt_output,
520 vt_alias
521} var_type;
522
523
Brian Paul7aebaf32005-10-30 21:23:23 +0000524/**
525 * Setting an explicit field for each of the binding properties is a bit
526 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000527 */
528struct var_cache
529{
Brian Paul6a769d92006-04-28 15:42:15 +0000530 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000531 var_type type;
532 GLuint address_binding; /* The index of the address register we should
533 * be using */
534 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000535 GLuint attrib_is_generic; /* If the attrib was specified through a generic
536 * vertex attrib */
537 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000538 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000539 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
540 * that this is aliased to */
541 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
542 * PROGRAM_ENV_PARAM} */
543 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
544 * the tokens representing our bound state (or constants)
545 * start */
546 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
547 * we take up with our state tokens or constants. Note that
548 * this is _not_ the same as the number of param registers
549 * we eventually use */
550 struct var_cache *next;
551};
552
553static GLvoid
554var_cache_create (struct var_cache **va)
555{
556 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
557 if (*va) {
558 (**va).name = NULL;
559 (**va).type = vt_none;
560 (**va).attrib_binding = ~0;
561 (**va).attrib_is_generic = 0;
562 (**va).temp_binding = ~0;
563 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000564 (**va).param_binding_type = ~0;
565 (**va).param_binding_begin = ~0;
566 (**va).param_binding_length = ~0;
567 (**va).alias_binding = NULL;
568 (**va).next = NULL;
569 }
570}
571
572static GLvoid
573var_cache_destroy (struct var_cache **va)
574{
575 if (*va) {
576 var_cache_destroy (&(**va).next);
577 _mesa_free (*va);
578 *va = NULL;
579 }
580}
581
582static GLvoid
583var_cache_append (struct var_cache **va, struct var_cache *nv)
584{
585 if (*va)
586 var_cache_append (&(**va).next, nv);
587 else
588 *va = nv;
589}
590
591static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000592var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000593{
Brian Paul0a360cf2005-01-17 01:21:03 +0000594 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000595
596 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000597 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000598 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000599 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000600 return va;
601 }
602
603 va = va->next;
604 }
605
606 return NULL;
607}
608
Brian Paula088f162006-09-05 23:08:51 +0000609
610
611/**
612 * Called when an error is detected while parsing/compiling a program.
613 * Sets the ctx->Program.ErrorString field to descript and records a
614 * GL_INVALID_OPERATION error.
615 * \param position position of error in program string
616 * \param descrip verbose error description
617 */
618static void
619program_error(GLcontext *ctx, GLint position, const char *descrip)
620{
621 if (descrip) {
622 const char *prefix = "glProgramString(", *suffix = ")";
623 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
624 _mesa_strlen(prefix) +
625 _mesa_strlen(suffix) + 1);
626 if (str) {
627 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
628 _mesa_error(ctx, GL_INVALID_OPERATION, str);
629 _mesa_free(str);
630 }
631 }
632 _mesa_set_program_error(ctx, position, descrip);
633}
634
635
Brianab31a3a2007-09-13 11:41:49 -0600636/**
637 * As above, but with an extra string parameter for more info.
638 */
639static void
640program_error2(GLcontext *ctx, GLint position, const char *descrip,
641 const char *var)
642{
643 if (descrip) {
644 const char *prefix = "glProgramString(", *suffix = ")";
645 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
646 _mesa_strlen(": ") +
647 _mesa_strlen(var) +
648 _mesa_strlen(prefix) +
649 _mesa_strlen(suffix) + 1);
650 if (str) {
651 _mesa_sprintf(str, "%s%s: %s%s", prefix, descrip, var, suffix);
652 _mesa_error(ctx, GL_INVALID_OPERATION, str);
653 _mesa_free(str);
654 }
655 }
656 {
657 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
658 _mesa_strlen(": ") +
659 _mesa_strlen(var) + 1);
660 if (str) {
661 _mesa_sprintf(str, "%s: %s", descrip, var);
662 }
663 _mesa_set_program_error(ctx, position, str);
664 if (str) {
665 _mesa_free(str);
666 }
667 }
668}
669
670
Brian Paula088f162006-09-05 23:08:51 +0000671
Jouk Jansen40322e12004-04-05 08:50:36 +0000672/**
673 * constructs an integer from 4 GLubytes in LE format
674 */
675static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000676parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000677{
678 GLuint value;
679
680 value = (GLuint) (*(*inst)++);
681 value += (GLuint) (*(*inst)++) * 0x100;
682 value += (GLuint) (*(*inst)++) * 0x10000;
683 value += (GLuint) (*(*inst)++) * 0x1000000;
684
685 return value;
686}
687
688/**
689 * This will, given a string, lookup the string as a variable name in the
690 * var cache. If the name is found, the var cache node corresponding to the
691 * var name is returned. If it is not found, a new entry is allocated
692 *
693 * \param I Points into the binary array where the string identifier begins
694 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
695 * \return The location on the var_cache corresponding the the string starting at I
696 */
697static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000698parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000699 struct arb_program *Program, GLuint * found)
700{
Brian Paula088f162006-09-05 23:08:51 +0000701 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000702 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000703 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000704
705 *inst += _mesa_strlen ((char *) i) + 1;
706
707 va = var_cache_find (*vc_head, i);
708
709 if (va) {
710 *found = 1;
711 return va;
712 }
713
714 *found = 0;
715 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000716 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000717
718 var_cache_append (vc_head, va);
719
720 return va;
721}
722
723static char *
Brian Paula088f162006-09-05 23:08:51 +0000724parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000725{
Brian Paula088f162006-09-05 23:08:51 +0000726 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000727 (void) Program;
728
Jouk Jansen40322e12004-04-05 08:50:36 +0000729 *inst += _mesa_strlen ((char *) i) + 1;
730
731 return (char *) i;
732}
733
734/**
Brian Paul05908952004-06-08 15:20:23 +0000735 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000736 */
Brian Paul05908952004-06-08 15:20:23 +0000737static GLint
Brian Paula088f162006-09-05 23:08:51 +0000738parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000739{
740 /*return *(*inst)++ != '+'; */
741
742 if (**inst == '-') {
743 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000744 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000745 }
746 else if (**inst == '+') {
747 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000748 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000749 }
750
Brian Paul05908952004-06-08 15:20:23 +0000751 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000752}
753
754/**
755 * parses and returns signed integer
756 */
757static GLint
Brian Paula088f162006-09-05 23:08:51 +0000758parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000759{
760 GLint sign;
761 GLint value;
762
763 /* check if *inst points to '+' or '-'
764 * if yes, grab the sign and increment *inst
765 */
766 sign = parse_sign (inst);
767
768 /* now check if *inst points to 0
769 * if yes, increment the *inst and return the default value
770 */
771 if (**inst == 0) {
772 (*inst)++;
773 return 0;
774 }
775
776 /* parse the integer as you normally would do it */
777 value = _mesa_atoi (parse_string_without_adding (inst, Program));
778
779 /* now, after terminating 0 there is a position
780 * to parse it - parse_position()
781 */
782 Program->Position = parse_position (inst);
783
Brian Paul05908952004-06-08 15:20:23 +0000784 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000785}
786
787/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000788 Accumulate this string of digits, and return them as
789 a large integer represented in floating point (for range).
790 If scale is not NULL, also accumulates a power-of-ten
791 integer scale factor that represents the number of digits
792 in the string.
793*/
794static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000795parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000796{
797 GLdouble value = 0.0;
798 GLdouble oscale = 1.0;
799
800 if (**inst == 0) { /* this string of digits is empty-- do nothing */
801 (*inst)++;
802 }
803 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000804 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000805 GLubyte digit = *((*inst)++);
806 value = value * 10.0 + (GLint) (digit - '0');
807 oscale *= 10.0;
808 }
809 assert(**inst == 0); /* integer string should end with 0 */
810 (*inst)++; /* skip over terminating 0 */
811 Program->Position = parse_position(inst); /* skip position (from integer) */
812 }
813 if (scale)
814 *scale = oscale;
815 return value;
816}
817
818/**
819 Parse an unsigned floating-point number from this stream of tokenized
820 characters. Example floating-point formats supported:
821 12.34
822 12
823 0.34
824 .34
825 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000826 */
827static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000828parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000829{
Brian Paul1ff8f502005-02-16 15:08:29 +0000830 GLint exponent;
831 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000832
Brian Paul1ff8f502005-02-16 15:08:29 +0000833 whole = parse_float_string(inst, Program, 0);
834 fraction = parse_float_string(inst, Program, &fracScale);
835
836 /* Parse signed exponent */
837 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000838
Brian Paul1ff8f502005-02-16 15:08:29 +0000839 /* Assemble parts of floating-point number: */
840 return (GLfloat) ((whole + fraction / fracScale) *
841 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000842}
843
844
845/**
846 */
847static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000848parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000849{
Brian Paul05908952004-06-08 15:20:23 +0000850 GLint sign = parse_sign (inst);
851 GLfloat value = parse_float (inst, Program);
852 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000853}
854
855/**
856 * This picks out a constant value from the parsed array. The constant vector is r
857 * returned in the *values array, which should be of length 4.
858 *
859 * \param values - The 4 component vector with the constant value in it
860 */
861static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000862parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000863 GLboolean use)
864{
865 GLuint components, i;
866
867
868 switch (*(*inst)++) {
869 case CONSTANT_SCALAR:
870 if (use == GL_TRUE) {
871 values[0] =
872 values[1] =
873 values[2] = values[3] = parse_float (inst, Program);
874 }
875 else {
876 values[0] =
877 values[1] =
878 values[2] = values[3] = parse_signed_float (inst, Program);
879 }
880
881 break;
882 case CONSTANT_VECTOR:
883 values[0] = values[1] = values[2] = 0;
884 values[3] = 1;
885 components = *(*inst)++;
886 for (i = 0; i < components; i++) {
887 values[i] = parse_signed_float (inst, Program);
888 }
889 break;
890 }
891}
892
893/**
894 * \param offset The offset from the address register that we should
895 * address
896 *
897 * \return 0 on sucess, 1 on error
898 */
899static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000900parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
901 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000902{
Brian Paul6a769d92006-04-28 15:42:15 +0000903 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000904 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000905 return 0;
906}
907
908/**
909 * \param color 0 if color type is primary, 1 if color type is secondary
910 * \return 0 on sucess, 1 on error
911 */
912static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000913parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000914 GLint * color)
915{
Brian Paula6c423d2004-08-25 15:59:48 +0000916 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000917 *color = *(*inst)++ != COLOR_PRIMARY;
918 return 0;
919}
920
921/**
922 * Get an integer corresponding to a generic vertex attribute.
923 *
924 * \return 0 on sucess, 1 on error
925 */
926static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000927parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000928 struct arb_program *Program, GLuint *attrib)
929{
Brian Paulbd997cd2004-07-20 21:12:56 +0000930 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000931
Michal Krolbb38cad2006-04-11 11:41:11 +0000932 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000933 {
Brian Paula088f162006-09-05 23:08:51 +0000934 program_error(ctx, Program->Position,
935 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000936 return 1;
937 }
938
Brian Paulbd997cd2004-07-20 21:12:56 +0000939 *attrib = (GLuint) i;
940
Jouk Jansen40322e12004-04-05 08:50:36 +0000941 return 0;
942}
943
944
945/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000946 * \param color The index of the color buffer to write into
947 * \return 0 on sucess, 1 on error
948 */
949static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000950parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000951 struct arb_program *Program, GLuint * color)
952{
953 GLint i = parse_integer (inst, Program);
954
955 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000956 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000957 return 1;
958 }
959
960 *color = (GLuint) i;
961 return 0;
962}
963
964
965/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000966 * \param coord The texture unit index
967 * \return 0 on sucess, 1 on error
968 */
969static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000970parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000971 struct arb_program *Program, GLuint * coord)
972{
Brian Paulbd997cd2004-07-20 21:12:56 +0000973 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000974
Brian Paula6c423d2004-08-25 15:59:48 +0000975 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000976 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000977 return 1;
978 }
979
Brian Paulbd997cd2004-07-20 21:12:56 +0000980 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000981 return 0;
982}
983
984/**
985 * \param coord The weight index
986 * \return 0 on sucess, 1 on error
987 */
988static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000989parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000990 GLint * coord)
991{
992 *coord = parse_integer (inst, Program);
993
994 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000995 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000996 return 1;
997 }
998
999 return 0;
1000}
1001
1002/**
1003 * \param coord The clip plane index
1004 * \return 0 on sucess, 1 on error
1005 */
1006static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001007parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001008 struct arb_program *Program, GLint * coord)
1009{
1010 *coord = parse_integer (inst, Program);
1011
1012 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +00001013 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +00001014 return 1;
1015 }
1016
1017 return 0;
1018}
1019
1020
1021/**
1022 * \return 0 on front face, 1 on back face
1023 */
1024static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001025parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +00001026{
1027 switch (*(*inst)++) {
1028 case FACE_FRONT:
1029 return 0;
1030
1031 case FACE_BACK:
1032 return 1;
1033 }
1034 return 0;
1035}
1036
1037
1038/**
1039 * Given a matrix and a modifier token on the binary array, return tokens
1040 * that _mesa_fetch_state() [program.c] can understand.
1041 *
1042 * \param matrix - the matrix we are talking about
1043 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1044 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1045 * \return 0 on sucess, 1 on failure
1046 */
1047static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001048parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001049 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1050{
1051 GLubyte mat = *(*inst)++;
1052
1053 *matrix_idx = 0;
1054
1055 switch (mat) {
1056 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001057 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001058 *matrix_idx = parse_integer (inst, Program);
1059 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001060 program_error(ctx, Program->Position,
1061 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001062 return 1;
1063 }
1064 break;
1065
1066 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001067 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001068 break;
1069
1070 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001071 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001072 break;
1073
1074 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001075 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001076 *matrix_idx = parse_integer (inst, Program);
1077 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001078 program_error(ctx, Program->Position, "Invalid Texture Unit");
1079 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001080 return 1;
1081 }
1082 break;
1083
1084 /* This is not currently supported (ARB_matrix_palette) */
1085 case MATRIX_PALETTE:
1086 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001087 program_error(ctx, Program->Position,
1088 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001089 return 1;
1090 break;
1091
1092 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001093 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001094 *matrix_idx = parse_integer (inst, Program);
1095 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001096 program_error(ctx, Program->Position, "Invalid Program Matrix");
1097 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001098 return 1;
1099 }
1100 break;
1101 }
1102
1103 switch (*(*inst)++) {
1104 case MATRIX_MODIFIER_IDENTITY:
1105 *matrix_modifier = 0;
1106 break;
1107 case MATRIX_MODIFIER_INVERSE:
1108 *matrix_modifier = STATE_MATRIX_INVERSE;
1109 break;
1110 case MATRIX_MODIFIER_TRANSPOSE:
1111 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1112 break;
1113 case MATRIX_MODIFIER_INVTRANS:
1114 *matrix_modifier = STATE_MATRIX_INVTRANS;
1115 break;
1116 }
1117
1118 return 0;
1119}
1120
1121
1122/**
1123 * This parses a state string (rather, the binary version of it) into
1124 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1125 *
1126 * \param inst - the start in the binary arry to start working from
1127 * \param state_tokens - the storage for the 6-token state description
1128 * \return - 0 on sucess, 1 on error
1129 */
1130static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001131parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001132 struct arb_program *Program,
1133 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001134{
Brian Paul4ca0af12008-07-09 08:35:50 -06001135 GLubyte token = *(*inst)++;
1136
1137 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001138 case STATE_MATERIAL_PARSER:
1139 state_tokens[0] = STATE_MATERIAL;
1140 state_tokens[1] = parse_face_type (inst);
1141 switch (*(*inst)++) {
1142 case MATERIAL_AMBIENT:
1143 state_tokens[2] = STATE_AMBIENT;
1144 break;
1145 case MATERIAL_DIFFUSE:
1146 state_tokens[2] = STATE_DIFFUSE;
1147 break;
1148 case MATERIAL_SPECULAR:
1149 state_tokens[2] = STATE_SPECULAR;
1150 break;
1151 case MATERIAL_EMISSION:
1152 state_tokens[2] = STATE_EMISSION;
1153 break;
1154 case MATERIAL_SHININESS:
1155 state_tokens[2] = STATE_SHININESS;
1156 break;
1157 }
1158 break;
1159
1160 case STATE_LIGHT_PARSER:
1161 state_tokens[0] = STATE_LIGHT;
1162 state_tokens[1] = parse_integer (inst, Program);
1163
1164 /* Check the value of state_tokens[1] against the # of lights */
1165 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001166 program_error(ctx, Program->Position, "Invalid Light Number");
1167 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001168 return 1;
1169 }
1170
1171 switch (*(*inst)++) {
1172 case LIGHT_AMBIENT:
1173 state_tokens[2] = STATE_AMBIENT;
1174 break;
1175 case LIGHT_DIFFUSE:
1176 state_tokens[2] = STATE_DIFFUSE;
1177 break;
1178 case LIGHT_SPECULAR:
1179 state_tokens[2] = STATE_SPECULAR;
1180 break;
1181 case LIGHT_POSITION:
1182 state_tokens[2] = STATE_POSITION;
1183 break;
1184 case LIGHT_ATTENUATION:
1185 state_tokens[2] = STATE_ATTENUATION;
1186 break;
1187 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001188 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001189 break;
1190 case LIGHT_SPOT_DIRECTION:
1191 state_tokens[2] = STATE_SPOT_DIRECTION;
1192 break;
1193 }
1194 break;
1195
1196 case STATE_LIGHT_MODEL:
1197 switch (*(*inst)++) {
1198 case LIGHT_MODEL_AMBIENT:
1199 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1200 break;
1201 case LIGHT_MODEL_SCENECOLOR:
1202 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1203 state_tokens[1] = parse_face_type (inst);
1204 break;
1205 }
1206 break;
1207
1208 case STATE_LIGHT_PROD:
1209 state_tokens[0] = STATE_LIGHTPROD;
1210 state_tokens[1] = parse_integer (inst, Program);
1211
1212 /* Check the value of state_tokens[1] against the # of lights */
1213 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001214 program_error(ctx, Program->Position, "Invalid Light Number");
1215 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001216 return 1;
1217 }
1218
1219 state_tokens[2] = parse_face_type (inst);
1220 switch (*(*inst)++) {
1221 case LIGHT_PROD_AMBIENT:
1222 state_tokens[3] = STATE_AMBIENT;
1223 break;
1224 case LIGHT_PROD_DIFFUSE:
1225 state_tokens[3] = STATE_DIFFUSE;
1226 break;
1227 case LIGHT_PROD_SPECULAR:
1228 state_tokens[3] = STATE_SPECULAR;
1229 break;
1230 }
1231 break;
1232
1233
1234 case STATE_FOG:
1235 switch (*(*inst)++) {
1236 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001237 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001238 break;
1239 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001240 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001241 break;
1242 }
1243 break;
1244
1245 case STATE_TEX_ENV:
1246 state_tokens[1] = parse_integer (inst, Program);
1247 switch (*(*inst)++) {
1248 case TEX_ENV_COLOR:
1249 state_tokens[0] = STATE_TEXENV_COLOR;
1250 break;
1251 }
1252 break;
1253
1254 case STATE_TEX_GEN:
1255 {
1256 GLuint type, coord;
1257
1258 state_tokens[0] = STATE_TEXGEN;
1259 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1260
1261 if (parse_texcoord_num (ctx, inst, Program, &coord))
1262 return 1;
1263 state_tokens[1] = coord;
1264
1265 /* EYE or OBJECT */
Briand799b7a2007-09-13 11:29:00 -06001266 type = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001267
1268 /* 0 - s, 1 - t, 2 - r, 3 - q */
Briand799b7a2007-09-13 11:29:00 -06001269 coord = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001270
1271 if (type == TEX_GEN_EYE) {
1272 switch (coord) {
1273 case COMPONENT_X:
1274 state_tokens[2] = STATE_TEXGEN_EYE_S;
1275 break;
1276 case COMPONENT_Y:
1277 state_tokens[2] = STATE_TEXGEN_EYE_T;
1278 break;
1279 case COMPONENT_Z:
1280 state_tokens[2] = STATE_TEXGEN_EYE_R;
1281 break;
1282 case COMPONENT_W:
1283 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1284 break;
Briand799b7a2007-09-13 11:29:00 -06001285 default:
1286 _mesa_problem(ctx, "bad texgen component in "
1287 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001288 }
1289 }
1290 else {
1291 switch (coord) {
1292 case COMPONENT_X:
1293 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1294 break;
1295 case COMPONENT_Y:
1296 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1297 break;
1298 case COMPONENT_Z:
1299 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1300 break;
1301 case COMPONENT_W:
1302 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1303 break;
Briand799b7a2007-09-13 11:29:00 -06001304 default:
1305 _mesa_problem(ctx, "bad texgen component in "
1306 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001307 }
1308 }
1309 }
1310 break;
1311
1312 case STATE_DEPTH:
1313 switch (*(*inst)++) {
1314 case DEPTH_RANGE:
1315 state_tokens[0] = STATE_DEPTH_RANGE;
1316 break;
1317 }
1318 break;
1319
1320 case STATE_CLIP_PLANE:
1321 state_tokens[0] = STATE_CLIPPLANE;
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:
Briand799b7a2007-09-13 11:29:00 -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 }
1621
Jouk Jansen40322e12004-04-05 08:50:36 +00001622 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) {
Brianab31a3a2007-09-13 11:41:49 -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;
Roland Scheidegger8dc18842007-11-29 03:08:18 +01001770 gl_state_index state_tokens[STATE_LENGTH] = {0, 0, 0, 0, 0};
Jouk Jansen40322e12004-04-05 08:50:36 +00001771 GLfloat const_values[4];
1772
Brian Paul4ca0af12008-07-09 08:35:50 -06001773 GLubyte token = *(*inst)++;
1774
1775 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001776 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001777 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1778 return 1;
1779
1780 /* If we adding STATE_MATRIX that has multiple rows, we need to
1781 * unroll it and call _mesa_add_state_reference() for each row
1782 */
Brian65319522007-02-21 11:08:21 -07001783 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1784 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1785 state_tokens[0] == STATE_MVP_MATRIX ||
1786 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1787 state_tokens[0] == STATE_PROGRAM_MATRIX)
1788 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001789 GLint row;
Brian65319522007-02-21 11:08:21 -07001790 const GLint first_row = state_tokens[2];
1791 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001792
1793 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001794 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001795
Brian Paulde997602005-11-12 17:53:14 +00001796 idx = _mesa_add_state_reference(Program->Base.Parameters,
1797 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001798 if (param_var->param_binding_begin == ~0U)
1799 param_var->param_binding_begin = idx;
1800 param_var->param_binding_length++;
1801 Program->Base.NumParameters++;
1802 }
1803 }
1804 else {
Brian Paulde997602005-11-12 17:53:14 +00001805 idx = _mesa_add_state_reference(Program->Base.Parameters,
1806 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001807 if (param_var->param_binding_begin == ~0U)
1808 param_var->param_binding_begin = idx;
1809 param_var->param_binding_length++;
1810 Program->Base.NumParameters++;
1811 }
1812 break;
1813
1814 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001815 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1816 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001817 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001818 if (param_var->param_binding_begin == ~0U)
1819 param_var->param_binding_begin = idx;
1820 param_var->param_binding_length++;
1821 Program->Base.NumParameters++;
1822
1823 /* Check if there is more: 0 -> we're done, else its an integer */
1824 if (**inst) {
1825 GLuint out_of_range, new_idx;
1826 GLuint start_idx = state_tokens[2] + 1;
1827 GLuint end_idx = parse_integer (inst, Program);
1828
1829 out_of_range = 0;
1830 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1831 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001832 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001833 || ((state_tokens[1] == STATE_LOCAL)
1834 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001835 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001836 out_of_range = 1;
1837 }
1838 else {
1839 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001840 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001841 || ((state_tokens[1] == STATE_LOCAL)
1842 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001843 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001844 out_of_range = 1;
1845 }
1846 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001847 program_error(ctx, Program->Position,
1848 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001849 return 1;
1850 }
1851
1852 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1853 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001854 idx = _mesa_add_state_reference(Program->Base.Parameters,
1855 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001856 param_var->param_binding_length++;
1857 Program->Base.NumParameters++;
1858 }
1859 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001860 else {
1861 (*inst)++;
1862 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001863 break;
1864
1865 case PARAM_CONSTANT:
Brian978145a2008-03-27 16:00:15 -06001866 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
Jouk Jansen40322e12004-04-05 08:50:36 +00001867 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001868 idx = _mesa_add_named_constant(Program->Base.Parameters,
1869 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001870 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001871 if (param_var->param_binding_begin == ~0U)
1872 param_var->param_binding_begin = idx;
Brian978145a2008-03-27 16:00:15 -06001873 param_var->param_binding_type = PROGRAM_CONSTANT;
Jouk Jansen40322e12004-04-05 08:50:36 +00001874 param_var->param_binding_length++;
1875 Program->Base.NumParameters++;
1876 break;
1877
1878 default:
Brian Paula088f162006-09-05 23:08:51 +00001879 program_error(ctx, Program->Position,
1880 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001881 return 1;
1882 }
1883
1884 /* Make sure we haven't blown past our parameter limits */
1885 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1886 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001887 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001888 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1889 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001890 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001891 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001892 return 1;
1893 }
1894
1895 return err;
1896}
1897
Brian Paul7aebaf32005-10-30 21:23:23 +00001898
Jouk Jansen40322e12004-04-05 08:50:36 +00001899/**
1900 * This picks out PARAM program parameter bindings.
1901 *
1902 * XXX: This needs to be stressed & tested
1903 *
1904 * \return 0 on sucess, 1 on error
1905 */
1906static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001907parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001908 struct arb_program *Program)
1909{
Brian Paulbd997cd2004-07-20 21:12:56 +00001910 GLuint found, err;
1911 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001912 struct var_cache *param_var;
1913
1914 err = 0;
1915 param_var = parse_string (inst, vc_head, Program, &found);
1916 Program->Position = parse_position (inst);
1917
1918 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06001919 program_error2(ctx, Program->Position,
1920 "Duplicate variable declaration",
1921 (char *) param_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001922 return 1;
1923 }
1924
1925 specified_length = parse_integer (inst, Program);
1926
1927 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001928 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001929 return 1;
1930 }
1931
1932 param_var->type = vt_param;
1933 param_var->param_binding_length = 0;
1934
1935 /* Right now, everything is shoved into the main state register file.
1936 *
1937 * In the future, it would be nice to leave things ENV/LOCAL params
1938 * in their respective register files, if possible
1939 */
1940 param_var->param_binding_type = PROGRAM_STATE_VAR;
1941
1942 /* Remember to:
1943 * * - add each guy to the parameter list
1944 * * - increment the param_var->param_binding_len
1945 * * - store the param_var->param_binding_begin for the first one
1946 * * - compare the actual len to the specified len at the end
1947 */
1948 while (**inst != PARAM_NULL) {
1949 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1950 return 1;
1951 }
1952
1953 /* Test array length here! */
1954 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001955 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001956 program_error(ctx, Program->Position,
1957 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001958 }
1959 }
1960
1961 (*inst)++;
1962
1963 return 0;
1964}
1965
1966/**
1967 *
1968 */
1969static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001970parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001971 struct arb_program *Program, struct var_cache **new_var)
1972{
1973 struct var_cache *param_var;
1974
1975 /* First, insert a dummy entry into the var_cache */
1976 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001977 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001978 param_var->type = vt_param;
1979
1980 param_var->param_binding_length = 0;
1981 /* Don't fill in binding_begin; We use the default value of -1
1982 * to tell if its already initialized, elsewhere.
1983 *
1984 * param_var->param_binding_begin = 0;
1985 */
1986 param_var->param_binding_type = PROGRAM_STATE_VAR;
1987
1988 var_cache_append (vc_head, param_var);
1989
1990 /* Then fill it with juicy parameter goodness */
1991 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1992 return 1;
1993
1994 *new_var = param_var;
1995
1996 return 0;
1997}
1998
1999
2000/**
2001 * This handles the declaration of TEMP variables
2002 *
2003 * \return 0 on sucess, 1 on error
2004 */
2005static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002006parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002007 struct arb_program *Program)
2008{
2009 GLuint found;
2010 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002011
2012 while (**inst != 0) {
2013 temp_var = parse_string (inst, vc_head, Program, &found);
2014 Program->Position = parse_position (inst);
2015 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002016 program_error2(ctx, Program->Position,
2017 "Duplicate variable declaration",
2018 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002019 return 1;
2020 }
2021
2022 temp_var->type = vt_temp;
2023
2024 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2025 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002026 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002027 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2028 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002029 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00002030 program_error(ctx, Program->Position,
2031 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002032 return 1;
2033 }
2034
2035 temp_var->temp_binding = Program->Base.NumTemporaries;
2036 Program->Base.NumTemporaries++;
2037 }
2038 (*inst)++;
2039
2040 return 0;
2041}
2042
2043/**
2044 * This handles variables of the OUTPUT variety
2045 *
2046 * \return 0 on sucess, 1 on error
2047 */
2048static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002049parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002050 struct arb_program *Program)
2051{
2052 GLuint found;
2053 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002054 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002055
2056 output_var = parse_string (inst, vc_head, Program, &found);
2057 Program->Position = parse_position (inst);
2058 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002059 program_error2(ctx, Program->Position,
2060 "Duplicate variable declaration",
2061 (char *) output_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002062 return 1;
2063 }
2064
2065 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002066
2067 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2068 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002069}
2070
2071/**
2072 * This handles variables of the ALIAS kind
2073 *
2074 * \return 0 on sucess, 1 on error
2075 */
2076static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002077parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002078 struct arb_program *Program)
2079{
2080 GLuint found;
2081 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002082
2083 temp_var = parse_string (inst, vc_head, Program, &found);
2084 Program->Position = parse_position (inst);
2085
2086 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002087 program_error2(ctx, Program->Position,
2088 "Duplicate variable declaration",
2089 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002090 return 1;
2091 }
2092
2093 temp_var->type = vt_alias;
2094 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2095 Program->Position = parse_position (inst);
2096
2097 if (!found)
2098 {
Brianab31a3a2007-09-13 11:41:49 -06002099 program_error2(ctx, Program->Position,
2100 "Undefined alias value",
2101 (char *) temp_var->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002102 return 1;
2103 }
2104
2105 return 0;
2106}
2107
2108/**
2109 * This handles variables of the ADDRESS kind
2110 *
2111 * \return 0 on sucess, 1 on error
2112 */
2113static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002114parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002115 struct arb_program *Program)
2116{
2117 GLuint found;
2118 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002119
2120 while (**inst != 0) {
2121 temp_var = parse_string (inst, vc_head, Program, &found);
2122 Program->Position = parse_position (inst);
2123 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002124 program_error2(ctx, Program->Position,
2125 "Duplicate variable declaration",
2126 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002127 return 1;
2128 }
2129
2130 temp_var->type = vt_address;
2131
2132 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002133 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002134 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002135 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002136 return 1;
2137 }
2138
2139 temp_var->address_binding = Program->Base.NumAddressRegs;
2140 Program->Base.NumAddressRegs++;
2141 }
2142 (*inst)++;
2143
2144 return 0;
2145}
2146
2147/**
2148 * Parse a program declaration
2149 *
2150 * \return 0 on sucess, 1 on error
2151 */
2152static GLint
Brian Paula088f162006-09-05 23:08:51 +00002153parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002154 struct arb_program *Program)
2155{
2156 GLint err = 0;
2157
2158 switch (*(*inst)++) {
2159 case ADDRESS:
2160 err = parse_address (ctx, inst, vc_head, Program);
2161 break;
2162
2163 case ALIAS:
2164 err = parse_alias (ctx, inst, vc_head, Program);
2165 break;
2166
2167 case ATTRIB:
2168 err = parse_attrib (ctx, inst, vc_head, Program);
2169 break;
2170
2171 case OUTPUT:
2172 err = parse_output (ctx, inst, vc_head, Program);
2173 break;
2174
2175 case PARAM:
2176 err = parse_param (ctx, inst, vc_head, Program);
2177 break;
2178
2179 case TEMP:
2180 err = parse_temp (ctx, inst, vc_head, Program);
2181 break;
2182 }
2183
2184 return err;
2185}
2186
2187/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002188 * Handle the parsing out of a masked destination register, either for a
2189 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002190 *
2191 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002192 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002193 * position invariant
2194 *
2195 * \param File - The register file we write to
2196 * \param Index - The register index we write to
2197 * \param WriteMask - The mask controlling which components we write (1->write)
2198 *
2199 * \return 0 on sucess, 1 on error
2200 */
2201static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002202parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002203 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002204 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002205{
Brian Paul7aebaf32005-10-30 21:23:23 +00002206 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002207 struct var_cache *dst;
2208
2209 /* We either have a result register specified, or a
2210 * variable that may or may not be writable
2211 */
2212 switch (*(*inst)++) {
2213 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002214 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002215 return 1;
2216 *File = PROGRAM_OUTPUT;
2217 break;
2218
2219 case REGISTER_ESTABLISHED_NAME:
2220 dst = parse_string (inst, vc_head, Program, &result);
2221 Program->Position = parse_position (inst);
2222
2223 /* If the name has never been added to our symbol table, we're hosed */
2224 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002225 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002226 return 1;
2227 }
2228
2229 switch (dst->type) {
2230 case vt_output:
2231 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002232 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002233 break;
2234
2235 case vt_temp:
2236 *File = PROGRAM_TEMPORARY;
2237 *Index = dst->temp_binding;
2238 break;
2239
2240 /* If the var type is not vt_output or vt_temp, no go */
2241 default:
Brian Paula088f162006-09-05 23:08:51 +00002242 program_error(ctx, Program->Position,
2243 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002244 return 1;
2245 }
2246 break;
2247
2248 default:
Brian Paula088f162006-09-05 23:08:51 +00002249 program_error(ctx, Program->Position,
2250 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002251 return 1;
2252 }
2253
2254
2255 /* Position invariance test */
2256 /* This test is done now in syntax portion - when position invariance OPTION
2257 is specified, "result.position" rule is disabled so there is no way
2258 to write the position
2259 */
2260 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2261 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002262 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002263 "Vertex program specified position invariance and wrote vertex position");
2264 }*/
2265
2266 /* And then the mask.
2267 * w,a -> bit 0
2268 * z,b -> bit 1
2269 * y,g -> bit 2
2270 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002271 *
2272 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002273 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002274 tmp = (GLint) *(*inst)++;
2275 *WriteMask = (((tmp>>3) & 0x1) |
2276 ((tmp>>1) & 0x2) |
2277 ((tmp<<1) & 0x4) |
2278 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002279
2280 return 0;
2281}
2282
2283
2284/**
2285 * Handle the parsing of a address register
2286 *
2287 * \param Index - The register index we write to
2288 *
2289 * \return 0 on sucess, 1 on error
2290 */
2291static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002292parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002293 struct var_cache **vc_head,
2294 struct arb_program *Program, GLint * Index)
2295{
2296 struct var_cache *dst;
2297 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002298
2299 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002300
2301 dst = parse_string (inst, vc_head, Program, &result);
2302 Program->Position = parse_position (inst);
2303
2304 /* If the name has never been added to our symbol table, we're hosed */
2305 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002306 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002307 return 1;
2308 }
2309
2310 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002311 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002312 return 1;
2313 }
2314
2315 return 0;
2316}
2317
Brian Paul8e8fa632005-07-01 02:03:33 +00002318#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002319/**
2320 * Handle the parsing out of a masked address register
2321 *
2322 * \param Index - The register index we write to
2323 * \param WriteMask - The mask controlling which components we write (1->write)
2324 *
2325 * \return 0 on sucess, 1 on error
2326 */
2327static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002328parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002329 struct var_cache **vc_head,
2330 struct arb_program *Program, GLint * Index,
2331 GLboolean * WriteMask)
2332{
2333 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2334 return 1;
2335
2336 /* This should be 0x8 */
2337 (*inst)++;
2338
2339 /* Writemask of .x is implied */
2340 WriteMask[0] = 1;
2341 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2342
2343 return 0;
2344}
Brian Paul8e8fa632005-07-01 02:03:33 +00002345#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002346
2347/**
2348 * Parse out a swizzle mask.
2349 *
Brian Paul32df89e2005-10-29 18:26:43 +00002350 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002351 *
2352 * The len parameter allows us to grab 4 components for a vector
2353 * swizzle, or just 1 component for a scalar src register selection
2354 */
Brian Paul32df89e2005-10-29 18:26:43 +00002355static void
Brian Paula088f162006-09-05 23:08:51 +00002356parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002357{
Brian Paul32df89e2005-10-29 18:26:43 +00002358 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002359
Brian Paul32df89e2005-10-29 18:26:43 +00002360 for (i = 0; i < 4; i++)
2361 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002362
Brian Paul32df89e2005-10-29 18:26:43 +00002363 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002364 switch (*(*inst)++) {
2365 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002366 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002367 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002368 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002369 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002370 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002371 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002372 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002373 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002374 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002375 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002376 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002377 default:
2378 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2379 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002380 }
2381 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002382}
2383
Jouk Jansen40322e12004-04-05 08:50:36 +00002384
Brian Paul32df89e2005-10-29 18:26:43 +00002385/**
2386 * Parse an extended swizzle mask which is a sequence of
2387 * four x/y/z/w/0/1 tokens.
2388 * \return swizzle four swizzle values
2389 * \return negateMask four element bitfield
2390 */
2391static void
Brian Paula088f162006-09-05 23:08:51 +00002392parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002393 GLubyte *negateMask)
2394{
2395 GLint i;
2396
2397 *negateMask = 0x0;
2398 for (i = 0; i < 4; i++) {
2399 GLubyte swz;
2400 if (parse_sign(inst) == -1)
2401 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002402
2403 swz = *(*inst)++;
2404
2405 switch (swz) {
2406 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002407 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002408 break;
2409 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002410 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002411 break;
2412 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002413 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002414 break;
2415 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002416 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002417 break;
2418 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002419 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002420 break;
2421 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002422 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002423 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002424 default:
2425 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2426 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002427 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002428 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002429}
2430
2431
2432static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002433parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2434 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002435 struct arb_program *Program,
2436 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002437 GLboolean *IsRelOffset )
2438{
2439 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002440 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002441 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002442
Keith Whitwell7c26b612005-04-21 14:46:57 +00002443 *IsRelOffset = 0;
2444
Jouk Jansen40322e12004-04-05 08:50:36 +00002445 /* And the binding for the src */
2446 switch (*(*inst)++) {
2447 case REGISTER_ATTRIB:
2448 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002449 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002450 return 1;
2451 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002452 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002453
2454 /* We need to insert a dummy variable into the var_cache so we can
2455 * catch generic vertex attrib aliasing errors
2456 */
2457 var_cache_create(&src);
2458 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002459 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002460 src->attrib_binding = binding;
2461 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002462 var_cache_append(vc_head, src);
2463 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002464 program_error(ctx, Program->Position,
2465 "Cannot use both a generic vertex attribute "
2466 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002467 return 1;
2468 }
2469 break;
2470
2471 case REGISTER_PARAM:
2472 switch (**inst) {
2473 case PARAM_ARRAY_ELEMENT:
2474 (*inst)++;
2475 src = parse_string (inst, vc_head, Program, &found);
2476 Program->Position = parse_position (inst);
2477
2478 if (!found) {
Brianab31a3a2007-09-13 11:41:49 -06002479 program_error2(ctx, Program->Position,
2480 "Undefined variable",
2481 (char *) src->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002482 return 1;
2483 }
2484
Brian Paul95801792005-12-06 15:41:43 +00002485 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002486
2487 switch (*(*inst)++) {
2488 case ARRAY_INDEX_ABSOLUTE:
2489 offset = parse_integer (inst, Program);
2490
2491 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002492 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002493 program_error(ctx, Program->Position,
2494 "Index out of range");
2495 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002496 return 1;
2497 }
2498
2499 *Index = src->param_binding_begin + offset;
2500 break;
2501
2502 case ARRAY_INDEX_RELATIVE:
2503 {
2504 GLint addr_reg_idx, rel_off;
2505
2506 /* First, grab the address regiseter */
2507 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2508 return 1;
2509
2510 /* And the .x */
2511 ((*inst)++);
2512 ((*inst)++);
2513 ((*inst)++);
2514 ((*inst)++);
2515
2516 /* Then the relative offset */
2517 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2518
2519 /* And store it properly */
2520 *Index = src->param_binding_begin + rel_off;
2521 *IsRelOffset = 1;
2522 }
2523 break;
2524 }
2525 break;
2526
2527 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002528 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2529 return 1;
2530
Brian Paul95801792005-12-06 15:41:43 +00002531 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002532 *Index = src->param_binding_begin;
2533 break;
2534 }
2535 break;
2536
2537 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002538 src = parse_string (inst, vc_head, Program, &found);
2539 Program->Position = parse_position (inst);
2540
2541 /* If the name has never been added to our symbol table, we're hosed */
2542 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002543 program_error(ctx, Program->Position,
2544 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002545 return 1;
2546 }
2547
2548 switch (src->type) {
2549 case vt_attrib:
2550 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002551 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002552 break;
2553
2554 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2555 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002556 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002557 *Index = src->param_binding_begin;
2558 break;
2559
2560 case vt_temp:
2561 *File = PROGRAM_TEMPORARY;
2562 *Index = src->temp_binding;
2563 break;
2564
2565 /* If the var type is vt_output no go */
2566 default:
Brian Paula088f162006-09-05 23:08:51 +00002567 program_error(ctx, Program->Position,
2568 "destination register is read only");
2569 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002570 return 1;
2571 }
2572 break;
2573
2574 default:
Brian Paula088f162006-09-05 23:08:51 +00002575 program_error(ctx, Program->Position,
2576 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002577 return 1;
2578 }
2579
Markus Amslerf2b91422008-03-17 08:35:27 -06002580 /* Add attributes to InputsRead only if they are used the program.
2581 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2582 if (*File == PROGRAM_INPUT)
2583 Program->Base.InputsRead |= (1 << *Index);
2584
Jouk Jansen40322e12004-04-05 08:50:36 +00002585 return 0;
2586}
2587
Brian7d2b6a02008-03-27 16:17:37 -06002588
Jouk Jansen40322e12004-04-05 08:50:36 +00002589/**
Brian7d2b6a02008-03-27 16:17:37 -06002590 * Parse vertex/fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002591 */
2592static GLuint
Brian7d2b6a02008-03-27 16:17:37 -06002593parse_vector_src_reg(GLcontext *ctx, const GLubyte **inst,
2594 struct var_cache **vc_head,
2595 struct arb_program *program,
2596 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002597{
Brian Paul7aebaf32005-10-30 21:23:23 +00002598 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002599 GLint index;
Brian7d2b6a02008-03-27 16:17:37 -06002600 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002601 GLubyte swizzle[4];
2602 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002603
Jouk Jansen40322e12004-04-05 08:50:36 +00002604 /* Grab the sign */
Brian7d2b6a02008-03-27 16:17:37 -06002605 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002606
2607 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002608 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002609 return 1;
2610
2611 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002612 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002613
Brian Paul32df89e2005-10-29 18:26:43 +00002614 reg->File = file;
2615 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002616 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Brian7d2b6a02008-03-27 16:17:37 -06002617 reg->NegateBase = negateMask;
2618 reg->RelAddr = isRelOffset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002619 return 0;
2620}
2621
Jouk Jansen40322e12004-04-05 08:50:36 +00002622
Brian Paulb7fc1c32006-08-30 23:38:03 +00002623/**
Brian7d2b6a02008-03-27 16:17:37 -06002624 * Parse vertex/fragment program scalar source register.
2625 */
2626static GLuint
2627parse_scalar_src_reg(GLcontext *ctx, const GLubyte **inst,
2628 struct var_cache **vc_head,
2629 struct arb_program *program,
2630 struct prog_src_register *reg)
2631{
2632 enum register_file file;
2633 GLint index;
2634 GLubyte negateMask;
2635 GLubyte swizzle[4];
2636 GLboolean isRelOffset;
2637
2638 /* Grab the sign */
2639 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
2640
2641 /* And the src reg */
2642 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
2643 return 1;
2644
2645 /* finally, the swizzle */
2646 parse_swizzle_mask(inst, swizzle, 1);
2647
2648 reg->File = file;
2649 reg->Index = index;
2650 reg->Swizzle = (swizzle[0] << 0);
2651 reg->NegateBase = negateMask;
2652 reg->RelAddr = isRelOffset;
2653 return 0;
2654}
2655
2656
2657/**
2658 * Parse vertex/fragment program destination register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002659 * \return 1 if error, 0 if no error.
2660 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002661static GLuint
Brian7d2b6a02008-03-27 16:17:37 -06002662parse_dst_reg(GLcontext * ctx, const GLubyte ** inst,
2663 struct var_cache **vc_head, struct arb_program *program,
2664 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002665{
Brian Paul7aebaf32005-10-30 21:23:23 +00002666 GLint mask;
2667 GLuint idx;
2668 enum register_file file;
2669
Brian7d2b6a02008-03-27 16:17:37 -06002670 if (parse_masked_dst_reg (ctx, inst, vc_head, program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002671 return 1;
2672
Keith Whitwell7c26b612005-04-21 14:46:57 +00002673 reg->File = file;
2674 reg->Index = idx;
2675 reg->WriteMask = mask;
2676 return 0;
2677}
2678
2679
Brian Paul7aebaf32005-10-30 21:23:23 +00002680/**
Jouk Jansen40322e12004-04-05 08:50:36 +00002681 * This is a big mother that handles getting opcodes into the instruction
2682 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002683 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002684 */
2685static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002686parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002687 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002688 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002689{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002690 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002691 GLuint texcoord;
2692 GLubyte instClass, type, code;
2693 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002694 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002695
Brian Pauld6272e02006-10-29 18:03:16 +00002696 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002697
2698 /* Record the position in the program string for debugging */
2699 fp->StringPos = Program->Position;
2700
2701 /* OP_ALU_INST or OP_TEX_INST */
2702 instClass = *(*inst)++;
2703
2704 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2705 * OP_TEX_{SAMPLE, KIL}
2706 */
2707 type = *(*inst)++;
2708
2709 /* The actual opcode name */
2710 code = *(*inst)++;
2711
2712 /* Increment the correct count */
2713 switch (instClass) {
2714 case OP_ALU_INST:
2715 Program->NumAluInstructions++;
2716 break;
2717 case OP_TEX_INST:
2718 Program->NumTexInstructions++;
2719 break;
2720 }
2721
Jouk Jansen40322e12004-04-05 08:50:36 +00002722 switch (type) {
2723 case OP_ALU_VECTOR:
2724 switch (code) {
2725 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002726 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002727 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002728 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002729 break;
2730
2731 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002732 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002733 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002734 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002735 break;
2736
2737 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002738 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002739 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002740 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002741 break;
2742
2743 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002744 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002745 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002746 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 break;
2748
2749 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002750 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002752 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 break;
2754 }
2755
Brian7d2b6a02008-03-27 16:17:37 -06002756 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002757 return 1;
2758
Brian7d2b6a02008-03-27 16:17:37 -06002759 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002761 break;
2762
2763 case OP_ALU_SCALAR:
2764 switch (code) {
2765 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002766 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002768 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002769 break;
2770
2771 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002772 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002773 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002774 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 break;
2776
2777 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002778 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002779 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002780 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002781 break;
2782
2783 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002784 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002786 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 break;
2788
2789 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002790 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002792 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 break;
2794
2795 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002796 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002798 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002799 break;
2800
2801 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002802 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002803 case OP_SCS:
2804
Brian Paul7e807512005-11-05 17:10:45 +00002805 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002806 break;
2807 }
2808
Brian7d2b6a02008-03-27 16:17:37 -06002809 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002810 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002811
Brian7d2b6a02008-03-27 16:17:37 -06002812 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002814 break;
2815
2816 case OP_ALU_BINSC:
2817 switch (code) {
2818 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002819 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002820 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002821 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002822 break;
2823 }
2824
Brian7d2b6a02008-03-27 16:17:37 -06002825 if (parse_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002826 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002827
Jouk Jansen40322e12004-04-05 08:50:36 +00002828 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002829 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002830 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 }
2832 break;
2833
2834
2835 case OP_ALU_BIN:
2836 switch (code) {
2837 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002838 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002839 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002840 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 break;
2842
2843 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002844 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002846 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 break;
2848
2849 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002850 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002851 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002852 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 break;
2854
2855 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002856 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002857 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002858 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002859 break;
2860
2861 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002862 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002864 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002865 break;
2866
2867 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002868 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002869 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002870 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002871 break;
2872
2873 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002874 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002875 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002876 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002877 break;
2878
2879 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002880 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002881 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002882 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 break;
2884
2885 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002886 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002887 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002888 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 break;
2890
2891 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002892 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002894 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002895 break;
2896
2897 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002898 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002900 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002901 break;
2902
2903 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002904 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002905 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002906 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002907 break;
2908 }
2909
Brian7d2b6a02008-03-27 16:17:37 -06002910 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002911 return 1;
2912 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002913 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002914 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002915 }
2916 break;
2917
2918 case OP_ALU_TRI:
2919 switch (code) {
2920 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002921 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002922 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002923 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002924 break;
2925
2926 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002927 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002928 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002929 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002930 break;
2931
2932 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002933 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002934 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002935 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 break;
2937 }
2938
Brian7d2b6a02008-03-27 16:17:37 -06002939 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002940 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002941
Jouk Jansen40322e12004-04-05 08:50:36 +00002942 for (a = 0; a < 3; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002943 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002944 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002945 }
2946 break;
2947
2948 case OP_ALU_SWZ:
2949 switch (code) {
2950 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002951 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002952 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002953 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002954 break;
2955 }
Brian7d2b6a02008-03-27 16:17:37 -06002956 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002957 return 1;
2958
Keith Whitwell7c26b612005-04-21 14:46:57 +00002959 {
Brian Paul32df89e2005-10-29 18:26:43 +00002960 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002961 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002962 enum register_file file;
2963 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002964
Brian Paul32df89e2005-10-29 18:26:43 +00002965 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002966 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002967 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2968 fp->SrcReg[0].File = file;
2969 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002970 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002971 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2972 swizzle[1],
2973 swizzle[2],
2974 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002975 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002976 break;
2977
2978 case OP_TEX_SAMPLE:
2979 switch (code) {
2980 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002981 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002982 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002983 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002984 break;
2985
2986 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002987 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002988 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002989 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002990 break;
2991
2992 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002993 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002994 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002995 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002996 break;
2997 }
2998
Brian7d2b6a02008-03-27 16:17:37 -06002999 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003000 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003001
Brian7d2b6a02008-03-27 16:17:37 -06003002 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003003 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003004
3005 /* texImageUnit */
3006 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3007 return 1;
3008 fp->TexSrcUnit = texcoord;
3009
3010 /* texTarget */
3011 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07003012 case TEXTARGET_SHADOW1D:
3013 shadow_tex = 1 << texcoord;
3014 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003015 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003016 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003017 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003018 case TEXTARGET_SHADOW2D:
3019 shadow_tex = 1 << texcoord;
3020 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003021 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003022 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003023 break;
3024 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003025 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003026 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003027 case TEXTARGET_SHADOWRECT:
3028 shadow_tex = 1 << texcoord;
3029 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003030 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003031 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003032 break;
3033 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003034 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003035 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003036 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003037 shadow_tex = 1 << texcoord;
3038 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003039 case TEXTARGET_1D_ARRAY:
3040 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3041 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003042 case TEXTARGET_SHADOW2D_ARRAY:
3043 shadow_tex = 1 << texcoord;
3044 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003045 case TEXTARGET_2D_ARRAY:
3046 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3047 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003048 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003049
3050 /* Don't test the first time a particular sampler is seen. Each time
3051 * after that, make sure the shadow state is the same.
3052 */
3053 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3054 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3055 program_error(ctx, Program->Position,
3056 "texture image unit used for shadow sampling and non-shadow sampling");
3057 return 1;
3058 }
3059
Brian Paulb7fc1c32006-08-30 23:38:03 +00003060 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3061 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3062 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003063 program_error(ctx, Program->Position,
3064 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003065 return 1;
3066 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003067
3068
3069 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003070 break;
3071
3072 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003073 Program->UsesKill = 1;
Brian7d2b6a02008-03-27 16:17:37 -06003074 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003075 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003076 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003077 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003078 default:
3079 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3080 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003081 }
3082
3083 return 0;
3084}
3085
Keith Whitwell7c26b612005-04-21 14:46:57 +00003086
3087/**
3088 * Handle the parsing out of a masked address register
3089 *
3090 * \param Index - The register index we write to
3091 * \param WriteMask - The mask controlling which components we write (1->write)
3092 *
3093 * \return 0 on sucess, 1 on error
3094 */
3095static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003096parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003097 struct var_cache **vc_head,
3098 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003099 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003100{
3101 GLint idx;
3102
3103 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3104 return 1;
3105
3106 /* This should be 0x8 */
3107 (*inst)++;
3108
3109 reg->File = PROGRAM_ADDRESS;
3110 reg->Index = idx;
3111
3112 /* Writemask of .x is implied */
3113 reg->WriteMask = 0x1;
3114 return 0;
3115}
3116
Keith Whitwell7c26b612005-04-21 14:46:57 +00003117
Jouk Jansen40322e12004-04-05 08:50:36 +00003118/**
3119 * This is a big mother that handles getting opcodes into the instruction
3120 * and handling the src & dst registers for vertex program instructions
3121 */
3122static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003123parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003124 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003125 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003126{
3127 GLint a;
3128 GLubyte type, code;
3129
3130 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3131 type = *(*inst)++;
3132
3133 /* The actual opcode name */
3134 code = *(*inst)++;
3135
Brian Pauld6272e02006-10-29 18:03:16 +00003136 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003137 /* Record the position in the program string for debugging */
3138 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003139
3140 switch (type) {
3141 /* XXX: */
3142 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003143 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003144
3145 /* Remember to set SrcReg.RelAddr; */
3146
3147 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003148 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003149 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003150
Jouk Jansen40322e12004-04-05 08:50:36 +00003151 vp->DstReg.File = PROGRAM_ADDRESS;
3152
3153 /* Get a scalar src register */
Brian7d2b6a02008-03-27 16:17:37 -06003154 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003155 return 1;
3156
3157 break;
3158
3159 case OP_ALU_VECTOR:
3160 switch (code) {
3161 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003162 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003163 break;
3164 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003165 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003166 break;
3167 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003168 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003169 break;
3170 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003171 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003172 break;
3173 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003174 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003175 break;
3176 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003177
Brian7d2b6a02008-03-27 16:17:37 -06003178 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003179 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003180
Brian7d2b6a02008-03-27 16:17:37 -06003181 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003182 return 1;
3183 break;
3184
3185 case OP_ALU_SCALAR:
3186 switch (code) {
3187 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003188 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003189 break;
3190 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003191 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003192 break;
3193 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003194 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003195 break;
3196 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003197 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003198 break;
3199 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003200 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003201 break;
3202 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003203 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003204 break;
3205 }
Brian7d2b6a02008-03-27 16:17:37 -06003206 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003207 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003208
Brian7d2b6a02008-03-27 16:17:37 -06003209 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003210 return 1;
3211 break;
3212
3213 case OP_ALU_BINSC:
3214 switch (code) {
3215 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003216 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003217 break;
3218 }
Brian7d2b6a02008-03-27 16:17:37 -06003219 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003220 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003221
Jouk Jansen40322e12004-04-05 08:50:36 +00003222 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003223 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003224 return 1;
3225 }
3226 break;
3227
3228 case OP_ALU_BIN:
3229 switch (code) {
3230 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003231 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003232 break;
3233 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003234 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 break;
3236 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003237 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003238 break;
3239 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003240 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003241 break;
3242 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003243 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003244 break;
3245 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003246 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003247 break;
3248 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003249 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003250 break;
3251 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003252 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003253 break;
3254 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003255 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003256 break;
3257 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003258 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003259 break;
3260 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003261 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003262 break;
3263 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003264 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003265 break;
3266 }
Brian7d2b6a02008-03-27 16:17:37 -06003267 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003268 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003269
Jouk Jansen40322e12004-04-05 08:50:36 +00003270 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003271 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003272 return 1;
3273 }
3274 break;
3275
3276 case OP_ALU_TRI:
3277 switch (code) {
3278 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003279 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003280 break;
3281 }
3282
Brian7d2b6a02008-03-27 16:17:37 -06003283 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003284 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003285
Jouk Jansen40322e12004-04-05 08:50:36 +00003286 for (a = 0; a < 3; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003287 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 return 1;
3289 }
3290 break;
3291
3292 case OP_ALU_SWZ:
3293 switch (code) {
3294 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003295 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003296 break;
3297 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003298 {
Brian Paul32df89e2005-10-29 18:26:43 +00003299 GLubyte swizzle[4];
3300 GLubyte negateMask;
3301 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003302 enum register_file file;
3303 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003304
Brian7d2b6a02008-03-27 16:17:37 -06003305 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003306 return 1;
3307
Brian Paul32df89e2005-10-29 18:26:43 +00003308 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003309 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003310 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3311 vp->SrcReg[0].File = file;
3312 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003313 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003314 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3315 swizzle[1],
3316 swizzle[2],
3317 swizzle[3]);
3318 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003319 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003320 break;
3321 }
3322 return 0;
3323}
3324
3325#if DEBUG_PARSING
3326
3327static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003328debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3329 struct arb_program *Program)
3330{
3331 struct var_cache *vc;
3332 GLint a, b;
3333
Brianb618ac82007-02-22 09:39:25 -07003334 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003335
3336 /* First of all, print out the contents of the var_cache */
3337 vc = vc_head;
3338 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003339 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003340 switch (vc->type) {
3341 case vt_none:
3342 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3343 break;
3344 case vt_attrib:
3345 fprintf (stderr, "ATTRIB %s\n", vc->name);
3346 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3347 break;
3348 case vt_param:
3349 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3350 vc->param_binding_begin, vc->param_binding_length);
3351 b = vc->param_binding_begin;
3352 for (a = 0; a < vc->param_binding_length; a++) {
3353 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003354 Program->Base.Parameters->Parameters[a + b].Name);
3355 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3356 const char *s;
3357 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3358 [a + b].StateIndexes);
3359 fprintf(stderr, "%s\n", s);
3360 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003361 }
3362 else
3363 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003364 Program->Base.Parameters->ParameterValues[a + b][0],
3365 Program->Base.Parameters->ParameterValues[a + b][1],
3366 Program->Base.Parameters->ParameterValues[a + b][2],
3367 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003368 }
3369 break;
3370 case vt_temp:
3371 fprintf (stderr, "TEMP %s\n", vc->name);
3372 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3373 break;
3374 case vt_output:
3375 fprintf (stderr, "OUTPUT %s\n", vc->name);
3376 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3377 break;
3378 case vt_alias:
3379 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003380 fprintf (stderr, " binding: 0x%p (%s)\n",
3381 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003382 break;
Brianb618ac82007-02-22 09:39:25 -07003383 default:
3384 /* nothing */
3385 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003386 }
3387 vc = vc->next;
3388 }
3389}
3390
Brian Paul72030e02005-11-03 03:30:34 +00003391#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003392
3393
3394/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003395 * The main loop for parsing a fragment or vertex program
3396 *
Brian Paul72030e02005-11-03 03:30:34 +00003397 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003398 */
Brian Paul72030e02005-11-03 03:30:34 +00003399static GLint
Brian Paula088f162006-09-05 23:08:51 +00003400parse_instructions(GLcontext * ctx, const GLubyte * inst,
3401 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003402{
Brian Paul72030e02005-11-03 03:30:34 +00003403 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3404 ? ctx->Const.FragmentProgram.MaxInstructions
3405 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003406 GLint err = 0;
3407
Brian Paul72030e02005-11-03 03:30:34 +00003408 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3409
Jouk Jansen40322e12004-04-05 08:50:36 +00003410 Program->MajorVersion = (GLuint) * inst++;
3411 Program->MinorVersion = (GLuint) * inst++;
3412
3413 while (*inst != END) {
3414 switch (*inst++) {
3415
3416 case OPTION:
3417 switch (*inst++) {
3418 case ARB_PRECISION_HINT_FASTEST:
3419 Program->PrecisionOption = GL_FASTEST;
3420 break;
3421
3422 case ARB_PRECISION_HINT_NICEST:
3423 Program->PrecisionOption = GL_NICEST;
3424 break;
3425
3426 case ARB_FOG_EXP:
3427 Program->FogOption = GL_EXP;
3428 break;
3429
3430 case ARB_FOG_EXP2:
3431 Program->FogOption = GL_EXP2;
3432 break;
3433
3434 case ARB_FOG_LINEAR:
3435 Program->FogOption = GL_LINEAR;
3436 break;
3437
3438 case ARB_POSITION_INVARIANT:
3439 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003440 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003441 break;
3442
3443 case ARB_FRAGMENT_PROGRAM_SHADOW:
3444 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3445 /* TODO ARB_fragment_program_shadow code */
3446 }
3447 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003448
3449 case ARB_DRAW_BUFFERS:
3450 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3451 /* do nothing for now */
3452 }
3453 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003454
3455 case MESA_TEXTURE_ARRAY:
3456 /* do nothing for now */
3457 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003458 }
3459 break;
3460
3461 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003462 /* check length */
3463 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003464 program_error(ctx, Program->Position,
3465 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003466 return 1;
3467 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003468 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003469 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003470 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003471 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003472 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003473 }
3474 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003475 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003476 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003477 }
3478
Brian Paul72030e02005-11-03 03:30:34 +00003479 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003480 Program->Base.NumInstructions++;
3481 break;
3482
3483 case DECLARATION:
3484 err = parse_declaration (ctx, &inst, vc_head, Program);
3485 break;
3486
3487 default:
3488 break;
3489 }
3490
3491 if (err)
3492 break;
3493 }
3494
3495 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003496 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003497 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003498 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003499 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003500 /* YYY Wrong Position in program, whatever, at least not random -> crash
3501 Program->Position = parse_position (&inst);
3502 */
Brian Paul8c41a142005-11-19 15:36:28 +00003503 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003504 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003505 Program->Base.NumInstructions++;
3506
Brian Paul05051032005-11-01 04:36:33 +00003507 /*
3508 * Initialize native counts to logical counts. The device driver may
3509 * change them if program is translated into a hardware program.
3510 */
3511 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3512 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3513 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3514 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3515 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003516
Jouk Jansen40322e12004-04-05 08:50:36 +00003517 return err;
3518}
3519
Brian Paul7aebaf32005-10-30 21:23:23 +00003520
Jouk Jansen40322e12004-04-05 08:50:36 +00003521/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003522LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003523#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003524;
3525
Brian Paul7aebaf32005-10-30 21:23:23 +00003526
Brian Paul8c41a142005-11-19 15:36:28 +00003527/**
3528 * Set a grammar parameter.
3529 * \param name the grammar parameter
3530 * \param value the new parameter value
3531 * \return 0 if OK, 1 if error
3532 */
3533static int
3534set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003535{
3536 char error_msg[300];
3537 GLint error_pos;
3538
Brian Paul8c41a142005-11-19 15:36:28 +00003539 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003540 return 0;
3541
3542 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3543 _mesa_set_program_error (ctx, error_pos, error_msg);
3544 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3545 return 1;
3546}
3547
Brian Paul8c41a142005-11-19 15:36:28 +00003548
3549/**
3550 * Enable support for the given language option in the parser.
3551 * \return 1 if OK, 0 if error
3552 */
3553static int
3554enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003555{
Brian Paul8c41a142005-11-19 15:36:28 +00003556 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003557}
3558
Brian Paul8c41a142005-11-19 15:36:28 +00003559
3560/**
3561 * Enable parser extensions based on which OpenGL extensions are supported
3562 * by this rendering context.
3563 *
3564 * \return GL_TRUE if OK, GL_FALSE if error.
3565 */
3566static GLboolean
3567enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003568{
Brian Paul8c41a142005-11-19 15:36:28 +00003569#if 0
3570 /* These are not supported at this time */
3571 if ((ctx->Extensions.ARB_vertex_blend ||
3572 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003573 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003574 return GL_FALSE;
3575 if (ctx->Extensions.ARB_matrix_palette
3576 && !enable_ext(ctx, id, "matrix_palette"))
3577 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003578#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003579 if (ctx->Extensions.ARB_fragment_program_shadow
3580 && !enable_ext(ctx, id, "fragment_program_shadow"))
3581 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003582 if (ctx->Extensions.EXT_point_parameters
3583 && !enable_ext(ctx, id, "point_parameters"))
3584 return GL_FALSE;
3585 if (ctx->Extensions.EXT_secondary_color
3586 && !enable_ext(ctx, id, "secondary_color"))
3587 return GL_FALSE;
3588 if (ctx->Extensions.EXT_fog_coord
3589 && !enable_ext(ctx, id, "fog_coord"))
3590 return GL_FALSE;
3591 if (ctx->Extensions.NV_texture_rectangle
3592 && !enable_ext(ctx, id, "texture_rectangle"))
3593 return GL_FALSE;
3594 if (ctx->Extensions.ARB_draw_buffers
3595 && !enable_ext(ctx, id, "draw_buffers"))
3596 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003597 if (ctx->Extensions.MESA_texture_array
3598 && !enable_ext(ctx, id, "texture_array"))
3599 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003600#if 1
3601 /* hack for Warcraft (see bug 8060) */
3602 enable_ext(ctx, id, "vertex_blend");
3603#endif
3604
Brian Paul8c41a142005-11-19 15:36:28 +00003605 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003606}
3607
Brian Paul8c41a142005-11-19 15:36:28 +00003608
Jouk Jansen40322e12004-04-05 08:50:36 +00003609/**
3610 * This kicks everything off.
3611 *
3612 * \param ctx - The GL Context
3613 * \param str - The program string
3614 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003615 * \param program - The arb_program struct to return all the parsed info in
3616 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003617 */
Brian Paul8c41a142005-11-19 15:36:28 +00003618static GLboolean
3619_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3620 const GLubyte *str, GLsizei len,
3621 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003622{
3623 GLint a, err, error_pos;
3624 char error_msg[300];
3625 GLuint parsed_len;
3626 struct var_cache *vc_head;
3627 grammar arbprogram_syn_id;
3628 GLubyte *parsed, *inst;
3629 GLubyte *strz = NULL;
3630 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3631
Brian Paul8c41a142005-11-19 15:36:28 +00003632 /* set the program target before parsing */
3633 program->Base.Target = target;
3634
Brian Paul7f76b8f2004-09-10 01:05:39 +00003635 /* Reset error state */
3636 _mesa_set_program_error(ctx, -1, NULL);
3637
Brian Paul8c41a142005-11-19 15:36:28 +00003638 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003639 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003640 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003641 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003642 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003643
3644 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3645 if (grammar_syn_id == 0) {
3646 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003647 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003648 _mesa_set_program_error (ctx, error_pos, error_msg);
3649 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003650 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003651 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003652 }
3653
Brian Paul8c41a142005-11-19 15:36:28 +00003654 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3655 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003656
Brian Paul49a80ca2006-04-28 15:40:11 +00003657 /* 'parsed' is unused here */
3658 _mesa_free (parsed);
3659 parsed = NULL;
3660
Brian Paul45703642005-10-29 15:52:31 +00003661 /* NOTE: we can't destroy grammar_syn_id right here because
3662 * grammar_destroy() can reset the last error
3663 */
Brian Paul8c41a142005-11-19 15:36:28 +00003664 if (err) {
3665 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003666 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3667 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003668 _mesa_error (ctx, GL_INVALID_OPERATION,
3669 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003670 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003671 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003672 }
3673
3674 grammar_destroy (grammar_syn_id);
3675
3676 arbprogram_syn_is_ok = 1;
3677 }
3678
3679 /* create the grammar object */
3680 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3681 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003682 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003683 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3684 _mesa_set_program_error (ctx, error_pos, error_msg);
3685 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003686 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003687 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003688 }
3689
3690 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003691 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003692 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3693 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003694 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003695 }
3696
Brian Paul8c41a142005-11-19 15:36:28 +00003697 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3698 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003699 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003700 }
3701
3702 /* check for NULL character occurences */
3703 {
Brian Paul8c41a142005-11-19 15:36:28 +00003704 GLint i;
3705 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003706 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003707 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003708 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003709 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003710 }
Brian Paul8c41a142005-11-19 15:36:28 +00003711 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003712 }
3713
3714 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003715 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003716 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003717 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3718 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003719 return GL_FALSE;
3720 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003721 _mesa_memcpy (strz, str, len);
3722 strz[len] = '\0';
3723
Michal Krolb80bc052004-10-21 14:09:54 +00003724 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003725 err = !grammar_fast_check(arbprogram_syn_id, strz,
3726 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003727
3728 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003729 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003730 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3731 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003732
Brian Paulb3aefd12005-09-19 20:12:32 +00003733#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003734 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003735 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003736 int line, col;
3737 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003738 fprintf(stderr, "program: %s\n", (char *) strz);
Eric Anholtb039b782008-01-15 15:09:21 -08003739 fprintf(stderr, "Error Pos: %d\n", ctx->Program.ErrorPos);
3740 s = (char *) _mesa_find_line_column(strz, strz+ctx->Program.ErrorPos,
Brian Paula088f162006-09-05 23:08:51 +00003741 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003742 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
Eric Anholtb039b782008-01-15 15:09:21 -08003743 } while (0);
Brian Paulb3aefd12005-09-19 20:12:32 +00003744#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003745
Brian Paula088f162006-09-05 23:08:51 +00003746 _mesa_free(strz);
3747 _mesa_free(parsed);
3748
Jouk Jansen40322e12004-04-05 08:50:36 +00003749 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003750 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003751 }
3752
Jouk Jansen40322e12004-04-05 08:50:36 +00003753 grammar_destroy (arbprogram_syn_id);
3754
Brian Paul8c41a142005-11-19 15:36:28 +00003755 /*
3756 * Program string is syntactically correct at this point
3757 * Parse the tokenized version of the program now, generating
3758 * vertex/fragment program instructions.
3759 */
3760
Jouk Jansen40322e12004-04-05 08:50:36 +00003761 /* Initialize the arb_program struct */
3762 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003763 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003764 program->Base.NumInstructions =
3765 program->Base.NumTemporaries =
3766 program->Base.NumParameters =
3767 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003768 program->Base.Parameters = _mesa_new_parameter_list ();
3769 program->Base.InputsRead = 0x0;
3770 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003771 program->Position = 0;
3772 program->MajorVersion = program->MinorVersion = 0;
3773 program->PrecisionOption = GL_DONT_CARE;
3774 program->FogOption = GL_NONE;
3775 program->HintPositionInvariant = GL_FALSE;
3776 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003777 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003778 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003779 program->NumAluInstructions =
3780 program->NumTexInstructions =
3781 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003782 program->UsesKill = 0;
3783
Jouk Jansen40322e12004-04-05 08:50:36 +00003784 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003785 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003786
3787 /* Start examining the tokens in the array */
3788 inst = parsed;
3789
3790 /* Check the grammer rev */
3791 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003792 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003793 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003794 }
3795 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003796 /* ignore program target */
3797 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003798 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003799 }
3800
3801 /*debug_variables(ctx, vc_head, program); */
3802
3803 /* We're done with the parsed binary array */
3804 var_cache_destroy (&vc_head);
3805
3806 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003807
Brian Paul8c41a142005-11-19 15:36:28 +00003808 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3809 * to size [ap.Base.NumInstructions].
3810 */
Brian Paula7543902006-08-24 21:58:32 +00003811 program->Base.Instructions
3812 = _mesa_realloc_instructions(program->Base.Instructions,
3813 MAX_INSTRUCTIONS,
3814 program->Base.NumInstructions);
3815
Brian Paul45703642005-10-29 15:52:31 +00003816 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003817}
Brian Paul8c41a142005-11-19 15:36:28 +00003818
3819
3820
3821void
3822_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3823 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003824 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003825{
3826 struct arb_program ap;
3827 GLuint i;
3828
3829 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003830 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003831 /* Error in the program. Just return. */
3832 return;
3833 }
3834
3835 /* Copy the relevant contents of the arb_program struct into the
3836 * fragment_program struct.
3837 */
3838 program->Base.String = ap.Base.String;
3839 program->Base.NumInstructions = ap.Base.NumInstructions;
3840 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3841 program->Base.NumParameters = ap.Base.NumParameters;
3842 program->Base.NumAttributes = ap.Base.NumAttributes;
3843 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003844 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3845 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3846 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3847 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3848 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003849 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3850 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3851 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3852 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3853 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3854 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003855 program->Base.InputsRead = ap.Base.InputsRead;
3856 program->Base.OutputsWritten = ap.Base.OutputsWritten;
Brian Paul1af2b142008-05-14 16:44:48 -06003857 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
Brianc9db2232007-01-04 17:22:19 -07003858 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Brian Paul1af2b142008-05-14 16:44:48 -06003859 if (ap.TexturesUsed[i])
3860 program->Base.SamplersUsed |= (1 << i);
3861 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003862 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003863 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003864 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003865
3866 if (program->Base.Instructions)
3867 _mesa_free(program->Base.Instructions);
3868 program->Base.Instructions = ap.Base.Instructions;
3869
3870 if (program->Base.Parameters)
3871 _mesa_free_parameter_list(program->Base.Parameters);
3872 program->Base.Parameters = ap.Base.Parameters;
3873
3874#if DEBUG_FP
Zack Rusin74964ff2008-06-10 16:59:44 -04003875 _mesa_printf("____________Fragment program %u ________\n", program->Base.Id);
Brian Paul11a54c32006-11-15 19:54:25 +00003876 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003877#endif
3878}
3879
3880
3881
3882/**
3883 * Parse the vertex program string. If success, update the given
3884 * vertex_program object with the new program. Else, leave the vertex_program
3885 * object unchanged.
3886 */
3887void
3888_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3889 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003890 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003891{
3892 struct arb_program ap;
3893
3894 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3895
Brian Paul95801792005-12-06 15:41:43 +00003896 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian3075f262008-02-20 08:53:41 -07003897 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
Brian Paul8c41a142005-11-19 15:36:28 +00003898 return;
3899 }
3900
3901 /* Copy the relevant contents of the arb_program struct into the
3902 * vertex_program struct.
3903 */
3904 program->Base.String = ap.Base.String;
3905 program->Base.NumInstructions = ap.Base.NumInstructions;
3906 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3907 program->Base.NumParameters = ap.Base.NumParameters;
3908 program->Base.NumAttributes = ap.Base.NumAttributes;
3909 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003910 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3911 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3912 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3913 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3914 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003915 program->Base.InputsRead = ap.Base.InputsRead;
3916 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3917 program->IsPositionInvariant = ap.HintPositionInvariant;
3918
3919 if (program->Base.Instructions)
3920 _mesa_free(program->Base.Instructions);
3921 program->Base.Instructions = ap.Base.Instructions;
3922
3923 if (program->Base.Parameters)
3924 _mesa_free_parameter_list(program->Base.Parameters);
3925 program->Base.Parameters = ap.Base.Parameters;
3926
3927#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01003928 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00003929 _mesa_print_program(&program->Base);
3930#endif
3931}