blob: 737194d3b65e71ef6fa2446d4e7e740865d59597 [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 */
Briand799b7a2007-09-13 11:29:00 -06001229 type = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001230
1231 /* 0 - s, 1 - t, 2 - r, 3 - q */
Briand799b7a2007-09-13 11:29:00 -06001232 coord = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001233
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;
Briand799b7a2007-09-13 11:29:00 -06001248 default:
1249 _mesa_problem(ctx, "bad texgen component in "
1250 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001251 }
1252 }
1253 else {
1254 switch (coord) {
1255 case COMPONENT_X:
1256 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1257 break;
1258 case COMPONENT_Y:
1259 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1260 break;
1261 case COMPONENT_Z:
1262 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1263 break;
1264 case COMPONENT_W:
1265 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1266 break;
Briand799b7a2007-09-13 11:29:00 -06001267 default:
1268 _mesa_problem(ctx, "bad texgen component in "
1269 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001270 }
1271 }
1272 }
1273 break;
1274
1275 case STATE_DEPTH:
1276 switch (*(*inst)++) {
1277 case DEPTH_RANGE:
1278 state_tokens[0] = STATE_DEPTH_RANGE;
1279 break;
1280 }
1281 break;
1282
1283 case STATE_CLIP_PLANE:
1284 state_tokens[0] = STATE_CLIPPLANE;
1285 state_tokens[1] = parse_integer (inst, Program);
Brianaa9d22a2007-02-23 11:21:03 -07001286 if (parse_clipplane_num (ctx, inst, Program,
1287 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001288 return 1;
1289 break;
1290
1291 case STATE_POINT:
Briand799b7a2007-09-13 11:29:00 -06001292 switch (*(*inst)++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001293 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001294 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001295 break;
1296
1297 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001298 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001299 break;
1300 }
1301 break;
1302
1303 /* XXX: I think this is the correct format for a matrix row */
1304 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001305 if (parse_matrix(ctx, inst, Program,
1306 (GLint *) &state_tokens[0],
1307 (GLint *) &state_tokens[1],
1308 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001309 return 1;
1310
Brian65319522007-02-21 11:08:21 -07001311 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001312
1313 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001314 state_tokens[3] = parse_integer (inst, Program);
1315 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001316 program_error(ctx, Program->Position,
1317 "Second matrix index less than the first");
1318 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001319 return 1;
1320 }
1321 }
1322 else {
Brian65319522007-02-21 11:08:21 -07001323 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001324 (*inst)++;
1325 }
1326 break;
1327 }
1328
1329 return 0;
1330}
1331
1332/**
1333 * This parses a state string (rather, the binary version of it) into
1334 * a 6-token similar for the state fetching code in program.c
1335 *
1336 * One might ask, why fetch these parameters into just like you fetch
1337 * state when they are already stored in other places?
1338 *
1339 * Because of array offsets -> We can stick env/local parameters in the
1340 * middle of a parameter array and then index someplace into the array
1341 * when we execute.
1342 *
1343 * One optimization might be to only do this for the cases where the
1344 * env/local parameters end up inside of an array, and leave the
1345 * single parameters (or arrays of pure env/local pareameters) in their
1346 * respective register files.
1347 *
1348 * For ENV parameters, the format is:
1349 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1350 * state_tokens[1] = STATE_ENV
1351 * state_tokens[2] = the parameter index
1352 *
1353 * for LOCAL parameters, the format is:
1354 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1355 * state_tokens[1] = STATE_LOCAL
1356 * state_tokens[2] = the parameter index
1357 *
1358 * \param inst - the start in the binary arry to start working from
1359 * \param state_tokens - the storage for the 6-token state description
1360 * \return - 0 on sucess, 1 on failure
1361 */
1362static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001363parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001364 struct arb_program *Program,
1365 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001366{
1367 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1368 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1369 else
1370 state_tokens[0] = STATE_VERTEX_PROGRAM;
1371
1372
1373 switch (*(*inst)++) {
1374 case PROGRAM_PARAM_ENV:
1375 state_tokens[1] = STATE_ENV;
1376 state_tokens[2] = parse_integer (inst, Program);
1377
1378 /* Check state_tokens[2] against the number of ENV parameters available */
1379 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001380 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001381 ||
1382 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001383 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001384 program_error(ctx, Program->Position,
1385 "Invalid Program Env Parameter");
1386 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001387 return 1;
1388 }
1389
1390 break;
1391
1392 case PROGRAM_PARAM_LOCAL:
1393 state_tokens[1] = STATE_LOCAL;
1394 state_tokens[2] = parse_integer (inst, Program);
1395
1396 /* Check state_tokens[2] against the number of LOCAL parameters available */
1397 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001398 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001399 ||
1400 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001401 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001402 program_error(ctx, Program->Position,
1403 "Invalid Program Local Parameter");
1404 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001405 return 1;
1406 }
1407 break;
1408 }
1409
1410 return 0;
1411}
1412
1413/**
1414 * For ARB_vertex_program, programs are not allowed to use both an explicit
1415 * vertex attribute and a generic vertex attribute corresponding to the same
1416 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1417 *
1418 * This will walk our var_cache and make sure that nobody does anything fishy.
1419 *
1420 * \return 0 on sucess, 1 on error
1421 */
1422static GLuint
1423generic_attrib_check(struct var_cache *vc_head)
1424{
1425 int a;
1426 struct var_cache *curr;
1427 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1428 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1429
1430 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1431 explicitAttrib[a] = GL_FALSE;
1432 genericAttrib[a] = GL_FALSE;
1433 }
1434
1435 curr = vc_head;
1436 while (curr) {
1437 if (curr->type == vt_attrib) {
1438 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001439 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001440 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001441 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001442 }
1443
1444 curr = curr->next;
1445 }
1446
1447 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1448 if ((explicitAttrib[a]) && (genericAttrib[a]))
1449 return 1;
1450 }
1451
1452 return 0;
1453}
1454
1455/**
1456 * This will handle the binding side of an ATTRIB var declaration
1457 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001458 * \param inputReg returns the input register index, one of the
1459 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001460 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001461 */
1462static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001463parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001464 struct arb_program *Program,
1465 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001466{
Jouk Jansen40322e12004-04-05 08:50:36 +00001467 GLint err = 0;
1468
1469 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001470
Jouk Jansen40322e12004-04-05 08:50:36 +00001471 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1472 switch (*(*inst)++) {
1473 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001474 {
1475 GLint coord;
1476 err = parse_color_type (ctx, inst, Program, &coord);
1477 *inputReg = FRAG_ATTRIB_COL0 + coord;
1478 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001479 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001480 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001481 {
Brian8fa6f732007-02-01 09:24:41 -07001482 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001483 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1484 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1485 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001486 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001487 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001488 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001489 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001490 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001491 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001492 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001493 default:
1494 err = 1;
1495 break;
1496 }
1497 }
1498 else {
1499 switch (*(*inst)++) {
1500 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001501 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001502 break;
1503
1504 case VERTEX_ATTRIB_WEIGHT:
1505 {
1506 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001507 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001508 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001509#if 1
1510 /* hack for Warcraft (see bug 8060) */
1511 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001512 break;
Brian Paul3a557502006-09-05 23:15:29 +00001513#else
Brian Paula088f162006-09-05 23:08:51 +00001514 program_error(ctx, Program->Position,
1515 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001516 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001517#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001518 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001519
1520 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001521 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001522 break;
1523
1524 case VERTEX_ATTRIB_COLOR:
1525 {
1526 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001527 err = parse_color_type (ctx, inst, Program, &color);
1528 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001529 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001530 }
1531 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001532 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001533 }
1534 }
1535 break;
1536
1537 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001538 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001539 break;
1540
1541 case VERTEX_ATTRIB_TEXCOORD:
1542 {
Brian8fa6f732007-02-01 09:24:41 -07001543 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001544 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001545 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001546 }
1547 break;
1548
Jouk Jansen40322e12004-04-05 08:50:36 +00001549 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001550 /* Not supported at this time */
1551 {
1552 const char *msg = "ARB_palette_matrix not supported";
1553 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001554 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001555 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001556 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001557
1558 case VERTEX_ATTRIB_GENERIC:
1559 {
1560 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001561 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001562 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001563 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001564 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1565 * attributes do not alias the conventional vertex
1566 * attributes.
1567 */
1568 if (attrib > 0)
1569 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001570 else
1571 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001572 }
1573 }
1574 break;
1575
1576 default:
1577 err = 1;
1578 break;
1579 }
1580 }
1581
Jouk Jansen40322e12004-04-05 08:50:36 +00001582 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001583 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001584 }
1585
Brian Paulde997602005-11-12 17:53:14 +00001586 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001587
1588 return err;
1589}
1590
Brian Paul7aebaf32005-10-30 21:23:23 +00001591
Jouk Jansen40322e12004-04-05 08:50:36 +00001592/**
1593 * This translates between a binary token for an output variable type
1594 * and the mesa token for the same thing.
1595 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001596 * \param inst The parsed tokens
1597 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001598 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001599 */
1600static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001601parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001602 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001603{
Brian Paul7aebaf32005-10-30 21:23:23 +00001604 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001605
Brian Paul7aebaf32005-10-30 21:23:23 +00001606 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001608 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001609 GLuint out_color;
1610
Brian Paulbe76b7f2004-10-04 14:40:05 +00001611 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001612 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001613 */
1614 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001615 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001616 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001618 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001619 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1620 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 }
1622 break;
1623
1624 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001625 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001626 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001627 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001629 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001630 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001631 GLint color_type;
1632 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001633 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1634 if (err)
1635 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001636
Jouk Jansen40322e12004-04-05 08:50:36 +00001637 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001638 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001639 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001640 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001642 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001643 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001644 }
1645 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001646 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001647 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001648 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001649 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001650 }
1651 /* primary color */
1652 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001653 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001654 }
1655 }
1656 }
1657 break;
1658
1659 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001660 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001661 break;
1662
1663 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001664 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001665 break;
1666
1667 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001668 {
1669 GLuint unit;
1670 if (parse_texcoord_num (ctx, inst, Program, &unit))
1671 return 1;
1672 *outputReg = VERT_RESULT_TEX0 + unit;
1673 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001674 break;
1675 }
1676
Brian Paulde997602005-11-12 17:53:14 +00001677 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001678
1679 return 0;
1680}
1681
Brian Paul7aebaf32005-10-30 21:23:23 +00001682
Jouk Jansen40322e12004-04-05 08:50:36 +00001683/**
1684 * This handles the declaration of ATTRIB variables
1685 *
1686 * XXX: Still needs
1687 * parse_vert_attrib_binding(), or something like that
1688 *
1689 * \return 0 on sucess, 1 on error
1690 */
1691static GLint
Brian Paula088f162006-09-05 23:08:51 +00001692parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001693 struct arb_program *Program)
1694{
1695 GLuint found;
1696 char *error_msg;
1697 struct var_cache *attrib_var;
1698
1699 attrib_var = parse_string (inst, vc_head, Program, &found);
1700 Program->Position = parse_position (inst);
1701 if (found) {
1702 error_msg = (char *)
1703 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1704 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1705 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001706 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001707 _mesa_free (error_msg);
1708 return 1;
1709 }
1710
1711 attrib_var->type = vt_attrib;
1712
Brian Paul7aebaf32005-10-30 21:23:23 +00001713 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001714 &attrib_var->attrib_is_generic))
1715 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001716
Brian Paul7aebaf32005-10-30 21:23:23 +00001717 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001718 program_error(ctx, Program->Position,
1719 "Cannot use both a generic vertex attribute "
1720 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001721 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001722 }
1723
1724 Program->Base.NumAttributes++;
1725 return 0;
1726}
1727
1728/**
1729 * \param use -- TRUE if we're called when declaring implicit parameters,
1730 * FALSE if we're declaraing variables. This has to do with
1731 * if we get a signed or unsigned float for scalar constants
1732 */
1733static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001734parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001735 struct var_cache *param_var,
1736 struct arb_program *Program, GLboolean use)
1737{
1738 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001739 GLuint err = 0;
Brianaa9d22a2007-02-23 11:21:03 -07001740 gl_state_index state_tokens[STATE_LENGTH];
Jouk Jansen40322e12004-04-05 08:50:36 +00001741 GLfloat const_values[4];
1742
Jouk Jansen40322e12004-04-05 08:50:36 +00001743 switch (*(*inst)++) {
1744 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001745 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1746 return 1;
1747
1748 /* If we adding STATE_MATRIX that has multiple rows, we need to
1749 * unroll it and call _mesa_add_state_reference() for each row
1750 */
Brian65319522007-02-21 11:08:21 -07001751 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1752 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1753 state_tokens[0] == STATE_MVP_MATRIX ||
1754 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1755 state_tokens[0] == STATE_PROGRAM_MATRIX)
1756 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001757 GLint row;
Brian65319522007-02-21 11:08:21 -07001758 const GLint first_row = state_tokens[2];
1759 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001760
1761 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001762 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001763
Brian Paulde997602005-11-12 17:53:14 +00001764 idx = _mesa_add_state_reference(Program->Base.Parameters,
1765 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001766 if (param_var->param_binding_begin == ~0U)
1767 param_var->param_binding_begin = idx;
1768 param_var->param_binding_length++;
1769 Program->Base.NumParameters++;
1770 }
1771 }
1772 else {
Brian Paulde997602005-11-12 17:53:14 +00001773 idx = _mesa_add_state_reference(Program->Base.Parameters,
1774 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001775 if (param_var->param_binding_begin == ~0U)
1776 param_var->param_binding_begin = idx;
1777 param_var->param_binding_length++;
1778 Program->Base.NumParameters++;
1779 }
1780 break;
1781
1782 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001783 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1784 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001785 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001786 if (param_var->param_binding_begin == ~0U)
1787 param_var->param_binding_begin = idx;
1788 param_var->param_binding_length++;
1789 Program->Base.NumParameters++;
1790
1791 /* Check if there is more: 0 -> we're done, else its an integer */
1792 if (**inst) {
1793 GLuint out_of_range, new_idx;
1794 GLuint start_idx = state_tokens[2] + 1;
1795 GLuint end_idx = parse_integer (inst, Program);
1796
1797 out_of_range = 0;
1798 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1799 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001800 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001801 || ((state_tokens[1] == STATE_LOCAL)
1802 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001803 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001804 out_of_range = 1;
1805 }
1806 else {
1807 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001808 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001809 || ((state_tokens[1] == STATE_LOCAL)
1810 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001811 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001812 out_of_range = 1;
1813 }
1814 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001815 program_error(ctx, Program->Position,
1816 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001817 return 1;
1818 }
1819
1820 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1821 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001822 idx = _mesa_add_state_reference(Program->Base.Parameters,
1823 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001824 param_var->param_binding_length++;
1825 Program->Base.NumParameters++;
1826 }
1827 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001828 else {
1829 (*inst)++;
1830 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001831 break;
1832
1833 case PARAM_CONSTANT:
1834 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001835 idx = _mesa_add_named_constant(Program->Base.Parameters,
1836 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001837 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001838 if (param_var->param_binding_begin == ~0U)
1839 param_var->param_binding_begin = idx;
1840 param_var->param_binding_length++;
1841 Program->Base.NumParameters++;
1842 break;
1843
1844 default:
Brian Paula088f162006-09-05 23:08:51 +00001845 program_error(ctx, Program->Position,
1846 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001847 return 1;
1848 }
1849
1850 /* Make sure we haven't blown past our parameter limits */
1851 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1852 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001853 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001854 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1855 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001856 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001857 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001858 return 1;
1859 }
1860
1861 return err;
1862}
1863
Brian Paul7aebaf32005-10-30 21:23:23 +00001864
Jouk Jansen40322e12004-04-05 08:50:36 +00001865/**
1866 * This picks out PARAM program parameter bindings.
1867 *
1868 * XXX: This needs to be stressed & tested
1869 *
1870 * \return 0 on sucess, 1 on error
1871 */
1872static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001873parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001874 struct arb_program *Program)
1875{
Brian Paulbd997cd2004-07-20 21:12:56 +00001876 GLuint found, err;
1877 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001878 struct var_cache *param_var;
1879
1880 err = 0;
1881 param_var = parse_string (inst, vc_head, Program, &found);
1882 Program->Position = parse_position (inst);
1883
1884 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001885 char *error_msg = (char *)
1886 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001887 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1888 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001889 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001890 _mesa_free (error_msg);
1891 return 1;
1892 }
1893
1894 specified_length = parse_integer (inst, Program);
1895
1896 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001897 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001898 return 1;
1899 }
1900
1901 param_var->type = vt_param;
1902 param_var->param_binding_length = 0;
1903
1904 /* Right now, everything is shoved into the main state register file.
1905 *
1906 * In the future, it would be nice to leave things ENV/LOCAL params
1907 * in their respective register files, if possible
1908 */
1909 param_var->param_binding_type = PROGRAM_STATE_VAR;
1910
1911 /* Remember to:
1912 * * - add each guy to the parameter list
1913 * * - increment the param_var->param_binding_len
1914 * * - store the param_var->param_binding_begin for the first one
1915 * * - compare the actual len to the specified len at the end
1916 */
1917 while (**inst != PARAM_NULL) {
1918 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1919 return 1;
1920 }
1921
1922 /* Test array length here! */
1923 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001924 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001925 program_error(ctx, Program->Position,
1926 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001927 }
1928 }
1929
1930 (*inst)++;
1931
1932 return 0;
1933}
1934
1935/**
1936 *
1937 */
1938static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001939parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001940 struct arb_program *Program, struct var_cache **new_var)
1941{
1942 struct var_cache *param_var;
1943
1944 /* First, insert a dummy entry into the var_cache */
1945 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001946 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001947 param_var->type = vt_param;
1948
1949 param_var->param_binding_length = 0;
1950 /* Don't fill in binding_begin; We use the default value of -1
1951 * to tell if its already initialized, elsewhere.
1952 *
1953 * param_var->param_binding_begin = 0;
1954 */
1955 param_var->param_binding_type = PROGRAM_STATE_VAR;
1956
1957 var_cache_append (vc_head, param_var);
1958
1959 /* Then fill it with juicy parameter goodness */
1960 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1961 return 1;
1962
1963 *new_var = param_var;
1964
1965 return 0;
1966}
1967
1968
1969/**
1970 * This handles the declaration of TEMP variables
1971 *
1972 * \return 0 on sucess, 1 on error
1973 */
1974static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001975parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001976 struct arb_program *Program)
1977{
1978 GLuint found;
1979 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001980
1981 while (**inst != 0) {
1982 temp_var = parse_string (inst, vc_head, Program, &found);
1983 Program->Position = parse_position (inst);
1984 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001985 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001986 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1987 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1988 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001989 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001990 _mesa_free (error_msg);
1991 return 1;
1992 }
1993
1994 temp_var->type = vt_temp;
1995
1996 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1997 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001998 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001999 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2000 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002001 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00002002 program_error(ctx, Program->Position,
2003 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002004 return 1;
2005 }
2006
2007 temp_var->temp_binding = Program->Base.NumTemporaries;
2008 Program->Base.NumTemporaries++;
2009 }
2010 (*inst)++;
2011
2012 return 0;
2013}
2014
2015/**
2016 * This handles variables of the OUTPUT variety
2017 *
2018 * \return 0 on sucess, 1 on error
2019 */
2020static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002021parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002022 struct arb_program *Program)
2023{
2024 GLuint found;
2025 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002026 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002027
2028 output_var = parse_string (inst, vc_head, Program, &found);
2029 Program->Position = parse_position (inst);
2030 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002031 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002032 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2033 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2034 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002035 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002036 _mesa_free (error_msg);
2037 return 1;
2038 }
2039
2040 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002041
2042 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2043 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002044}
2045
2046/**
2047 * This handles variables of the ALIAS kind
2048 *
2049 * \return 0 on sucess, 1 on error
2050 */
2051static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002052parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002053 struct arb_program *Program)
2054{
2055 GLuint found;
2056 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002057
2058 temp_var = parse_string (inst, vc_head, Program, &found);
2059 Program->Position = parse_position (inst);
2060
2061 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002062 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002063 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2064 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2065 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002066 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002067 _mesa_free (error_msg);
2068 return 1;
2069 }
2070
2071 temp_var->type = vt_alias;
2072 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2073 Program->Position = parse_position (inst);
2074
2075 if (!found)
2076 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002077 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002078 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2079 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2080 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002081 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002082 _mesa_free (error_msg);
2083 return 1;
2084 }
2085
2086 return 0;
2087}
2088
2089/**
2090 * This handles variables of the ADDRESS kind
2091 *
2092 * \return 0 on sucess, 1 on error
2093 */
2094static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002095parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002096 struct arb_program *Program)
2097{
2098 GLuint found;
2099 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002100
2101 while (**inst != 0) {
2102 temp_var = parse_string (inst, vc_head, Program, &found);
2103 Program->Position = parse_position (inst);
2104 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002105 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002106 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2107 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2108 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002109 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002110 _mesa_free (error_msg);
2111 return 1;
2112 }
2113
2114 temp_var->type = vt_address;
2115
2116 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002117 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002118 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002119 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002120 return 1;
2121 }
2122
2123 temp_var->address_binding = Program->Base.NumAddressRegs;
2124 Program->Base.NumAddressRegs++;
2125 }
2126 (*inst)++;
2127
2128 return 0;
2129}
2130
2131/**
2132 * Parse a program declaration
2133 *
2134 * \return 0 on sucess, 1 on error
2135 */
2136static GLint
Brian Paula088f162006-09-05 23:08:51 +00002137parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002138 struct arb_program *Program)
2139{
2140 GLint err = 0;
2141
2142 switch (*(*inst)++) {
2143 case ADDRESS:
2144 err = parse_address (ctx, inst, vc_head, Program);
2145 break;
2146
2147 case ALIAS:
2148 err = parse_alias (ctx, inst, vc_head, Program);
2149 break;
2150
2151 case ATTRIB:
2152 err = parse_attrib (ctx, inst, vc_head, Program);
2153 break;
2154
2155 case OUTPUT:
2156 err = parse_output (ctx, inst, vc_head, Program);
2157 break;
2158
2159 case PARAM:
2160 err = parse_param (ctx, inst, vc_head, Program);
2161 break;
2162
2163 case TEMP:
2164 err = parse_temp (ctx, inst, vc_head, Program);
2165 break;
2166 }
2167
2168 return err;
2169}
2170
2171/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002172 * Handle the parsing out of a masked destination register, either for a
2173 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002174 *
2175 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002176 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002177 * position invariant
2178 *
2179 * \param File - The register file we write to
2180 * \param Index - The register index we write to
2181 * \param WriteMask - The mask controlling which components we write (1->write)
2182 *
2183 * \return 0 on sucess, 1 on error
2184 */
2185static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002186parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002187 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002188 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002189{
Brian Paul7aebaf32005-10-30 21:23:23 +00002190 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002191 struct var_cache *dst;
2192
2193 /* We either have a result register specified, or a
2194 * variable that may or may not be writable
2195 */
2196 switch (*(*inst)++) {
2197 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002198 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002199 return 1;
2200 *File = PROGRAM_OUTPUT;
2201 break;
2202
2203 case REGISTER_ESTABLISHED_NAME:
2204 dst = parse_string (inst, vc_head, Program, &result);
2205 Program->Position = parse_position (inst);
2206
2207 /* If the name has never been added to our symbol table, we're hosed */
2208 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002209 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002210 return 1;
2211 }
2212
2213 switch (dst->type) {
2214 case vt_output:
2215 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002216 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002217 break;
2218
2219 case vt_temp:
2220 *File = PROGRAM_TEMPORARY;
2221 *Index = dst->temp_binding;
2222 break;
2223
2224 /* If the var type is not vt_output or vt_temp, no go */
2225 default:
Brian Paula088f162006-09-05 23:08:51 +00002226 program_error(ctx, Program->Position,
2227 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002228 return 1;
2229 }
2230 break;
2231
2232 default:
Brian Paula088f162006-09-05 23:08:51 +00002233 program_error(ctx, Program->Position,
2234 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002235 return 1;
2236 }
2237
2238
2239 /* Position invariance test */
2240 /* This test is done now in syntax portion - when position invariance OPTION
2241 is specified, "result.position" rule is disabled so there is no way
2242 to write the position
2243 */
2244 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2245 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002246 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002247 "Vertex program specified position invariance and wrote vertex position");
2248 }*/
2249
2250 /* And then the mask.
2251 * w,a -> bit 0
2252 * z,b -> bit 1
2253 * y,g -> bit 2
2254 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002255 *
2256 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002257 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002258 tmp = (GLint) *(*inst)++;
2259 *WriteMask = (((tmp>>3) & 0x1) |
2260 ((tmp>>1) & 0x2) |
2261 ((tmp<<1) & 0x4) |
2262 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002263
2264 return 0;
2265}
2266
2267
2268/**
2269 * Handle the parsing of a address register
2270 *
2271 * \param Index - The register index we write to
2272 *
2273 * \return 0 on sucess, 1 on error
2274 */
2275static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002276parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002277 struct var_cache **vc_head,
2278 struct arb_program *Program, GLint * Index)
2279{
2280 struct var_cache *dst;
2281 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002282
2283 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002284
2285 dst = parse_string (inst, vc_head, Program, &result);
2286 Program->Position = parse_position (inst);
2287
2288 /* If the name has never been added to our symbol table, we're hosed */
2289 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002290 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002291 return 1;
2292 }
2293
2294 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002295 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002296 return 1;
2297 }
2298
2299 return 0;
2300}
2301
Brian Paul8e8fa632005-07-01 02:03:33 +00002302#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002303/**
2304 * Handle the parsing out of a masked address register
2305 *
2306 * \param Index - The register index we write to
2307 * \param WriteMask - The mask controlling which components we write (1->write)
2308 *
2309 * \return 0 on sucess, 1 on error
2310 */
2311static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002312parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002313 struct var_cache **vc_head,
2314 struct arb_program *Program, GLint * Index,
2315 GLboolean * WriteMask)
2316{
2317 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2318 return 1;
2319
2320 /* This should be 0x8 */
2321 (*inst)++;
2322
2323 /* Writemask of .x is implied */
2324 WriteMask[0] = 1;
2325 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2326
2327 return 0;
2328}
Brian Paul8e8fa632005-07-01 02:03:33 +00002329#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002330
2331/**
2332 * Parse out a swizzle mask.
2333 *
Brian Paul32df89e2005-10-29 18:26:43 +00002334 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002335 *
2336 * The len parameter allows us to grab 4 components for a vector
2337 * swizzle, or just 1 component for a scalar src register selection
2338 */
Brian Paul32df89e2005-10-29 18:26:43 +00002339static void
Brian Paula088f162006-09-05 23:08:51 +00002340parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002341{
Brian Paul32df89e2005-10-29 18:26:43 +00002342 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002343
Brian Paul32df89e2005-10-29 18:26:43 +00002344 for (i = 0; i < 4; i++)
2345 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002346
Brian Paul32df89e2005-10-29 18:26:43 +00002347 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002348 switch (*(*inst)++) {
2349 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002350 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002351 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002352 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002353 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002354 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002355 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002356 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002357 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002358 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002359 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002360 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002361 default:
2362 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2363 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002364 }
2365 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002366}
2367
Jouk Jansen40322e12004-04-05 08:50:36 +00002368
Brian Paul32df89e2005-10-29 18:26:43 +00002369/**
2370 * Parse an extended swizzle mask which is a sequence of
2371 * four x/y/z/w/0/1 tokens.
2372 * \return swizzle four swizzle values
2373 * \return negateMask four element bitfield
2374 */
2375static void
Brian Paula088f162006-09-05 23:08:51 +00002376parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002377 GLubyte *negateMask)
2378{
2379 GLint i;
2380
2381 *negateMask = 0x0;
2382 for (i = 0; i < 4; i++) {
2383 GLubyte swz;
2384 if (parse_sign(inst) == -1)
2385 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002386
2387 swz = *(*inst)++;
2388
2389 switch (swz) {
2390 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002391 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002392 break;
2393 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002394 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002395 break;
2396 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002397 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002398 break;
2399 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002400 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002401 break;
2402 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002403 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002404 break;
2405 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002406 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002407 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002408 default:
2409 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2410 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002411 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002412 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002413}
2414
2415
2416static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002417parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2418 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002419 struct arb_program *Program,
2420 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002421 GLboolean *IsRelOffset )
2422{
2423 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002424 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002425 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002426
Keith Whitwell7c26b612005-04-21 14:46:57 +00002427 *IsRelOffset = 0;
2428
Jouk Jansen40322e12004-04-05 08:50:36 +00002429 /* And the binding for the src */
2430 switch (*(*inst)++) {
2431 case REGISTER_ATTRIB:
2432 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002433 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002434 return 1;
2435 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002436 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002437
2438 /* We need to insert a dummy variable into the var_cache so we can
2439 * catch generic vertex attrib aliasing errors
2440 */
2441 var_cache_create(&src);
2442 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002443 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002444 src->attrib_binding = binding;
2445 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002446 var_cache_append(vc_head, src);
2447 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002448 program_error(ctx, Program->Position,
2449 "Cannot use both a generic vertex attribute "
2450 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002451 return 1;
2452 }
2453 break;
2454
2455 case REGISTER_PARAM:
2456 switch (**inst) {
2457 case PARAM_ARRAY_ELEMENT:
2458 (*inst)++;
2459 src = parse_string (inst, vc_head, Program, &found);
2460 Program->Position = parse_position (inst);
2461
2462 if (!found) {
Briand799b7a2007-09-13 11:29:00 -06002463 char s[1000];
2464 sprintf(s, "Undefined variable: %s", src->name);
2465 program_error(ctx, Program->Position, s);
Jouk Jansen40322e12004-04-05 08:50:36 +00002466 return 1;
2467 }
2468
Brian Paul95801792005-12-06 15:41:43 +00002469 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002470
2471 switch (*(*inst)++) {
2472 case ARRAY_INDEX_ABSOLUTE:
2473 offset = parse_integer (inst, Program);
2474
2475 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002476 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002477 program_error(ctx, Program->Position,
2478 "Index out of range");
2479 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002480 return 1;
2481 }
2482
2483 *Index = src->param_binding_begin + offset;
2484 break;
2485
2486 case ARRAY_INDEX_RELATIVE:
2487 {
2488 GLint addr_reg_idx, rel_off;
2489
2490 /* First, grab the address regiseter */
2491 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2492 return 1;
2493
2494 /* And the .x */
2495 ((*inst)++);
2496 ((*inst)++);
2497 ((*inst)++);
2498 ((*inst)++);
2499
2500 /* Then the relative offset */
2501 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2502
2503 /* And store it properly */
2504 *Index = src->param_binding_begin + rel_off;
2505 *IsRelOffset = 1;
2506 }
2507 break;
2508 }
2509 break;
2510
2511 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002512 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2513 return 1;
2514
Brian Paul95801792005-12-06 15:41:43 +00002515 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002516 *Index = src->param_binding_begin;
2517 break;
2518 }
2519 break;
2520
2521 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002522 src = parse_string (inst, vc_head, Program, &found);
2523 Program->Position = parse_position (inst);
2524
2525 /* If the name has never been added to our symbol table, we're hosed */
2526 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002527 program_error(ctx, Program->Position,
2528 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002529 return 1;
2530 }
2531
2532 switch (src->type) {
2533 case vt_attrib:
2534 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002535 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002536 break;
2537
2538 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2539 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002540 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002541 *Index = src->param_binding_begin;
2542 break;
2543
2544 case vt_temp:
2545 *File = PROGRAM_TEMPORARY;
2546 *Index = src->temp_binding;
2547 break;
2548
2549 /* If the var type is vt_output no go */
2550 default:
Brian Paula088f162006-09-05 23:08:51 +00002551 program_error(ctx, Program->Position,
2552 "destination register is read only");
2553 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002554 return 1;
2555 }
2556 break;
2557
2558 default:
Brian Paula088f162006-09-05 23:08:51 +00002559 program_error(ctx, Program->Position,
2560 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002561 return 1;
2562 }
2563
2564 return 0;
2565}
2566
2567/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002568 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002569 */
2570static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002571parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002572 struct var_cache **vc_head,
2573 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002574 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002575{
Brian Paul7aebaf32005-10-30 21:23:23 +00002576 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002577 GLint index;
2578 GLboolean negate;
2579 GLubyte swizzle[4];
2580 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002581
Jouk Jansen40322e12004-04-05 08:50:36 +00002582 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002583 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002584
2585 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002586 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002587 return 1;
2588
2589 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002590 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002591
Brian Paul32df89e2005-10-29 18:26:43 +00002592 reg->File = file;
2593 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002594 reg->NegateBase = negate;
2595 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002596 return 0;
2597}
2598
Jouk Jansen40322e12004-04-05 08:50:36 +00002599
Brian Paulb7fc1c32006-08-30 23:38:03 +00002600/**
2601 * Parse fragment program destination register.
2602 * \return 1 if error, 0 if no error.
2603 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002604static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002605parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002606 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002607 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002608{
Brian Paul7aebaf32005-10-30 21:23:23 +00002609 GLint mask;
2610 GLuint idx;
2611 enum register_file file;
2612
Keith Whitwell7c26b612005-04-21 14:46:57 +00002613 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002614 return 1;
2615
Keith Whitwell7c26b612005-04-21 14:46:57 +00002616 reg->File = file;
2617 reg->Index = idx;
2618 reg->WriteMask = mask;
2619 return 0;
2620}
2621
2622
Brian Paul7aebaf32005-10-30 21:23:23 +00002623/**
2624 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002625 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002626 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002627static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002628parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002629 struct var_cache **vc_head,
2630 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002631 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002632{
Brian Paul7aebaf32005-10-30 21:23:23 +00002633 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002634 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002635 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002636 GLubyte Swizzle[4];
2637 GLboolean IsRelOffset;
2638
2639 /* Grab the sign */
2640 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2641
2642 /* And the src reg */
2643 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2644 return 1;
2645
2646 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002647 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002648
Keith Whitwell7c26b612005-04-21 14:46:57 +00002649 reg->File = File;
2650 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002651 reg->NegateBase = Negate;
2652 reg->Swizzle = (Swizzle[0] << 0);
2653
Jouk Jansen40322e12004-04-05 08:50:36 +00002654 return 0;
2655}
2656
Keith Whitwell7c26b612005-04-21 14:46:57 +00002657
Jouk Jansen40322e12004-04-05 08:50:36 +00002658/**
2659 * This is a big mother that handles getting opcodes into the instruction
2660 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002661 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002662 */
2663static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002664parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002665 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002666 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002667{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002668 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002669 GLuint texcoord;
2670 GLubyte instClass, type, code;
2671 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002672 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002673
Brian Pauld6272e02006-10-29 18:03:16 +00002674 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002675
2676 /* Record the position in the program string for debugging */
2677 fp->StringPos = Program->Position;
2678
2679 /* OP_ALU_INST or OP_TEX_INST */
2680 instClass = *(*inst)++;
2681
2682 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2683 * OP_TEX_{SAMPLE, KIL}
2684 */
2685 type = *(*inst)++;
2686
2687 /* The actual opcode name */
2688 code = *(*inst)++;
2689
2690 /* Increment the correct count */
2691 switch (instClass) {
2692 case OP_ALU_INST:
2693 Program->NumAluInstructions++;
2694 break;
2695 case OP_TEX_INST:
2696 Program->NumTexInstructions++;
2697 break;
2698 }
2699
Jouk Jansen40322e12004-04-05 08:50:36 +00002700 switch (type) {
2701 case OP_ALU_VECTOR:
2702 switch (code) {
2703 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002704 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002705 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002706 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002707 break;
2708
2709 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002710 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002711 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002712 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002713 break;
2714
2715 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002716 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002717 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002718 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002719 break;
2720
2721 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002722 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002723 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002724 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002725 break;
2726
2727 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002728 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002729 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002730 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002731 break;
2732 }
2733
Keith Whitwell7c26b612005-04-21 14:46:57 +00002734 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002735 return 1;
2736
Keith Whitwell7c26b612005-04-21 14:46:57 +00002737 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002738 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002739 break;
2740
2741 case OP_ALU_SCALAR:
2742 switch (code) {
2743 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002744 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002745 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002746 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 break;
2748
2749 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002750 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002752 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 break;
2754
2755 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002756 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002757 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002758 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002759 break;
2760
2761 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002762 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002763 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002764 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002765 break;
2766
2767 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002768 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002769 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002770 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002771 break;
2772
2773 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002774 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002776 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002777 break;
2778
2779 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002780 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002781 case OP_SCS:
2782
Brian Paul7e807512005-11-05 17:10:45 +00002783 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002784 break;
2785 }
2786
Keith Whitwell7c26b612005-04-21 14:46:57 +00002787 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002788 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002789
2790 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002792 break;
2793
2794 case OP_ALU_BINSC:
2795 switch (code) {
2796 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002797 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002798 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002799 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 break;
2801 }
2802
Keith Whitwell7c26b612005-04-21 14:46:57 +00002803 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002804 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002805
Jouk Jansen40322e12004-04-05 08:50:36 +00002806 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002807 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002808 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002809 }
2810 break;
2811
2812
2813 case OP_ALU_BIN:
2814 switch (code) {
2815 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002816 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002817 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002818 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002819 break;
2820
2821 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002822 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002823 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002824 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002825 break;
2826
2827 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002828 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002829 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002830 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 break;
2832
2833 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002834 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002836 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002837 break;
2838
2839 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002840 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002842 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002843 break;
2844
2845 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002846 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002848 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 break;
2850
2851 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002852 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002854 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 break;
2856
2857 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002858 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002859 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002860 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002861 break;
2862
2863 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002864 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002865 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002866 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002867 break;
2868
2869 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002870 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002871 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002872 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002873 break;
2874
2875 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002876 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002877 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002878 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002879 break;
2880
2881 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002882 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002884 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002885 break;
2886 }
2887
Keith Whitwell7c26b612005-04-21 14:46:57 +00002888 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 return 1;
2890 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002891 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2892 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 }
2894 break;
2895
2896 case OP_ALU_TRI:
2897 switch (code) {
2898 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002899 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002900 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002901 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002902 break;
2903
2904 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002905 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002906 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002907 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 break;
2909
2910 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002911 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002912 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002913 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 break;
2915 }
2916
Keith Whitwell7c26b612005-04-21 14:46:57 +00002917 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002918 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002919
Jouk Jansen40322e12004-04-05 08:50:36 +00002920 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002921 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2922 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002923 }
2924 break;
2925
2926 case OP_ALU_SWZ:
2927 switch (code) {
2928 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002929 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002930 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002931 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002932 break;
2933 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002934 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002935 return 1;
2936
Keith Whitwell7c26b612005-04-21 14:46:57 +00002937 {
Brian Paul32df89e2005-10-29 18:26:43 +00002938 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002939 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002940 enum register_file file;
2941 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002942
Brian Paul32df89e2005-10-29 18:26:43 +00002943 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002944 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002945 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2946 fp->SrcReg[0].File = file;
2947 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002948 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002949 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2950 swizzle[1],
2951 swizzle[2],
2952 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002953 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002954 break;
2955
2956 case OP_TEX_SAMPLE:
2957 switch (code) {
2958 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002959 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002960 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002961 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002962 break;
2963
2964 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002965 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002966 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002967 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002968 break;
2969
2970 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002971 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002972 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002973 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002974 break;
2975 }
2976
Keith Whitwell7c26b612005-04-21 14:46:57 +00002977 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002978 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002979
2980 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002981 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002982
2983 /* texImageUnit */
2984 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2985 return 1;
2986 fp->TexSrcUnit = texcoord;
2987
2988 /* texTarget */
2989 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07002990 case TEXTARGET_SHADOW1D:
2991 shadow_tex = 1 << texcoord;
2992 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00002993 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00002994 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002995 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07002996 case TEXTARGET_SHADOW2D:
2997 shadow_tex = 1 << texcoord;
2998 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00002999 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003000 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003001 break;
3002 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003003 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003004 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003005 case TEXTARGET_SHADOWRECT:
3006 shadow_tex = 1 << texcoord;
3007 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003008 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003009 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003010 break;
3011 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003012 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003013 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003014 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003015 shadow_tex = 1 << texcoord;
3016 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003017 case TEXTARGET_1D_ARRAY:
3018 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3019 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003020 case TEXTARGET_SHADOW2D_ARRAY:
3021 shadow_tex = 1 << texcoord;
3022 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003023 case TEXTARGET_2D_ARRAY:
3024 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3025 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003026 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003027
3028 /* Don't test the first time a particular sampler is seen. Each time
3029 * after that, make sure the shadow state is the same.
3030 */
3031 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3032 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3033 program_error(ctx, Program->Position,
3034 "texture image unit used for shadow sampling and non-shadow sampling");
3035 return 1;
3036 }
3037
Brian Paulb7fc1c32006-08-30 23:38:03 +00003038 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3039 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3040 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003041 program_error(ctx, Program->Position,
3042 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003043 return 1;
3044 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003045
3046
3047 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003048 break;
3049
3050 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003051 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003052 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003053 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003054 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003055 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003056 default:
3057 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3058 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003059 }
3060
3061 return 0;
3062}
3063
Keith Whitwell7c26b612005-04-21 14:46:57 +00003064static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003065parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003066 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003067 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003068{
Brian Paul7aebaf32005-10-30 21:23:23 +00003069 GLint mask;
3070 GLuint idx;
3071 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003072
3073 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3074 return 1;
3075
3076 reg->File = file;
3077 reg->Index = idx;
3078 reg->WriteMask = mask;
3079 return 0;
3080}
3081
3082/**
3083 * Handle the parsing out of a masked address register
3084 *
3085 * \param Index - The register index we write to
3086 * \param WriteMask - The mask controlling which components we write (1->write)
3087 *
3088 * \return 0 on sucess, 1 on error
3089 */
3090static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003091parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003092 struct var_cache **vc_head,
3093 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003094 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003095{
3096 GLint idx;
3097
3098 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3099 return 1;
3100
3101 /* This should be 0x8 */
3102 (*inst)++;
3103
3104 reg->File = PROGRAM_ADDRESS;
3105 reg->Index = idx;
3106
3107 /* Writemask of .x is implied */
3108 reg->WriteMask = 0x1;
3109 return 0;
3110}
3111
3112/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003113 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003114 */
3115static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003116parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003117 struct var_cache **vc_head,
3118 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003119 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003120{
Brian Paul7aebaf32005-10-30 21:23:23 +00003121 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003122 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003123 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003124 GLubyte swizzle[4];
3125 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003126
3127 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003128 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003129
3130 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003131 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003132 return 1;
3133
3134 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003135 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003136
Brian Paul32df89e2005-10-29 18:26:43 +00003137 reg->File = file;
3138 reg->Index = index;
3139 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3140 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003141 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003142 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003143 return 0;
3144}
3145
3146
3147static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003148parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003149 struct var_cache **vc_head,
3150 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003151 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003152{
Brian Paul7aebaf32005-10-30 21:23:23 +00003153 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003154 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003155 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003156 GLubyte Swizzle[4];
3157 GLboolean IsRelOffset;
3158
3159 /* Grab the sign */
3160 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3161
3162 /* And the src reg */
3163 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3164 return 1;
3165
3166 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003167 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003168
3169 reg->File = File;
3170 reg->Index = Index;
3171 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003172 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003173 reg->RelAddr = IsRelOffset;
3174 return 0;
3175}
3176
3177
Jouk Jansen40322e12004-04-05 08:50:36 +00003178/**
3179 * This is a big mother that handles getting opcodes into the instruction
3180 * and handling the src & dst registers for vertex program instructions
3181 */
3182static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003183parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003184 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003185 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003186{
3187 GLint a;
3188 GLubyte type, code;
3189
3190 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3191 type = *(*inst)++;
3192
3193 /* The actual opcode name */
3194 code = *(*inst)++;
3195
Brian Pauld6272e02006-10-29 18:03:16 +00003196 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003197 /* Record the position in the program string for debugging */
3198 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003199
3200 switch (type) {
3201 /* XXX: */
3202 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003203 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003204
3205 /* Remember to set SrcReg.RelAddr; */
3206
3207 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003208 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003209 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003210
Jouk Jansen40322e12004-04-05 08:50:36 +00003211 vp->DstReg.File = PROGRAM_ADDRESS;
3212
3213 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003214 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003215 return 1;
3216
3217 break;
3218
3219 case OP_ALU_VECTOR:
3220 switch (code) {
3221 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003222 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003223 break;
3224 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003225 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003226 break;
3227 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003228 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003229 break;
3230 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003231 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003232 break;
3233 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003234 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 break;
3236 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003237
3238 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003239 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003240
3241 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003242 return 1;
3243 break;
3244
3245 case OP_ALU_SCALAR:
3246 switch (code) {
3247 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003248 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003249 break;
3250 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003251 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003252 break;
3253 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003254 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003255 break;
3256 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003257 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003258 break;
3259 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003260 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003261 break;
3262 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003263 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003264 break;
3265 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003266 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003267 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003268
3269 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003270 return 1;
3271 break;
3272
3273 case OP_ALU_BINSC:
3274 switch (code) {
3275 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003276 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003277 break;
3278 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003279 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003280 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003281
Jouk Jansen40322e12004-04-05 08:50:36 +00003282 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003283 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003284 return 1;
3285 }
3286 break;
3287
3288 case OP_ALU_BIN:
3289 switch (code) {
3290 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003291 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003292 break;
3293 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003294 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003295 break;
3296 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003297 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003298 break;
3299 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003300 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003301 break;
3302 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003303 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003304 break;
3305 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003306 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003307 break;
3308 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003309 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003310 break;
3311 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003312 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003313 break;
3314 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003315 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003316 break;
3317 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003318 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003319 break;
3320 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003321 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003322 break;
3323 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003324 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003325 break;
3326 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003327 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003328 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003329
Jouk Jansen40322e12004-04-05 08:50:36 +00003330 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003331 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003332 return 1;
3333 }
3334 break;
3335
3336 case OP_ALU_TRI:
3337 switch (code) {
3338 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003339 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003340 break;
3341 }
3342
Keith Whitwell7c26b612005-04-21 14:46:57 +00003343 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003344 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003345
Jouk Jansen40322e12004-04-05 08:50:36 +00003346 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003347 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003348 return 1;
3349 }
3350 break;
3351
3352 case OP_ALU_SWZ:
3353 switch (code) {
3354 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003355 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003356 break;
3357 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003358 {
Brian Paul32df89e2005-10-29 18:26:43 +00003359 GLubyte swizzle[4];
3360 GLubyte negateMask;
3361 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003362 enum register_file file;
3363 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003364
Keith Whitwell7c26b612005-04-21 14:46:57 +00003365 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3366 return 1;
3367
Brian Paul32df89e2005-10-29 18:26:43 +00003368 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003369 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003370 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3371 vp->SrcReg[0].File = file;
3372 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003373 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003374 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3375 swizzle[1],
3376 swizzle[2],
3377 swizzle[3]);
3378 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003379 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003380 break;
3381 }
3382 return 0;
3383}
3384
3385#if DEBUG_PARSING
3386
3387static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003388debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3389 struct arb_program *Program)
3390{
3391 struct var_cache *vc;
3392 GLint a, b;
3393
Brianb618ac82007-02-22 09:39:25 -07003394 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003395
3396 /* First of all, print out the contents of the var_cache */
3397 vc = vc_head;
3398 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003399 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003400 switch (vc->type) {
3401 case vt_none:
3402 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3403 break;
3404 case vt_attrib:
3405 fprintf (stderr, "ATTRIB %s\n", vc->name);
3406 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3407 break;
3408 case vt_param:
3409 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3410 vc->param_binding_begin, vc->param_binding_length);
3411 b = vc->param_binding_begin;
3412 for (a = 0; a < vc->param_binding_length; a++) {
3413 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003414 Program->Base.Parameters->Parameters[a + b].Name);
3415 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3416 const char *s;
3417 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3418 [a + b].StateIndexes);
3419 fprintf(stderr, "%s\n", s);
3420 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003421 }
3422 else
3423 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003424 Program->Base.Parameters->ParameterValues[a + b][0],
3425 Program->Base.Parameters->ParameterValues[a + b][1],
3426 Program->Base.Parameters->ParameterValues[a + b][2],
3427 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003428 }
3429 break;
3430 case vt_temp:
3431 fprintf (stderr, "TEMP %s\n", vc->name);
3432 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3433 break;
3434 case vt_output:
3435 fprintf (stderr, "OUTPUT %s\n", vc->name);
3436 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3437 break;
3438 case vt_alias:
3439 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003440 fprintf (stderr, " binding: 0x%p (%s)\n",
3441 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003442 break;
Brianb618ac82007-02-22 09:39:25 -07003443 default:
3444 /* nothing */
3445 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003446 }
3447 vc = vc->next;
3448 }
3449}
3450
Brian Paul72030e02005-11-03 03:30:34 +00003451#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003452
3453
3454/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003455 * The main loop for parsing a fragment or vertex program
3456 *
Brian Paul72030e02005-11-03 03:30:34 +00003457 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003458 */
Brian Paul72030e02005-11-03 03:30:34 +00003459static GLint
Brian Paula088f162006-09-05 23:08:51 +00003460parse_instructions(GLcontext * ctx, const GLubyte * inst,
3461 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003462{
Brian Paul72030e02005-11-03 03:30:34 +00003463 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3464 ? ctx->Const.FragmentProgram.MaxInstructions
3465 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003466 GLint err = 0;
3467
Brian Paul72030e02005-11-03 03:30:34 +00003468 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3469
Jouk Jansen40322e12004-04-05 08:50:36 +00003470 Program->MajorVersion = (GLuint) * inst++;
3471 Program->MinorVersion = (GLuint) * inst++;
3472
3473 while (*inst != END) {
3474 switch (*inst++) {
3475
3476 case OPTION:
3477 switch (*inst++) {
3478 case ARB_PRECISION_HINT_FASTEST:
3479 Program->PrecisionOption = GL_FASTEST;
3480 break;
3481
3482 case ARB_PRECISION_HINT_NICEST:
3483 Program->PrecisionOption = GL_NICEST;
3484 break;
3485
3486 case ARB_FOG_EXP:
3487 Program->FogOption = GL_EXP;
3488 break;
3489
3490 case ARB_FOG_EXP2:
3491 Program->FogOption = GL_EXP2;
3492 break;
3493
3494 case ARB_FOG_LINEAR:
3495 Program->FogOption = GL_LINEAR;
3496 break;
3497
3498 case ARB_POSITION_INVARIANT:
3499 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003500 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003501 break;
3502
3503 case ARB_FRAGMENT_PROGRAM_SHADOW:
3504 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3505 /* TODO ARB_fragment_program_shadow code */
3506 }
3507 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003508
3509 case ARB_DRAW_BUFFERS:
3510 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3511 /* do nothing for now */
3512 }
3513 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003514
3515 case MESA_TEXTURE_ARRAY:
3516 /* do nothing for now */
3517 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003518 }
3519 break;
3520
3521 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003522 /* check length */
3523 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003524 program_error(ctx, Program->Position,
3525 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003526 return 1;
3527 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003528 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003529 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003530 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003531 err = parse_fp_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 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003535 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003536 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003537 }
3538
Brian Paul72030e02005-11-03 03:30:34 +00003539 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003540 Program->Base.NumInstructions++;
3541 break;
3542
3543 case DECLARATION:
3544 err = parse_declaration (ctx, &inst, vc_head, Program);
3545 break;
3546
3547 default:
3548 break;
3549 }
3550
3551 if (err)
3552 break;
3553 }
3554
3555 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003556 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003557 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003558 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003559 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003560 /* YYY Wrong Position in program, whatever, at least not random -> crash
3561 Program->Position = parse_position (&inst);
3562 */
Brian Paul8c41a142005-11-19 15:36:28 +00003563 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003564 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003565 Program->Base.NumInstructions++;
3566
Brian Paul05051032005-11-01 04:36:33 +00003567 /*
3568 * Initialize native counts to logical counts. The device driver may
3569 * change them if program is translated into a hardware program.
3570 */
3571 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3572 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3573 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3574 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3575 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003576
Jouk Jansen40322e12004-04-05 08:50:36 +00003577 return err;
3578}
3579
Brian Paul7aebaf32005-10-30 21:23:23 +00003580
Jouk Jansen40322e12004-04-05 08:50:36 +00003581/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003582LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003583#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003584;
3585
Brian Paul7aebaf32005-10-30 21:23:23 +00003586
Brian Paul8c41a142005-11-19 15:36:28 +00003587/**
3588 * Set a grammar parameter.
3589 * \param name the grammar parameter
3590 * \param value the new parameter value
3591 * \return 0 if OK, 1 if error
3592 */
3593static int
3594set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003595{
3596 char error_msg[300];
3597 GLint error_pos;
3598
Brian Paul8c41a142005-11-19 15:36:28 +00003599 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003600 return 0;
3601
3602 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3603 _mesa_set_program_error (ctx, error_pos, error_msg);
3604 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3605 return 1;
3606}
3607
Brian Paul8c41a142005-11-19 15:36:28 +00003608
3609/**
3610 * Enable support for the given language option in the parser.
3611 * \return 1 if OK, 0 if error
3612 */
3613static int
3614enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003615{
Brian Paul8c41a142005-11-19 15:36:28 +00003616 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003617}
3618
Brian Paul8c41a142005-11-19 15:36:28 +00003619
3620/**
3621 * Enable parser extensions based on which OpenGL extensions are supported
3622 * by this rendering context.
3623 *
3624 * \return GL_TRUE if OK, GL_FALSE if error.
3625 */
3626static GLboolean
3627enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003628{
Brian Paul8c41a142005-11-19 15:36:28 +00003629#if 0
3630 /* These are not supported at this time */
3631 if ((ctx->Extensions.ARB_vertex_blend ||
3632 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003633 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003634 return GL_FALSE;
3635 if (ctx->Extensions.ARB_matrix_palette
3636 && !enable_ext(ctx, id, "matrix_palette"))
3637 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003638#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003639 if (ctx->Extensions.ARB_fragment_program_shadow
3640 && !enable_ext(ctx, id, "fragment_program_shadow"))
3641 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003642 if (ctx->Extensions.EXT_point_parameters
3643 && !enable_ext(ctx, id, "point_parameters"))
3644 return GL_FALSE;
3645 if (ctx->Extensions.EXT_secondary_color
3646 && !enable_ext(ctx, id, "secondary_color"))
3647 return GL_FALSE;
3648 if (ctx->Extensions.EXT_fog_coord
3649 && !enable_ext(ctx, id, "fog_coord"))
3650 return GL_FALSE;
3651 if (ctx->Extensions.NV_texture_rectangle
3652 && !enable_ext(ctx, id, "texture_rectangle"))
3653 return GL_FALSE;
3654 if (ctx->Extensions.ARB_draw_buffers
3655 && !enable_ext(ctx, id, "draw_buffers"))
3656 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003657 if (ctx->Extensions.MESA_texture_array
3658 && !enable_ext(ctx, id, "texture_array"))
3659 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003660#if 1
3661 /* hack for Warcraft (see bug 8060) */
3662 enable_ext(ctx, id, "vertex_blend");
3663#endif
3664
Brian Paul8c41a142005-11-19 15:36:28 +00003665 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003666}
3667
Brian Paul8c41a142005-11-19 15:36:28 +00003668
Jouk Jansen40322e12004-04-05 08:50:36 +00003669/**
3670 * This kicks everything off.
3671 *
3672 * \param ctx - The GL Context
3673 * \param str - The program string
3674 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003675 * \param program - The arb_program struct to return all the parsed info in
3676 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003677 */
Brian Paul8c41a142005-11-19 15:36:28 +00003678static GLboolean
3679_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3680 const GLubyte *str, GLsizei len,
3681 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003682{
3683 GLint a, err, error_pos;
3684 char error_msg[300];
3685 GLuint parsed_len;
3686 struct var_cache *vc_head;
3687 grammar arbprogram_syn_id;
3688 GLubyte *parsed, *inst;
3689 GLubyte *strz = NULL;
3690 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3691
Brian Paul8c41a142005-11-19 15:36:28 +00003692 /* set the program target before parsing */
3693 program->Base.Target = target;
3694
Brian Paul7f76b8f2004-09-10 01:05:39 +00003695 /* Reset error state */
3696 _mesa_set_program_error(ctx, -1, NULL);
3697
Brian Paul8c41a142005-11-19 15:36:28 +00003698 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003699 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003700 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003701 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003702 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003703
3704 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3705 if (grammar_syn_id == 0) {
3706 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003707 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003708 _mesa_set_program_error (ctx, error_pos, error_msg);
3709 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003710 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003711 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003712 }
3713
Brian Paul8c41a142005-11-19 15:36:28 +00003714 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3715 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003716
Brian Paul49a80ca2006-04-28 15:40:11 +00003717 /* 'parsed' is unused here */
3718 _mesa_free (parsed);
3719 parsed = NULL;
3720
Brian Paul45703642005-10-29 15:52:31 +00003721 /* NOTE: we can't destroy grammar_syn_id right here because
3722 * grammar_destroy() can reset the last error
3723 */
Brian Paul8c41a142005-11-19 15:36:28 +00003724 if (err) {
3725 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003726 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3727 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003728 _mesa_error (ctx, GL_INVALID_OPERATION,
3729 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003730 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003731 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003732 }
3733
3734 grammar_destroy (grammar_syn_id);
3735
3736 arbprogram_syn_is_ok = 1;
3737 }
3738
3739 /* create the grammar object */
3740 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3741 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003742 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003743 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3744 _mesa_set_program_error (ctx, error_pos, error_msg);
3745 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003746 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003747 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003748 }
3749
3750 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003751 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003752 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3753 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003754 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003755 }
3756
Brian Paul8c41a142005-11-19 15:36:28 +00003757 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3758 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003759 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003760 }
3761
3762 /* check for NULL character occurences */
3763 {
Brian Paul8c41a142005-11-19 15:36:28 +00003764 GLint i;
3765 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003766 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003767 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003768 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003769 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003770 }
Brian Paul8c41a142005-11-19 15:36:28 +00003771 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003772 }
3773
3774 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003775 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003776 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003777 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3778 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003779 return GL_FALSE;
3780 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003781 _mesa_memcpy (strz, str, len);
3782 strz[len] = '\0';
3783
Michal Krolb80bc052004-10-21 14:09:54 +00003784 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003785 err = !grammar_fast_check(arbprogram_syn_id, strz,
3786 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003787
3788 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003789 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003790 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3791 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003792
Brian Paulb3aefd12005-09-19 20:12:32 +00003793#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003794 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003795 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003796 int line, col;
3797 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003798 fprintf(stderr, "program: %s\n", (char *) strz);
3799 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003800 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3801 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003802 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3803 } while (0)
3804#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003805
Brian Paula088f162006-09-05 23:08:51 +00003806 _mesa_free(strz);
3807 _mesa_free(parsed);
3808
Jouk Jansen40322e12004-04-05 08:50:36 +00003809 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003810 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003811 }
3812
Jouk Jansen40322e12004-04-05 08:50:36 +00003813 grammar_destroy (arbprogram_syn_id);
3814
Brian Paul8c41a142005-11-19 15:36:28 +00003815 /*
3816 * Program string is syntactically correct at this point
3817 * Parse the tokenized version of the program now, generating
3818 * vertex/fragment program instructions.
3819 */
3820
Jouk Jansen40322e12004-04-05 08:50:36 +00003821 /* Initialize the arb_program struct */
3822 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003823 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003824 program->Base.NumInstructions =
3825 program->Base.NumTemporaries =
3826 program->Base.NumParameters =
3827 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003828 program->Base.Parameters = _mesa_new_parameter_list ();
3829 program->Base.InputsRead = 0x0;
3830 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003831 program->Position = 0;
3832 program->MajorVersion = program->MinorVersion = 0;
3833 program->PrecisionOption = GL_DONT_CARE;
3834 program->FogOption = GL_NONE;
3835 program->HintPositionInvariant = GL_FALSE;
3836 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003837 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003838 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003839 program->NumAluInstructions =
3840 program->NumTexInstructions =
3841 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003842 program->UsesKill = 0;
3843
Jouk Jansen40322e12004-04-05 08:50:36 +00003844 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003845 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003846
3847 /* Start examining the tokens in the array */
3848 inst = parsed;
3849
3850 /* Check the grammer rev */
3851 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003852 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003853 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003854 }
3855 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003856 /* ignore program target */
3857 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003858 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003859 }
3860
3861 /*debug_variables(ctx, vc_head, program); */
3862
3863 /* We're done with the parsed binary array */
3864 var_cache_destroy (&vc_head);
3865
3866 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003867
Brian Paul8c41a142005-11-19 15:36:28 +00003868 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3869 * to size [ap.Base.NumInstructions].
3870 */
Brian Paula7543902006-08-24 21:58:32 +00003871 program->Base.Instructions
3872 = _mesa_realloc_instructions(program->Base.Instructions,
3873 MAX_INSTRUCTIONS,
3874 program->Base.NumInstructions);
3875
Brian Paul45703642005-10-29 15:52:31 +00003876 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003877}
Brian Paul8c41a142005-11-19 15:36:28 +00003878
3879
3880
3881void
3882_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3883 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003884 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003885{
3886 struct arb_program ap;
3887 GLuint i;
3888
3889 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003890 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003891 /* Error in the program. Just return. */
3892 return;
3893 }
3894
3895 /* Copy the relevant contents of the arb_program struct into the
3896 * fragment_program struct.
3897 */
3898 program->Base.String = ap.Base.String;
3899 program->Base.NumInstructions = ap.Base.NumInstructions;
3900 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3901 program->Base.NumParameters = ap.Base.NumParameters;
3902 program->Base.NumAttributes = ap.Base.NumAttributes;
3903 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003904 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3905 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3906 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3907 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3908 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003909 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3910 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3911 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3912 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3913 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3914 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003915 program->Base.InputsRead = ap.Base.InputsRead;
3916 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3917 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
Brianc9db2232007-01-04 17:22:19 -07003918 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Ian Romanick7b559a92007-06-07 13:58:50 -07003919 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003920 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003921 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003922
3923 if (program->Base.Instructions)
3924 _mesa_free(program->Base.Instructions);
3925 program->Base.Instructions = ap.Base.Instructions;
3926
3927 if (program->Base.Parameters)
3928 _mesa_free_parameter_list(program->Base.Parameters);
3929 program->Base.Parameters = ap.Base.Parameters;
3930
3931#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00003932 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
3933 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003934#endif
3935}
3936
3937
3938
3939/**
3940 * Parse the vertex program string. If success, update the given
3941 * vertex_program object with the new program. Else, leave the vertex_program
3942 * object unchanged.
3943 */
3944void
3945_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3946 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003947 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003948{
3949 struct arb_program ap;
3950
3951 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3952
Brian Paul95801792005-12-06 15:41:43 +00003953 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003954 /* Error in the program. Just return. */
3955 return;
3956 }
3957
3958 /* Copy the relevant contents of the arb_program struct into the
3959 * vertex_program struct.
3960 */
3961 program->Base.String = ap.Base.String;
3962 program->Base.NumInstructions = ap.Base.NumInstructions;
3963 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3964 program->Base.NumParameters = ap.Base.NumParameters;
3965 program->Base.NumAttributes = ap.Base.NumAttributes;
3966 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003967 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3968 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3969 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3970 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3971 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003972 program->Base.InputsRead = ap.Base.InputsRead;
3973 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3974 program->IsPositionInvariant = ap.HintPositionInvariant;
3975
3976 if (program->Base.Instructions)
3977 _mesa_free(program->Base.Instructions);
3978 program->Base.Instructions = ap.Base.Instructions;
3979
3980 if (program->Base.Parameters)
3981 _mesa_free_parameter_list(program->Base.Parameters);
3982 program->Base.Parameters = ap.Base.Parameters;
3983
3984#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01003985 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00003986 _mesa_print_program(&program->Base);
3987#endif
3988}