blob: e385b9d99736673d28b98b5a9306a7c33b72caf9 [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brianb7978af2007-01-09 19:17:17 -07003 * Version: 6.5.3
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
636
Jouk Jansen40322e12004-04-05 08:50:36 +0000637/**
638 * constructs an integer from 4 GLubytes in LE format
639 */
640static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000641parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000642{
643 GLuint value;
644
645 value = (GLuint) (*(*inst)++);
646 value += (GLuint) (*(*inst)++) * 0x100;
647 value += (GLuint) (*(*inst)++) * 0x10000;
648 value += (GLuint) (*(*inst)++) * 0x1000000;
649
650 return value;
651}
652
653/**
654 * This will, given a string, lookup the string as a variable name in the
655 * var cache. If the name is found, the var cache node corresponding to the
656 * var name is returned. If it is not found, a new entry is allocated
657 *
658 * \param I Points into the binary array where the string identifier begins
659 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
660 * \return The location on the var_cache corresponding the the string starting at I
661 */
662static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000663parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000664 struct arb_program *Program, GLuint * found)
665{
Brian Paula088f162006-09-05 23:08:51 +0000666 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000667 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000668 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000669
670 *inst += _mesa_strlen ((char *) i) + 1;
671
672 va = var_cache_find (*vc_head, i);
673
674 if (va) {
675 *found = 1;
676 return va;
677 }
678
679 *found = 0;
680 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000681 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000682
683 var_cache_append (vc_head, va);
684
685 return va;
686}
687
688static char *
Brian Paula088f162006-09-05 23:08:51 +0000689parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000690{
Brian Paula088f162006-09-05 23:08:51 +0000691 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000692 (void) Program;
693
Jouk Jansen40322e12004-04-05 08:50:36 +0000694 *inst += _mesa_strlen ((char *) i) + 1;
695
696 return (char *) i;
697}
698
699/**
Brian Paul05908952004-06-08 15:20:23 +0000700 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000701 */
Brian Paul05908952004-06-08 15:20:23 +0000702static GLint
Brian Paula088f162006-09-05 23:08:51 +0000703parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000704{
705 /*return *(*inst)++ != '+'; */
706
707 if (**inst == '-') {
708 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000709 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000710 }
711 else if (**inst == '+') {
712 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000713 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000714 }
715
Brian Paul05908952004-06-08 15:20:23 +0000716 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000717}
718
719/**
720 * parses and returns signed integer
721 */
722static GLint
Brian Paula088f162006-09-05 23:08:51 +0000723parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000724{
725 GLint sign;
726 GLint value;
727
728 /* check if *inst points to '+' or '-'
729 * if yes, grab the sign and increment *inst
730 */
731 sign = parse_sign (inst);
732
733 /* now check if *inst points to 0
734 * if yes, increment the *inst and return the default value
735 */
736 if (**inst == 0) {
737 (*inst)++;
738 return 0;
739 }
740
741 /* parse the integer as you normally would do it */
742 value = _mesa_atoi (parse_string_without_adding (inst, Program));
743
744 /* now, after terminating 0 there is a position
745 * to parse it - parse_position()
746 */
747 Program->Position = parse_position (inst);
748
Brian Paul05908952004-06-08 15:20:23 +0000749 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000750}
751
752/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000753 Accumulate this string of digits, and return them as
754 a large integer represented in floating point (for range).
755 If scale is not NULL, also accumulates a power-of-ten
756 integer scale factor that represents the number of digits
757 in the string.
758*/
759static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000760parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000761{
762 GLdouble value = 0.0;
763 GLdouble oscale = 1.0;
764
765 if (**inst == 0) { /* this string of digits is empty-- do nothing */
766 (*inst)++;
767 }
768 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000769 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000770 GLubyte digit = *((*inst)++);
771 value = value * 10.0 + (GLint) (digit - '0');
772 oscale *= 10.0;
773 }
774 assert(**inst == 0); /* integer string should end with 0 */
775 (*inst)++; /* skip over terminating 0 */
776 Program->Position = parse_position(inst); /* skip position (from integer) */
777 }
778 if (scale)
779 *scale = oscale;
780 return value;
781}
782
783/**
784 Parse an unsigned floating-point number from this stream of tokenized
785 characters. Example floating-point formats supported:
786 12.34
787 12
788 0.34
789 .34
790 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000791 */
792static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000793parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000794{
Brian Paul1ff8f502005-02-16 15:08:29 +0000795 GLint exponent;
796 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000797
Brian Paul1ff8f502005-02-16 15:08:29 +0000798 whole = parse_float_string(inst, Program, 0);
799 fraction = parse_float_string(inst, Program, &fracScale);
800
801 /* Parse signed exponent */
802 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000803
Brian Paul1ff8f502005-02-16 15:08:29 +0000804 /* Assemble parts of floating-point number: */
805 return (GLfloat) ((whole + fraction / fracScale) *
806 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000807}
808
809
810/**
811 */
812static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000813parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000814{
Brian Paul05908952004-06-08 15:20:23 +0000815 GLint sign = parse_sign (inst);
816 GLfloat value = parse_float (inst, Program);
817 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000818}
819
820/**
821 * This picks out a constant value from the parsed array. The constant vector is r
822 * returned in the *values array, which should be of length 4.
823 *
824 * \param values - The 4 component vector with the constant value in it
825 */
826static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000827parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000828 GLboolean use)
829{
830 GLuint components, i;
831
832
833 switch (*(*inst)++) {
834 case CONSTANT_SCALAR:
835 if (use == GL_TRUE) {
836 values[0] =
837 values[1] =
838 values[2] = values[3] = parse_float (inst, Program);
839 }
840 else {
841 values[0] =
842 values[1] =
843 values[2] = values[3] = parse_signed_float (inst, Program);
844 }
845
846 break;
847 case CONSTANT_VECTOR:
848 values[0] = values[1] = values[2] = 0;
849 values[3] = 1;
850 components = *(*inst)++;
851 for (i = 0; i < components; i++) {
852 values[i] = parse_signed_float (inst, Program);
853 }
854 break;
855 }
856}
857
858/**
859 * \param offset The offset from the address register that we should
860 * address
861 *
862 * \return 0 on sucess, 1 on error
863 */
864static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000865parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
866 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000867{
Brian Paul6a769d92006-04-28 15:42:15 +0000868 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000869 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000870 return 0;
871}
872
873/**
874 * \param color 0 if color type is primary, 1 if color type is secondary
875 * \return 0 on sucess, 1 on error
876 */
877static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000878parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000879 GLint * color)
880{
Brian Paula6c423d2004-08-25 15:59:48 +0000881 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000882 *color = *(*inst)++ != COLOR_PRIMARY;
883 return 0;
884}
885
886/**
887 * Get an integer corresponding to a generic vertex attribute.
888 *
889 * \return 0 on sucess, 1 on error
890 */
891static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000892parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000893 struct arb_program *Program, GLuint *attrib)
894{
Brian Paulbd997cd2004-07-20 21:12:56 +0000895 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000896
Michal Krolbb38cad2006-04-11 11:41:11 +0000897 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000898 {
Brian Paula088f162006-09-05 23:08:51 +0000899 program_error(ctx, Program->Position,
900 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000901 return 1;
902 }
903
Brian Paulbd997cd2004-07-20 21:12:56 +0000904 *attrib = (GLuint) i;
905
Jouk Jansen40322e12004-04-05 08:50:36 +0000906 return 0;
907}
908
909
910/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000911 * \param color The index of the color buffer to write into
912 * \return 0 on sucess, 1 on error
913 */
914static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000915parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000916 struct arb_program *Program, GLuint * color)
917{
918 GLint i = parse_integer (inst, Program);
919
920 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000921 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000922 return 1;
923 }
924
925 *color = (GLuint) i;
926 return 0;
927}
928
929
930/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000931 * \param coord The texture unit index
932 * \return 0 on sucess, 1 on error
933 */
934static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000935parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000936 struct arb_program *Program, GLuint * coord)
937{
Brian Paulbd997cd2004-07-20 21:12:56 +0000938 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000939
Brian Paula6c423d2004-08-25 15:59:48 +0000940 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000941 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000942 return 1;
943 }
944
Brian Paulbd997cd2004-07-20 21:12:56 +0000945 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000946 return 0;
947}
948
949/**
950 * \param coord The weight index
951 * \return 0 on sucess, 1 on error
952 */
953static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000954parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000955 GLint * coord)
956{
957 *coord = parse_integer (inst, Program);
958
959 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000960 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000961 return 1;
962 }
963
964 return 0;
965}
966
967/**
968 * \param coord The clip plane index
969 * \return 0 on sucess, 1 on error
970 */
971static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000972parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000973 struct arb_program *Program, GLint * coord)
974{
975 *coord = parse_integer (inst, Program);
976
977 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +0000978 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000979 return 1;
980 }
981
982 return 0;
983}
984
985
986/**
987 * \return 0 on front face, 1 on back face
988 */
989static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000990parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000991{
992 switch (*(*inst)++) {
993 case FACE_FRONT:
994 return 0;
995
996 case FACE_BACK:
997 return 1;
998 }
999 return 0;
1000}
1001
1002
1003/**
1004 * Given a matrix and a modifier token on the binary array, return tokens
1005 * that _mesa_fetch_state() [program.c] can understand.
1006 *
1007 * \param matrix - the matrix we are talking about
1008 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1009 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1010 * \return 0 on sucess, 1 on failure
1011 */
1012static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001013parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001014 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1015{
1016 GLubyte mat = *(*inst)++;
1017
1018 *matrix_idx = 0;
1019
1020 switch (mat) {
1021 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001022 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001023 *matrix_idx = parse_integer (inst, Program);
1024 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001025 program_error(ctx, Program->Position,
1026 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001027 return 1;
1028 }
1029 break;
1030
1031 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001032 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001033 break;
1034
1035 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001036 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001037 break;
1038
1039 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001040 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001041 *matrix_idx = parse_integer (inst, Program);
1042 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001043 program_error(ctx, Program->Position, "Invalid Texture Unit");
1044 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001045 return 1;
1046 }
1047 break;
1048
1049 /* This is not currently supported (ARB_matrix_palette) */
1050 case MATRIX_PALETTE:
1051 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001052 program_error(ctx, Program->Position,
1053 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001054 return 1;
1055 break;
1056
1057 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001058 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001059 *matrix_idx = parse_integer (inst, Program);
1060 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001061 program_error(ctx, Program->Position, "Invalid Program Matrix");
1062 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001063 return 1;
1064 }
1065 break;
1066 }
1067
1068 switch (*(*inst)++) {
1069 case MATRIX_MODIFIER_IDENTITY:
1070 *matrix_modifier = 0;
1071 break;
1072 case MATRIX_MODIFIER_INVERSE:
1073 *matrix_modifier = STATE_MATRIX_INVERSE;
1074 break;
1075 case MATRIX_MODIFIER_TRANSPOSE:
1076 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1077 break;
1078 case MATRIX_MODIFIER_INVTRANS:
1079 *matrix_modifier = STATE_MATRIX_INVTRANS;
1080 break;
1081 }
1082
1083 return 0;
1084}
1085
1086
1087/**
1088 * This parses a state string (rather, the binary version of it) into
1089 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1090 *
1091 * \param inst - the start in the binary arry to start working from
1092 * \param state_tokens - the storage for the 6-token state description
1093 * \return - 0 on sucess, 1 on error
1094 */
1095static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001096parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001097 struct arb_program *Program,
1098 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001099{
1100 switch (*(*inst)++) {
1101 case STATE_MATERIAL_PARSER:
1102 state_tokens[0] = STATE_MATERIAL;
1103 state_tokens[1] = parse_face_type (inst);
1104 switch (*(*inst)++) {
1105 case MATERIAL_AMBIENT:
1106 state_tokens[2] = STATE_AMBIENT;
1107 break;
1108 case MATERIAL_DIFFUSE:
1109 state_tokens[2] = STATE_DIFFUSE;
1110 break;
1111 case MATERIAL_SPECULAR:
1112 state_tokens[2] = STATE_SPECULAR;
1113 break;
1114 case MATERIAL_EMISSION:
1115 state_tokens[2] = STATE_EMISSION;
1116 break;
1117 case MATERIAL_SHININESS:
1118 state_tokens[2] = STATE_SHININESS;
1119 break;
1120 }
1121 break;
1122
1123 case STATE_LIGHT_PARSER:
1124 state_tokens[0] = STATE_LIGHT;
1125 state_tokens[1] = parse_integer (inst, Program);
1126
1127 /* Check the value of state_tokens[1] against the # of lights */
1128 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001129 program_error(ctx, Program->Position, "Invalid Light Number");
1130 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001131 return 1;
1132 }
1133
1134 switch (*(*inst)++) {
1135 case LIGHT_AMBIENT:
1136 state_tokens[2] = STATE_AMBIENT;
1137 break;
1138 case LIGHT_DIFFUSE:
1139 state_tokens[2] = STATE_DIFFUSE;
1140 break;
1141 case LIGHT_SPECULAR:
1142 state_tokens[2] = STATE_SPECULAR;
1143 break;
1144 case LIGHT_POSITION:
1145 state_tokens[2] = STATE_POSITION;
1146 break;
1147 case LIGHT_ATTENUATION:
1148 state_tokens[2] = STATE_ATTENUATION;
1149 break;
1150 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001151 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001152 break;
1153 case LIGHT_SPOT_DIRECTION:
1154 state_tokens[2] = STATE_SPOT_DIRECTION;
1155 break;
1156 }
1157 break;
1158
1159 case STATE_LIGHT_MODEL:
1160 switch (*(*inst)++) {
1161 case LIGHT_MODEL_AMBIENT:
1162 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1163 break;
1164 case LIGHT_MODEL_SCENECOLOR:
1165 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1166 state_tokens[1] = parse_face_type (inst);
1167 break;
1168 }
1169 break;
1170
1171 case STATE_LIGHT_PROD:
1172 state_tokens[0] = STATE_LIGHTPROD;
1173 state_tokens[1] = parse_integer (inst, Program);
1174
1175 /* Check the value of state_tokens[1] against the # of lights */
1176 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001177 program_error(ctx, Program->Position, "Invalid Light Number");
1178 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001179 return 1;
1180 }
1181
1182 state_tokens[2] = parse_face_type (inst);
1183 switch (*(*inst)++) {
1184 case LIGHT_PROD_AMBIENT:
1185 state_tokens[3] = STATE_AMBIENT;
1186 break;
1187 case LIGHT_PROD_DIFFUSE:
1188 state_tokens[3] = STATE_DIFFUSE;
1189 break;
1190 case LIGHT_PROD_SPECULAR:
1191 state_tokens[3] = STATE_SPECULAR;
1192 break;
1193 }
1194 break;
1195
1196
1197 case STATE_FOG:
1198 switch (*(*inst)++) {
1199 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001200 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001201 break;
1202 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001203 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001204 break;
1205 }
1206 break;
1207
1208 case STATE_TEX_ENV:
1209 state_tokens[1] = parse_integer (inst, Program);
1210 switch (*(*inst)++) {
1211 case TEX_ENV_COLOR:
1212 state_tokens[0] = STATE_TEXENV_COLOR;
1213 break;
1214 }
1215 break;
1216
1217 case STATE_TEX_GEN:
1218 {
1219 GLuint type, coord;
1220
1221 state_tokens[0] = STATE_TEXGEN;
1222 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1223
1224 if (parse_texcoord_num (ctx, inst, Program, &coord))
1225 return 1;
1226 state_tokens[1] = coord;
1227
1228 /* EYE or OBJECT */
1229 type = *(*inst++);
1230
1231 /* 0 - s, 1 - t, 2 - r, 3 - q */
1232 coord = *(*inst++);
1233
1234 if (type == TEX_GEN_EYE) {
1235 switch (coord) {
1236 case COMPONENT_X:
1237 state_tokens[2] = STATE_TEXGEN_EYE_S;
1238 break;
1239 case COMPONENT_Y:
1240 state_tokens[2] = STATE_TEXGEN_EYE_T;
1241 break;
1242 case COMPONENT_Z:
1243 state_tokens[2] = STATE_TEXGEN_EYE_R;
1244 break;
1245 case COMPONENT_W:
1246 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1247 break;
1248 }
1249 }
1250 else {
1251 switch (coord) {
1252 case COMPONENT_X:
1253 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1254 break;
1255 case COMPONENT_Y:
1256 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1257 break;
1258 case COMPONENT_Z:
1259 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1260 break;
1261 case COMPONENT_W:
1262 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1263 break;
1264 }
1265 }
1266 }
1267 break;
1268
1269 case STATE_DEPTH:
1270 switch (*(*inst)++) {
1271 case DEPTH_RANGE:
1272 state_tokens[0] = STATE_DEPTH_RANGE;
1273 break;
1274 }
1275 break;
1276
1277 case STATE_CLIP_PLANE:
1278 state_tokens[0] = STATE_CLIPPLANE;
1279 state_tokens[1] = parse_integer (inst, Program);
Brianaa9d22a2007-02-23 11:21:03 -07001280 if (parse_clipplane_num (ctx, inst, Program,
1281 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001282 return 1;
1283 break;
1284
1285 case STATE_POINT:
1286 switch (*(*inst++)) {
1287 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001288 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001289 break;
1290
1291 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001292 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001293 break;
1294 }
1295 break;
1296
1297 /* XXX: I think this is the correct format for a matrix row */
1298 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001299 if (parse_matrix(ctx, inst, Program,
1300 (GLint *) &state_tokens[0],
1301 (GLint *) &state_tokens[1],
1302 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001303 return 1;
1304
Brian65319522007-02-21 11:08:21 -07001305 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001306
1307 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001308 state_tokens[3] = parse_integer (inst, Program);
1309 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001310 program_error(ctx, Program->Position,
1311 "Second matrix index less than the first");
1312 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001313 return 1;
1314 }
1315 }
1316 else {
Brian65319522007-02-21 11:08:21 -07001317 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001318 (*inst)++;
1319 }
1320 break;
1321 }
1322
1323 return 0;
1324}
1325
1326/**
1327 * This parses a state string (rather, the binary version of it) into
1328 * a 6-token similar for the state fetching code in program.c
1329 *
1330 * One might ask, why fetch these parameters into just like you fetch
1331 * state when they are already stored in other places?
1332 *
1333 * Because of array offsets -> We can stick env/local parameters in the
1334 * middle of a parameter array and then index someplace into the array
1335 * when we execute.
1336 *
1337 * One optimization might be to only do this for the cases where the
1338 * env/local parameters end up inside of an array, and leave the
1339 * single parameters (or arrays of pure env/local pareameters) in their
1340 * respective register files.
1341 *
1342 * For ENV parameters, the format is:
1343 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1344 * state_tokens[1] = STATE_ENV
1345 * state_tokens[2] = the parameter index
1346 *
1347 * for LOCAL parameters, the format is:
1348 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1349 * state_tokens[1] = STATE_LOCAL
1350 * state_tokens[2] = the parameter index
1351 *
1352 * \param inst - the start in the binary arry to start working from
1353 * \param state_tokens - the storage for the 6-token state description
1354 * \return - 0 on sucess, 1 on failure
1355 */
1356static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001357parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001358 struct arb_program *Program,
1359 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001360{
1361 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1362 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1363 else
1364 state_tokens[0] = STATE_VERTEX_PROGRAM;
1365
1366
1367 switch (*(*inst)++) {
1368 case PROGRAM_PARAM_ENV:
1369 state_tokens[1] = STATE_ENV;
1370 state_tokens[2] = parse_integer (inst, Program);
1371
1372 /* Check state_tokens[2] against the number of ENV parameters available */
1373 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001374 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001375 ||
1376 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001377 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001378 program_error(ctx, Program->Position,
1379 "Invalid Program Env Parameter");
1380 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001381 return 1;
1382 }
1383
1384 break;
1385
1386 case PROGRAM_PARAM_LOCAL:
1387 state_tokens[1] = STATE_LOCAL;
1388 state_tokens[2] = parse_integer (inst, Program);
1389
1390 /* Check state_tokens[2] against the number of LOCAL parameters available */
1391 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001392 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001393 ||
1394 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001395 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001396 program_error(ctx, Program->Position,
1397 "Invalid Program Local Parameter");
1398 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001399 return 1;
1400 }
1401 break;
1402 }
1403
1404 return 0;
1405}
1406
1407/**
1408 * For ARB_vertex_program, programs are not allowed to use both an explicit
1409 * vertex attribute and a generic vertex attribute corresponding to the same
1410 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1411 *
1412 * This will walk our var_cache and make sure that nobody does anything fishy.
1413 *
1414 * \return 0 on sucess, 1 on error
1415 */
1416static GLuint
1417generic_attrib_check(struct var_cache *vc_head)
1418{
1419 int a;
1420 struct var_cache *curr;
1421 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1422 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1423
1424 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1425 explicitAttrib[a] = GL_FALSE;
1426 genericAttrib[a] = GL_FALSE;
1427 }
1428
1429 curr = vc_head;
1430 while (curr) {
1431 if (curr->type == vt_attrib) {
1432 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001433 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001434 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001435 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001436 }
1437
1438 curr = curr->next;
1439 }
1440
1441 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1442 if ((explicitAttrib[a]) && (genericAttrib[a]))
1443 return 1;
1444 }
1445
1446 return 0;
1447}
1448
1449/**
1450 * This will handle the binding side of an ATTRIB var declaration
1451 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001452 * \param inputReg returns the input register index, one of the
1453 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001454 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001455 */
1456static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001457parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001458 struct arb_program *Program,
1459 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001460{
Jouk Jansen40322e12004-04-05 08:50:36 +00001461 GLint err = 0;
1462
1463 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001464
Jouk Jansen40322e12004-04-05 08:50:36 +00001465 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1466 switch (*(*inst)++) {
1467 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001468 {
1469 GLint coord;
1470 err = parse_color_type (ctx, inst, Program, &coord);
1471 *inputReg = FRAG_ATTRIB_COL0 + coord;
1472 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001473 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001475 {
Brian8fa6f732007-02-01 09:24:41 -07001476 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001477 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1478 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1479 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001480 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001481 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001482 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001483 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001484 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001485 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001486 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001487 default:
1488 err = 1;
1489 break;
1490 }
1491 }
1492 else {
1493 switch (*(*inst)++) {
1494 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001495 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001496 break;
1497
1498 case VERTEX_ATTRIB_WEIGHT:
1499 {
1500 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001501 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001502 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001503#if 1
1504 /* hack for Warcraft (see bug 8060) */
1505 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001506 break;
Brian Paul3a557502006-09-05 23:15:29 +00001507#else
Brian Paula088f162006-09-05 23:08:51 +00001508 program_error(ctx, Program->Position,
1509 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001510 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001511#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001512 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001513
1514 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001515 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001516 break;
1517
1518 case VERTEX_ATTRIB_COLOR:
1519 {
1520 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001521 err = parse_color_type (ctx, inst, Program, &color);
1522 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001523 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001524 }
1525 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001526 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001527 }
1528 }
1529 break;
1530
1531 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001532 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001533 break;
1534
1535 case VERTEX_ATTRIB_TEXCOORD:
1536 {
Brian8fa6f732007-02-01 09:24:41 -07001537 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001538 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001539 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001540 }
1541 break;
1542
Jouk Jansen40322e12004-04-05 08:50:36 +00001543 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001544 /* Not supported at this time */
1545 {
1546 const char *msg = "ARB_palette_matrix not supported";
1547 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001548 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001549 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001550 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001551
1552 case VERTEX_ATTRIB_GENERIC:
1553 {
1554 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001555 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001556 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001557 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001558 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1559 * attributes do not alias the conventional vertex
1560 * attributes.
1561 */
1562 if (attrib > 0)
1563 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001564 else
1565 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001566 }
1567 }
1568 break;
1569
1570 default:
1571 err = 1;
1572 break;
1573 }
1574 }
1575
Jouk Jansen40322e12004-04-05 08:50:36 +00001576 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001577 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001578 }
Jerome Glisse8d94dab2008-02-27 07:42:48 -07001579 else {
1580 Program->Base.InputsRead |= (1 << *inputReg);
1581 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001582
1583 return err;
1584}
1585
Brian Paul7aebaf32005-10-30 21:23:23 +00001586
Jouk Jansen40322e12004-04-05 08:50:36 +00001587/**
1588 * This translates between a binary token for an output variable type
1589 * and the mesa token for the same thing.
1590 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001591 * \param inst The parsed tokens
1592 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001593 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001594 */
1595static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001596parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001597 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001598{
Brian Paul7aebaf32005-10-30 21:23:23 +00001599 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001600
Brian Paul7aebaf32005-10-30 21:23:23 +00001601 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001602 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001603 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001604 GLuint out_color;
1605
Brian Paulbe76b7f2004-10-04 14:40:05 +00001606 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001607 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001608 */
1609 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001610 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001611 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001612 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001613 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001614 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1615 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001616 }
1617 break;
1618
1619 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001620 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001621 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001622 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001623 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001625 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 GLint color_type;
1627 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001628 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1629 if (err)
1630 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001631
Jouk Jansen40322e12004-04-05 08:50:36 +00001632 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001633 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001634 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001635 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001636 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001637 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001638 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001639 }
1640 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001642 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001643 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001644 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001645 }
1646 /* primary color */
1647 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001648 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001649 }
1650 }
1651 }
1652 break;
1653
1654 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001655 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001656 break;
1657
1658 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001659 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001660 break;
1661
1662 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001663 {
1664 GLuint unit;
1665 if (parse_texcoord_num (ctx, inst, Program, &unit))
1666 return 1;
1667 *outputReg = VERT_RESULT_TEX0 + unit;
1668 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001669 break;
1670 }
1671
Brian Paulde997602005-11-12 17:53:14 +00001672 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001673
1674 return 0;
1675}
1676
Brian Paul7aebaf32005-10-30 21:23:23 +00001677
Jouk Jansen40322e12004-04-05 08:50:36 +00001678/**
1679 * This handles the declaration of ATTRIB variables
1680 *
1681 * XXX: Still needs
1682 * parse_vert_attrib_binding(), or something like that
1683 *
1684 * \return 0 on sucess, 1 on error
1685 */
1686static GLint
Brian Paula088f162006-09-05 23:08:51 +00001687parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001688 struct arb_program *Program)
1689{
1690 GLuint found;
1691 char *error_msg;
1692 struct var_cache *attrib_var;
1693
1694 attrib_var = parse_string (inst, vc_head, Program, &found);
1695 Program->Position = parse_position (inst);
1696 if (found) {
1697 error_msg = (char *)
1698 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07001699 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00001700 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001701 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001702 _mesa_free (error_msg);
1703 return 1;
1704 }
1705
1706 attrib_var->type = vt_attrib;
1707
Brian Paul7aebaf32005-10-30 21:23:23 +00001708 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001709 &attrib_var->attrib_is_generic))
1710 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001711
Brian Paul7aebaf32005-10-30 21:23:23 +00001712 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001713 program_error(ctx, Program->Position,
1714 "Cannot use both a generic vertex attribute "
1715 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001716 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001717 }
1718
1719 Program->Base.NumAttributes++;
1720 return 0;
1721}
1722
1723/**
1724 * \param use -- TRUE if we're called when declaring implicit parameters,
1725 * FALSE if we're declaraing variables. This has to do with
1726 * if we get a signed or unsigned float for scalar constants
1727 */
1728static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001729parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001730 struct var_cache *param_var,
1731 struct arb_program *Program, GLboolean use)
1732{
1733 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001734 GLuint err = 0;
Brianaa9d22a2007-02-23 11:21:03 -07001735 gl_state_index state_tokens[STATE_LENGTH];
Jouk Jansen40322e12004-04-05 08:50:36 +00001736 GLfloat const_values[4];
1737
Jouk Jansen40322e12004-04-05 08:50:36 +00001738 switch (*(*inst)++) {
1739 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001740 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1741 return 1;
1742
1743 /* If we adding STATE_MATRIX that has multiple rows, we need to
1744 * unroll it and call _mesa_add_state_reference() for each row
1745 */
Brian65319522007-02-21 11:08:21 -07001746 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1747 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1748 state_tokens[0] == STATE_MVP_MATRIX ||
1749 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1750 state_tokens[0] == STATE_PROGRAM_MATRIX)
1751 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001752 GLint row;
Brian65319522007-02-21 11:08:21 -07001753 const GLint first_row = state_tokens[2];
1754 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001755
1756 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001757 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001758
Brian Paulde997602005-11-12 17:53:14 +00001759 idx = _mesa_add_state_reference(Program->Base.Parameters,
1760 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001761 if (param_var->param_binding_begin == ~0U)
1762 param_var->param_binding_begin = idx;
1763 param_var->param_binding_length++;
1764 Program->Base.NumParameters++;
1765 }
1766 }
1767 else {
Brian Paulde997602005-11-12 17:53:14 +00001768 idx = _mesa_add_state_reference(Program->Base.Parameters,
1769 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001770 if (param_var->param_binding_begin == ~0U)
1771 param_var->param_binding_begin = idx;
1772 param_var->param_binding_length++;
1773 Program->Base.NumParameters++;
1774 }
1775 break;
1776
1777 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001778 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1779 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001780 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001781 if (param_var->param_binding_begin == ~0U)
1782 param_var->param_binding_begin = idx;
1783 param_var->param_binding_length++;
1784 Program->Base.NumParameters++;
1785
1786 /* Check if there is more: 0 -> we're done, else its an integer */
1787 if (**inst) {
1788 GLuint out_of_range, new_idx;
1789 GLuint start_idx = state_tokens[2] + 1;
1790 GLuint end_idx = parse_integer (inst, Program);
1791
1792 out_of_range = 0;
1793 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1794 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001795 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001796 || ((state_tokens[1] == STATE_LOCAL)
1797 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001798 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001799 out_of_range = 1;
1800 }
1801 else {
1802 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001803 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001804 || ((state_tokens[1] == STATE_LOCAL)
1805 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001806 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001807 out_of_range = 1;
1808 }
1809 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001810 program_error(ctx, Program->Position,
1811 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001812 return 1;
1813 }
1814
1815 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1816 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001817 idx = _mesa_add_state_reference(Program->Base.Parameters,
1818 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001819 param_var->param_binding_length++;
1820 Program->Base.NumParameters++;
1821 }
1822 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001823 else {
1824 (*inst)++;
1825 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001826 break;
1827
1828 case PARAM_CONSTANT:
Brian7cbfe8c2008-01-18 12:45:55 -07001829 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
Jouk Jansen40322e12004-04-05 08:50:36 +00001830 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001831 idx = _mesa_add_named_constant(Program->Base.Parameters,
1832 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001833 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001834 if (param_var->param_binding_begin == ~0U)
1835 param_var->param_binding_begin = idx;
Brian7cbfe8c2008-01-18 12:45:55 -07001836 param_var->param_binding_type = PROGRAM_CONSTANT;
Jouk Jansen40322e12004-04-05 08:50:36 +00001837 param_var->param_binding_length++;
1838 Program->Base.NumParameters++;
1839 break;
1840
1841 default:
Brian Paula088f162006-09-05 23:08:51 +00001842 program_error(ctx, Program->Position,
1843 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001844 return 1;
1845 }
1846
1847 /* Make sure we haven't blown past our parameter limits */
1848 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1849 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001850 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001851 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1852 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001853 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001854 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001855 return 1;
1856 }
1857
1858 return err;
1859}
1860
Brian Paul7aebaf32005-10-30 21:23:23 +00001861
Jouk Jansen40322e12004-04-05 08:50:36 +00001862/**
1863 * This picks out PARAM program parameter bindings.
1864 *
1865 * XXX: This needs to be stressed & tested
1866 *
1867 * \return 0 on sucess, 1 on error
1868 */
1869static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001870parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001871 struct arb_program *Program)
1872{
Brian Paulbd997cd2004-07-20 21:12:56 +00001873 GLuint found, err;
1874 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001875 struct var_cache *param_var;
1876
1877 err = 0;
1878 param_var = parse_string (inst, vc_head, Program, &found);
1879 Program->Position = parse_position (inst);
1880
1881 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001882 char *error_msg = (char *)
1883 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07001884 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00001885 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001886 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001887 _mesa_free (error_msg);
1888 return 1;
1889 }
1890
1891 specified_length = parse_integer (inst, Program);
1892
1893 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001894 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001895 return 1;
1896 }
1897
1898 param_var->type = vt_param;
1899 param_var->param_binding_length = 0;
1900
1901 /* Right now, everything is shoved into the main state register file.
1902 *
1903 * In the future, it would be nice to leave things ENV/LOCAL params
1904 * in their respective register files, if possible
1905 */
1906 param_var->param_binding_type = PROGRAM_STATE_VAR;
1907
1908 /* Remember to:
1909 * * - add each guy to the parameter list
1910 * * - increment the param_var->param_binding_len
1911 * * - store the param_var->param_binding_begin for the first one
1912 * * - compare the actual len to the specified len at the end
1913 */
1914 while (**inst != PARAM_NULL) {
1915 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1916 return 1;
1917 }
1918
1919 /* Test array length here! */
1920 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001921 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001922 program_error(ctx, Program->Position,
1923 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001924 }
1925 }
1926
1927 (*inst)++;
1928
1929 return 0;
1930}
1931
1932/**
1933 *
1934 */
1935static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001936parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001937 struct arb_program *Program, struct var_cache **new_var)
1938{
1939 struct var_cache *param_var;
1940
1941 /* First, insert a dummy entry into the var_cache */
1942 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001943 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001944 param_var->type = vt_param;
1945
1946 param_var->param_binding_length = 0;
1947 /* Don't fill in binding_begin; We use the default value of -1
1948 * to tell if its already initialized, elsewhere.
1949 *
1950 * param_var->param_binding_begin = 0;
1951 */
1952 param_var->param_binding_type = PROGRAM_STATE_VAR;
1953
1954 var_cache_append (vc_head, param_var);
1955
1956 /* Then fill it with juicy parameter goodness */
1957 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1958 return 1;
1959
1960 *new_var = param_var;
1961
1962 return 0;
1963}
1964
1965
1966/**
1967 * This handles the declaration of TEMP variables
1968 *
1969 * \return 0 on sucess, 1 on error
1970 */
1971static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001972parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001973 struct arb_program *Program)
1974{
1975 GLuint found;
1976 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001977
1978 while (**inst != 0) {
1979 temp_var = parse_string (inst, vc_head, Program, &found);
1980 Program->Position = parse_position (inst);
1981 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001982 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001983 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07001984 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00001985 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001986 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001987 _mesa_free (error_msg);
1988 return 1;
1989 }
1990
1991 temp_var->type = vt_temp;
1992
1993 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1994 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001995 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001996 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1997 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001998 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00001999 program_error(ctx, Program->Position,
2000 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002001 return 1;
2002 }
2003
2004 temp_var->temp_binding = Program->Base.NumTemporaries;
2005 Program->Base.NumTemporaries++;
2006 }
2007 (*inst)++;
2008
2009 return 0;
2010}
2011
2012/**
2013 * This handles variables of the OUTPUT variety
2014 *
2015 * \return 0 on sucess, 1 on error
2016 */
2017static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002018parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002019 struct arb_program *Program)
2020{
2021 GLuint found;
2022 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002023 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002024
2025 output_var = parse_string (inst, vc_head, Program, &found);
2026 Program->Position = parse_position (inst);
2027 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002028 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002029 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07002030 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00002031 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002032 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002033 _mesa_free (error_msg);
2034 return 1;
2035 }
2036
2037 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002038
2039 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2040 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002041}
2042
2043/**
2044 * This handles variables of the ALIAS kind
2045 *
2046 * \return 0 on sucess, 1 on error
2047 */
2048static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002049parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002050 struct arb_program *Program)
2051{
2052 GLuint found;
2053 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002054
2055 temp_var = parse_string (inst, vc_head, Program, &found);
2056 Program->Position = parse_position (inst);
2057
2058 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002059 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002060 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07002061 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00002062 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002063 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002064 _mesa_free (error_msg);
2065 return 1;
2066 }
2067
2068 temp_var->type = vt_alias;
2069 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2070 Program->Position = parse_position (inst);
2071
2072 if (!found)
2073 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002074 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002075 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2076 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2077 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002078 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002079 _mesa_free (error_msg);
2080 return 1;
2081 }
2082
2083 return 0;
2084}
2085
2086/**
2087 * This handles variables of the ADDRESS kind
2088 *
2089 * \return 0 on sucess, 1 on error
2090 */
2091static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002092parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002093 struct arb_program *Program)
2094{
2095 GLuint found;
2096 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002097
2098 while (**inst != 0) {
2099 temp_var = parse_string (inst, vc_head, Program, &found);
2100 Program->Position = parse_position (inst);
2101 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002102 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002103 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
Brian06b019d2008-01-18 12:47:20 -07002104 _mesa_sprintf (error_msg, "Duplicate Variable Declaration: %s",
Jouk Jansen40322e12004-04-05 08:50:36 +00002105 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002106 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002107 _mesa_free (error_msg);
2108 return 1;
2109 }
2110
2111 temp_var->type = vt_address;
2112
2113 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002114 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002115 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002116 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002117 return 1;
2118 }
2119
2120 temp_var->address_binding = Program->Base.NumAddressRegs;
2121 Program->Base.NumAddressRegs++;
2122 }
2123 (*inst)++;
2124
2125 return 0;
2126}
2127
2128/**
2129 * Parse a program declaration
2130 *
2131 * \return 0 on sucess, 1 on error
2132 */
2133static GLint
Brian Paula088f162006-09-05 23:08:51 +00002134parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002135 struct arb_program *Program)
2136{
2137 GLint err = 0;
2138
2139 switch (*(*inst)++) {
2140 case ADDRESS:
2141 err = parse_address (ctx, inst, vc_head, Program);
2142 break;
2143
2144 case ALIAS:
2145 err = parse_alias (ctx, inst, vc_head, Program);
2146 break;
2147
2148 case ATTRIB:
2149 err = parse_attrib (ctx, inst, vc_head, Program);
2150 break;
2151
2152 case OUTPUT:
2153 err = parse_output (ctx, inst, vc_head, Program);
2154 break;
2155
2156 case PARAM:
2157 err = parse_param (ctx, inst, vc_head, Program);
2158 break;
2159
2160 case TEMP:
2161 err = parse_temp (ctx, inst, vc_head, Program);
2162 break;
2163 }
2164
2165 return err;
2166}
2167
2168/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002169 * Handle the parsing out of a masked destination register, either for a
2170 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002171 *
2172 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002173 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002174 * position invariant
2175 *
2176 * \param File - The register file we write to
2177 * \param Index - The register index we write to
2178 * \param WriteMask - The mask controlling which components we write (1->write)
2179 *
2180 * \return 0 on sucess, 1 on error
2181 */
2182static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002183parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002184 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002185 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002186{
Brian Paul7aebaf32005-10-30 21:23:23 +00002187 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002188 struct var_cache *dst;
2189
2190 /* We either have a result register specified, or a
2191 * variable that may or may not be writable
2192 */
2193 switch (*(*inst)++) {
2194 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002195 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002196 return 1;
2197 *File = PROGRAM_OUTPUT;
2198 break;
2199
2200 case REGISTER_ESTABLISHED_NAME:
2201 dst = parse_string (inst, vc_head, Program, &result);
2202 Program->Position = parse_position (inst);
2203
2204 /* If the name has never been added to our symbol table, we're hosed */
2205 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002206 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002207 return 1;
2208 }
2209
2210 switch (dst->type) {
2211 case vt_output:
2212 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002213 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002214 break;
2215
2216 case vt_temp:
2217 *File = PROGRAM_TEMPORARY;
2218 *Index = dst->temp_binding;
2219 break;
2220
2221 /* If the var type is not vt_output or vt_temp, no go */
2222 default:
Brian Paula088f162006-09-05 23:08:51 +00002223 program_error(ctx, Program->Position,
2224 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002225 return 1;
2226 }
2227 break;
2228
2229 default:
Brian Paula088f162006-09-05 23:08:51 +00002230 program_error(ctx, Program->Position,
2231 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002232 return 1;
2233 }
2234
2235
2236 /* Position invariance test */
2237 /* This test is done now in syntax portion - when position invariance OPTION
2238 is specified, "result.position" rule is disabled so there is no way
2239 to write the position
2240 */
2241 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2242 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002243 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002244 "Vertex program specified position invariance and wrote vertex position");
2245 }*/
2246
2247 /* And then the mask.
2248 * w,a -> bit 0
2249 * z,b -> bit 1
2250 * y,g -> bit 2
2251 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002252 *
2253 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002254 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002255 tmp = (GLint) *(*inst)++;
2256 *WriteMask = (((tmp>>3) & 0x1) |
2257 ((tmp>>1) & 0x2) |
2258 ((tmp<<1) & 0x4) |
2259 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002260
2261 return 0;
2262}
2263
2264
2265/**
2266 * Handle the parsing of a address register
2267 *
2268 * \param Index - The register index we write to
2269 *
2270 * \return 0 on sucess, 1 on error
2271 */
2272static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002273parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002274 struct var_cache **vc_head,
2275 struct arb_program *Program, GLint * Index)
2276{
2277 struct var_cache *dst;
2278 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002279
2280 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002281
2282 dst = parse_string (inst, vc_head, Program, &result);
2283 Program->Position = parse_position (inst);
2284
2285 /* If the name has never been added to our symbol table, we're hosed */
2286 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002287 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002288 return 1;
2289 }
2290
2291 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002292 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002293 return 1;
2294 }
2295
2296 return 0;
2297}
2298
Brian Paul8e8fa632005-07-01 02:03:33 +00002299#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002300/**
2301 * Handle the parsing out of a masked address register
2302 *
2303 * \param Index - The register index we write to
2304 * \param WriteMask - The mask controlling which components we write (1->write)
2305 *
2306 * \return 0 on sucess, 1 on error
2307 */
2308static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002309parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002310 struct var_cache **vc_head,
2311 struct arb_program *Program, GLint * Index,
2312 GLboolean * WriteMask)
2313{
2314 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2315 return 1;
2316
2317 /* This should be 0x8 */
2318 (*inst)++;
2319
2320 /* Writemask of .x is implied */
2321 WriteMask[0] = 1;
2322 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2323
2324 return 0;
2325}
Brian Paul8e8fa632005-07-01 02:03:33 +00002326#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002327
2328/**
2329 * Parse out a swizzle mask.
2330 *
Brian Paul32df89e2005-10-29 18:26:43 +00002331 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002332 *
2333 * The len parameter allows us to grab 4 components for a vector
2334 * swizzle, or just 1 component for a scalar src register selection
2335 */
Brian Paul32df89e2005-10-29 18:26:43 +00002336static void
Brian Paula088f162006-09-05 23:08:51 +00002337parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002338{
Brian Paul32df89e2005-10-29 18:26:43 +00002339 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002340
Brian Paul32df89e2005-10-29 18:26:43 +00002341 for (i = 0; i < 4; i++)
2342 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002343
Brian Paul32df89e2005-10-29 18:26:43 +00002344 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002345 switch (*(*inst)++) {
2346 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002347 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002348 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002349 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002350 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002351 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002352 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002353 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002354 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002355 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002356 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002357 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002358 default:
2359 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2360 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002361 }
2362 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002363}
2364
Jouk Jansen40322e12004-04-05 08:50:36 +00002365
Brian Paul32df89e2005-10-29 18:26:43 +00002366/**
2367 * Parse an extended swizzle mask which is a sequence of
2368 * four x/y/z/w/0/1 tokens.
2369 * \return swizzle four swizzle values
2370 * \return negateMask four element bitfield
2371 */
2372static void
Brian Paula088f162006-09-05 23:08:51 +00002373parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002374 GLubyte *negateMask)
2375{
2376 GLint i;
2377
2378 *negateMask = 0x0;
2379 for (i = 0; i < 4; i++) {
2380 GLubyte swz;
2381 if (parse_sign(inst) == -1)
2382 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002383
2384 swz = *(*inst)++;
2385
2386 switch (swz) {
2387 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002388 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002389 break;
2390 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002391 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002392 break;
2393 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002394 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002395 break;
2396 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002397 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002398 break;
2399 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002400 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002401 break;
2402 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002403 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002404 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002405 default:
2406 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2407 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002408 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002409 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002410}
2411
2412
2413static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002414parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2415 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002416 struct arb_program *Program,
2417 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002418 GLboolean *IsRelOffset )
2419{
2420 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002421 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002422 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002423
Keith Whitwell7c26b612005-04-21 14:46:57 +00002424 *IsRelOffset = 0;
2425
Jouk Jansen40322e12004-04-05 08:50:36 +00002426 /* And the binding for the src */
2427 switch (*(*inst)++) {
2428 case REGISTER_ATTRIB:
2429 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002430 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002431 return 1;
2432 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002433 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002434
2435 /* We need to insert a dummy variable into the var_cache so we can
2436 * catch generic vertex attrib aliasing errors
2437 */
2438 var_cache_create(&src);
2439 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002440 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002441 src->attrib_binding = binding;
2442 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002443 var_cache_append(vc_head, src);
2444 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002445 program_error(ctx, Program->Position,
2446 "Cannot use both a generic vertex attribute "
2447 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002448 return 1;
2449 }
2450 break;
2451
2452 case REGISTER_PARAM:
2453 switch (**inst) {
2454 case PARAM_ARRAY_ELEMENT:
2455 (*inst)++;
2456 src = parse_string (inst, vc_head, Program, &found);
2457 Program->Position = parse_position (inst);
2458
2459 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002460 program_error(ctx, Program->Position,
2461 "2: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002462 return 1;
2463 }
2464
Brian Paul95801792005-12-06 15:41:43 +00002465 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002466
2467 switch (*(*inst)++) {
2468 case ARRAY_INDEX_ABSOLUTE:
2469 offset = parse_integer (inst, Program);
2470
2471 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002472 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002473 program_error(ctx, Program->Position,
2474 "Index out of range");
2475 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002476 return 1;
2477 }
2478
2479 *Index = src->param_binding_begin + offset;
2480 break;
2481
2482 case ARRAY_INDEX_RELATIVE:
2483 {
2484 GLint addr_reg_idx, rel_off;
2485
2486 /* First, grab the address regiseter */
2487 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2488 return 1;
2489
2490 /* And the .x */
2491 ((*inst)++);
2492 ((*inst)++);
2493 ((*inst)++);
2494 ((*inst)++);
2495
2496 /* Then the relative offset */
2497 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2498
2499 /* And store it properly */
2500 *Index = src->param_binding_begin + rel_off;
2501 *IsRelOffset = 1;
2502 }
2503 break;
2504 }
2505 break;
2506
2507 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002508 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2509 return 1;
2510
Brian Paul95801792005-12-06 15:41:43 +00002511 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002512 *Index = src->param_binding_begin;
2513 break;
2514 }
2515 break;
2516
2517 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002518 src = parse_string (inst, vc_head, Program, &found);
2519 Program->Position = parse_position (inst);
2520
2521 /* If the name has never been added to our symbol table, we're hosed */
2522 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002523 program_error(ctx, Program->Position,
2524 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002525 return 1;
2526 }
2527
2528 switch (src->type) {
2529 case vt_attrib:
2530 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002531 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002532 break;
2533
2534 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2535 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002536 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002537 *Index = src->param_binding_begin;
2538 break;
2539
2540 case vt_temp:
2541 *File = PROGRAM_TEMPORARY;
2542 *Index = src->temp_binding;
2543 break;
2544
2545 /* If the var type is vt_output no go */
2546 default:
Brian Paula088f162006-09-05 23:08:51 +00002547 program_error(ctx, Program->Position,
2548 "destination register is read only");
2549 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002550 return 1;
2551 }
2552 break;
2553
2554 default:
Brian Paula088f162006-09-05 23:08:51 +00002555 program_error(ctx, Program->Position,
2556 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002557 return 1;
2558 }
2559
2560 return 0;
2561}
2562
2563/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002564 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002565 */
2566static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002567parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002568 struct var_cache **vc_head,
2569 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002570 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002571{
Brian Paul7aebaf32005-10-30 21:23:23 +00002572 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002573 GLint index;
2574 GLboolean negate;
2575 GLubyte swizzle[4];
2576 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002577
Jouk Jansen40322e12004-04-05 08:50:36 +00002578 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002579 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002580
2581 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002582 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002583 return 1;
2584
2585 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002586 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002587
Brian Paul32df89e2005-10-29 18:26:43 +00002588 reg->File = file;
2589 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002590 reg->NegateBase = negate;
2591 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002592 return 0;
2593}
2594
Jouk Jansen40322e12004-04-05 08:50:36 +00002595
Brian Paulb7fc1c32006-08-30 23:38:03 +00002596/**
2597 * Parse fragment program destination register.
2598 * \return 1 if error, 0 if no error.
2599 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002600static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002601parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002602 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002603 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002604{
Brian Paul7aebaf32005-10-30 21:23:23 +00002605 GLint mask;
2606 GLuint idx;
2607 enum register_file file;
2608
Keith Whitwell7c26b612005-04-21 14:46:57 +00002609 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002610 return 1;
2611
Keith Whitwell7c26b612005-04-21 14:46:57 +00002612 reg->File = file;
2613 reg->Index = idx;
2614 reg->WriteMask = mask;
2615 return 0;
2616}
2617
2618
Brian Paul7aebaf32005-10-30 21:23:23 +00002619/**
2620 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002621 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002622 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002623static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002624parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002625 struct var_cache **vc_head,
2626 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002627 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002628{
Brian Paul7aebaf32005-10-30 21:23:23 +00002629 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002630 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002631 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002632 GLubyte Swizzle[4];
2633 GLboolean IsRelOffset;
2634
2635 /* Grab the sign */
2636 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2637
2638 /* And the src reg */
2639 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2640 return 1;
2641
2642 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002643 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002644
Keith Whitwell7c26b612005-04-21 14:46:57 +00002645 reg->File = File;
2646 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002647 reg->NegateBase = Negate;
2648 reg->Swizzle = (Swizzle[0] << 0);
2649
Jouk Jansen40322e12004-04-05 08:50:36 +00002650 return 0;
2651}
2652
Keith Whitwell7c26b612005-04-21 14:46:57 +00002653
Jouk Jansen40322e12004-04-05 08:50:36 +00002654/**
2655 * This is a big mother that handles getting opcodes into the instruction
2656 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002657 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002658 */
2659static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002660parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002661 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002662 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002663{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002664 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002665 GLuint texcoord;
2666 GLubyte instClass, type, code;
2667 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002668 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002669
Brian Pauld6272e02006-10-29 18:03:16 +00002670 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002671
2672 /* Record the position in the program string for debugging */
2673 fp->StringPos = Program->Position;
2674
2675 /* OP_ALU_INST or OP_TEX_INST */
2676 instClass = *(*inst)++;
2677
2678 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2679 * OP_TEX_{SAMPLE, KIL}
2680 */
2681 type = *(*inst)++;
2682
2683 /* The actual opcode name */
2684 code = *(*inst)++;
2685
2686 /* Increment the correct count */
2687 switch (instClass) {
2688 case OP_ALU_INST:
2689 Program->NumAluInstructions++;
2690 break;
2691 case OP_TEX_INST:
2692 Program->NumTexInstructions++;
2693 break;
2694 }
2695
Jouk Jansen40322e12004-04-05 08:50:36 +00002696 switch (type) {
2697 case OP_ALU_VECTOR:
2698 switch (code) {
2699 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002700 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002701 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002702 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002703 break;
2704
2705 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002706 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002707 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002708 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002709 break;
2710
2711 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002712 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002713 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002714 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002715 break;
2716
2717 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002718 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002719 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002720 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002721 break;
2722
2723 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002724 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002725 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002726 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002727 break;
2728 }
2729
Keith Whitwell7c26b612005-04-21 14:46:57 +00002730 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002731 return 1;
2732
Keith Whitwell7c26b612005-04-21 14:46:57 +00002733 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002734 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002735 break;
2736
2737 case OP_ALU_SCALAR:
2738 switch (code) {
2739 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002740 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002741 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002742 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002743 break;
2744
2745 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002746 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002748 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002749 break;
2750
2751 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002752 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002754 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002755 break;
2756
2757 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002758 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002759 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002760 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002761 break;
2762
2763 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002764 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002765 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002766 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002767 break;
2768
2769 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002770 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002771 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002772 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002773 break;
2774
2775 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002776 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002777 case OP_SCS:
2778
Brian Paul7e807512005-11-05 17:10:45 +00002779 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002780 break;
2781 }
2782
Keith Whitwell7c26b612005-04-21 14:46:57 +00002783 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002784 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002785
2786 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002788 break;
2789
2790 case OP_ALU_BINSC:
2791 switch (code) {
2792 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002793 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002794 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002795 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002796 break;
2797 }
2798
Keith Whitwell7c26b612005-04-21 14:46:57 +00002799 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002801
Jouk Jansen40322e12004-04-05 08:50:36 +00002802 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002803 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002804 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002805 }
2806 break;
2807
2808
2809 case OP_ALU_BIN:
2810 switch (code) {
2811 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002812 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002813 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002814 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002815 break;
2816
2817 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002818 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002820 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002821 break;
2822
2823 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002824 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002826 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002827 break;
2828
2829 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002830 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002832 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002833 break;
2834
2835 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002836 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002837 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002838 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002839 break;
2840
2841 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002842 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002843 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002844 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 break;
2846
2847 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002848 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002850 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002851 break;
2852
2853 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002854 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002856 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002857 break;
2858
2859 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002860 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002861 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002862 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 break;
2864
2865 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002866 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002867 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002868 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002869 break;
2870
2871 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002872 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002873 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002874 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002875 break;
2876
2877 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002878 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002879 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002880 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002881 break;
2882 }
2883
Keith Whitwell7c26b612005-04-21 14:46:57 +00002884 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 return 1;
2886 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002887 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2888 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 }
2890 break;
2891
2892 case OP_ALU_TRI:
2893 switch (code) {
2894 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002895 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002896 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002897 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002898 break;
2899
2900 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002901 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002902 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002903 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002904 break;
2905
2906 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002907 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002909 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002910 break;
2911 }
2912
Keith Whitwell7c26b612005-04-21 14:46:57 +00002913 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002915
Jouk Jansen40322e12004-04-05 08:50:36 +00002916 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002917 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2918 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002919 }
2920 break;
2921
2922 case OP_ALU_SWZ:
2923 switch (code) {
2924 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002925 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002926 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002927 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002928 break;
2929 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002930 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002931 return 1;
2932
Keith Whitwell7c26b612005-04-21 14:46:57 +00002933 {
Brian Paul32df89e2005-10-29 18:26:43 +00002934 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002935 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002936 enum register_file file;
2937 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002938
Brian Paul32df89e2005-10-29 18:26:43 +00002939 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002940 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002941 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2942 fp->SrcReg[0].File = file;
2943 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002944 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002945 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2946 swizzle[1],
2947 swizzle[2],
2948 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002949 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002950 break;
2951
2952 case OP_TEX_SAMPLE:
2953 switch (code) {
2954 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002955 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002956 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002957 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002958 break;
2959
2960 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002961 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002962 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002963 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002964 break;
2965
2966 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002967 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002968 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002969 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002970 break;
2971 }
2972
Keith Whitwell7c26b612005-04-21 14:46:57 +00002973 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002974 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002975
2976 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002977 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002978
2979 /* texImageUnit */
2980 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2981 return 1;
2982 fp->TexSrcUnit = texcoord;
2983
2984 /* texTarget */
2985 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07002986 case TEXTARGET_SHADOW1D:
2987 shadow_tex = 1 << texcoord;
2988 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00002989 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00002990 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002991 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07002992 case TEXTARGET_SHADOW2D:
2993 shadow_tex = 1 << texcoord;
2994 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00002995 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00002996 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002997 break;
2998 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00002999 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003000 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003001 case TEXTARGET_SHADOWRECT:
3002 shadow_tex = 1 << texcoord;
3003 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003004 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003005 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003006 break;
3007 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003008 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003010 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003011 shadow_tex = 1 << texcoord;
3012 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003013 case TEXTARGET_1D_ARRAY:
3014 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3015 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003016 case TEXTARGET_SHADOW2D_ARRAY:
3017 shadow_tex = 1 << texcoord;
3018 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003019 case TEXTARGET_2D_ARRAY:
3020 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3021 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003022 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003023
3024 /* Don't test the first time a particular sampler is seen. Each time
3025 * after that, make sure the shadow state is the same.
3026 */
3027 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3028 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3029 program_error(ctx, Program->Position,
3030 "texture image unit used for shadow sampling and non-shadow sampling");
3031 return 1;
3032 }
3033
Brian Paulb7fc1c32006-08-30 23:38:03 +00003034 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3035 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3036 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003037 program_error(ctx, Program->Position,
3038 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003039 return 1;
3040 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003041
3042
3043 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003044 break;
3045
3046 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003047 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003048 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003049 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003050 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003051 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003052 default:
3053 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3054 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003055 }
3056
3057 return 0;
3058}
3059
Keith Whitwell7c26b612005-04-21 14:46:57 +00003060static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003061parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003062 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003063 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003064{
Brian Paul7aebaf32005-10-30 21:23:23 +00003065 GLint mask;
3066 GLuint idx;
3067 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003068
3069 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3070 return 1;
3071
3072 reg->File = file;
3073 reg->Index = idx;
3074 reg->WriteMask = mask;
3075 return 0;
3076}
3077
3078/**
3079 * Handle the parsing out of a masked address register
3080 *
3081 * \param Index - The register index we write to
3082 * \param WriteMask - The mask controlling which components we write (1->write)
3083 *
3084 * \return 0 on sucess, 1 on error
3085 */
3086static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003087parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003088 struct var_cache **vc_head,
3089 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003090 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003091{
3092 GLint idx;
3093
3094 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3095 return 1;
3096
3097 /* This should be 0x8 */
3098 (*inst)++;
3099
3100 reg->File = PROGRAM_ADDRESS;
3101 reg->Index = idx;
3102
3103 /* Writemask of .x is implied */
3104 reg->WriteMask = 0x1;
3105 return 0;
3106}
3107
3108/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003109 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003110 */
3111static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003112parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003113 struct var_cache **vc_head,
3114 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003115 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003116{
Brian Paul7aebaf32005-10-30 21:23:23 +00003117 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003118 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003119 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003120 GLubyte swizzle[4];
3121 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003122
3123 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003124 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003125
3126 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003127 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003128 return 1;
3129
3130 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003131 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003132
Brian Paul32df89e2005-10-29 18:26:43 +00003133 reg->File = file;
3134 reg->Index = index;
3135 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3136 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003137 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003138 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003139 return 0;
3140}
3141
3142
3143static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003144parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003145 struct var_cache **vc_head,
3146 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003147 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003148{
Brian Paul7aebaf32005-10-30 21:23:23 +00003149 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003150 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003151 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003152 GLubyte Swizzle[4];
3153 GLboolean IsRelOffset;
3154
3155 /* Grab the sign */
3156 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3157
3158 /* And the src reg */
3159 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3160 return 1;
3161
3162 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003163 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003164
3165 reg->File = File;
3166 reg->Index = Index;
3167 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003168 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003169 reg->RelAddr = IsRelOffset;
3170 return 0;
3171}
3172
3173
Jouk Jansen40322e12004-04-05 08:50:36 +00003174/**
3175 * This is a big mother that handles getting opcodes into the instruction
3176 * and handling the src & dst registers for vertex program instructions
3177 */
3178static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003179parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003180 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003181 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003182{
3183 GLint a;
3184 GLubyte type, code;
3185
3186 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3187 type = *(*inst)++;
3188
3189 /* The actual opcode name */
3190 code = *(*inst)++;
3191
Brian Pauld6272e02006-10-29 18:03:16 +00003192 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003193 /* Record the position in the program string for debugging */
3194 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003195
3196 switch (type) {
3197 /* XXX: */
3198 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003199 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003200
3201 /* Remember to set SrcReg.RelAddr; */
3202
3203 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003204 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003205 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003206
Jouk Jansen40322e12004-04-05 08:50:36 +00003207 vp->DstReg.File = PROGRAM_ADDRESS;
3208
3209 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003210 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003211 return 1;
3212
3213 break;
3214
3215 case OP_ALU_VECTOR:
3216 switch (code) {
3217 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003218 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003219 break;
3220 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003221 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003222 break;
3223 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003224 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003225 break;
3226 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003227 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 break;
3229 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003230 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003231 break;
3232 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003233
3234 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003236
3237 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003238 return 1;
3239 break;
3240
3241 case OP_ALU_SCALAR:
3242 switch (code) {
3243 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003244 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 break;
3246 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003247 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 break;
3249 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003250 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 break;
3252 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003253 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 break;
3255 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003256 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003257 break;
3258 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003259 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003260 break;
3261 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003262 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003263 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003264
3265 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003266 return 1;
3267 break;
3268
3269 case OP_ALU_BINSC:
3270 switch (code) {
3271 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003272 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003273 break;
3274 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003275 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003277
Jouk Jansen40322e12004-04-05 08:50:36 +00003278 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003279 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003280 return 1;
3281 }
3282 break;
3283
3284 case OP_ALU_BIN:
3285 switch (code) {
3286 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003287 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 break;
3289 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003290 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003291 break;
3292 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003293 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003294 break;
3295 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003296 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 break;
3298 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003299 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 break;
3301 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003302 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003303 break;
3304 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003305 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003306 break;
3307 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003308 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 break;
3310 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003311 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003312 break;
3313 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003314 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003315 break;
3316 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003317 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003318 break;
3319 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003320 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003321 break;
3322 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003323 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003324 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003325
Jouk Jansen40322e12004-04-05 08:50:36 +00003326 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003327 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003328 return 1;
3329 }
3330 break;
3331
3332 case OP_ALU_TRI:
3333 switch (code) {
3334 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003335 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003336 break;
3337 }
3338
Keith Whitwell7c26b612005-04-21 14:46:57 +00003339 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003340 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003341
Jouk Jansen40322e12004-04-05 08:50:36 +00003342 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003343 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003344 return 1;
3345 }
3346 break;
3347
3348 case OP_ALU_SWZ:
3349 switch (code) {
3350 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003351 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003352 break;
3353 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003354 {
Brian Paul32df89e2005-10-29 18:26:43 +00003355 GLubyte swizzle[4];
3356 GLubyte negateMask;
3357 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003358 enum register_file file;
3359 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003360
Keith Whitwell7c26b612005-04-21 14:46:57 +00003361 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3362 return 1;
3363
Brian Paul32df89e2005-10-29 18:26:43 +00003364 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003365 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003366 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3367 vp->SrcReg[0].File = file;
3368 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003369 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003370 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3371 swizzle[1],
3372 swizzle[2],
3373 swizzle[3]);
3374 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003375 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003376 break;
3377 }
3378 return 0;
3379}
3380
3381#if DEBUG_PARSING
3382
3383static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003384debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3385 struct arb_program *Program)
3386{
3387 struct var_cache *vc;
3388 GLint a, b;
3389
Brianb618ac82007-02-22 09:39:25 -07003390 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003391
3392 /* First of all, print out the contents of the var_cache */
3393 vc = vc_head;
3394 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003395 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003396 switch (vc->type) {
3397 case vt_none:
3398 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3399 break;
3400 case vt_attrib:
3401 fprintf (stderr, "ATTRIB %s\n", vc->name);
3402 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3403 break;
3404 case vt_param:
3405 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3406 vc->param_binding_begin, vc->param_binding_length);
3407 b = vc->param_binding_begin;
3408 for (a = 0; a < vc->param_binding_length; a++) {
3409 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003410 Program->Base.Parameters->Parameters[a + b].Name);
3411 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3412 const char *s;
3413 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3414 [a + b].StateIndexes);
3415 fprintf(stderr, "%s\n", s);
3416 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003417 }
3418 else
3419 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003420 Program->Base.Parameters->ParameterValues[a + b][0],
3421 Program->Base.Parameters->ParameterValues[a + b][1],
3422 Program->Base.Parameters->ParameterValues[a + b][2],
3423 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003424 }
3425 break;
3426 case vt_temp:
3427 fprintf (stderr, "TEMP %s\n", vc->name);
3428 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3429 break;
3430 case vt_output:
3431 fprintf (stderr, "OUTPUT %s\n", vc->name);
3432 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3433 break;
3434 case vt_alias:
3435 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003436 fprintf (stderr, " binding: 0x%p (%s)\n",
3437 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003438 break;
Brianb618ac82007-02-22 09:39:25 -07003439 default:
3440 /* nothing */
3441 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003442 }
3443 vc = vc->next;
3444 }
3445}
3446
Brian Paul72030e02005-11-03 03:30:34 +00003447#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003448
3449
3450/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003451 * The main loop for parsing a fragment or vertex program
3452 *
Brian Paul72030e02005-11-03 03:30:34 +00003453 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003454 */
Brian Paul72030e02005-11-03 03:30:34 +00003455static GLint
Brian Paula088f162006-09-05 23:08:51 +00003456parse_instructions(GLcontext * ctx, const GLubyte * inst,
3457 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003458{
Brian Paul72030e02005-11-03 03:30:34 +00003459 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3460 ? ctx->Const.FragmentProgram.MaxInstructions
3461 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003462 GLint err = 0;
3463
Brian Paul72030e02005-11-03 03:30:34 +00003464 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3465
Jouk Jansen40322e12004-04-05 08:50:36 +00003466 Program->MajorVersion = (GLuint) * inst++;
3467 Program->MinorVersion = (GLuint) * inst++;
3468
3469 while (*inst != END) {
3470 switch (*inst++) {
3471
3472 case OPTION:
3473 switch (*inst++) {
3474 case ARB_PRECISION_HINT_FASTEST:
3475 Program->PrecisionOption = GL_FASTEST;
3476 break;
3477
3478 case ARB_PRECISION_HINT_NICEST:
3479 Program->PrecisionOption = GL_NICEST;
3480 break;
3481
3482 case ARB_FOG_EXP:
3483 Program->FogOption = GL_EXP;
3484 break;
3485
3486 case ARB_FOG_EXP2:
3487 Program->FogOption = GL_EXP2;
3488 break;
3489
3490 case ARB_FOG_LINEAR:
3491 Program->FogOption = GL_LINEAR;
3492 break;
3493
3494 case ARB_POSITION_INVARIANT:
3495 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003496 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003497 break;
3498
3499 case ARB_FRAGMENT_PROGRAM_SHADOW:
3500 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3501 /* TODO ARB_fragment_program_shadow code */
3502 }
3503 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003504
3505 case ARB_DRAW_BUFFERS:
3506 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3507 /* do nothing for now */
3508 }
3509 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003510
3511 case MESA_TEXTURE_ARRAY:
3512 /* do nothing for now */
3513 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003514 }
3515 break;
3516
3517 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003518 /* check length */
3519 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003520 program_error(ctx, Program->Position,
3521 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003522 return 1;
3523 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003524 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003525 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003526 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003527 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003528 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003529 }
3530 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003531 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003532 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003533 }
3534
Brian Paul72030e02005-11-03 03:30:34 +00003535 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003536 Program->Base.NumInstructions++;
3537 break;
3538
3539 case DECLARATION:
3540 err = parse_declaration (ctx, &inst, vc_head, Program);
3541 break;
3542
3543 default:
3544 break;
3545 }
3546
3547 if (err)
3548 break;
3549 }
3550
3551 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003552 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003553 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003554 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003555 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003556 /* YYY Wrong Position in program, whatever, at least not random -> crash
3557 Program->Position = parse_position (&inst);
3558 */
Brian Paul8c41a142005-11-19 15:36:28 +00003559 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003560 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003561 Program->Base.NumInstructions++;
3562
Brian Paul05051032005-11-01 04:36:33 +00003563 /*
3564 * Initialize native counts to logical counts. The device driver may
3565 * change them if program is translated into a hardware program.
3566 */
3567 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3568 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3569 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3570 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3571 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003572
Jouk Jansen40322e12004-04-05 08:50:36 +00003573 return err;
3574}
3575
Brian Paul7aebaf32005-10-30 21:23:23 +00003576
Jouk Jansen40322e12004-04-05 08:50:36 +00003577/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003578LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003579#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003580;
3581
Brian Paul7aebaf32005-10-30 21:23:23 +00003582
Brian Paul8c41a142005-11-19 15:36:28 +00003583/**
3584 * Set a grammar parameter.
3585 * \param name the grammar parameter
3586 * \param value the new parameter value
3587 * \return 0 if OK, 1 if error
3588 */
3589static int
3590set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003591{
3592 char error_msg[300];
3593 GLint error_pos;
3594
Brian Paul8c41a142005-11-19 15:36:28 +00003595 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003596 return 0;
3597
3598 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3599 _mesa_set_program_error (ctx, error_pos, error_msg);
3600 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3601 return 1;
3602}
3603
Brian Paul8c41a142005-11-19 15:36:28 +00003604
3605/**
3606 * Enable support for the given language option in the parser.
3607 * \return 1 if OK, 0 if error
3608 */
3609static int
3610enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003611{
Brian Paul8c41a142005-11-19 15:36:28 +00003612 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003613}
3614
Brian Paul8c41a142005-11-19 15:36:28 +00003615
3616/**
3617 * Enable parser extensions based on which OpenGL extensions are supported
3618 * by this rendering context.
3619 *
3620 * \return GL_TRUE if OK, GL_FALSE if error.
3621 */
3622static GLboolean
3623enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003624{
Brian Paul8c41a142005-11-19 15:36:28 +00003625#if 0
3626 /* These are not supported at this time */
3627 if ((ctx->Extensions.ARB_vertex_blend ||
3628 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003629 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003630 return GL_FALSE;
3631 if (ctx->Extensions.ARB_matrix_palette
3632 && !enable_ext(ctx, id, "matrix_palette"))
3633 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003634#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003635 if (ctx->Extensions.ARB_fragment_program_shadow
3636 && !enable_ext(ctx, id, "fragment_program_shadow"))
3637 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003638 if (ctx->Extensions.EXT_point_parameters
3639 && !enable_ext(ctx, id, "point_parameters"))
3640 return GL_FALSE;
3641 if (ctx->Extensions.EXT_secondary_color
3642 && !enable_ext(ctx, id, "secondary_color"))
3643 return GL_FALSE;
3644 if (ctx->Extensions.EXT_fog_coord
3645 && !enable_ext(ctx, id, "fog_coord"))
3646 return GL_FALSE;
3647 if (ctx->Extensions.NV_texture_rectangle
3648 && !enable_ext(ctx, id, "texture_rectangle"))
3649 return GL_FALSE;
3650 if (ctx->Extensions.ARB_draw_buffers
3651 && !enable_ext(ctx, id, "draw_buffers"))
3652 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003653 if (ctx->Extensions.MESA_texture_array
3654 && !enable_ext(ctx, id, "texture_array"))
3655 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003656#if 1
3657 /* hack for Warcraft (see bug 8060) */
3658 enable_ext(ctx, id, "vertex_blend");
3659#endif
3660
Brian Paul8c41a142005-11-19 15:36:28 +00003661 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003662}
3663
Brian Paul8c41a142005-11-19 15:36:28 +00003664
Jouk Jansen40322e12004-04-05 08:50:36 +00003665/**
3666 * This kicks everything off.
3667 *
3668 * \param ctx - The GL Context
3669 * \param str - The program string
3670 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003671 * \param program - The arb_program struct to return all the parsed info in
3672 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003673 */
Brian Paul8c41a142005-11-19 15:36:28 +00003674static GLboolean
3675_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3676 const GLubyte *str, GLsizei len,
3677 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003678{
3679 GLint a, err, error_pos;
3680 char error_msg[300];
3681 GLuint parsed_len;
3682 struct var_cache *vc_head;
3683 grammar arbprogram_syn_id;
3684 GLubyte *parsed, *inst;
3685 GLubyte *strz = NULL;
3686 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3687
Brian Paul8c41a142005-11-19 15:36:28 +00003688 /* set the program target before parsing */
3689 program->Base.Target = target;
3690
Brian Paul7f76b8f2004-09-10 01:05:39 +00003691 /* Reset error state */
3692 _mesa_set_program_error(ctx, -1, NULL);
3693
Brian Paul8c41a142005-11-19 15:36:28 +00003694 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003695 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003696 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003697 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003698 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003699
3700 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3701 if (grammar_syn_id == 0) {
3702 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003703 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003704 _mesa_set_program_error (ctx, error_pos, error_msg);
3705 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003706 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003707 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003708 }
3709
Brian Paul8c41a142005-11-19 15:36:28 +00003710 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3711 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003712
Brian Paul49a80ca2006-04-28 15:40:11 +00003713 /* 'parsed' is unused here */
3714 _mesa_free (parsed);
3715 parsed = NULL;
3716
Brian Paul45703642005-10-29 15:52:31 +00003717 /* NOTE: we can't destroy grammar_syn_id right here because
3718 * grammar_destroy() can reset the last error
3719 */
Brian Paul8c41a142005-11-19 15:36:28 +00003720 if (err) {
3721 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003722 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3723 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003724 _mesa_error (ctx, GL_INVALID_OPERATION,
3725 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003726 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003727 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003728 }
3729
3730 grammar_destroy (grammar_syn_id);
3731
3732 arbprogram_syn_is_ok = 1;
3733 }
3734
3735 /* create the grammar object */
3736 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3737 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003738 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003739 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3740 _mesa_set_program_error (ctx, error_pos, error_msg);
3741 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003742 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003743 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003744 }
3745
3746 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003747 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003748 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3749 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003750 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003751 }
3752
Brian Paul8c41a142005-11-19 15:36:28 +00003753 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3754 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003755 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003756 }
3757
3758 /* check for NULL character occurences */
3759 {
Brian Paul8c41a142005-11-19 15:36:28 +00003760 GLint i;
3761 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003762 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003763 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003764 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003765 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003766 }
Brian Paul8c41a142005-11-19 15:36:28 +00003767 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003768 }
3769
3770 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003771 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003772 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003773 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3774 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003775 return GL_FALSE;
3776 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003777 _mesa_memcpy (strz, str, len);
3778 strz[len] = '\0';
3779
Michal Krolb80bc052004-10-21 14:09:54 +00003780 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003781 err = !grammar_fast_check(arbprogram_syn_id, strz,
3782 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003783
3784 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003785 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003786 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3787 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003788
Brian Paulb3aefd12005-09-19 20:12:32 +00003789#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003790 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003791 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003792 int line, col;
3793 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003794 fprintf(stderr, "program: %s\n", (char *) strz);
3795 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003796 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3797 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003798 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3799 } while (0)
3800#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003801
Brian Paula088f162006-09-05 23:08:51 +00003802 _mesa_free(strz);
3803 _mesa_free(parsed);
3804
Jouk Jansen40322e12004-04-05 08:50:36 +00003805 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003806 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003807 }
3808
Jouk Jansen40322e12004-04-05 08:50:36 +00003809 grammar_destroy (arbprogram_syn_id);
3810
Brian Paul8c41a142005-11-19 15:36:28 +00003811 /*
3812 * Program string is syntactically correct at this point
3813 * Parse the tokenized version of the program now, generating
3814 * vertex/fragment program instructions.
3815 */
3816
Jouk Jansen40322e12004-04-05 08:50:36 +00003817 /* Initialize the arb_program struct */
3818 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003819 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003820 program->Base.NumInstructions =
3821 program->Base.NumTemporaries =
3822 program->Base.NumParameters =
3823 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003824 program->Base.Parameters = _mesa_new_parameter_list ();
3825 program->Base.InputsRead = 0x0;
3826 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003827 program->Position = 0;
3828 program->MajorVersion = program->MinorVersion = 0;
3829 program->PrecisionOption = GL_DONT_CARE;
3830 program->FogOption = GL_NONE;
3831 program->HintPositionInvariant = GL_FALSE;
3832 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003833 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003834 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003835 program->NumAluInstructions =
3836 program->NumTexInstructions =
3837 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003838 program->UsesKill = 0;
3839
Jouk Jansen40322e12004-04-05 08:50:36 +00003840 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003841 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003842
3843 /* Start examining the tokens in the array */
3844 inst = parsed;
3845
3846 /* Check the grammer rev */
3847 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003848 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003849 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003850 }
3851 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003852 /* ignore program target */
3853 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003854 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003855 }
3856
3857 /*debug_variables(ctx, vc_head, program); */
3858
3859 /* We're done with the parsed binary array */
3860 var_cache_destroy (&vc_head);
3861
3862 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003863
Brian Paul8c41a142005-11-19 15:36:28 +00003864 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3865 * to size [ap.Base.NumInstructions].
3866 */
Brian Paula7543902006-08-24 21:58:32 +00003867 program->Base.Instructions
3868 = _mesa_realloc_instructions(program->Base.Instructions,
3869 MAX_INSTRUCTIONS,
3870 program->Base.NumInstructions);
3871
Brian Paul45703642005-10-29 15:52:31 +00003872 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003873}
Brian Paul8c41a142005-11-19 15:36:28 +00003874
3875
3876
3877void
3878_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3879 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003880 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003881{
3882 struct arb_program ap;
3883 GLuint i;
3884
3885 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003886 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003887 /* Error in the program. Just return. */
3888 return;
3889 }
3890
3891 /* Copy the relevant contents of the arb_program struct into the
3892 * fragment_program struct.
3893 */
3894 program->Base.String = ap.Base.String;
3895 program->Base.NumInstructions = ap.Base.NumInstructions;
3896 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3897 program->Base.NumParameters = ap.Base.NumParameters;
3898 program->Base.NumAttributes = ap.Base.NumAttributes;
3899 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003900 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3901 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3902 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3903 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3904 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003905 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3906 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3907 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3908 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3909 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3910 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003911 program->Base.InputsRead = ap.Base.InputsRead;
3912 program->Base.OutputsWritten = ap.Base.OutputsWritten;
Briand1284d32008-03-12 15:33:41 -06003913 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
Brianc9db2232007-01-04 17:22:19 -07003914 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Briand1284d32008-03-12 15:33:41 -06003915 if (ap.TexturesUsed[i])
3916 program->Base.SamplersUsed |= (1 << i);
3917 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003918 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003919 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003920 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003921
3922 if (program->Base.Instructions)
3923 _mesa_free(program->Base.Instructions);
3924 program->Base.Instructions = ap.Base.Instructions;
3925
3926 if (program->Base.Parameters)
3927 _mesa_free_parameter_list(program->Base.Parameters);
3928 program->Base.Parameters = ap.Base.Parameters;
3929
3930#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00003931 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
3932 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003933#endif
3934}
3935
3936
3937
3938/**
3939 * Parse the vertex program string. If success, update the given
3940 * vertex_program object with the new program. Else, leave the vertex_program
3941 * object unchanged.
3942 */
3943void
3944_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3945 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003946 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003947{
3948 struct arb_program ap;
3949
3950 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3951
Brian Paul95801792005-12-06 15:41:43 +00003952 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003953 /* Error in the program. Just return. */
3954 return;
3955 }
3956
3957 /* Copy the relevant contents of the arb_program struct into the
3958 * vertex_program struct.
3959 */
3960 program->Base.String = ap.Base.String;
3961 program->Base.NumInstructions = ap.Base.NumInstructions;
3962 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3963 program->Base.NumParameters = ap.Base.NumParameters;
3964 program->Base.NumAttributes = ap.Base.NumAttributes;
3965 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003966 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3967 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3968 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3969 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3970 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003971 program->Base.InputsRead = ap.Base.InputsRead;
3972 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3973 program->IsPositionInvariant = ap.HintPositionInvariant;
3974
3975 if (program->Base.Instructions)
3976 _mesa_free(program->Base.Instructions);
3977 program->Base.Instructions = ap.Base.Instructions;
3978
3979 if (program->Base.Parameters)
3980 _mesa_free_parameter_list(program->Base.Parameters);
3981 program->Base.Parameters = ap.Base.Parameters;
3982
3983#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01003984 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00003985 _mesa_print_program(&program->Base);
3986#endif
3987}