blob: 8803658c1e982ed2c156728918bc23d011552f45 [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 *
Brianb7978af2007-01-09 19:17:17 -07005 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Jouk Jansen40322e12004-04-05 08:50:36 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#define DEBUG_PARSING 0
26
27/**
28 * \file arbprogparse.c
29 * ARB_*_program parser core
30 * \author Karl Rasche
31 */
32
Brianc223c6b2007-07-04 13:15:20 -060033#include "main/glheader.h"
34#include "main/imports.h"
35#include "shader/grammar/grammar_mesa.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000036#include "arbprogparse.h"
Brian Paul32df89e2005-10-29 18:26:43 +000037#include "program.h"
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{
1135 switch (*(*inst)++) {
1136 case STATE_MATERIAL_PARSER:
1137 state_tokens[0] = STATE_MATERIAL;
1138 state_tokens[1] = parse_face_type (inst);
1139 switch (*(*inst)++) {
1140 case MATERIAL_AMBIENT:
1141 state_tokens[2] = STATE_AMBIENT;
1142 break;
1143 case MATERIAL_DIFFUSE:
1144 state_tokens[2] = STATE_DIFFUSE;
1145 break;
1146 case MATERIAL_SPECULAR:
1147 state_tokens[2] = STATE_SPECULAR;
1148 break;
1149 case MATERIAL_EMISSION:
1150 state_tokens[2] = STATE_EMISSION;
1151 break;
1152 case MATERIAL_SHININESS:
1153 state_tokens[2] = STATE_SHININESS;
1154 break;
1155 }
1156 break;
1157
1158 case STATE_LIGHT_PARSER:
1159 state_tokens[0] = STATE_LIGHT;
1160 state_tokens[1] = parse_integer (inst, Program);
1161
1162 /* Check the value of state_tokens[1] against the # of lights */
1163 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001164 program_error(ctx, Program->Position, "Invalid Light Number");
1165 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001166 return 1;
1167 }
1168
1169 switch (*(*inst)++) {
1170 case LIGHT_AMBIENT:
1171 state_tokens[2] = STATE_AMBIENT;
1172 break;
1173 case LIGHT_DIFFUSE:
1174 state_tokens[2] = STATE_DIFFUSE;
1175 break;
1176 case LIGHT_SPECULAR:
1177 state_tokens[2] = STATE_SPECULAR;
1178 break;
1179 case LIGHT_POSITION:
1180 state_tokens[2] = STATE_POSITION;
1181 break;
1182 case LIGHT_ATTENUATION:
1183 state_tokens[2] = STATE_ATTENUATION;
1184 break;
1185 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001186 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001187 break;
1188 case LIGHT_SPOT_DIRECTION:
1189 state_tokens[2] = STATE_SPOT_DIRECTION;
1190 break;
1191 }
1192 break;
1193
1194 case STATE_LIGHT_MODEL:
1195 switch (*(*inst)++) {
1196 case LIGHT_MODEL_AMBIENT:
1197 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1198 break;
1199 case LIGHT_MODEL_SCENECOLOR:
1200 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1201 state_tokens[1] = parse_face_type (inst);
1202 break;
1203 }
1204 break;
1205
1206 case STATE_LIGHT_PROD:
1207 state_tokens[0] = STATE_LIGHTPROD;
1208 state_tokens[1] = parse_integer (inst, Program);
1209
1210 /* Check the value of state_tokens[1] against the # of lights */
1211 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001212 program_error(ctx, Program->Position, "Invalid Light Number");
1213 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001214 return 1;
1215 }
1216
1217 state_tokens[2] = parse_face_type (inst);
1218 switch (*(*inst)++) {
1219 case LIGHT_PROD_AMBIENT:
1220 state_tokens[3] = STATE_AMBIENT;
1221 break;
1222 case LIGHT_PROD_DIFFUSE:
1223 state_tokens[3] = STATE_DIFFUSE;
1224 break;
1225 case LIGHT_PROD_SPECULAR:
1226 state_tokens[3] = STATE_SPECULAR;
1227 break;
1228 }
1229 break;
1230
1231
1232 case STATE_FOG:
1233 switch (*(*inst)++) {
1234 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001235 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001236 break;
1237 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001238 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001239 break;
1240 }
1241 break;
1242
1243 case STATE_TEX_ENV:
1244 state_tokens[1] = parse_integer (inst, Program);
1245 switch (*(*inst)++) {
1246 case TEX_ENV_COLOR:
1247 state_tokens[0] = STATE_TEXENV_COLOR;
1248 break;
1249 }
1250 break;
1251
1252 case STATE_TEX_GEN:
1253 {
1254 GLuint type, coord;
1255
1256 state_tokens[0] = STATE_TEXGEN;
1257 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1258
1259 if (parse_texcoord_num (ctx, inst, Program, &coord))
1260 return 1;
1261 state_tokens[1] = coord;
1262
1263 /* EYE or OBJECT */
Briand799b7a2007-09-13 11:29:00 -06001264 type = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001265
1266 /* 0 - s, 1 - t, 2 - r, 3 - q */
Briand799b7a2007-09-13 11:29:00 -06001267 coord = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001268
1269 if (type == TEX_GEN_EYE) {
1270 switch (coord) {
1271 case COMPONENT_X:
1272 state_tokens[2] = STATE_TEXGEN_EYE_S;
1273 break;
1274 case COMPONENT_Y:
1275 state_tokens[2] = STATE_TEXGEN_EYE_T;
1276 break;
1277 case COMPONENT_Z:
1278 state_tokens[2] = STATE_TEXGEN_EYE_R;
1279 break;
1280 case COMPONENT_W:
1281 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1282 break;
Briand799b7a2007-09-13 11:29:00 -06001283 default:
1284 _mesa_problem(ctx, "bad texgen component in "
1285 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001286 }
1287 }
1288 else {
1289 switch (coord) {
1290 case COMPONENT_X:
1291 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1292 break;
1293 case COMPONENT_Y:
1294 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1295 break;
1296 case COMPONENT_Z:
1297 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1298 break;
1299 case COMPONENT_W:
1300 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1301 break;
Briand799b7a2007-09-13 11:29:00 -06001302 default:
1303 _mesa_problem(ctx, "bad texgen component in "
1304 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001305 }
1306 }
1307 }
1308 break;
1309
1310 case STATE_DEPTH:
1311 switch (*(*inst)++) {
1312 case DEPTH_RANGE:
1313 state_tokens[0] = STATE_DEPTH_RANGE;
1314 break;
1315 }
1316 break;
1317
1318 case STATE_CLIP_PLANE:
1319 state_tokens[0] = STATE_CLIPPLANE;
1320 state_tokens[1] = parse_integer (inst, Program);
Brianaa9d22a2007-02-23 11:21:03 -07001321 if (parse_clipplane_num (ctx, inst, Program,
1322 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001323 return 1;
1324 break;
1325
1326 case STATE_POINT:
Briand799b7a2007-09-13 11:29:00 -06001327 switch (*(*inst)++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001328 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001329 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001330 break;
1331
1332 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001333 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001334 break;
1335 }
1336 break;
1337
1338 /* XXX: I think this is the correct format for a matrix row */
1339 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001340 if (parse_matrix(ctx, inst, Program,
1341 (GLint *) &state_tokens[0],
1342 (GLint *) &state_tokens[1],
1343 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001344 return 1;
1345
Brian65319522007-02-21 11:08:21 -07001346 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001347
1348 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001349 state_tokens[3] = parse_integer (inst, Program);
1350 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001351 program_error(ctx, Program->Position,
1352 "Second matrix index less than the first");
1353 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001354 return 1;
1355 }
1356 }
1357 else {
Brian65319522007-02-21 11:08:21 -07001358 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001359 (*inst)++;
1360 }
1361 break;
1362 }
1363
1364 return 0;
1365}
1366
1367/**
1368 * This parses a state string (rather, the binary version of it) into
1369 * a 6-token similar for the state fetching code in program.c
1370 *
1371 * One might ask, why fetch these parameters into just like you fetch
1372 * state when they are already stored in other places?
1373 *
1374 * Because of array offsets -> We can stick env/local parameters in the
1375 * middle of a parameter array and then index someplace into the array
1376 * when we execute.
1377 *
1378 * One optimization might be to only do this for the cases where the
1379 * env/local parameters end up inside of an array, and leave the
1380 * single parameters (or arrays of pure env/local pareameters) in their
1381 * respective register files.
1382 *
1383 * For ENV parameters, the format is:
1384 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1385 * state_tokens[1] = STATE_ENV
1386 * state_tokens[2] = the parameter index
1387 *
1388 * for LOCAL parameters, the format is:
1389 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1390 * state_tokens[1] = STATE_LOCAL
1391 * state_tokens[2] = the parameter index
1392 *
1393 * \param inst - the start in the binary arry to start working from
1394 * \param state_tokens - the storage for the 6-token state description
1395 * \return - 0 on sucess, 1 on failure
1396 */
1397static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001398parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001399 struct arb_program *Program,
1400 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001401{
1402 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1403 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1404 else
1405 state_tokens[0] = STATE_VERTEX_PROGRAM;
1406
1407
1408 switch (*(*inst)++) {
1409 case PROGRAM_PARAM_ENV:
1410 state_tokens[1] = STATE_ENV;
1411 state_tokens[2] = parse_integer (inst, Program);
1412
1413 /* Check state_tokens[2] against the number of ENV parameters available */
1414 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001415 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001416 ||
1417 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001418 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001419 program_error(ctx, Program->Position,
1420 "Invalid Program Env Parameter");
1421 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001422 return 1;
1423 }
1424
1425 break;
1426
1427 case PROGRAM_PARAM_LOCAL:
1428 state_tokens[1] = STATE_LOCAL;
1429 state_tokens[2] = parse_integer (inst, Program);
1430
1431 /* Check state_tokens[2] against the number of LOCAL parameters available */
1432 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001433 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001434 ||
1435 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001436 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001437 program_error(ctx, Program->Position,
1438 "Invalid Program Local Parameter");
1439 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001440 return 1;
1441 }
1442 break;
1443 }
1444
1445 return 0;
1446}
1447
1448/**
1449 * For ARB_vertex_program, programs are not allowed to use both an explicit
1450 * vertex attribute and a generic vertex attribute corresponding to the same
1451 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1452 *
1453 * This will walk our var_cache and make sure that nobody does anything fishy.
1454 *
1455 * \return 0 on sucess, 1 on error
1456 */
1457static GLuint
1458generic_attrib_check(struct var_cache *vc_head)
1459{
1460 int a;
1461 struct var_cache *curr;
1462 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1463 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1464
1465 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1466 explicitAttrib[a] = GL_FALSE;
1467 genericAttrib[a] = GL_FALSE;
1468 }
1469
1470 curr = vc_head;
1471 while (curr) {
1472 if (curr->type == vt_attrib) {
1473 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001474 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001475 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001476 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001477 }
1478
1479 curr = curr->next;
1480 }
1481
1482 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1483 if ((explicitAttrib[a]) && (genericAttrib[a]))
1484 return 1;
1485 }
1486
1487 return 0;
1488}
1489
1490/**
1491 * This will handle the binding side of an ATTRIB var declaration
1492 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001493 * \param inputReg returns the input register index, one of the
1494 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001495 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001496 */
1497static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001498parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001499 struct arb_program *Program,
1500 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001501{
Jouk Jansen40322e12004-04-05 08:50:36 +00001502 GLint err = 0;
1503
1504 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001505
Jouk Jansen40322e12004-04-05 08:50:36 +00001506 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1507 switch (*(*inst)++) {
1508 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001509 {
1510 GLint coord;
1511 err = parse_color_type (ctx, inst, Program, &coord);
1512 *inputReg = FRAG_ATTRIB_COL0 + coord;
1513 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001514 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001515 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001516 {
Brian8fa6f732007-02-01 09:24:41 -07001517 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001518 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1519 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1520 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001521 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001522 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001523 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001524 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001525 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001526 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001527 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001528 default:
1529 err = 1;
1530 break;
1531 }
1532 }
1533 else {
1534 switch (*(*inst)++) {
1535 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001536 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001537 break;
1538
1539 case VERTEX_ATTRIB_WEIGHT:
1540 {
1541 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001542 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001543 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001544#if 1
1545 /* hack for Warcraft (see bug 8060) */
1546 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001547 break;
Brian Paul3a557502006-09-05 23:15:29 +00001548#else
Brian Paula088f162006-09-05 23:08:51 +00001549 program_error(ctx, Program->Position,
1550 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001551 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001552#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001553 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001554
1555 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001556 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001557 break;
1558
1559 case VERTEX_ATTRIB_COLOR:
1560 {
1561 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001562 err = parse_color_type (ctx, inst, Program, &color);
1563 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001564 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001565 }
1566 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001567 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001568 }
1569 }
1570 break;
1571
1572 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001573 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001574 break;
1575
1576 case VERTEX_ATTRIB_TEXCOORD:
1577 {
Brian8fa6f732007-02-01 09:24:41 -07001578 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001579 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001580 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001581 }
1582 break;
1583
Jouk Jansen40322e12004-04-05 08:50:36 +00001584 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001585 /* Not supported at this time */
1586 {
1587 const char *msg = "ARB_palette_matrix not supported";
1588 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001589 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001590 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001591 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001592
1593 case VERTEX_ATTRIB_GENERIC:
1594 {
1595 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001596 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001597 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001598 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001599 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1600 * attributes do not alias the conventional vertex
1601 * attributes.
1602 */
1603 if (attrib > 0)
1604 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001605 else
1606 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 }
1608 }
1609 break;
1610
1611 default:
1612 err = 1;
1613 break;
1614 }
1615 }
1616
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001618 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001619 }
1620
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 return err;
1622}
1623
Brian Paul7aebaf32005-10-30 21:23:23 +00001624
Jouk Jansen40322e12004-04-05 08:50:36 +00001625/**
1626 * This translates between a binary token for an output variable type
1627 * and the mesa token for the same thing.
1628 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001629 * \param inst The parsed tokens
1630 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001631 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001632 */
1633static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001634parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001635 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001636{
Brian Paul7aebaf32005-10-30 21:23:23 +00001637 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001638
Brian Paul7aebaf32005-10-30 21:23:23 +00001639 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001640 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001642 GLuint out_color;
1643
Brian Paulbe76b7f2004-10-04 14:40:05 +00001644 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001645 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001646 */
1647 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001648 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001649 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001650 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001651 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001652 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1653 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001654 }
1655 break;
1656
1657 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001658 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001659 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001660 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001661 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001662 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001663 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001664 GLint color_type;
1665 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001666 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1667 if (err)
1668 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001669
Jouk Jansen40322e12004-04-05 08:50:36 +00001670 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001671 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001672 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001673 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001674 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001675 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001676 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001677 }
1678 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001679 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001680 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001681 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001682 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001683 }
1684 /* primary color */
1685 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001686 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001687 }
1688 }
1689 }
1690 break;
1691
1692 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001693 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001694 break;
1695
1696 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001697 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001698 break;
1699
1700 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001701 {
1702 GLuint unit;
1703 if (parse_texcoord_num (ctx, inst, Program, &unit))
1704 return 1;
1705 *outputReg = VERT_RESULT_TEX0 + unit;
1706 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001707 break;
1708 }
1709
Brian Paulde997602005-11-12 17:53:14 +00001710 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001711
1712 return 0;
1713}
1714
Brian Paul7aebaf32005-10-30 21:23:23 +00001715
Jouk Jansen40322e12004-04-05 08:50:36 +00001716/**
1717 * This handles the declaration of ATTRIB variables
1718 *
1719 * XXX: Still needs
1720 * parse_vert_attrib_binding(), or something like that
1721 *
1722 * \return 0 on sucess, 1 on error
1723 */
1724static GLint
Brian Paula088f162006-09-05 23:08:51 +00001725parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001726 struct arb_program *Program)
1727{
1728 GLuint found;
Jouk Jansen40322e12004-04-05 08:50:36 +00001729 struct var_cache *attrib_var;
1730
1731 attrib_var = parse_string (inst, vc_head, Program, &found);
1732 Program->Position = parse_position (inst);
1733 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06001734 program_error2(ctx, Program->Position,
1735 "Duplicate variable declaration",
1736 (char *) attrib_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001737 return 1;
1738 }
1739
1740 attrib_var->type = vt_attrib;
1741
Brian Paul7aebaf32005-10-30 21:23:23 +00001742 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001743 &attrib_var->attrib_is_generic))
1744 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001745
Brian Paul7aebaf32005-10-30 21:23:23 +00001746 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001747 program_error(ctx, Program->Position,
1748 "Cannot use both a generic vertex attribute "
1749 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001750 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001751 }
1752
1753 Program->Base.NumAttributes++;
1754 return 0;
1755}
1756
1757/**
1758 * \param use -- TRUE if we're called when declaring implicit parameters,
1759 * FALSE if we're declaraing variables. This has to do with
1760 * if we get a signed or unsigned float for scalar constants
1761 */
1762static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001763parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001764 struct var_cache *param_var,
1765 struct arb_program *Program, GLboolean use)
1766{
1767 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001768 GLuint err = 0;
Roland Scheidegger8dc18842007-11-29 03:08:18 +01001769 gl_state_index state_tokens[STATE_LENGTH] = {0, 0, 0, 0, 0};
Jouk Jansen40322e12004-04-05 08:50:36 +00001770 GLfloat const_values[4];
1771
Jouk Jansen40322e12004-04-05 08:50:36 +00001772 switch (*(*inst)++) {
1773 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001774 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1775 return 1;
1776
1777 /* If we adding STATE_MATRIX that has multiple rows, we need to
1778 * unroll it and call _mesa_add_state_reference() for each row
1779 */
Brian65319522007-02-21 11:08:21 -07001780 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1781 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1782 state_tokens[0] == STATE_MVP_MATRIX ||
1783 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1784 state_tokens[0] == STATE_PROGRAM_MATRIX)
1785 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001786 GLint row;
Brian65319522007-02-21 11:08:21 -07001787 const GLint first_row = state_tokens[2];
1788 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001789
1790 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001791 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001792
Brian Paulde997602005-11-12 17:53:14 +00001793 idx = _mesa_add_state_reference(Program->Base.Parameters,
1794 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001795 if (param_var->param_binding_begin == ~0U)
1796 param_var->param_binding_begin = idx;
1797 param_var->param_binding_length++;
1798 Program->Base.NumParameters++;
1799 }
1800 }
1801 else {
Brian Paulde997602005-11-12 17:53:14 +00001802 idx = _mesa_add_state_reference(Program->Base.Parameters,
1803 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001804 if (param_var->param_binding_begin == ~0U)
1805 param_var->param_binding_begin = idx;
1806 param_var->param_binding_length++;
1807 Program->Base.NumParameters++;
1808 }
1809 break;
1810
1811 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001812 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1813 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001814 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001815 if (param_var->param_binding_begin == ~0U)
1816 param_var->param_binding_begin = idx;
1817 param_var->param_binding_length++;
1818 Program->Base.NumParameters++;
1819
1820 /* Check if there is more: 0 -> we're done, else its an integer */
1821 if (**inst) {
1822 GLuint out_of_range, new_idx;
1823 GLuint start_idx = state_tokens[2] + 1;
1824 GLuint end_idx = parse_integer (inst, Program);
1825
1826 out_of_range = 0;
1827 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1828 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001829 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001830 || ((state_tokens[1] == STATE_LOCAL)
1831 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001832 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001833 out_of_range = 1;
1834 }
1835 else {
1836 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001837 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001838 || ((state_tokens[1] == STATE_LOCAL)
1839 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001840 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001841 out_of_range = 1;
1842 }
1843 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001844 program_error(ctx, Program->Position,
1845 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001846 return 1;
1847 }
1848
1849 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1850 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001851 idx = _mesa_add_state_reference(Program->Base.Parameters,
1852 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001853 param_var->param_binding_length++;
1854 Program->Base.NumParameters++;
1855 }
1856 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001857 else {
1858 (*inst)++;
1859 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001860 break;
1861
1862 case PARAM_CONSTANT:
1863 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001864 idx = _mesa_add_named_constant(Program->Base.Parameters,
1865 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001866 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001867 if (param_var->param_binding_begin == ~0U)
1868 param_var->param_binding_begin = idx;
1869 param_var->param_binding_length++;
1870 Program->Base.NumParameters++;
1871 break;
1872
1873 default:
Brian Paula088f162006-09-05 23:08:51 +00001874 program_error(ctx, Program->Position,
1875 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001876 return 1;
1877 }
1878
1879 /* Make sure we haven't blown past our parameter limits */
1880 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1881 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001882 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001883 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1884 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001885 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001886 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001887 return 1;
1888 }
1889
1890 return err;
1891}
1892
Brian Paul7aebaf32005-10-30 21:23:23 +00001893
Jouk Jansen40322e12004-04-05 08:50:36 +00001894/**
1895 * This picks out PARAM program parameter bindings.
1896 *
1897 * XXX: This needs to be stressed & tested
1898 *
1899 * \return 0 on sucess, 1 on error
1900 */
1901static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001902parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001903 struct arb_program *Program)
1904{
Brian Paulbd997cd2004-07-20 21:12:56 +00001905 GLuint found, err;
1906 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001907 struct var_cache *param_var;
1908
1909 err = 0;
1910 param_var = parse_string (inst, vc_head, Program, &found);
1911 Program->Position = parse_position (inst);
1912
1913 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06001914 program_error2(ctx, Program->Position,
1915 "Duplicate variable declaration",
1916 (char *) param_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001917 return 1;
1918 }
1919
1920 specified_length = parse_integer (inst, Program);
1921
1922 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001923 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001924 return 1;
1925 }
1926
1927 param_var->type = vt_param;
1928 param_var->param_binding_length = 0;
1929
1930 /* Right now, everything is shoved into the main state register file.
1931 *
1932 * In the future, it would be nice to leave things ENV/LOCAL params
1933 * in their respective register files, if possible
1934 */
1935 param_var->param_binding_type = PROGRAM_STATE_VAR;
1936
1937 /* Remember to:
1938 * * - add each guy to the parameter list
1939 * * - increment the param_var->param_binding_len
1940 * * - store the param_var->param_binding_begin for the first one
1941 * * - compare the actual len to the specified len at the end
1942 */
1943 while (**inst != PARAM_NULL) {
1944 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1945 return 1;
1946 }
1947
1948 /* Test array length here! */
1949 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001950 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001951 program_error(ctx, Program->Position,
1952 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001953 }
1954 }
1955
1956 (*inst)++;
1957
1958 return 0;
1959}
1960
1961/**
1962 *
1963 */
1964static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001965parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001966 struct arb_program *Program, struct var_cache **new_var)
1967{
1968 struct var_cache *param_var;
1969
1970 /* First, insert a dummy entry into the var_cache */
1971 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001972 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001973 param_var->type = vt_param;
1974
1975 param_var->param_binding_length = 0;
1976 /* Don't fill in binding_begin; We use the default value of -1
1977 * to tell if its already initialized, elsewhere.
1978 *
1979 * param_var->param_binding_begin = 0;
1980 */
1981 param_var->param_binding_type = PROGRAM_STATE_VAR;
1982
1983 var_cache_append (vc_head, param_var);
1984
1985 /* Then fill it with juicy parameter goodness */
1986 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1987 return 1;
1988
1989 *new_var = param_var;
1990
1991 return 0;
1992}
1993
1994
1995/**
1996 * This handles the declaration of TEMP variables
1997 *
1998 * \return 0 on sucess, 1 on error
1999 */
2000static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002001parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002002 struct arb_program *Program)
2003{
2004 GLuint found;
2005 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002006
2007 while (**inst != 0) {
2008 temp_var = parse_string (inst, vc_head, Program, &found);
2009 Program->Position = parse_position (inst);
2010 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002011 program_error2(ctx, Program->Position,
2012 "Duplicate variable declaration",
2013 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002014 return 1;
2015 }
2016
2017 temp_var->type = vt_temp;
2018
2019 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2020 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002021 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002022 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2023 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002024 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00002025 program_error(ctx, Program->Position,
2026 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002027 return 1;
2028 }
2029
2030 temp_var->temp_binding = Program->Base.NumTemporaries;
2031 Program->Base.NumTemporaries++;
2032 }
2033 (*inst)++;
2034
2035 return 0;
2036}
2037
2038/**
2039 * This handles variables of the OUTPUT variety
2040 *
2041 * \return 0 on sucess, 1 on error
2042 */
2043static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002044parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002045 struct arb_program *Program)
2046{
2047 GLuint found;
2048 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002049 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002050
2051 output_var = parse_string (inst, vc_head, Program, &found);
2052 Program->Position = parse_position (inst);
2053 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002054 program_error2(ctx, Program->Position,
2055 "Duplicate variable declaration",
2056 (char *) output_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002057 return 1;
2058 }
2059
2060 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002061
2062 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2063 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002064}
2065
2066/**
2067 * This handles variables of the ALIAS kind
2068 *
2069 * \return 0 on sucess, 1 on error
2070 */
2071static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002072parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002073 struct arb_program *Program)
2074{
2075 GLuint found;
2076 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002077
2078 temp_var = parse_string (inst, vc_head, Program, &found);
2079 Program->Position = parse_position (inst);
2080
2081 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002082 program_error2(ctx, Program->Position,
2083 "Duplicate variable declaration",
2084 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002085 return 1;
2086 }
2087
2088 temp_var->type = vt_alias;
2089 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2090 Program->Position = parse_position (inst);
2091
2092 if (!found)
2093 {
Brianab31a3a2007-09-13 11:41:49 -06002094 program_error2(ctx, Program->Position,
2095 "Undefined alias value",
2096 (char *) temp_var->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002097 return 1;
2098 }
2099
2100 return 0;
2101}
2102
2103/**
2104 * This handles variables of the ADDRESS kind
2105 *
2106 * \return 0 on sucess, 1 on error
2107 */
2108static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002109parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002110 struct arb_program *Program)
2111{
2112 GLuint found;
2113 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002114
2115 while (**inst != 0) {
2116 temp_var = parse_string (inst, vc_head, Program, &found);
2117 Program->Position = parse_position (inst);
2118 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002119 program_error2(ctx, Program->Position,
2120 "Duplicate variable declaration",
2121 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002122 return 1;
2123 }
2124
2125 temp_var->type = vt_address;
2126
2127 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002128 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002129 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002130 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002131 return 1;
2132 }
2133
2134 temp_var->address_binding = Program->Base.NumAddressRegs;
2135 Program->Base.NumAddressRegs++;
2136 }
2137 (*inst)++;
2138
2139 return 0;
2140}
2141
2142/**
2143 * Parse a program declaration
2144 *
2145 * \return 0 on sucess, 1 on error
2146 */
2147static GLint
Brian Paula088f162006-09-05 23:08:51 +00002148parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002149 struct arb_program *Program)
2150{
2151 GLint err = 0;
2152
2153 switch (*(*inst)++) {
2154 case ADDRESS:
2155 err = parse_address (ctx, inst, vc_head, Program);
2156 break;
2157
2158 case ALIAS:
2159 err = parse_alias (ctx, inst, vc_head, Program);
2160 break;
2161
2162 case ATTRIB:
2163 err = parse_attrib (ctx, inst, vc_head, Program);
2164 break;
2165
2166 case OUTPUT:
2167 err = parse_output (ctx, inst, vc_head, Program);
2168 break;
2169
2170 case PARAM:
2171 err = parse_param (ctx, inst, vc_head, Program);
2172 break;
2173
2174 case TEMP:
2175 err = parse_temp (ctx, inst, vc_head, Program);
2176 break;
2177 }
2178
2179 return err;
2180}
2181
2182/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002183 * Handle the parsing out of a masked destination register, either for a
2184 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002185 *
2186 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002187 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002188 * position invariant
2189 *
2190 * \param File - The register file we write to
2191 * \param Index - The register index we write to
2192 * \param WriteMask - The mask controlling which components we write (1->write)
2193 *
2194 * \return 0 on sucess, 1 on error
2195 */
2196static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002197parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002198 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002199 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002200{
Brian Paul7aebaf32005-10-30 21:23:23 +00002201 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002202 struct var_cache *dst;
2203
2204 /* We either have a result register specified, or a
2205 * variable that may or may not be writable
2206 */
2207 switch (*(*inst)++) {
2208 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002209 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002210 return 1;
2211 *File = PROGRAM_OUTPUT;
2212 break;
2213
2214 case REGISTER_ESTABLISHED_NAME:
2215 dst = parse_string (inst, vc_head, Program, &result);
2216 Program->Position = parse_position (inst);
2217
2218 /* If the name has never been added to our symbol table, we're hosed */
2219 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002220 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002221 return 1;
2222 }
2223
2224 switch (dst->type) {
2225 case vt_output:
2226 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002227 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002228 break;
2229
2230 case vt_temp:
2231 *File = PROGRAM_TEMPORARY;
2232 *Index = dst->temp_binding;
2233 break;
2234
2235 /* If the var type is not vt_output or vt_temp, no go */
2236 default:
Brian Paula088f162006-09-05 23:08:51 +00002237 program_error(ctx, Program->Position,
2238 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002239 return 1;
2240 }
2241 break;
2242
2243 default:
Brian Paula088f162006-09-05 23:08:51 +00002244 program_error(ctx, Program->Position,
2245 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002246 return 1;
2247 }
2248
2249
2250 /* Position invariance test */
2251 /* This test is done now in syntax portion - when position invariance OPTION
2252 is specified, "result.position" rule is disabled so there is no way
2253 to write the position
2254 */
2255 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2256 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002257 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002258 "Vertex program specified position invariance and wrote vertex position");
2259 }*/
2260
2261 /* And then the mask.
2262 * w,a -> bit 0
2263 * z,b -> bit 1
2264 * y,g -> bit 2
2265 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002266 *
2267 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002268 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002269 tmp = (GLint) *(*inst)++;
2270 *WriteMask = (((tmp>>3) & 0x1) |
2271 ((tmp>>1) & 0x2) |
2272 ((tmp<<1) & 0x4) |
2273 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002274
2275 return 0;
2276}
2277
2278
2279/**
2280 * Handle the parsing of a address register
2281 *
2282 * \param Index - The register index we write to
2283 *
2284 * \return 0 on sucess, 1 on error
2285 */
2286static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002287parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002288 struct var_cache **vc_head,
2289 struct arb_program *Program, GLint * Index)
2290{
2291 struct var_cache *dst;
2292 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002293
2294 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002295
2296 dst = parse_string (inst, vc_head, Program, &result);
2297 Program->Position = parse_position (inst);
2298
2299 /* If the name has never been added to our symbol table, we're hosed */
2300 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002301 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002302 return 1;
2303 }
2304
2305 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002306 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002307 return 1;
2308 }
2309
2310 return 0;
2311}
2312
Brian Paul8e8fa632005-07-01 02:03:33 +00002313#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002314/**
2315 * Handle the parsing out of a masked address register
2316 *
2317 * \param Index - The register index we write to
2318 * \param WriteMask - The mask controlling which components we write (1->write)
2319 *
2320 * \return 0 on sucess, 1 on error
2321 */
2322static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002323parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002324 struct var_cache **vc_head,
2325 struct arb_program *Program, GLint * Index,
2326 GLboolean * WriteMask)
2327{
2328 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2329 return 1;
2330
2331 /* This should be 0x8 */
2332 (*inst)++;
2333
2334 /* Writemask of .x is implied */
2335 WriteMask[0] = 1;
2336 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2337
2338 return 0;
2339}
Brian Paul8e8fa632005-07-01 02:03:33 +00002340#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002341
2342/**
2343 * Parse out a swizzle mask.
2344 *
Brian Paul32df89e2005-10-29 18:26:43 +00002345 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002346 *
2347 * The len parameter allows us to grab 4 components for a vector
2348 * swizzle, or just 1 component for a scalar src register selection
2349 */
Brian Paul32df89e2005-10-29 18:26:43 +00002350static void
Brian Paula088f162006-09-05 23:08:51 +00002351parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002352{
Brian Paul32df89e2005-10-29 18:26:43 +00002353 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002354
Brian Paul32df89e2005-10-29 18:26:43 +00002355 for (i = 0; i < 4; i++)
2356 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002357
Brian Paul32df89e2005-10-29 18:26:43 +00002358 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002359 switch (*(*inst)++) {
2360 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002361 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002362 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002363 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002364 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002365 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002366 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002367 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002368 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002369 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002370 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002371 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002372 default:
2373 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2374 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002375 }
2376 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002377}
2378
Jouk Jansen40322e12004-04-05 08:50:36 +00002379
Brian Paul32df89e2005-10-29 18:26:43 +00002380/**
2381 * Parse an extended swizzle mask which is a sequence of
2382 * four x/y/z/w/0/1 tokens.
2383 * \return swizzle four swizzle values
2384 * \return negateMask four element bitfield
2385 */
2386static void
Brian Paula088f162006-09-05 23:08:51 +00002387parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002388 GLubyte *negateMask)
2389{
2390 GLint i;
2391
2392 *negateMask = 0x0;
2393 for (i = 0; i < 4; i++) {
2394 GLubyte swz;
2395 if (parse_sign(inst) == -1)
2396 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002397
2398 swz = *(*inst)++;
2399
2400 switch (swz) {
2401 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002402 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002403 break;
2404 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002405 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002406 break;
2407 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002408 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002409 break;
2410 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002411 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002412 break;
2413 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002414 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002415 break;
2416 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002417 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002418 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002419 default:
2420 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2421 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002422 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002423 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002424}
2425
2426
2427static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002428parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2429 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002430 struct arb_program *Program,
2431 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002432 GLboolean *IsRelOffset )
2433{
2434 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002435 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002436 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002437
Keith Whitwell7c26b612005-04-21 14:46:57 +00002438 *IsRelOffset = 0;
2439
Jouk Jansen40322e12004-04-05 08:50:36 +00002440 /* And the binding for the src */
2441 switch (*(*inst)++) {
2442 case REGISTER_ATTRIB:
2443 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002444 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002445 return 1;
2446 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002447 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002448
2449 /* We need to insert a dummy variable into the var_cache so we can
2450 * catch generic vertex attrib aliasing errors
2451 */
2452 var_cache_create(&src);
2453 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002454 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002455 src->attrib_binding = binding;
2456 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002457 var_cache_append(vc_head, src);
2458 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002459 program_error(ctx, Program->Position,
2460 "Cannot use both a generic vertex attribute "
2461 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002462 return 1;
2463 }
2464 break;
2465
2466 case REGISTER_PARAM:
2467 switch (**inst) {
2468 case PARAM_ARRAY_ELEMENT:
2469 (*inst)++;
2470 src = parse_string (inst, vc_head, Program, &found);
2471 Program->Position = parse_position (inst);
2472
2473 if (!found) {
Brianab31a3a2007-09-13 11:41:49 -06002474 program_error2(ctx, Program->Position,
2475 "Undefined variable",
2476 (char *) src->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002477 return 1;
2478 }
2479
Brian Paul95801792005-12-06 15:41:43 +00002480 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002481
2482 switch (*(*inst)++) {
2483 case ARRAY_INDEX_ABSOLUTE:
2484 offset = parse_integer (inst, Program);
2485
2486 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002487 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002488 program_error(ctx, Program->Position,
2489 "Index out of range");
2490 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002491 return 1;
2492 }
2493
2494 *Index = src->param_binding_begin + offset;
2495 break;
2496
2497 case ARRAY_INDEX_RELATIVE:
2498 {
2499 GLint addr_reg_idx, rel_off;
2500
2501 /* First, grab the address regiseter */
2502 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2503 return 1;
2504
2505 /* And the .x */
2506 ((*inst)++);
2507 ((*inst)++);
2508 ((*inst)++);
2509 ((*inst)++);
2510
2511 /* Then the relative offset */
2512 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2513
2514 /* And store it properly */
2515 *Index = src->param_binding_begin + rel_off;
2516 *IsRelOffset = 1;
2517 }
2518 break;
2519 }
2520 break;
2521
2522 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002523 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2524 return 1;
2525
Brian Paul95801792005-12-06 15:41:43 +00002526 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002527 *Index = src->param_binding_begin;
2528 break;
2529 }
2530 break;
2531
2532 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002533 src = parse_string (inst, vc_head, Program, &found);
2534 Program->Position = parse_position (inst);
2535
2536 /* If the name has never been added to our symbol table, we're hosed */
2537 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002538 program_error(ctx, Program->Position,
2539 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002540 return 1;
2541 }
2542
2543 switch (src->type) {
2544 case vt_attrib:
2545 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002546 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002547 break;
2548
2549 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2550 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002551 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002552 *Index = src->param_binding_begin;
2553 break;
2554
2555 case vt_temp:
2556 *File = PROGRAM_TEMPORARY;
2557 *Index = src->temp_binding;
2558 break;
2559
2560 /* If the var type is vt_output no go */
2561 default:
Brian Paula088f162006-09-05 23:08:51 +00002562 program_error(ctx, Program->Position,
2563 "destination register is read only");
2564 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002565 return 1;
2566 }
2567 break;
2568
2569 default:
Brian Paula088f162006-09-05 23:08:51 +00002570 program_error(ctx, Program->Position,
2571 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002572 return 1;
2573 }
2574
Markus Amslerf2b91422008-03-17 08:35:27 -06002575 /* Add attributes to InputsRead only if they are used the program.
2576 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2577 if (*File == PROGRAM_INPUT)
2578 Program->Base.InputsRead |= (1 << *Index);
2579
Jouk Jansen40322e12004-04-05 08:50:36 +00002580 return 0;
2581}
2582
2583/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002584 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002585 */
2586static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002587parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002588 struct var_cache **vc_head,
2589 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002590 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002591{
Brian Paul7aebaf32005-10-30 21:23:23 +00002592 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002593 GLint index;
2594 GLboolean negate;
2595 GLubyte swizzle[4];
2596 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002597
Jouk Jansen40322e12004-04-05 08:50:36 +00002598 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002599 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002600
2601 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002602 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002603 return 1;
2604
2605 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002606 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002607
Brian Paul32df89e2005-10-29 18:26:43 +00002608 reg->File = file;
2609 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002610 reg->NegateBase = negate;
2611 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002612 return 0;
2613}
2614
Jouk Jansen40322e12004-04-05 08:50:36 +00002615
Brian Paulb7fc1c32006-08-30 23:38:03 +00002616/**
2617 * Parse fragment program destination register.
2618 * \return 1 if error, 0 if no error.
2619 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002620static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002621parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002622 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002623 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002624{
Brian Paul7aebaf32005-10-30 21:23:23 +00002625 GLint mask;
2626 GLuint idx;
2627 enum register_file file;
2628
Keith Whitwell7c26b612005-04-21 14:46:57 +00002629 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002630 return 1;
2631
Keith Whitwell7c26b612005-04-21 14:46:57 +00002632 reg->File = file;
2633 reg->Index = idx;
2634 reg->WriteMask = mask;
2635 return 0;
2636}
2637
2638
Brian Paul7aebaf32005-10-30 21:23:23 +00002639/**
2640 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002641 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002642 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002643static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002644parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002645 struct var_cache **vc_head,
2646 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002647 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002648{
Brian Paul7aebaf32005-10-30 21:23:23 +00002649 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002650 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002651 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002652 GLubyte Swizzle[4];
2653 GLboolean IsRelOffset;
2654
2655 /* Grab the sign */
2656 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2657
2658 /* And the src reg */
2659 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2660 return 1;
2661
2662 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002663 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002664
Keith Whitwell7c26b612005-04-21 14:46:57 +00002665 reg->File = File;
2666 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002667 reg->NegateBase = Negate;
2668 reg->Swizzle = (Swizzle[0] << 0);
2669
Jouk Jansen40322e12004-04-05 08:50:36 +00002670 return 0;
2671}
2672
Keith Whitwell7c26b612005-04-21 14:46:57 +00002673
Jouk Jansen40322e12004-04-05 08:50:36 +00002674/**
2675 * This is a big mother that handles getting opcodes into the instruction
2676 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002677 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002678 */
2679static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002680parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002681 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002682 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002683{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002684 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002685 GLuint texcoord;
2686 GLubyte instClass, type, code;
2687 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002688 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002689
Brian Pauld6272e02006-10-29 18:03:16 +00002690 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002691
2692 /* Record the position in the program string for debugging */
2693 fp->StringPos = Program->Position;
2694
2695 /* OP_ALU_INST or OP_TEX_INST */
2696 instClass = *(*inst)++;
2697
2698 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2699 * OP_TEX_{SAMPLE, KIL}
2700 */
2701 type = *(*inst)++;
2702
2703 /* The actual opcode name */
2704 code = *(*inst)++;
2705
2706 /* Increment the correct count */
2707 switch (instClass) {
2708 case OP_ALU_INST:
2709 Program->NumAluInstructions++;
2710 break;
2711 case OP_TEX_INST:
2712 Program->NumTexInstructions++;
2713 break;
2714 }
2715
Jouk Jansen40322e12004-04-05 08:50:36 +00002716 switch (type) {
2717 case OP_ALU_VECTOR:
2718 switch (code) {
2719 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002720 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002721 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002722 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002723 break;
2724
2725 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002726 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002727 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002728 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002729 break;
2730
2731 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002732 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002733 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002734 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002735 break;
2736
2737 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002738 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002739 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002740 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002741 break;
2742
2743 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002744 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002745 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002746 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 break;
2748 }
2749
Keith Whitwell7c26b612005-04-21 14:46:57 +00002750 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 return 1;
2752
Keith Whitwell7c26b612005-04-21 14:46:57 +00002753 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002755 break;
2756
2757 case OP_ALU_SCALAR:
2758 switch (code) {
2759 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002760 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002761 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002762 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002763 break;
2764
2765 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002766 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002768 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002769 break;
2770
2771 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002772 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002773 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002774 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 break;
2776
2777 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002778 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002779 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002780 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002781 break;
2782
2783 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002784 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002786 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 break;
2788
2789 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002790 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002792 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 break;
2794
2795 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002796 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 case OP_SCS:
2798
Brian Paul7e807512005-11-05 17:10:45 +00002799 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 break;
2801 }
2802
Keith Whitwell7c26b612005-04-21 14:46:57 +00002803 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002804 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002805
2806 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002807 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002808 break;
2809
2810 case OP_ALU_BINSC:
2811 switch (code) {
2812 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002813 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002814 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002815 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002816 break;
2817 }
2818
Keith Whitwell7c26b612005-04-21 14:46:57 +00002819 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002820 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002821
Jouk Jansen40322e12004-04-05 08:50:36 +00002822 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002823 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002824 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 }
2826 break;
2827
2828
2829 case OP_ALU_BIN:
2830 switch (code) {
2831 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002832 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002833 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002834 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 break;
2836
2837 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002838 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002839 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002840 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 break;
2842
2843 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002844 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002846 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 break;
2848
2849 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002850 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002851 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002852 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 break;
2854
2855 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002856 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002857 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002858 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002859 break;
2860
2861 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002862 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002864 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002865 break;
2866
2867 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002868 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002869 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002870 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002871 break;
2872
2873 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002874 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002875 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002876 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002877 break;
2878
2879 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002880 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002881 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002882 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 break;
2884
2885 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002886 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002887 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002888 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 break;
2890
2891 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002892 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002894 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002895 break;
2896
2897 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002898 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002900 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002901 break;
2902 }
2903
Keith Whitwell7c26b612005-04-21 14:46:57 +00002904 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002905 return 1;
2906 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002907 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2908 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002909 }
2910 break;
2911
2912 case OP_ALU_TRI:
2913 switch (code) {
2914 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002915 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002916 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002917 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002918 break;
2919
2920 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002921 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002922 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002923 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002924 break;
2925
2926 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002927 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002928 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002929 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002930 break;
2931 }
2932
Keith Whitwell7c26b612005-04-21 14:46:57 +00002933 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002934 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002935
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002937 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2938 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002939 }
2940 break;
2941
2942 case OP_ALU_SWZ:
2943 switch (code) {
2944 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002945 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002946 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002947 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002948 break;
2949 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002950 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002951 return 1;
2952
Keith Whitwell7c26b612005-04-21 14:46:57 +00002953 {
Brian Paul32df89e2005-10-29 18:26:43 +00002954 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002955 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002956 enum register_file file;
2957 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002958
Brian Paul32df89e2005-10-29 18:26:43 +00002959 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002960 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002961 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2962 fp->SrcReg[0].File = file;
2963 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002964 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002965 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2966 swizzle[1],
2967 swizzle[2],
2968 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002969 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002970 break;
2971
2972 case OP_TEX_SAMPLE:
2973 switch (code) {
2974 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002975 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002976 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002977 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002978 break;
2979
2980 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002981 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002982 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002983 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002984 break;
2985
2986 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002987 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002988 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002989 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002990 break;
2991 }
2992
Keith Whitwell7c26b612005-04-21 14:46:57 +00002993 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002994 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002995
2996 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002997 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002998
2999 /* texImageUnit */
3000 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3001 return 1;
3002 fp->TexSrcUnit = texcoord;
3003
3004 /* texTarget */
3005 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07003006 case TEXTARGET_SHADOW1D:
3007 shadow_tex = 1 << texcoord;
3008 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003010 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003011 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003012 case TEXTARGET_SHADOW2D:
3013 shadow_tex = 1 << texcoord;
3014 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003015 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003016 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003017 break;
3018 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003019 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003020 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003021 case TEXTARGET_SHADOWRECT:
3022 shadow_tex = 1 << texcoord;
3023 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003024 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003025 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003026 break;
3027 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003028 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003029 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003030 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003031 shadow_tex = 1 << texcoord;
3032 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003033 case TEXTARGET_1D_ARRAY:
3034 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3035 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003036 case TEXTARGET_SHADOW2D_ARRAY:
3037 shadow_tex = 1 << texcoord;
3038 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003039 case TEXTARGET_2D_ARRAY:
3040 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3041 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003042 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003043
3044 /* Don't test the first time a particular sampler is seen. Each time
3045 * after that, make sure the shadow state is the same.
3046 */
3047 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3048 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3049 program_error(ctx, Program->Position,
3050 "texture image unit used for shadow sampling and non-shadow sampling");
3051 return 1;
3052 }
3053
Brian Paulb7fc1c32006-08-30 23:38:03 +00003054 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3055 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3056 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003057 program_error(ctx, Program->Position,
3058 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003059 return 1;
3060 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003061
3062
3063 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003064 break;
3065
3066 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003067 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003068 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003069 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003070 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003071 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003072 default:
3073 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3074 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003075 }
3076
3077 return 0;
3078}
3079
Keith Whitwell7c26b612005-04-21 14:46:57 +00003080static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003081parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003082 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003083 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003084{
Brian Paul7aebaf32005-10-30 21:23:23 +00003085 GLint mask;
3086 GLuint idx;
3087 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003088
3089 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3090 return 1;
3091
3092 reg->File = file;
3093 reg->Index = idx;
3094 reg->WriteMask = mask;
3095 return 0;
3096}
3097
3098/**
3099 * Handle the parsing out of a masked address register
3100 *
3101 * \param Index - The register index we write to
3102 * \param WriteMask - The mask controlling which components we write (1->write)
3103 *
3104 * \return 0 on sucess, 1 on error
3105 */
3106static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003107parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003108 struct var_cache **vc_head,
3109 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003110 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003111{
3112 GLint idx;
3113
3114 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3115 return 1;
3116
3117 /* This should be 0x8 */
3118 (*inst)++;
3119
3120 reg->File = PROGRAM_ADDRESS;
3121 reg->Index = idx;
3122
3123 /* Writemask of .x is implied */
3124 reg->WriteMask = 0x1;
3125 return 0;
3126}
3127
3128/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003129 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003130 */
3131static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003132parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003133 struct var_cache **vc_head,
3134 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003135 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003136{
Brian Paul7aebaf32005-10-30 21:23:23 +00003137 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003138 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003139 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003140 GLubyte swizzle[4];
3141 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003142
3143 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003144 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003145
3146 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003147 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003148 return 1;
3149
3150 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003151 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003152
Brian Paul32df89e2005-10-29 18:26:43 +00003153 reg->File = file;
3154 reg->Index = index;
3155 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3156 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003157 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003158 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003159 return 0;
3160}
3161
3162
3163static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003164parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003165 struct var_cache **vc_head,
3166 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003167 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003168{
Brian Paul7aebaf32005-10-30 21:23:23 +00003169 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003170 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003171 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003172 GLubyte Swizzle[4];
3173 GLboolean IsRelOffset;
3174
3175 /* Grab the sign */
3176 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3177
3178 /* And the src reg */
3179 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3180 return 1;
3181
3182 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003183 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003184
3185 reg->File = File;
3186 reg->Index = Index;
3187 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003188 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003189 reg->RelAddr = IsRelOffset;
3190 return 0;
3191}
3192
3193
Jouk Jansen40322e12004-04-05 08:50:36 +00003194/**
3195 * This is a big mother that handles getting opcodes into the instruction
3196 * and handling the src & dst registers for vertex program instructions
3197 */
3198static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003199parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003200 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003201 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003202{
3203 GLint a;
3204 GLubyte type, code;
3205
3206 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3207 type = *(*inst)++;
3208
3209 /* The actual opcode name */
3210 code = *(*inst)++;
3211
Brian Pauld6272e02006-10-29 18:03:16 +00003212 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003213 /* Record the position in the program string for debugging */
3214 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003215
3216 switch (type) {
3217 /* XXX: */
3218 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003219 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003220
3221 /* Remember to set SrcReg.RelAddr; */
3222
3223 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003224 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003225 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003226
Jouk Jansen40322e12004-04-05 08:50:36 +00003227 vp->DstReg.File = PROGRAM_ADDRESS;
3228
3229 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003230 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003231 return 1;
3232
3233 break;
3234
3235 case OP_ALU_VECTOR:
3236 switch (code) {
3237 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003238 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003239 break;
3240 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003241 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003242 break;
3243 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003244 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 break;
3246 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003247 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 break;
3249 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003250 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 break;
3252 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003253
3254 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003255 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003256
3257 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003258 return 1;
3259 break;
3260
3261 case OP_ALU_SCALAR:
3262 switch (code) {
3263 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003264 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003265 break;
3266 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003267 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003268 break;
3269 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003270 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003271 break;
3272 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003273 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003274 break;
3275 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003276 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003277 break;
3278 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003279 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003280 break;
3281 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003282 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003283 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003284
3285 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003286 return 1;
3287 break;
3288
3289 case OP_ALU_BINSC:
3290 switch (code) {
3291 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003292 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003293 break;
3294 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003295 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003296 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003297
Jouk Jansen40322e12004-04-05 08:50:36 +00003298 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003299 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 return 1;
3301 }
3302 break;
3303
3304 case OP_ALU_BIN:
3305 switch (code) {
3306 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003307 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003308 break;
3309 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003310 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003311 break;
3312 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003313 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003314 break;
3315 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003316 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003317 break;
3318 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003319 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003320 break;
3321 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003322 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003323 break;
3324 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003325 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003326 break;
3327 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003328 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003329 break;
3330 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003331 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003332 break;
3333 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003334 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003335 break;
3336 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003337 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003338 break;
3339 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003340 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003341 break;
3342 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003343 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003344 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003345
Jouk Jansen40322e12004-04-05 08:50:36 +00003346 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003347 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003348 return 1;
3349 }
3350 break;
3351
3352 case OP_ALU_TRI:
3353 switch (code) {
3354 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003355 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003356 break;
3357 }
3358
Keith Whitwell7c26b612005-04-21 14:46:57 +00003359 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003360 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003361
Jouk Jansen40322e12004-04-05 08:50:36 +00003362 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003363 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003364 return 1;
3365 }
3366 break;
3367
3368 case OP_ALU_SWZ:
3369 switch (code) {
3370 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003371 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003372 break;
3373 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003374 {
Brian Paul32df89e2005-10-29 18:26:43 +00003375 GLubyte swizzle[4];
3376 GLubyte negateMask;
3377 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003378 enum register_file file;
3379 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003380
Keith Whitwell7c26b612005-04-21 14:46:57 +00003381 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3382 return 1;
3383
Brian Paul32df89e2005-10-29 18:26:43 +00003384 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003385 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003386 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3387 vp->SrcReg[0].File = file;
3388 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003389 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003390 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3391 swizzle[1],
3392 swizzle[2],
3393 swizzle[3]);
3394 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003395 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003396 break;
3397 }
3398 return 0;
3399}
3400
3401#if DEBUG_PARSING
3402
3403static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003404debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3405 struct arb_program *Program)
3406{
3407 struct var_cache *vc;
3408 GLint a, b;
3409
Brianb618ac82007-02-22 09:39:25 -07003410 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003411
3412 /* First of all, print out the contents of the var_cache */
3413 vc = vc_head;
3414 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003415 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003416 switch (vc->type) {
3417 case vt_none:
3418 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3419 break;
3420 case vt_attrib:
3421 fprintf (stderr, "ATTRIB %s\n", vc->name);
3422 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3423 break;
3424 case vt_param:
3425 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3426 vc->param_binding_begin, vc->param_binding_length);
3427 b = vc->param_binding_begin;
3428 for (a = 0; a < vc->param_binding_length; a++) {
3429 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003430 Program->Base.Parameters->Parameters[a + b].Name);
3431 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3432 const char *s;
3433 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3434 [a + b].StateIndexes);
3435 fprintf(stderr, "%s\n", s);
3436 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003437 }
3438 else
3439 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003440 Program->Base.Parameters->ParameterValues[a + b][0],
3441 Program->Base.Parameters->ParameterValues[a + b][1],
3442 Program->Base.Parameters->ParameterValues[a + b][2],
3443 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003444 }
3445 break;
3446 case vt_temp:
3447 fprintf (stderr, "TEMP %s\n", vc->name);
3448 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3449 break;
3450 case vt_output:
3451 fprintf (stderr, "OUTPUT %s\n", vc->name);
3452 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3453 break;
3454 case vt_alias:
3455 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003456 fprintf (stderr, " binding: 0x%p (%s)\n",
3457 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003458 break;
Brianb618ac82007-02-22 09:39:25 -07003459 default:
3460 /* nothing */
3461 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003462 }
3463 vc = vc->next;
3464 }
3465}
3466
Brian Paul72030e02005-11-03 03:30:34 +00003467#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003468
3469
3470/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003471 * The main loop for parsing a fragment or vertex program
3472 *
Brian Paul72030e02005-11-03 03:30:34 +00003473 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003474 */
Brian Paul72030e02005-11-03 03:30:34 +00003475static GLint
Brian Paula088f162006-09-05 23:08:51 +00003476parse_instructions(GLcontext * ctx, const GLubyte * inst,
3477 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003478{
Brian Paul72030e02005-11-03 03:30:34 +00003479 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3480 ? ctx->Const.FragmentProgram.MaxInstructions
3481 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003482 GLint err = 0;
3483
Brian Paul72030e02005-11-03 03:30:34 +00003484 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3485
Jouk Jansen40322e12004-04-05 08:50:36 +00003486 Program->MajorVersion = (GLuint) * inst++;
3487 Program->MinorVersion = (GLuint) * inst++;
3488
3489 while (*inst != END) {
3490 switch (*inst++) {
3491
3492 case OPTION:
3493 switch (*inst++) {
3494 case ARB_PRECISION_HINT_FASTEST:
3495 Program->PrecisionOption = GL_FASTEST;
3496 break;
3497
3498 case ARB_PRECISION_HINT_NICEST:
3499 Program->PrecisionOption = GL_NICEST;
3500 break;
3501
3502 case ARB_FOG_EXP:
3503 Program->FogOption = GL_EXP;
3504 break;
3505
3506 case ARB_FOG_EXP2:
3507 Program->FogOption = GL_EXP2;
3508 break;
3509
3510 case ARB_FOG_LINEAR:
3511 Program->FogOption = GL_LINEAR;
3512 break;
3513
3514 case ARB_POSITION_INVARIANT:
3515 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003516 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003517 break;
3518
3519 case ARB_FRAGMENT_PROGRAM_SHADOW:
3520 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3521 /* TODO ARB_fragment_program_shadow code */
3522 }
3523 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003524
3525 case ARB_DRAW_BUFFERS:
3526 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3527 /* do nothing for now */
3528 }
3529 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003530
3531 case MESA_TEXTURE_ARRAY:
3532 /* do nothing for now */
3533 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003534 }
3535 break;
3536
3537 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003538 /* check length */
3539 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003540 program_error(ctx, Program->Position,
3541 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003542 return 1;
3543 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003544 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003545 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003546 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003547 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003548 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003549 }
3550 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003551 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003552 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003553 }
3554
Brian Paul72030e02005-11-03 03:30:34 +00003555 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003556 Program->Base.NumInstructions++;
3557 break;
3558
3559 case DECLARATION:
3560 err = parse_declaration (ctx, &inst, vc_head, Program);
3561 break;
3562
3563 default:
3564 break;
3565 }
3566
3567 if (err)
3568 break;
3569 }
3570
3571 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003572 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003573 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003574 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003575 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003576 /* YYY Wrong Position in program, whatever, at least not random -> crash
3577 Program->Position = parse_position (&inst);
3578 */
Brian Paul8c41a142005-11-19 15:36:28 +00003579 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003580 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003581 Program->Base.NumInstructions++;
3582
Brian Paul05051032005-11-01 04:36:33 +00003583 /*
3584 * Initialize native counts to logical counts. The device driver may
3585 * change them if program is translated into a hardware program.
3586 */
3587 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3588 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3589 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3590 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3591 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003592
Jouk Jansen40322e12004-04-05 08:50:36 +00003593 return err;
3594}
3595
Brian Paul7aebaf32005-10-30 21:23:23 +00003596
Jouk Jansen40322e12004-04-05 08:50:36 +00003597/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003598LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003599#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003600;
3601
Brian Paul7aebaf32005-10-30 21:23:23 +00003602
Brian Paul8c41a142005-11-19 15:36:28 +00003603/**
3604 * Set a grammar parameter.
3605 * \param name the grammar parameter
3606 * \param value the new parameter value
3607 * \return 0 if OK, 1 if error
3608 */
3609static int
3610set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003611{
3612 char error_msg[300];
3613 GLint error_pos;
3614
Brian Paul8c41a142005-11-19 15:36:28 +00003615 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003616 return 0;
3617
3618 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3619 _mesa_set_program_error (ctx, error_pos, error_msg);
3620 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3621 return 1;
3622}
3623
Brian Paul8c41a142005-11-19 15:36:28 +00003624
3625/**
3626 * Enable support for the given language option in the parser.
3627 * \return 1 if OK, 0 if error
3628 */
3629static int
3630enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003631{
Brian Paul8c41a142005-11-19 15:36:28 +00003632 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003633}
3634
Brian Paul8c41a142005-11-19 15:36:28 +00003635
3636/**
3637 * Enable parser extensions based on which OpenGL extensions are supported
3638 * by this rendering context.
3639 *
3640 * \return GL_TRUE if OK, GL_FALSE if error.
3641 */
3642static GLboolean
3643enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003644{
Brian Paul8c41a142005-11-19 15:36:28 +00003645#if 0
3646 /* These are not supported at this time */
3647 if ((ctx->Extensions.ARB_vertex_blend ||
3648 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003649 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003650 return GL_FALSE;
3651 if (ctx->Extensions.ARB_matrix_palette
3652 && !enable_ext(ctx, id, "matrix_palette"))
3653 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003654#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003655 if (ctx->Extensions.ARB_fragment_program_shadow
3656 && !enable_ext(ctx, id, "fragment_program_shadow"))
3657 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003658 if (ctx->Extensions.EXT_point_parameters
3659 && !enable_ext(ctx, id, "point_parameters"))
3660 return GL_FALSE;
3661 if (ctx->Extensions.EXT_secondary_color
3662 && !enable_ext(ctx, id, "secondary_color"))
3663 return GL_FALSE;
3664 if (ctx->Extensions.EXT_fog_coord
3665 && !enable_ext(ctx, id, "fog_coord"))
3666 return GL_FALSE;
3667 if (ctx->Extensions.NV_texture_rectangle
3668 && !enable_ext(ctx, id, "texture_rectangle"))
3669 return GL_FALSE;
3670 if (ctx->Extensions.ARB_draw_buffers
3671 && !enable_ext(ctx, id, "draw_buffers"))
3672 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003673 if (ctx->Extensions.MESA_texture_array
3674 && !enable_ext(ctx, id, "texture_array"))
3675 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003676#if 1
3677 /* hack for Warcraft (see bug 8060) */
3678 enable_ext(ctx, id, "vertex_blend");
3679#endif
3680
Brian Paul8c41a142005-11-19 15:36:28 +00003681 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003682}
3683
Brian Paul8c41a142005-11-19 15:36:28 +00003684
Jouk Jansen40322e12004-04-05 08:50:36 +00003685/**
3686 * This kicks everything off.
3687 *
3688 * \param ctx - The GL Context
3689 * \param str - The program string
3690 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003691 * \param program - The arb_program struct to return all the parsed info in
3692 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003693 */
Brian Paul8c41a142005-11-19 15:36:28 +00003694static GLboolean
3695_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3696 const GLubyte *str, GLsizei len,
3697 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003698{
3699 GLint a, err, error_pos;
3700 char error_msg[300];
3701 GLuint parsed_len;
3702 struct var_cache *vc_head;
3703 grammar arbprogram_syn_id;
3704 GLubyte *parsed, *inst;
3705 GLubyte *strz = NULL;
3706 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3707
Brian Paul8c41a142005-11-19 15:36:28 +00003708 /* set the program target before parsing */
3709 program->Base.Target = target;
3710
Brian Paul7f76b8f2004-09-10 01:05:39 +00003711 /* Reset error state */
3712 _mesa_set_program_error(ctx, -1, NULL);
3713
Brian Paul8c41a142005-11-19 15:36:28 +00003714 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003715 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003716 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003718 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003719
3720 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3721 if (grammar_syn_id == 0) {
3722 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003723 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003724 _mesa_set_program_error (ctx, error_pos, error_msg);
3725 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003726 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003727 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003728 }
3729
Brian Paul8c41a142005-11-19 15:36:28 +00003730 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3731 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003732
Brian Paul49a80ca2006-04-28 15:40:11 +00003733 /* 'parsed' is unused here */
3734 _mesa_free (parsed);
3735 parsed = NULL;
3736
Brian Paul45703642005-10-29 15:52:31 +00003737 /* NOTE: we can't destroy grammar_syn_id right here because
3738 * grammar_destroy() can reset the last error
3739 */
Brian Paul8c41a142005-11-19 15:36:28 +00003740 if (err) {
3741 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003742 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3743 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003744 _mesa_error (ctx, GL_INVALID_OPERATION,
3745 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003746 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003747 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003748 }
3749
3750 grammar_destroy (grammar_syn_id);
3751
3752 arbprogram_syn_is_ok = 1;
3753 }
3754
3755 /* create the grammar object */
3756 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3757 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003758 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003759 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3760 _mesa_set_program_error (ctx, error_pos, error_msg);
3761 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003762 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003763 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003764 }
3765
3766 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003767 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003768 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3769 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003770 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003771 }
3772
Brian Paul8c41a142005-11-19 15:36:28 +00003773 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3774 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003775 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003776 }
3777
3778 /* check for NULL character occurences */
3779 {
Brian Paul8c41a142005-11-19 15:36:28 +00003780 GLint i;
3781 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003782 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003783 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003784 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003785 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003786 }
Brian Paul8c41a142005-11-19 15:36:28 +00003787 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003788 }
3789
3790 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003791 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003792 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003793 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3794 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003795 return GL_FALSE;
3796 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003797 _mesa_memcpy (strz, str, len);
3798 strz[len] = '\0';
3799
Michal Krolb80bc052004-10-21 14:09:54 +00003800 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003801 err = !grammar_fast_check(arbprogram_syn_id, strz,
3802 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003803
3804 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003805 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003806 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3807 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003808
Brian Paulb3aefd12005-09-19 20:12:32 +00003809#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003810 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003811 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003812 int line, col;
3813 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003814 fprintf(stderr, "program: %s\n", (char *) strz);
Eric Anholtb039b782008-01-15 15:09:21 -08003815 fprintf(stderr, "Error Pos: %d\n", ctx->Program.ErrorPos);
3816 s = (char *) _mesa_find_line_column(strz, strz+ctx->Program.ErrorPos,
Brian Paula088f162006-09-05 23:08:51 +00003817 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003818 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
Eric Anholtb039b782008-01-15 15:09:21 -08003819 } while (0);
Brian Paulb3aefd12005-09-19 20:12:32 +00003820#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003821
Brian Paula088f162006-09-05 23:08:51 +00003822 _mesa_free(strz);
3823 _mesa_free(parsed);
3824
Jouk Jansen40322e12004-04-05 08:50:36 +00003825 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003826 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003827 }
3828
Jouk Jansen40322e12004-04-05 08:50:36 +00003829 grammar_destroy (arbprogram_syn_id);
3830
Brian Paul8c41a142005-11-19 15:36:28 +00003831 /*
3832 * Program string is syntactically correct at this point
3833 * Parse the tokenized version of the program now, generating
3834 * vertex/fragment program instructions.
3835 */
3836
Jouk Jansen40322e12004-04-05 08:50:36 +00003837 /* Initialize the arb_program struct */
3838 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003839 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003840 program->Base.NumInstructions =
3841 program->Base.NumTemporaries =
3842 program->Base.NumParameters =
3843 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003844 program->Base.Parameters = _mesa_new_parameter_list ();
3845 program->Base.InputsRead = 0x0;
3846 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003847 program->Position = 0;
3848 program->MajorVersion = program->MinorVersion = 0;
3849 program->PrecisionOption = GL_DONT_CARE;
3850 program->FogOption = GL_NONE;
3851 program->HintPositionInvariant = GL_FALSE;
3852 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003853 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003854 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003855 program->NumAluInstructions =
3856 program->NumTexInstructions =
3857 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003858 program->UsesKill = 0;
3859
Jouk Jansen40322e12004-04-05 08:50:36 +00003860 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003861 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003862
3863 /* Start examining the tokens in the array */
3864 inst = parsed;
3865
3866 /* Check the grammer rev */
3867 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003868 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003869 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003870 }
3871 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003872 /* ignore program target */
3873 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003874 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003875 }
3876
3877 /*debug_variables(ctx, vc_head, program); */
3878
3879 /* We're done with the parsed binary array */
3880 var_cache_destroy (&vc_head);
3881
3882 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003883
Brian Paul8c41a142005-11-19 15:36:28 +00003884 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3885 * to size [ap.Base.NumInstructions].
3886 */
Brian Paula7543902006-08-24 21:58:32 +00003887 program->Base.Instructions
3888 = _mesa_realloc_instructions(program->Base.Instructions,
3889 MAX_INSTRUCTIONS,
3890 program->Base.NumInstructions);
3891
Brian Paul45703642005-10-29 15:52:31 +00003892 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003893}
Brian Paul8c41a142005-11-19 15:36:28 +00003894
3895
3896
3897void
3898_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3899 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003900 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003901{
3902 struct arb_program ap;
3903 GLuint i;
3904
3905 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003906 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003907 /* Error in the program. Just return. */
3908 return;
3909 }
3910
3911 /* Copy the relevant contents of the arb_program struct into the
3912 * fragment_program struct.
3913 */
3914 program->Base.String = ap.Base.String;
3915 program->Base.NumInstructions = ap.Base.NumInstructions;
3916 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3917 program->Base.NumParameters = ap.Base.NumParameters;
3918 program->Base.NumAttributes = ap.Base.NumAttributes;
3919 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003920 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3921 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3922 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3923 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3924 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003925 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3926 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3927 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3928 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3929 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3930 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003931 program->Base.InputsRead = ap.Base.InputsRead;
3932 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3933 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
Brianc9db2232007-01-04 17:22:19 -07003934 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Ian Romanick7b559a92007-06-07 13:58:50 -07003935 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003936 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003937 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003938
3939 if (program->Base.Instructions)
3940 _mesa_free(program->Base.Instructions);
3941 program->Base.Instructions = ap.Base.Instructions;
3942
3943 if (program->Base.Parameters)
3944 _mesa_free_parameter_list(program->Base.Parameters);
3945 program->Base.Parameters = ap.Base.Parameters;
3946
3947#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00003948 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
3949 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003950#endif
3951}
3952
3953
3954
3955/**
3956 * Parse the vertex program string. If success, update the given
3957 * vertex_program object with the new program. Else, leave the vertex_program
3958 * object unchanged.
3959 */
3960void
3961_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3962 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003963 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003964{
3965 struct arb_program ap;
3966
3967 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3968
Brian Paul95801792005-12-06 15:41:43 +00003969 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian3075f262008-02-20 08:53:41 -07003970 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
Brian Paul8c41a142005-11-19 15:36:28 +00003971 return;
3972 }
3973
3974 /* Copy the relevant contents of the arb_program struct into the
3975 * vertex_program struct.
3976 */
3977 program->Base.String = ap.Base.String;
3978 program->Base.NumInstructions = ap.Base.NumInstructions;
3979 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3980 program->Base.NumParameters = ap.Base.NumParameters;
3981 program->Base.NumAttributes = ap.Base.NumAttributes;
3982 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003983 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3984 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3985 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3986 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3987 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003988 program->Base.InputsRead = ap.Base.InputsRead;
3989 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3990 program->IsPositionInvariant = ap.HintPositionInvariant;
3991
3992 if (program->Base.Instructions)
3993 _mesa_free(program->Base.Instructions);
3994 program->Base.Instructions = ap.Base.Instructions;
3995
3996 if (program->Base.Parameters)
3997 _mesa_free_parameter_list(program->Base.Parameters);
3998 program->Base.Parameters = ap.Base.Parameters;
3999
4000#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01004001 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00004002 _mesa_print_program(&program->Base);
4003#endif
4004}