blob: 12db64612ee4ffa27d0f1822b9264e26e56c830e [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian Paul54cfe692005-10-21 15:22:36 +00003 * Version: 6.5
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Michal Krolbb38cad2006-04-11 11:41:11 +00005 * Copyright (C) 1999-2006 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
Jouk Jansen40322e12004-04-05 08:50:36 +000033#include "glheader.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000034#include "imports.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000035#include "arbprogparse.h"
36#include "grammar_mesa.h"
Brian Paul32df89e2005-10-29 18:26:43 +000037#include "program.h"
Brian Paul8c41a142005-11-19 15:36:28 +000038#include "context.h"
39#include "mtypes.h"
40#include "program_instruction.h"
41
42
43#define MAX_INSTRUCTIONS 256
44
45
46/**
47 * This is basically a union of the vertex_program and fragment_program
48 * structs that we can use to parse the program into
49 *
50 * XXX we can probably get rid of this entirely someday.
51 */
52struct arb_program
53{
54 struct program Base;
55
56 GLuint Position; /* Just used for error reporting while parsing */
57 GLuint MajorVersion;
58 GLuint MinorVersion;
59
60 /* ARB_vertex_progmra options */
61 GLboolean HintPositionInvariant;
62
63 /* ARB_fragment_progmra options */
64 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
65 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
66
67 /* ARB_fragment_program specifics */
68 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
69 GLuint NumAluInstructions;
70 GLuint NumTexInstructions;
71 GLuint NumTexIndirections;
72
73 GLboolean UsesKill;
74};
75
Ian Romanick9bdfee32005-07-18 12:31:24 +000076
Alan Hourihane38b317d2004-12-14 09:11:52 +000077#ifndef __extension__
78#if !defined(__GNUC__) || (__GNUC__ < 2) || \
79 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
Brian Paula6c423d2004-08-25 15:59:48 +000080# define __extension__
81#endif
Alan Hourihane38b317d2004-12-14 09:11:52 +000082#endif
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
168/**
169 * This is the text describing the rules to parse the grammar
170 */
Brian Paula6c423d2004-08-25 15:59:48 +0000171__extension__ static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000172#include "arbprogram_syn.h"
173;
174
175/**
176 * These should match up with the values defined in arbprogram.syn
177 */
178
179/*
180 Changes:
181 - changed and merged V_* and F_* opcode values to OP_*.
182 - added GL_ARB_fragment_program_shadow specific tokens (michal)
183*/
Michal Krolb80bc052004-10-21 14:09:54 +0000184#define REVISION 0x09
Jouk Jansen40322e12004-04-05 08:50:36 +0000185
186/* program type */
187#define FRAGMENT_PROGRAM 0x01
188#define VERTEX_PROGRAM 0x02
189
190/* program section */
191#define OPTION 0x01
192#define INSTRUCTION 0x02
193#define DECLARATION 0x03
194#define END 0x04
195
Michal Krolb80bc052004-10-21 14:09:54 +0000196/* GL_ARB_fragment_program option */
197#define ARB_PRECISION_HINT_FASTEST 0x00
198#define ARB_PRECISION_HINT_NICEST 0x01
199#define ARB_FOG_EXP 0x02
200#define ARB_FOG_EXP2 0x03
201#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000202
Michal Krolb80bc052004-10-21 14:09:54 +0000203/* GL_ARB_vertex_program option */
204#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000205
Michal Krolb80bc052004-10-21 14:09:54 +0000206/* GL_ARB_fragment_program_shadow option */
207#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000208
Michal Krolb80bc052004-10-21 14:09:54 +0000209/* GL_ARB_draw_buffers option */
210#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000211
Jouk Jansen40322e12004-04-05 08:50:36 +0000212/* GL_ARB_fragment_program instruction class */
213#define OP_ALU_INST 0x00
214#define OP_TEX_INST 0x01
215
216/* GL_ARB_vertex_program instruction class */
217/* OP_ALU_INST */
218
219/* GL_ARB_fragment_program instruction type */
220#define OP_ALU_VECTOR 0x00
221#define OP_ALU_SCALAR 0x01
222#define OP_ALU_BINSC 0x02
223#define OP_ALU_BIN 0x03
224#define OP_ALU_TRI 0x04
225#define OP_ALU_SWZ 0x05
226#define OP_TEX_SAMPLE 0x06
227#define OP_TEX_KIL 0x07
228
229/* GL_ARB_vertex_program instruction type */
230#define OP_ALU_ARL 0x08
231/* OP_ALU_VECTOR */
232/* OP_ALU_SCALAR */
233/* OP_ALU_BINSC */
234/* OP_ALU_BIN */
235/* OP_ALU_TRI */
236/* OP_ALU_SWZ */
237
238/* GL_ARB_fragment_program instruction code */
239#define OP_ABS 0x00
240#define OP_ABS_SAT 0x1B
241#define OP_FLR 0x09
242#define OP_FLR_SAT 0x26
243#define OP_FRC 0x0A
244#define OP_FRC_SAT 0x27
245#define OP_LIT 0x0C
246#define OP_LIT_SAT 0x2A
247#define OP_MOV 0x11
248#define OP_MOV_SAT 0x30
249#define OP_COS 0x1F
250#define OP_COS_SAT 0x20
251#define OP_EX2 0x07
252#define OP_EX2_SAT 0x25
253#define OP_LG2 0x0B
254#define OP_LG2_SAT 0x29
255#define OP_RCP 0x14
256#define OP_RCP_SAT 0x33
257#define OP_RSQ 0x15
258#define OP_RSQ_SAT 0x34
259#define OP_SIN 0x38
260#define OP_SIN_SAT 0x39
261#define OP_SCS 0x35
262#define OP_SCS_SAT 0x36
263#define OP_POW 0x13
264#define OP_POW_SAT 0x32
265#define OP_ADD 0x01
266#define OP_ADD_SAT 0x1C
267#define OP_DP3 0x03
268#define OP_DP3_SAT 0x21
269#define OP_DP4 0x04
270#define OP_DP4_SAT 0x22
271#define OP_DPH 0x05
272#define OP_DPH_SAT 0x23
273#define OP_DST 0x06
274#define OP_DST_SAT 0x24
275#define OP_MAX 0x0F
276#define OP_MAX_SAT 0x2E
277#define OP_MIN 0x10
278#define OP_MIN_SAT 0x2F
279#define OP_MUL 0x12
280#define OP_MUL_SAT 0x31
281#define OP_SGE 0x16
282#define OP_SGE_SAT 0x37
283#define OP_SLT 0x17
284#define OP_SLT_SAT 0x3A
285#define OP_SUB 0x18
286#define OP_SUB_SAT 0x3B
287#define OP_XPD 0x1A
288#define OP_XPD_SAT 0x43
289#define OP_CMP 0x1D
290#define OP_CMP_SAT 0x1E
291#define OP_LRP 0x2B
292#define OP_LRP_SAT 0x2C
293#define OP_MAD 0x0E
294#define OP_MAD_SAT 0x2D
295#define OP_SWZ 0x19
296#define OP_SWZ_SAT 0x3C
297#define OP_TEX 0x3D
298#define OP_TEX_SAT 0x3E
299#define OP_TXB 0x3F
300#define OP_TXB_SAT 0x40
301#define OP_TXP 0x41
302#define OP_TXP_SAT 0x42
303#define OP_KIL 0x28
304
305/* GL_ARB_vertex_program instruction code */
306#define OP_ARL 0x02
307/* OP_ABS */
308/* OP_FLR */
309/* OP_FRC */
310/* OP_LIT */
311/* OP_MOV */
312/* OP_EX2 */
313#define OP_EXP 0x08
314/* OP_LG2 */
315#define OP_LOG 0x0D
316/* OP_RCP */
317/* OP_RSQ */
318/* OP_POW */
319/* OP_ADD */
320/* OP_DP3 */
321/* OP_DP4 */
322/* OP_DPH */
323/* OP_DST */
324/* OP_MAX */
325/* OP_MIN */
326/* OP_MUL */
327/* OP_SGE */
328/* OP_SLT */
329/* OP_SUB */
330/* OP_XPD */
331/* OP_MAD */
332/* OP_SWZ */
333
334/* fragment attribute binding */
335#define FRAGMENT_ATTRIB_COLOR 0x01
336#define FRAGMENT_ATTRIB_TEXCOORD 0x02
337#define FRAGMENT_ATTRIB_FOGCOORD 0x03
338#define FRAGMENT_ATTRIB_POSITION 0x04
339
340/* vertex attribute binding */
341#define VERTEX_ATTRIB_POSITION 0x01
342#define VERTEX_ATTRIB_WEIGHT 0x02
343#define VERTEX_ATTRIB_NORMAL 0x03
344#define VERTEX_ATTRIB_COLOR 0x04
345#define VERTEX_ATTRIB_FOGCOORD 0x05
346#define VERTEX_ATTRIB_TEXCOORD 0x06
347#define VERTEX_ATTRIB_MATRIXINDEX 0x07
348#define VERTEX_ATTRIB_GENERIC 0x08
349
350/* fragment result binding */
351#define FRAGMENT_RESULT_COLOR 0x01
352#define FRAGMENT_RESULT_DEPTH 0x02
353
354/* vertex result binding */
355#define VERTEX_RESULT_POSITION 0x01
356#define VERTEX_RESULT_COLOR 0x02
357#define VERTEX_RESULT_FOGCOORD 0x03
358#define VERTEX_RESULT_POINTSIZE 0x04
359#define VERTEX_RESULT_TEXCOORD 0x05
360
361/* texture target */
362#define TEXTARGET_1D 0x01
363#define TEXTARGET_2D 0x02
364#define TEXTARGET_3D 0x03
365#define TEXTARGET_RECT 0x04
366#define TEXTARGET_CUBE 0x05
367/* GL_ARB_fragment_program_shadow */
368#define TEXTARGET_SHADOW1D 0x06
369#define TEXTARGET_SHADOW2D 0x07
370#define TEXTARGET_SHADOWRECT 0x08
371
372/* face type */
373#define FACE_FRONT 0x00
374#define FACE_BACK 0x01
375
376/* color type */
377#define COLOR_PRIMARY 0x00
378#define COLOR_SECONDARY 0x01
379
380/* component */
381#define COMPONENT_X 0x00
382#define COMPONENT_Y 0x01
383#define COMPONENT_Z 0x02
384#define COMPONENT_W 0x03
385#define COMPONENT_0 0x04
386#define COMPONENT_1 0x05
387
388/* array index type */
389#define ARRAY_INDEX_ABSOLUTE 0x00
390#define ARRAY_INDEX_RELATIVE 0x01
391
392/* matrix name */
393#define MATRIX_MODELVIEW 0x01
394#define MATRIX_PROJECTION 0x02
395#define MATRIX_MVP 0x03
396#define MATRIX_TEXTURE 0x04
397#define MATRIX_PALETTE 0x05
398#define MATRIX_PROGRAM 0x06
399
400/* matrix modifier */
401#define MATRIX_MODIFIER_IDENTITY 0x00
402#define MATRIX_MODIFIER_INVERSE 0x01
403#define MATRIX_MODIFIER_TRANSPOSE 0x02
404#define MATRIX_MODIFIER_INVTRANS 0x03
405
406/* constant type */
407#define CONSTANT_SCALAR 0x01
408#define CONSTANT_VECTOR 0x02
409
410/* program param type */
411#define PROGRAM_PARAM_ENV 0x01
412#define PROGRAM_PARAM_LOCAL 0x02
413
414/* register type */
415#define REGISTER_ATTRIB 0x01
416#define REGISTER_PARAM 0x02
417#define REGISTER_RESULT 0x03
418#define REGISTER_ESTABLISHED_NAME 0x04
419
420/* param binding */
421#define PARAM_NULL 0x00
422#define PARAM_ARRAY_ELEMENT 0x01
423#define PARAM_STATE_ELEMENT 0x02
424#define PARAM_PROGRAM_ELEMENT 0x03
425#define PARAM_PROGRAM_ELEMENTS 0x04
426#define PARAM_CONSTANT 0x05
427
428/* param state property */
429#define STATE_MATERIAL_PARSER 0x01
430#define STATE_LIGHT_PARSER 0x02
431#define STATE_LIGHT_MODEL 0x03
432#define STATE_LIGHT_PROD 0x04
433#define STATE_FOG 0x05
434#define STATE_MATRIX_ROWS 0x06
435/* GL_ARB_fragment_program */
436#define STATE_TEX_ENV 0x07
437#define STATE_DEPTH 0x08
438/* GL_ARB_vertex_program */
439#define STATE_TEX_GEN 0x09
440#define STATE_CLIP_PLANE 0x0A
441#define STATE_POINT 0x0B
442
443/* state material property */
444#define MATERIAL_AMBIENT 0x01
445#define MATERIAL_DIFFUSE 0x02
446#define MATERIAL_SPECULAR 0x03
447#define MATERIAL_EMISSION 0x04
448#define MATERIAL_SHININESS 0x05
449
450/* state light property */
451#define LIGHT_AMBIENT 0x01
452#define LIGHT_DIFFUSE 0x02
453#define LIGHT_SPECULAR 0x03
454#define LIGHT_POSITION 0x04
455#define LIGHT_ATTENUATION 0x05
456#define LIGHT_HALF 0x06
457#define LIGHT_SPOT_DIRECTION 0x07
458
459/* state light model property */
460#define LIGHT_MODEL_AMBIENT 0x01
461#define LIGHT_MODEL_SCENECOLOR 0x02
462
463/* state light product property */
464#define LIGHT_PROD_AMBIENT 0x01
465#define LIGHT_PROD_DIFFUSE 0x02
466#define LIGHT_PROD_SPECULAR 0x03
467
468/* state texture environment property */
469#define TEX_ENV_COLOR 0x01
470
471/* state texture generation coord property */
472#define TEX_GEN_EYE 0x01
473#define TEX_GEN_OBJECT 0x02
474
475/* state fog property */
476#define FOG_COLOR 0x01
477#define FOG_PARAMS 0x02
478
479/* state depth property */
480#define DEPTH_RANGE 0x01
481
482/* state point parameters property */
483#define POINT_SIZE 0x01
484#define POINT_ATTENUATION 0x02
485
486/* declaration */
487#define ATTRIB 0x01
488#define PARAM 0x02
489#define TEMP 0x03
490#define OUTPUT 0x04
491#define ALIAS 0x05
492/* GL_ARB_vertex_program */
493#define ADDRESS 0x06
494
495/*-----------------------------------------------------------------------
496 * From here on down is the semantic checking portion
497 *
498 */
499
500/**
501 * Variable Table Handling functions
502 */
503typedef enum
504{
505 vt_none,
506 vt_address,
507 vt_attrib,
508 vt_param,
509 vt_temp,
510 vt_output,
511 vt_alias
512} var_type;
513
514
Brian Paul7aebaf32005-10-30 21:23:23 +0000515/**
516 * Setting an explicit field for each of the binding properties is a bit
517 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000518 */
519struct var_cache
520{
Brian Paul6a769d92006-04-28 15:42:15 +0000521 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000522 var_type type;
523 GLuint address_binding; /* The index of the address register we should
524 * be using */
525 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000526 GLuint attrib_is_generic; /* If the attrib was specified through a generic
527 * vertex attrib */
528 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000529 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000530 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
531 * that this is aliased to */
532 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
533 * PROGRAM_ENV_PARAM} */
534 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
535 * the tokens representing our bound state (or constants)
536 * start */
537 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
538 * we take up with our state tokens or constants. Note that
539 * this is _not_ the same as the number of param registers
540 * we eventually use */
541 struct var_cache *next;
542};
543
544static GLvoid
545var_cache_create (struct var_cache **va)
546{
547 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
548 if (*va) {
549 (**va).name = NULL;
550 (**va).type = vt_none;
551 (**va).attrib_binding = ~0;
552 (**va).attrib_is_generic = 0;
553 (**va).temp_binding = ~0;
554 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000555 (**va).param_binding_type = ~0;
556 (**va).param_binding_begin = ~0;
557 (**va).param_binding_length = ~0;
558 (**va).alias_binding = NULL;
559 (**va).next = NULL;
560 }
561}
562
563static GLvoid
564var_cache_destroy (struct var_cache **va)
565{
566 if (*va) {
567 var_cache_destroy (&(**va).next);
568 _mesa_free (*va);
569 *va = NULL;
570 }
571}
572
573static GLvoid
574var_cache_append (struct var_cache **va, struct var_cache *nv)
575{
576 if (*va)
577 var_cache_append (&(**va).next, nv);
578 else
579 *va = nv;
580}
581
582static struct var_cache *
583var_cache_find (struct var_cache *va, GLubyte * name)
584{
Brian Paul0a360cf2005-01-17 01:21:03 +0000585 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000586
587 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000588 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000589 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000590 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000591 return va;
592 }
593
594 va = va->next;
595 }
596
597 return NULL;
598}
599
600/**
601 * constructs an integer from 4 GLubytes in LE format
602 */
603static GLuint
604parse_position (GLubyte ** inst)
605{
606 GLuint value;
607
608 value = (GLuint) (*(*inst)++);
609 value += (GLuint) (*(*inst)++) * 0x100;
610 value += (GLuint) (*(*inst)++) * 0x10000;
611 value += (GLuint) (*(*inst)++) * 0x1000000;
612
613 return value;
614}
615
616/**
617 * This will, given a string, lookup the string as a variable name in the
618 * var cache. If the name is found, the var cache node corresponding to the
619 * var name is returned. If it is not found, a new entry is allocated
620 *
621 * \param I Points into the binary array where the string identifier begins
622 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
623 * \return The location on the var_cache corresponding the the string starting at I
624 */
625static struct var_cache *
626parse_string (GLubyte ** inst, struct var_cache **vc_head,
627 struct arb_program *Program, GLuint * found)
628{
629 GLubyte *i = *inst;
630 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000631 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000632
633 *inst += _mesa_strlen ((char *) i) + 1;
634
635 va = var_cache_find (*vc_head, i);
636
637 if (va) {
638 *found = 1;
639 return va;
640 }
641
642 *found = 0;
643 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000644 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000645
646 var_cache_append (vc_head, va);
647
648 return va;
649}
650
651static char *
652parse_string_without_adding (GLubyte ** inst, struct arb_program *Program)
653{
654 GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000655 (void) Program;
656
Jouk Jansen40322e12004-04-05 08:50:36 +0000657 *inst += _mesa_strlen ((char *) i) + 1;
658
659 return (char *) i;
660}
661
662/**
Brian Paul05908952004-06-08 15:20:23 +0000663 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000664 */
Brian Paul05908952004-06-08 15:20:23 +0000665static GLint
Jouk Jansen40322e12004-04-05 08:50:36 +0000666parse_sign (GLubyte ** inst)
667{
668 /*return *(*inst)++ != '+'; */
669
670 if (**inst == '-') {
671 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000672 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000673 }
674 else if (**inst == '+') {
675 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000676 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000677 }
678
Brian Paul05908952004-06-08 15:20:23 +0000679 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000680}
681
682/**
683 * parses and returns signed integer
684 */
685static GLint
686parse_integer (GLubyte ** inst, struct arb_program *Program)
687{
688 GLint sign;
689 GLint value;
690
691 /* check if *inst points to '+' or '-'
692 * if yes, grab the sign and increment *inst
693 */
694 sign = parse_sign (inst);
695
696 /* now check if *inst points to 0
697 * if yes, increment the *inst and return the default value
698 */
699 if (**inst == 0) {
700 (*inst)++;
701 return 0;
702 }
703
704 /* parse the integer as you normally would do it */
705 value = _mesa_atoi (parse_string_without_adding (inst, Program));
706
707 /* now, after terminating 0 there is a position
708 * to parse it - parse_position()
709 */
710 Program->Position = parse_position (inst);
711
Brian Paul05908952004-06-08 15:20:23 +0000712 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000713}
714
715/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000716 Accumulate this string of digits, and return them as
717 a large integer represented in floating point (for range).
718 If scale is not NULL, also accumulates a power-of-ten
719 integer scale factor that represents the number of digits
720 in the string.
721*/
722static GLdouble
723parse_float_string(GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
724{
725 GLdouble value = 0.0;
726 GLdouble oscale = 1.0;
727
728 if (**inst == 0) { /* this string of digits is empty-- do nothing */
729 (*inst)++;
730 }
731 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000732 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000733 GLubyte digit = *((*inst)++);
734 value = value * 10.0 + (GLint) (digit - '0');
735 oscale *= 10.0;
736 }
737 assert(**inst == 0); /* integer string should end with 0 */
738 (*inst)++; /* skip over terminating 0 */
739 Program->Position = parse_position(inst); /* skip position (from integer) */
740 }
741 if (scale)
742 *scale = oscale;
743 return value;
744}
745
746/**
747 Parse an unsigned floating-point number from this stream of tokenized
748 characters. Example floating-point formats supported:
749 12.34
750 12
751 0.34
752 .34
753 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000754 */
755static GLfloat
756parse_float (GLubyte ** inst, struct arb_program *Program)
757{
Brian Paul1ff8f502005-02-16 15:08:29 +0000758 GLint exponent;
759 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000760
Brian Paul1ff8f502005-02-16 15:08:29 +0000761 whole = parse_float_string(inst, Program, 0);
762 fraction = parse_float_string(inst, Program, &fracScale);
763
764 /* Parse signed exponent */
765 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000766
Brian Paul1ff8f502005-02-16 15:08:29 +0000767 /* Assemble parts of floating-point number: */
768 return (GLfloat) ((whole + fraction / fracScale) *
769 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000770}
771
772
773/**
774 */
775static GLfloat
776parse_signed_float (GLubyte ** inst, struct arb_program *Program)
777{
Brian Paul05908952004-06-08 15:20:23 +0000778 GLint sign = parse_sign (inst);
779 GLfloat value = parse_float (inst, Program);
780 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000781}
782
783/**
784 * This picks out a constant value from the parsed array. The constant vector is r
785 * returned in the *values array, which should be of length 4.
786 *
787 * \param values - The 4 component vector with the constant value in it
788 */
789static GLvoid
790parse_constant (GLubyte ** inst, GLfloat *values, struct arb_program *Program,
791 GLboolean use)
792{
793 GLuint components, i;
794
795
796 switch (*(*inst)++) {
797 case CONSTANT_SCALAR:
798 if (use == GL_TRUE) {
799 values[0] =
800 values[1] =
801 values[2] = values[3] = parse_float (inst, Program);
802 }
803 else {
804 values[0] =
805 values[1] =
806 values[2] = values[3] = parse_signed_float (inst, Program);
807 }
808
809 break;
810 case CONSTANT_VECTOR:
811 values[0] = values[1] = values[2] = 0;
812 values[3] = 1;
813 components = *(*inst)++;
814 for (i = 0; i < components; i++) {
815 values[i] = parse_signed_float (inst, Program);
816 }
817 break;
818 }
819}
820
821/**
822 * \param offset The offset from the address register that we should
823 * address
824 *
825 * \return 0 on sucess, 1 on error
826 */
827static GLuint
828parse_relative_offset (GLcontext *ctx, GLubyte **inst, struct arb_program *Program,
829 GLint *offset)
830{
Brian Paul6a769d92006-04-28 15:42:15 +0000831 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000832 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000833 return 0;
834}
835
836/**
837 * \param color 0 if color type is primary, 1 if color type is secondary
838 * \return 0 on sucess, 1 on error
839 */
840static GLuint
841parse_color_type (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
842 GLint * color)
843{
Brian Paula6c423d2004-08-25 15:59:48 +0000844 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000845 *color = *(*inst)++ != COLOR_PRIMARY;
846 return 0;
847}
848
849/**
850 * Get an integer corresponding to a generic vertex attribute.
851 *
852 * \return 0 on sucess, 1 on error
853 */
854static GLuint
855parse_generic_attrib_num(GLcontext *ctx, GLubyte ** inst,
856 struct arb_program *Program, GLuint *attrib)
857{
Brian Paulbd997cd2004-07-20 21:12:56 +0000858 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000859
Michal Krolbb38cad2006-04-11 11:41:11 +0000860 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000861 {
862 _mesa_set_program_error (ctx, Program->Position,
863 "Invalid generic vertex attribute index");
864 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid generic vertex attribute index");
865
866 return 1;
867 }
868
Brian Paulbd997cd2004-07-20 21:12:56 +0000869 *attrib = (GLuint) i;
870
Jouk Jansen40322e12004-04-05 08:50:36 +0000871 return 0;
872}
873
874
875/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000876 * \param color The index of the color buffer to write into
877 * \return 0 on sucess, 1 on error
878 */
879static GLuint
880parse_output_color_num (GLcontext * ctx, GLubyte ** inst,
881 struct arb_program *Program, GLuint * color)
882{
883 GLint i = parse_integer (inst, Program);
884
885 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
886 _mesa_set_program_error (ctx, Program->Position,
887 "Invalid draw buffer index");
888 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid draw buffer index");
889 return 1;
890 }
891
892 *color = (GLuint) i;
893 return 0;
894}
895
896
897/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000898 * \param coord The texture unit index
899 * \return 0 on sucess, 1 on error
900 */
901static GLuint
902parse_texcoord_num (GLcontext * ctx, GLubyte ** inst,
903 struct arb_program *Program, GLuint * coord)
904{
Brian Paulbd997cd2004-07-20 21:12:56 +0000905 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000906
Brian Paula6c423d2004-08-25 15:59:48 +0000907 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000908 _mesa_set_program_error (ctx, Program->Position,
909 "Invalid texture unit index");
910 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid texture unit index");
911 return 1;
912 }
913
Brian Paulbd997cd2004-07-20 21:12:56 +0000914 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000915 return 0;
916}
917
918/**
919 * \param coord The weight index
920 * \return 0 on sucess, 1 on error
921 */
922static GLuint
923parse_weight_num (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
924 GLint * coord)
925{
926 *coord = parse_integer (inst, Program);
927
928 if ((*coord < 0) || (*coord >= 1)) {
929 _mesa_set_program_error (ctx, Program->Position,
930 "Invalid weight index");
931 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid weight index");
932 return 1;
933 }
934
935 return 0;
936}
937
938/**
939 * \param coord The clip plane index
940 * \return 0 on sucess, 1 on error
941 */
942static GLuint
943parse_clipplane_num (GLcontext * ctx, GLubyte ** inst,
944 struct arb_program *Program, GLint * coord)
945{
946 *coord = parse_integer (inst, Program);
947
948 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
949 _mesa_set_program_error (ctx, Program->Position,
950 "Invalid clip plane index");
951 _mesa_error (ctx, GL_INVALID_OPERATION, "Invalid clip plane index");
952 return 1;
953 }
954
955 return 0;
956}
957
958
959/**
960 * \return 0 on front face, 1 on back face
961 */
962static GLuint
963parse_face_type (GLubyte ** inst)
964{
965 switch (*(*inst)++) {
966 case FACE_FRONT:
967 return 0;
968
969 case FACE_BACK:
970 return 1;
971 }
972 return 0;
973}
974
975
976/**
977 * Given a matrix and a modifier token on the binary array, return tokens
978 * that _mesa_fetch_state() [program.c] can understand.
979 *
980 * \param matrix - the matrix we are talking about
981 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
982 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
983 * \return 0 on sucess, 1 on failure
984 */
985static GLuint
986parse_matrix (GLcontext * ctx, GLubyte ** inst, struct arb_program *Program,
987 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
988{
989 GLubyte mat = *(*inst)++;
990
991 *matrix_idx = 0;
992
993 switch (mat) {
994 case MATRIX_MODELVIEW:
995 *matrix = STATE_MODELVIEW;
996 *matrix_idx = parse_integer (inst, Program);
997 if (*matrix_idx > 0) {
998 _mesa_set_program_error (ctx, Program->Position,
999 "ARB_vertex_blend not supported\n");
1000 _mesa_error (ctx, GL_INVALID_OPERATION,
1001 "ARB_vertex_blend not supported\n");
1002 return 1;
1003 }
1004 break;
1005
1006 case MATRIX_PROJECTION:
1007 *matrix = STATE_PROJECTION;
1008 break;
1009
1010 case MATRIX_MVP:
1011 *matrix = STATE_MVP;
1012 break;
1013
1014 case MATRIX_TEXTURE:
1015 *matrix = STATE_TEXTURE;
1016 *matrix_idx = parse_integer (inst, Program);
1017 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
1018 _mesa_set_program_error (ctx, Program->Position,
1019 "Invalid Texture Unit");
1020 _mesa_error (ctx, GL_INVALID_OPERATION,
1021 "Invalid Texture Unit: %d", *matrix_idx);
1022 return 1;
1023 }
1024 break;
1025
1026 /* This is not currently supported (ARB_matrix_palette) */
1027 case MATRIX_PALETTE:
1028 *matrix_idx = parse_integer (inst, Program);
1029 _mesa_set_program_error (ctx, Program->Position,
1030 "ARB_matrix_palette not supported\n");
1031 _mesa_error (ctx, GL_INVALID_OPERATION,
1032 "ARB_matrix_palette not supported\n");
1033 return 1;
1034 break;
1035
1036 case MATRIX_PROGRAM:
1037 *matrix = STATE_PROGRAM;
1038 *matrix_idx = parse_integer (inst, Program);
1039 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
1040 _mesa_set_program_error (ctx, Program->Position,
1041 "Invalid Program Matrix");
1042 _mesa_error (ctx, GL_INVALID_OPERATION,
1043 "Invalid Program Matrix: %d", *matrix_idx);
1044 return 1;
1045 }
1046 break;
1047 }
1048
1049 switch (*(*inst)++) {
1050 case MATRIX_MODIFIER_IDENTITY:
1051 *matrix_modifier = 0;
1052 break;
1053 case MATRIX_MODIFIER_INVERSE:
1054 *matrix_modifier = STATE_MATRIX_INVERSE;
1055 break;
1056 case MATRIX_MODIFIER_TRANSPOSE:
1057 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1058 break;
1059 case MATRIX_MODIFIER_INVTRANS:
1060 *matrix_modifier = STATE_MATRIX_INVTRANS;
1061 break;
1062 }
1063
1064 return 0;
1065}
1066
1067
1068/**
1069 * This parses a state string (rather, the binary version of it) into
1070 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1071 *
1072 * \param inst - the start in the binary arry to start working from
1073 * \param state_tokens - the storage for the 6-token state description
1074 * \return - 0 on sucess, 1 on error
1075 */
1076static GLuint
1077parse_state_single_item (GLcontext * ctx, GLubyte ** inst,
1078 struct arb_program *Program, GLint * state_tokens)
1079{
1080 switch (*(*inst)++) {
1081 case STATE_MATERIAL_PARSER:
1082 state_tokens[0] = STATE_MATERIAL;
1083 state_tokens[1] = parse_face_type (inst);
1084 switch (*(*inst)++) {
1085 case MATERIAL_AMBIENT:
1086 state_tokens[2] = STATE_AMBIENT;
1087 break;
1088 case MATERIAL_DIFFUSE:
1089 state_tokens[2] = STATE_DIFFUSE;
1090 break;
1091 case MATERIAL_SPECULAR:
1092 state_tokens[2] = STATE_SPECULAR;
1093 break;
1094 case MATERIAL_EMISSION:
1095 state_tokens[2] = STATE_EMISSION;
1096 break;
1097 case MATERIAL_SHININESS:
1098 state_tokens[2] = STATE_SHININESS;
1099 break;
1100 }
1101 break;
1102
1103 case STATE_LIGHT_PARSER:
1104 state_tokens[0] = STATE_LIGHT;
1105 state_tokens[1] = parse_integer (inst, Program);
1106
1107 /* Check the value of state_tokens[1] against the # of lights */
1108 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1109 _mesa_set_program_error (ctx, Program->Position,
1110 "Invalid Light Number");
1111 _mesa_error (ctx, GL_INVALID_OPERATION,
1112 "Invalid Light Number: %d", state_tokens[1]);
1113 return 1;
1114 }
1115
1116 switch (*(*inst)++) {
1117 case LIGHT_AMBIENT:
1118 state_tokens[2] = STATE_AMBIENT;
1119 break;
1120 case LIGHT_DIFFUSE:
1121 state_tokens[2] = STATE_DIFFUSE;
1122 break;
1123 case LIGHT_SPECULAR:
1124 state_tokens[2] = STATE_SPECULAR;
1125 break;
1126 case LIGHT_POSITION:
1127 state_tokens[2] = STATE_POSITION;
1128 break;
1129 case LIGHT_ATTENUATION:
1130 state_tokens[2] = STATE_ATTENUATION;
1131 break;
1132 case LIGHT_HALF:
1133 state_tokens[2] = STATE_HALF;
1134 break;
1135 case LIGHT_SPOT_DIRECTION:
1136 state_tokens[2] = STATE_SPOT_DIRECTION;
1137 break;
1138 }
1139 break;
1140
1141 case STATE_LIGHT_MODEL:
1142 switch (*(*inst)++) {
1143 case LIGHT_MODEL_AMBIENT:
1144 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1145 break;
1146 case LIGHT_MODEL_SCENECOLOR:
1147 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1148 state_tokens[1] = parse_face_type (inst);
1149 break;
1150 }
1151 break;
1152
1153 case STATE_LIGHT_PROD:
1154 state_tokens[0] = STATE_LIGHTPROD;
1155 state_tokens[1] = parse_integer (inst, Program);
1156
1157 /* Check the value of state_tokens[1] against the # of lights */
1158 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
1159 _mesa_set_program_error (ctx, Program->Position,
1160 "Invalid Light Number");
1161 _mesa_error (ctx, GL_INVALID_OPERATION,
1162 "Invalid Light Number: %d", state_tokens[1]);
1163 return 1;
1164 }
1165
1166 state_tokens[2] = parse_face_type (inst);
1167 switch (*(*inst)++) {
1168 case LIGHT_PROD_AMBIENT:
1169 state_tokens[3] = STATE_AMBIENT;
1170 break;
1171 case LIGHT_PROD_DIFFUSE:
1172 state_tokens[3] = STATE_DIFFUSE;
1173 break;
1174 case LIGHT_PROD_SPECULAR:
1175 state_tokens[3] = STATE_SPECULAR;
1176 break;
1177 }
1178 break;
1179
1180
1181 case STATE_FOG:
1182 switch (*(*inst)++) {
1183 case FOG_COLOR:
1184 state_tokens[0] = STATE_FOG_COLOR;
1185 break;
1186 case FOG_PARAMS:
1187 state_tokens[0] = STATE_FOG_PARAMS;
1188 break;
1189 }
1190 break;
1191
1192 case STATE_TEX_ENV:
1193 state_tokens[1] = parse_integer (inst, Program);
1194 switch (*(*inst)++) {
1195 case TEX_ENV_COLOR:
1196 state_tokens[0] = STATE_TEXENV_COLOR;
1197 break;
1198 }
1199 break;
1200
1201 case STATE_TEX_GEN:
1202 {
1203 GLuint type, coord;
1204
1205 state_tokens[0] = STATE_TEXGEN;
1206 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1207
1208 if (parse_texcoord_num (ctx, inst, Program, &coord))
1209 return 1;
1210 state_tokens[1] = coord;
1211
1212 /* EYE or OBJECT */
1213 type = *(*inst++);
1214
1215 /* 0 - s, 1 - t, 2 - r, 3 - q */
1216 coord = *(*inst++);
1217
1218 if (type == TEX_GEN_EYE) {
1219 switch (coord) {
1220 case COMPONENT_X:
1221 state_tokens[2] = STATE_TEXGEN_EYE_S;
1222 break;
1223 case COMPONENT_Y:
1224 state_tokens[2] = STATE_TEXGEN_EYE_T;
1225 break;
1226 case COMPONENT_Z:
1227 state_tokens[2] = STATE_TEXGEN_EYE_R;
1228 break;
1229 case COMPONENT_W:
1230 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1231 break;
1232 }
1233 }
1234 else {
1235 switch (coord) {
1236 case COMPONENT_X:
1237 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1238 break;
1239 case COMPONENT_Y:
1240 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1241 break;
1242 case COMPONENT_Z:
1243 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1244 break;
1245 case COMPONENT_W:
1246 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1247 break;
1248 }
1249 }
1250 }
1251 break;
1252
1253 case STATE_DEPTH:
1254 switch (*(*inst)++) {
1255 case DEPTH_RANGE:
1256 state_tokens[0] = STATE_DEPTH_RANGE;
1257 break;
1258 }
1259 break;
1260
1261 case STATE_CLIP_PLANE:
1262 state_tokens[0] = STATE_CLIPPLANE;
1263 state_tokens[1] = parse_integer (inst, Program);
1264 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1265 return 1;
1266 break;
1267
1268 case STATE_POINT:
1269 switch (*(*inst++)) {
1270 case POINT_SIZE:
1271 state_tokens[0] = STATE_POINT_SIZE;
1272 break;
1273
1274 case POINT_ATTENUATION:
1275 state_tokens[0] = STATE_POINT_ATTENUATION;
1276 break;
1277 }
1278 break;
1279
1280 /* XXX: I think this is the correct format for a matrix row */
1281 case STATE_MATRIX_ROWS:
1282 state_tokens[0] = STATE_MATRIX;
1283 if (parse_matrix
1284 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1285 &state_tokens[5]))
1286 return 1;
1287
1288 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1289
1290 if ((**inst) != 0) { /* Either the last row, 0 */
1291 state_tokens[4] = parse_integer (inst, Program);
1292 if (state_tokens[4] < state_tokens[3]) {
1293 _mesa_set_program_error (ctx, Program->Position,
1294 "Second matrix index less than the first");
1295 _mesa_error (ctx, GL_INVALID_OPERATION,
1296 "Second matrix index (%d) less than the first (%d)",
1297 state_tokens[4], state_tokens[3]);
1298 return 1;
1299 }
1300 }
1301 else {
1302 state_tokens[4] = state_tokens[3];
1303 (*inst)++;
1304 }
1305 break;
1306 }
1307
1308 return 0;
1309}
1310
1311/**
1312 * This parses a state string (rather, the binary version of it) into
1313 * a 6-token similar for the state fetching code in program.c
1314 *
1315 * One might ask, why fetch these parameters into just like you fetch
1316 * state when they are already stored in other places?
1317 *
1318 * Because of array offsets -> We can stick env/local parameters in the
1319 * middle of a parameter array and then index someplace into the array
1320 * when we execute.
1321 *
1322 * One optimization might be to only do this for the cases where the
1323 * env/local parameters end up inside of an array, and leave the
1324 * single parameters (or arrays of pure env/local pareameters) in their
1325 * respective register files.
1326 *
1327 * For ENV parameters, the format is:
1328 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1329 * state_tokens[1] = STATE_ENV
1330 * state_tokens[2] = the parameter index
1331 *
1332 * for LOCAL parameters, the format is:
1333 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1334 * state_tokens[1] = STATE_LOCAL
1335 * state_tokens[2] = the parameter index
1336 *
1337 * \param inst - the start in the binary arry to start working from
1338 * \param state_tokens - the storage for the 6-token state description
1339 * \return - 0 on sucess, 1 on failure
1340 */
1341static GLuint
1342parse_program_single_item (GLcontext * ctx, GLubyte ** inst,
1343 struct arb_program *Program, GLint * state_tokens)
1344{
1345 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1346 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1347 else
1348 state_tokens[0] = STATE_VERTEX_PROGRAM;
1349
1350
1351 switch (*(*inst)++) {
1352 case PROGRAM_PARAM_ENV:
1353 state_tokens[1] = STATE_ENV;
1354 state_tokens[2] = parse_integer (inst, Program);
1355
1356 /* Check state_tokens[2] against the number of ENV parameters available */
1357 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001358 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001359 ||
1360 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001361 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001362 _mesa_set_program_error (ctx, Program->Position,
1363 "Invalid Program Env Parameter");
1364 _mesa_error (ctx, GL_INVALID_OPERATION,
1365 "Invalid Program Env Parameter: %d",
1366 state_tokens[2]);
1367 return 1;
1368 }
1369
1370 break;
1371
1372 case PROGRAM_PARAM_LOCAL:
1373 state_tokens[1] = STATE_LOCAL;
1374 state_tokens[2] = parse_integer (inst, Program);
1375
1376 /* Check state_tokens[2] against the number of LOCAL parameters available */
1377 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001378 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001379 ||
1380 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001381 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001382 _mesa_set_program_error (ctx, Program->Position,
1383 "Invalid Program Local Parameter");
1384 _mesa_error (ctx, GL_INVALID_OPERATION,
1385 "Invalid Program Local Parameter: %d",
1386 state_tokens[2]);
1387 return 1;
1388 }
1389 break;
1390 }
1391
1392 return 0;
1393}
1394
1395/**
1396 * For ARB_vertex_program, programs are not allowed to use both an explicit
1397 * vertex attribute and a generic vertex attribute corresponding to the same
1398 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1399 *
1400 * This will walk our var_cache and make sure that nobody does anything fishy.
1401 *
1402 * \return 0 on sucess, 1 on error
1403 */
1404static GLuint
1405generic_attrib_check(struct var_cache *vc_head)
1406{
1407 int a;
1408 struct var_cache *curr;
1409 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1410 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1411
1412 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1413 explicitAttrib[a] = GL_FALSE;
1414 genericAttrib[a] = GL_FALSE;
1415 }
1416
1417 curr = vc_head;
1418 while (curr) {
1419 if (curr->type == vt_attrib) {
1420 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001421 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001422 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001423 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001424 }
1425
1426 curr = curr->next;
1427 }
1428
1429 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1430 if ((explicitAttrib[a]) && (genericAttrib[a]))
1431 return 1;
1432 }
1433
1434 return 0;
1435}
1436
1437/**
1438 * This will handle the binding side of an ATTRIB var declaration
1439 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001440 * \param inputReg returns the input register index, one of the
1441 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001442 * \return returns 0 on sucess, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001443 */
1444static GLuint
Brian Paul18e7c5c2005-10-30 21:46:00 +00001445parse_attrib_binding(GLcontext * ctx, GLubyte ** inst,
1446 struct arb_program *Program,
1447 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001448{
Jouk Jansen40322e12004-04-05 08:50:36 +00001449 GLint err = 0;
1450
1451 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001452
Jouk Jansen40322e12004-04-05 08:50:36 +00001453 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1454 switch (*(*inst)++) {
1455 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001456 {
1457 GLint coord;
1458 err = parse_color_type (ctx, inst, Program, &coord);
1459 *inputReg = FRAG_ATTRIB_COL0 + coord;
1460 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001461 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001462 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001463 {
1464 GLuint texcoord;
1465 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1466 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1467 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001468 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001469 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001470 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001471 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001473 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001474 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001475 default:
1476 err = 1;
1477 break;
1478 }
1479 }
1480 else {
1481 switch (*(*inst)++) {
1482 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001483 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001484 break;
1485
1486 case VERTEX_ATTRIB_WEIGHT:
1487 {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001488 const char *msg = "ARB_vertex_blend not supported";
Jouk Jansen40322e12004-04-05 08:50:36 +00001489 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001490 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001491 *inputReg = VERT_ATTRIB_WEIGHT;
1492 _mesa_set_program_error(ctx, Program->Position, msg);
1493 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001494 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001495 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001496
1497 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001498 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001499 break;
1500
1501 case VERTEX_ATTRIB_COLOR:
1502 {
1503 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001504 err = parse_color_type (ctx, inst, Program, &color);
1505 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001506 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001507 }
1508 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001509 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001510 }
1511 }
1512 break;
1513
1514 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001515 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001516 break;
1517
1518 case VERTEX_ATTRIB_TEXCOORD:
1519 {
1520 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001521 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001522 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001523 }
1524 break;
1525
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001527 /* Not supported at this time */
1528 {
1529 const char *msg = "ARB_palette_matrix not supported";
1530 parse_integer (inst, Program);
1531 _mesa_set_program_error (ctx, Program->Position, msg);
1532 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
1533 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001534 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001535
1536 case VERTEX_ATTRIB_GENERIC:
1537 {
1538 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001539
1540 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
1541
1542 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001543 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001544 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1545 * attributes do not alias the conventional vertex
1546 * attributes.
1547 */
1548 if (attrib > 0)
1549 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001550 else
1551 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001552 }
1553 }
1554 break;
1555
1556 default:
1557 err = 1;
1558 break;
1559 }
1560 }
1561
Jouk Jansen40322e12004-04-05 08:50:36 +00001562 if (err) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001563 const char *msg = "Bad attribute binding";
1564 _mesa_set_program_error(ctx, Program->Position, msg);
1565 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001566 }
1567
Brian Paulde997602005-11-12 17:53:14 +00001568 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001569
1570 return err;
1571}
1572
Brian Paul7aebaf32005-10-30 21:23:23 +00001573
Jouk Jansen40322e12004-04-05 08:50:36 +00001574/**
1575 * This translates between a binary token for an output variable type
1576 * and the mesa token for the same thing.
1577 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001578 * \param inst The parsed tokens
1579 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001580 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001581 */
1582static GLuint
Brian Paul7aebaf32005-10-30 21:23:23 +00001583parse_result_binding(GLcontext *ctx, GLubyte **inst,
1584 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001585{
Brian Paul7aebaf32005-10-30 21:23:23 +00001586 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001587
Brian Paul7aebaf32005-10-30 21:23:23 +00001588 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001589 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001590 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001591 GLuint out_color;
1592
Brian Paulbe76b7f2004-10-04 14:40:05 +00001593 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001594 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001595 */
1596 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001597 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001598 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001599 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001600 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001601 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1602 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001603 }
1604 break;
1605
1606 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001608 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001609 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001610 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001611 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001612 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001613 GLint color_type;
1614 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001615 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1616 if (err)
1617 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001618
Jouk Jansen40322e12004-04-05 08:50:36 +00001619 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001620 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001622 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001623 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001625 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 }
1627 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001629 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001630 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001631 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001632 }
1633 /* primary color */
1634 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001635 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001636 }
1637 }
1638 }
1639 break;
1640
1641 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001642 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001643 break;
1644
1645 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001646 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001647 break;
1648
1649 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001650 {
1651 GLuint unit;
1652 if (parse_texcoord_num (ctx, inst, Program, &unit))
1653 return 1;
1654 *outputReg = VERT_RESULT_TEX0 + unit;
1655 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001656 break;
1657 }
1658
Brian Paulde997602005-11-12 17:53:14 +00001659 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001660
1661 return 0;
1662}
1663
Brian Paul7aebaf32005-10-30 21:23:23 +00001664
Jouk Jansen40322e12004-04-05 08:50:36 +00001665/**
1666 * This handles the declaration of ATTRIB variables
1667 *
1668 * XXX: Still needs
1669 * parse_vert_attrib_binding(), or something like that
1670 *
1671 * \return 0 on sucess, 1 on error
1672 */
1673static GLint
1674parse_attrib (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1675 struct arb_program *Program)
1676{
1677 GLuint found;
1678 char *error_msg;
1679 struct var_cache *attrib_var;
1680
1681 attrib_var = parse_string (inst, vc_head, Program, &found);
1682 Program->Position = parse_position (inst);
1683 if (found) {
1684 error_msg = (char *)
1685 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1686 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1687 attrib_var->name);
1688
1689 _mesa_set_program_error (ctx, Program->Position, error_msg);
1690 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1691
1692 _mesa_free (error_msg);
1693 return 1;
1694 }
1695
1696 attrib_var->type = vt_attrib;
1697
Brian Paul7aebaf32005-10-30 21:23:23 +00001698 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001699 &attrib_var->attrib_is_generic))
1700 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001701
Brian Paul7aebaf32005-10-30 21:23:23 +00001702 if (generic_attrib_check(*vc_head)) {
1703 _mesa_set_program_error(ctx, Program->Position,
1704 "Cannot use both a generic vertex attribute "
1705 "and a specific attribute of the same type");
1706 _mesa_error(ctx, GL_INVALID_OPERATION,
1707 "Cannot use both a generic vertex attribute and a specific "
1708 "attribute of the same type");
1709 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001710 }
1711
1712 Program->Base.NumAttributes++;
1713 return 0;
1714}
1715
1716/**
1717 * \param use -- TRUE if we're called when declaring implicit parameters,
1718 * FALSE if we're declaraing variables. This has to do with
1719 * if we get a signed or unsigned float for scalar constants
1720 */
1721static GLuint
1722parse_param_elements (GLcontext * ctx, GLubyte ** inst,
1723 struct var_cache *param_var,
1724 struct arb_program *Program, GLboolean use)
1725{
1726 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001727 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001728 GLint state_tokens[6];
1729 GLfloat const_values[4];
1730
Jouk Jansen40322e12004-04-05 08:50:36 +00001731 switch (*(*inst)++) {
1732 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001733 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1734 return 1;
1735
1736 /* If we adding STATE_MATRIX that has multiple rows, we need to
1737 * unroll it and call _mesa_add_state_reference() for each row
1738 */
1739 if ((state_tokens[0] == STATE_MATRIX)
1740 && (state_tokens[3] != state_tokens[4])) {
1741 GLint row;
1742 GLint first_row = state_tokens[3];
1743 GLint last_row = state_tokens[4];
1744
1745 for (row = first_row; row <= last_row; row++) {
1746 state_tokens[3] = state_tokens[4] = row;
1747
Brian Paulde997602005-11-12 17:53:14 +00001748 idx = _mesa_add_state_reference(Program->Base.Parameters,
1749 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001750 if (param_var->param_binding_begin == ~0U)
1751 param_var->param_binding_begin = idx;
1752 param_var->param_binding_length++;
1753 Program->Base.NumParameters++;
1754 }
1755 }
1756 else {
Brian Paulde997602005-11-12 17:53:14 +00001757 idx = _mesa_add_state_reference(Program->Base.Parameters,
1758 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001759 if (param_var->param_binding_begin == ~0U)
1760 param_var->param_binding_begin = idx;
1761 param_var->param_binding_length++;
1762 Program->Base.NumParameters++;
1763 }
1764 break;
1765
1766 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001767 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1768 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001769 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001770 if (param_var->param_binding_begin == ~0U)
1771 param_var->param_binding_begin = idx;
1772 param_var->param_binding_length++;
1773 Program->Base.NumParameters++;
1774
1775 /* Check if there is more: 0 -> we're done, else its an integer */
1776 if (**inst) {
1777 GLuint out_of_range, new_idx;
1778 GLuint start_idx = state_tokens[2] + 1;
1779 GLuint end_idx = parse_integer (inst, Program);
1780
1781 out_of_range = 0;
1782 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1783 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001784 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001785 || ((state_tokens[1] == STATE_LOCAL)
1786 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001787 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001788 out_of_range = 1;
1789 }
1790 else {
1791 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001792 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001793 || ((state_tokens[1] == STATE_LOCAL)
1794 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001795 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001796 out_of_range = 1;
1797 }
1798 if (out_of_range) {
1799 _mesa_set_program_error (ctx, Program->Position,
1800 "Invalid Program Parameter");
1801 _mesa_error (ctx, GL_INVALID_OPERATION,
1802 "Invalid Program Parameter: %d", end_idx);
1803 return 1;
1804 }
1805
1806 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1807 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001808 idx = _mesa_add_state_reference(Program->Base.Parameters,
1809 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001810 param_var->param_binding_length++;
1811 Program->Base.NumParameters++;
1812 }
1813 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001814 else {
1815 (*inst)++;
1816 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001817 break;
1818
1819 case PARAM_CONSTANT:
1820 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001821 idx = _mesa_add_named_constant(Program->Base.Parameters,
1822 (char *) param_var->name,
1823 const_values);
Jouk Jansen40322e12004-04-05 08:50:36 +00001824 if (param_var->param_binding_begin == ~0U)
1825 param_var->param_binding_begin = idx;
1826 param_var->param_binding_length++;
1827 Program->Base.NumParameters++;
1828 break;
1829
1830 default:
Brian Paul7aebaf32005-10-30 21:23:23 +00001831 _mesa_set_program_error(ctx, Program->Position,
1832 "Unexpected token in parse_param_elements()");
1833 _mesa_error(ctx, GL_INVALID_OPERATION,
1834 "Unexpected token in parse_param_elements()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001835 return 1;
1836 }
1837
1838 /* Make sure we haven't blown past our parameter limits */
1839 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1840 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001841 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001842 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1843 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001844 ctx->Const.FragmentProgram.MaxLocalParams))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001845 _mesa_set_program_error (ctx, Program->Position,
1846 "Too many parameter variables");
1847 _mesa_error (ctx, GL_INVALID_OPERATION, "Too many parameter variables");
1848 return 1;
1849 }
1850
1851 return err;
1852}
1853
Brian Paul7aebaf32005-10-30 21:23:23 +00001854
Jouk Jansen40322e12004-04-05 08:50:36 +00001855/**
1856 * This picks out PARAM program parameter bindings.
1857 *
1858 * XXX: This needs to be stressed & tested
1859 *
1860 * \return 0 on sucess, 1 on error
1861 */
1862static GLuint
1863parse_param (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1864 struct arb_program *Program)
1865{
Brian Paulbd997cd2004-07-20 21:12:56 +00001866 GLuint found, err;
1867 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001868 struct var_cache *param_var;
1869
1870 err = 0;
1871 param_var = parse_string (inst, vc_head, Program, &found);
1872 Program->Position = parse_position (inst);
1873
1874 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001875 char *error_msg = (char *)
1876 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001877 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1878 param_var->name);
1879
1880 _mesa_set_program_error (ctx, Program->Position, error_msg);
1881 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1882
1883 _mesa_free (error_msg);
1884 return 1;
1885 }
1886
1887 specified_length = parse_integer (inst, Program);
1888
1889 if (specified_length < 0) {
1890 _mesa_set_program_error (ctx, Program->Position,
1891 "Negative parameter array length");
1892 _mesa_error (ctx, GL_INVALID_OPERATION,
1893 "Negative parameter array length: %d", specified_length);
1894 return 1;
1895 }
1896
1897 param_var->type = vt_param;
1898 param_var->param_binding_length = 0;
1899
1900 /* Right now, everything is shoved into the main state register file.
1901 *
1902 * In the future, it would be nice to leave things ENV/LOCAL params
1903 * in their respective register files, if possible
1904 */
1905 param_var->param_binding_type = PROGRAM_STATE_VAR;
1906
1907 /* Remember to:
1908 * * - add each guy to the parameter list
1909 * * - increment the param_var->param_binding_len
1910 * * - store the param_var->param_binding_begin for the first one
1911 * * - compare the actual len to the specified len at the end
1912 */
1913 while (**inst != PARAM_NULL) {
1914 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1915 return 1;
1916 }
1917
1918 /* Test array length here! */
1919 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001920 if (specified_length != (int)param_var->param_binding_length) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001921 const char *msg
1922 = "Declared parameter array length does not match parameter list";
1923 _mesa_set_program_error(ctx, Program->Position, msg);
1924 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001925 }
1926 }
1927
1928 (*inst)++;
1929
1930 return 0;
1931}
1932
1933/**
1934 *
1935 */
1936static GLuint
1937parse_param_use (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1938 struct arb_program *Program, struct var_cache **new_var)
1939{
1940 struct var_cache *param_var;
1941
1942 /* First, insert a dummy entry into the var_cache */
1943 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001944 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001945 param_var->type = vt_param;
1946
1947 param_var->param_binding_length = 0;
1948 /* Don't fill in binding_begin; We use the default value of -1
1949 * to tell if its already initialized, elsewhere.
1950 *
1951 * param_var->param_binding_begin = 0;
1952 */
1953 param_var->param_binding_type = PROGRAM_STATE_VAR;
1954
1955 var_cache_append (vc_head, param_var);
1956
1957 /* Then fill it with juicy parameter goodness */
1958 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1959 return 1;
1960
1961 *new_var = param_var;
1962
1963 return 0;
1964}
1965
1966
1967/**
1968 * This handles the declaration of TEMP variables
1969 *
1970 * \return 0 on sucess, 1 on error
1971 */
1972static GLuint
1973parse_temp (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
1974 struct arb_program *Program)
1975{
1976 GLuint found;
1977 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001978
1979 while (**inst != 0) {
1980 temp_var = parse_string (inst, vc_head, Program, &found);
1981 Program->Position = parse_position (inst);
1982 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001983 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001984 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1985 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1986 temp_var->name);
1987
1988 _mesa_set_program_error (ctx, Program->Position, error_msg);
1989 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
1990
1991 _mesa_free (error_msg);
1992 return 1;
1993 }
1994
1995 temp_var->type = vt_temp;
1996
1997 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1998 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001999 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002000 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2001 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002002 ctx->Const.VertexProgram.MaxTemps))) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002003 _mesa_set_program_error (ctx, Program->Position,
2004 "Too many TEMP variables declared");
2005 _mesa_error (ctx, GL_INVALID_OPERATION,
2006 "Too many TEMP variables declared");
2007 return 1;
2008 }
2009
2010 temp_var->temp_binding = Program->Base.NumTemporaries;
2011 Program->Base.NumTemporaries++;
2012 }
2013 (*inst)++;
2014
2015 return 0;
2016}
2017
2018/**
2019 * This handles variables of the OUTPUT variety
2020 *
2021 * \return 0 on sucess, 1 on error
2022 */
2023static GLuint
2024parse_output (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2025 struct arb_program *Program)
2026{
2027 GLuint found;
2028 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002029 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002030
2031 output_var = parse_string (inst, vc_head, Program, &found);
2032 Program->Position = parse_position (inst);
2033 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002034 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002035 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2036 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2037 output_var->name);
2038
2039 _mesa_set_program_error (ctx, Program->Position, error_msg);
2040 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2041
2042 _mesa_free (error_msg);
2043 return 1;
2044 }
2045
2046 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002047
2048 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2049 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002050}
2051
2052/**
2053 * This handles variables of the ALIAS kind
2054 *
2055 * \return 0 on sucess, 1 on error
2056 */
2057static GLuint
2058parse_alias (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2059 struct arb_program *Program)
2060{
2061 GLuint found;
2062 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002063
2064 temp_var = parse_string (inst, vc_head, Program, &found);
2065 Program->Position = parse_position (inst);
2066
2067 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002068 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002069 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2070 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2071 temp_var->name);
2072
2073 _mesa_set_program_error (ctx, Program->Position, error_msg);
2074 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2075
2076 _mesa_free (error_msg);
2077 return 1;
2078 }
2079
2080 temp_var->type = vt_alias;
2081 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2082 Program->Position = parse_position (inst);
2083
2084 if (!found)
2085 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002086 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002087 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2088 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2089 temp_var->alias_binding->name);
2090
2091 _mesa_set_program_error (ctx, Program->Position, error_msg);
2092 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2093
2094 _mesa_free (error_msg);
2095 return 1;
2096 }
2097
2098 return 0;
2099}
2100
2101/**
2102 * This handles variables of the ADDRESS kind
2103 *
2104 * \return 0 on sucess, 1 on error
2105 */
2106static GLuint
2107parse_address (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2108 struct arb_program *Program)
2109{
2110 GLuint found;
2111 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002112
2113 while (**inst != 0) {
2114 temp_var = parse_string (inst, vc_head, Program, &found);
2115 Program->Position = parse_position (inst);
2116 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002117 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002118 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2119 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2120 temp_var->name);
2121
2122 _mesa_set_program_error (ctx, Program->Position, error_msg);
2123 _mesa_error (ctx, GL_INVALID_OPERATION, error_msg);
2124
2125 _mesa_free (error_msg);
2126 return 1;
2127 }
2128
2129 temp_var->type = vt_address;
2130
2131 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002132 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002133 const char *msg = "Too many ADDRESS variables declared";
2134 _mesa_set_program_error(ctx, Program->Position, msg);
2135
2136 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002137 return 1;
2138 }
2139
2140 temp_var->address_binding = Program->Base.NumAddressRegs;
2141 Program->Base.NumAddressRegs++;
2142 }
2143 (*inst)++;
2144
2145 return 0;
2146}
2147
2148/**
2149 * Parse a program declaration
2150 *
2151 * \return 0 on sucess, 1 on error
2152 */
2153static GLint
2154parse_declaration (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
2155 struct arb_program *Program)
2156{
2157 GLint err = 0;
2158
2159 switch (*(*inst)++) {
2160 case ADDRESS:
2161 err = parse_address (ctx, inst, vc_head, Program);
2162 break;
2163
2164 case ALIAS:
2165 err = parse_alias (ctx, inst, vc_head, Program);
2166 break;
2167
2168 case ATTRIB:
2169 err = parse_attrib (ctx, inst, vc_head, Program);
2170 break;
2171
2172 case OUTPUT:
2173 err = parse_output (ctx, inst, vc_head, Program);
2174 break;
2175
2176 case PARAM:
2177 err = parse_param (ctx, inst, vc_head, Program);
2178 break;
2179
2180 case TEMP:
2181 err = parse_temp (ctx, inst, vc_head, Program);
2182 break;
2183 }
2184
2185 return err;
2186}
2187
2188/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002189 * Handle the parsing out of a masked destination register, either for a
2190 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002191 *
2192 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002193 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002194 * position invariant
2195 *
2196 * \param File - The register file we write to
2197 * \param Index - The register index we write to
2198 * \param WriteMask - The mask controlling which components we write (1->write)
2199 *
2200 * \return 0 on sucess, 1 on error
2201 */
2202static GLuint
2203parse_masked_dst_reg (GLcontext * ctx, GLubyte ** inst,
2204 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002205 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002206{
Brian Paul7aebaf32005-10-30 21:23:23 +00002207 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002208 struct var_cache *dst;
2209
2210 /* We either have a result register specified, or a
2211 * variable that may or may not be writable
2212 */
2213 switch (*(*inst)++) {
2214 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002215 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002216 return 1;
2217 *File = PROGRAM_OUTPUT;
2218 break;
2219
2220 case REGISTER_ESTABLISHED_NAME:
2221 dst = parse_string (inst, vc_head, Program, &result);
2222 Program->Position = parse_position (inst);
2223
2224 /* If the name has never been added to our symbol table, we're hosed */
2225 if (!result) {
2226 _mesa_set_program_error (ctx, Program->Position,
2227 "0: Undefined variable");
2228 _mesa_error (ctx, GL_INVALID_OPERATION, "0: Undefined variable: %s",
2229 dst->name);
2230 return 1;
2231 }
2232
2233 switch (dst->type) {
2234 case vt_output:
2235 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002236 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002237 break;
2238
2239 case vt_temp:
2240 *File = PROGRAM_TEMPORARY;
2241 *Index = dst->temp_binding;
2242 break;
2243
2244 /* If the var type is not vt_output or vt_temp, no go */
2245 default:
2246 _mesa_set_program_error (ctx, Program->Position,
2247 "Destination register is read only");
2248 _mesa_error (ctx, GL_INVALID_OPERATION,
2249 "Destination register is read only: %s",
2250 dst->name);
2251 return 1;
2252 }
2253 break;
2254
2255 default:
2256 _mesa_set_program_error (ctx, Program->Position,
2257 "Unexpected opcode in parse_masked_dst_reg()");
2258 _mesa_error (ctx, GL_INVALID_OPERATION,
2259 "Unexpected opcode in parse_masked_dst_reg()");
2260 return 1;
2261 }
2262
2263
2264 /* Position invariance test */
2265 /* This test is done now in syntax portion - when position invariance OPTION
2266 is specified, "result.position" rule is disabled so there is no way
2267 to write the position
2268 */
2269 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2270 (*Index == 0)) {
2271 _mesa_set_program_error (ctx, Program->Position,
2272 "Vertex program specified position invariance and wrote vertex position");
2273 _mesa_error (ctx, GL_INVALID_OPERATION,
2274 "Vertex program specified position invariance and wrote vertex position");
2275 }*/
2276
2277 /* And then the mask.
2278 * w,a -> bit 0
2279 * z,b -> bit 1
2280 * y,g -> bit 2
2281 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002282 *
2283 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002284 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002285 tmp = (GLint) *(*inst)++;
2286 *WriteMask = (((tmp>>3) & 0x1) |
2287 ((tmp>>1) & 0x2) |
2288 ((tmp<<1) & 0x4) |
2289 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002290
2291 return 0;
2292}
2293
2294
2295/**
2296 * Handle the parsing of a address register
2297 *
2298 * \param Index - The register index we write to
2299 *
2300 * \return 0 on sucess, 1 on error
2301 */
2302static GLuint
2303parse_address_reg (GLcontext * ctx, GLubyte ** inst,
2304 struct var_cache **vc_head,
2305 struct arb_program *Program, GLint * Index)
2306{
2307 struct var_cache *dst;
2308 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002309
2310 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002311
2312 dst = parse_string (inst, vc_head, Program, &result);
2313 Program->Position = parse_position (inst);
2314
2315 /* If the name has never been added to our symbol table, we're hosed */
2316 if (!result) {
2317 _mesa_set_program_error (ctx, Program->Position, "Undefined variable");
2318 _mesa_error (ctx, GL_INVALID_OPERATION, "Undefined variable: %s",
2319 dst->name);
2320 return 1;
2321 }
2322
2323 if (dst->type != vt_address) {
2324 _mesa_set_program_error (ctx, Program->Position,
2325 "Variable is not of type ADDRESS");
2326 _mesa_error (ctx, GL_INVALID_OPERATION,
2327 "Variable: %s is not of type ADDRESS", dst->name);
2328 return 1;
2329 }
2330
2331 return 0;
2332}
2333
Brian Paul8e8fa632005-07-01 02:03:33 +00002334#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002335/**
2336 * Handle the parsing out of a masked address register
2337 *
2338 * \param Index - The register index we write to
2339 * \param WriteMask - The mask controlling which components we write (1->write)
2340 *
2341 * \return 0 on sucess, 1 on error
2342 */
2343static GLuint
2344parse_masked_address_reg (GLcontext * ctx, GLubyte ** inst,
2345 struct var_cache **vc_head,
2346 struct arb_program *Program, GLint * Index,
2347 GLboolean * WriteMask)
2348{
2349 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2350 return 1;
2351
2352 /* This should be 0x8 */
2353 (*inst)++;
2354
2355 /* Writemask of .x is implied */
2356 WriteMask[0] = 1;
2357 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2358
2359 return 0;
2360}
Brian Paul8e8fa632005-07-01 02:03:33 +00002361#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002362
2363/**
2364 * Parse out a swizzle mask.
2365 *
Brian Paul32df89e2005-10-29 18:26:43 +00002366 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002367 *
2368 * The len parameter allows us to grab 4 components for a vector
2369 * swizzle, or just 1 component for a scalar src register selection
2370 */
Brian Paul32df89e2005-10-29 18:26:43 +00002371static void
2372parse_swizzle_mask(GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002373{
Brian Paul32df89e2005-10-29 18:26:43 +00002374 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002375
Brian Paul32df89e2005-10-29 18:26:43 +00002376 for (i = 0; i < 4; i++)
2377 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002378
Brian Paul32df89e2005-10-29 18:26:43 +00002379 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002380 switch (*(*inst)++) {
2381 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002382 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002383 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002384 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002385 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002386 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002388 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002389 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002390 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002391 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002392 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002393 default:
2394 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2395 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002396 }
2397 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002398}
2399
Jouk Jansen40322e12004-04-05 08:50:36 +00002400
Brian Paul32df89e2005-10-29 18:26:43 +00002401/**
2402 * Parse an extended swizzle mask which is a sequence of
2403 * four x/y/z/w/0/1 tokens.
2404 * \return swizzle four swizzle values
2405 * \return negateMask four element bitfield
2406 */
2407static void
2408parse_extended_swizzle_mask(GLubyte **inst, GLubyte swizzle[4],
2409 GLubyte *negateMask)
2410{
2411 GLint i;
2412
2413 *negateMask = 0x0;
2414 for (i = 0; i < 4; i++) {
2415 GLubyte swz;
2416 if (parse_sign(inst) == -1)
2417 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002418
2419 swz = *(*inst)++;
2420
2421 switch (swz) {
2422 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002423 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002424 break;
2425 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002426 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002427 break;
2428 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002429 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002430 break;
2431 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002432 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002433 break;
2434 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002435 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002436 break;
2437 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002438 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002439 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002440 default:
2441 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2442 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002443 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002444 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002445}
2446
2447
2448static GLuint
2449parse_src_reg (GLcontext * ctx, GLubyte ** inst, struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002450 struct arb_program *Program,
2451 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002452 GLboolean *IsRelOffset )
2453{
2454 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002455 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002456 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002457
Keith Whitwell7c26b612005-04-21 14:46:57 +00002458 *IsRelOffset = 0;
2459
Jouk Jansen40322e12004-04-05 08:50:36 +00002460 /* And the binding for the src */
2461 switch (*(*inst)++) {
2462 case REGISTER_ATTRIB:
2463 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002464 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002465 return 1;
2466 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002467 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002468
2469 /* We need to insert a dummy variable into the var_cache so we can
2470 * catch generic vertex attrib aliasing errors
2471 */
2472 var_cache_create(&src);
2473 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002474 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002475 src->attrib_binding = binding;
2476 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002477 var_cache_append(vc_head, src);
2478 if (generic_attrib_check(*vc_head)) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002479 const char *msg = "Cannot use both a generic vertex attribute "
2480 "and a specific attribute of the same type";
2481 _mesa_set_program_error (ctx, Program->Position, msg);
2482 _mesa_error (ctx, GL_INVALID_OPERATION, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002483 return 1;
2484 }
2485 break;
2486
2487 case REGISTER_PARAM:
2488 switch (**inst) {
2489 case PARAM_ARRAY_ELEMENT:
2490 (*inst)++;
2491 src = parse_string (inst, vc_head, Program, &found);
2492 Program->Position = parse_position (inst);
2493
2494 if (!found) {
2495 _mesa_set_program_error (ctx, Program->Position,
2496 "2: Undefined variable");
2497 _mesa_error (ctx, GL_INVALID_OPERATION,
2498 "2: Undefined variable: %s", src->name);
2499 return 1;
2500 }
2501
Brian Paul95801792005-12-06 15:41:43 +00002502 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002503
2504 switch (*(*inst)++) {
2505 case ARRAY_INDEX_ABSOLUTE:
2506 offset = parse_integer (inst, Program);
2507
2508 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002509 || (offset >= (int)src->param_binding_length)) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002510 _mesa_set_program_error (ctx, Program->Position,
2511 "Index out of range");
2512 _mesa_error (ctx, GL_INVALID_OPERATION,
2513 "Index %d out of range for %s", offset,
2514 src->name);
2515 return 1;
2516 }
2517
2518 *Index = src->param_binding_begin + offset;
2519 break;
2520
2521 case ARRAY_INDEX_RELATIVE:
2522 {
2523 GLint addr_reg_idx, rel_off;
2524
2525 /* First, grab the address regiseter */
2526 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2527 return 1;
2528
2529 /* And the .x */
2530 ((*inst)++);
2531 ((*inst)++);
2532 ((*inst)++);
2533 ((*inst)++);
2534
2535 /* Then the relative offset */
2536 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2537
2538 /* And store it properly */
2539 *Index = src->param_binding_begin + rel_off;
2540 *IsRelOffset = 1;
2541 }
2542 break;
2543 }
2544 break;
2545
2546 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002547 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2548 return 1;
2549
Brian Paul95801792005-12-06 15:41:43 +00002550 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002551 *Index = src->param_binding_begin;
2552 break;
2553 }
2554 break;
2555
2556 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002557 src = parse_string (inst, vc_head, Program, &found);
2558 Program->Position = parse_position (inst);
2559
2560 /* If the name has never been added to our symbol table, we're hosed */
2561 if (!found) {
2562 _mesa_set_program_error (ctx, Program->Position,
2563 "3: Undefined variable");
2564 _mesa_error (ctx, GL_INVALID_OPERATION, "3: Undefined variable: %s",
2565 src->name);
2566 return 1;
2567 }
2568
2569 switch (src->type) {
2570 case vt_attrib:
2571 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002572 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002573 break;
2574
2575 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2576 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002577 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002578 *Index = src->param_binding_begin;
2579 break;
2580
2581 case vt_temp:
2582 *File = PROGRAM_TEMPORARY;
2583 *Index = src->temp_binding;
2584 break;
2585
2586 /* If the var type is vt_output no go */
2587 default:
2588 _mesa_set_program_error (ctx, Program->Position,
2589 "destination register is read only");
2590 _mesa_error (ctx, GL_INVALID_OPERATION,
2591 "destination register is read only: %s",
2592 src->name);
2593 return 1;
2594 }
2595 break;
2596
2597 default:
2598 _mesa_set_program_error (ctx, Program->Position,
2599 "Unknown token in parse_src_reg");
2600 _mesa_error (ctx, GL_INVALID_OPERATION,
2601 "Unknown token in parse_src_reg");
2602 return 1;
2603 }
2604
2605 return 0;
2606}
2607
2608/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002609 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002610 */
2611static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00002612parse_fp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
2613 struct var_cache **vc_head,
2614 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002615 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002616{
Brian Paul7aebaf32005-10-30 21:23:23 +00002617 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002618 GLint index;
2619 GLboolean negate;
2620 GLubyte swizzle[4];
2621 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002622
Jouk Jansen40322e12004-04-05 08:50:36 +00002623 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002624 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002625
2626 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002627 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002628 return 1;
2629
2630 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002631 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002632
Brian Paul32df89e2005-10-29 18:26:43 +00002633 reg->File = file;
2634 reg->Index = index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002635 reg->Abs = 0; /* NV only */
2636 reg->NegateAbs = 0; /* NV only */
Brian Paul32df89e2005-10-29 18:26:43 +00002637 reg->NegateBase = negate;
2638 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002639 return 0;
2640}
2641
Jouk Jansen40322e12004-04-05 08:50:36 +00002642
Keith Whitwell7c26b612005-04-21 14:46:57 +00002643static GLuint
2644parse_fp_dst_reg(GLcontext * ctx, GLubyte ** inst,
2645 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002646 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002647{
Brian Paul7aebaf32005-10-30 21:23:23 +00002648 GLint mask;
2649 GLuint idx;
2650 enum register_file file;
2651
Keith Whitwell7c26b612005-04-21 14:46:57 +00002652 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002653 return 1;
2654
Keith Whitwell7c26b612005-04-21 14:46:57 +00002655 reg->CondMask = 0; /* NV only */
2656 reg->CondSwizzle = 0; /* NV only */
2657 reg->File = file;
2658 reg->Index = idx;
2659 reg->WriteMask = mask;
2660 return 0;
2661}
2662
2663
Brian Paul7aebaf32005-10-30 21:23:23 +00002664/**
2665 * Parse fragment program scalar src register.
2666 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002667static GLuint
2668parse_fp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002669 struct var_cache **vc_head,
2670 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002671 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002672{
Brian Paul7aebaf32005-10-30 21:23:23 +00002673 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002674 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002675 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002676 GLubyte Swizzle[4];
2677 GLboolean IsRelOffset;
2678
2679 /* Grab the sign */
2680 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2681
2682 /* And the src reg */
2683 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2684 return 1;
2685
2686 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002687 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002688
Keith Whitwell7c26b612005-04-21 14:46:57 +00002689 reg->File = File;
2690 reg->Index = Index;
2691 reg->Abs = 0; /* NV only */
2692 reg->NegateAbs = 0; /* NV only */
2693 reg->NegateBase = Negate;
2694 reg->Swizzle = (Swizzle[0] << 0);
2695
Jouk Jansen40322e12004-04-05 08:50:36 +00002696 return 0;
2697}
2698
Keith Whitwell7c26b612005-04-21 14:46:57 +00002699
Jouk Jansen40322e12004-04-05 08:50:36 +00002700/**
2701 * This is a big mother that handles getting opcodes into the instruction
2702 * and handling the src & dst registers for fragment program instructions
2703 */
2704static GLuint
2705parse_fp_instruction (GLcontext * ctx, GLubyte ** inst,
2706 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002707 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002708{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002709 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002710 GLuint texcoord;
2711 GLubyte instClass, type, code;
2712 GLboolean rel;
2713
Brian Paul7e807512005-11-05 17:10:45 +00002714 _mesa_init_instruction(fp);
Jouk Jansen40322e12004-04-05 08:50:36 +00002715
2716 /* Record the position in the program string for debugging */
2717 fp->StringPos = Program->Position;
2718
2719 /* OP_ALU_INST or OP_TEX_INST */
2720 instClass = *(*inst)++;
2721
2722 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2723 * OP_TEX_{SAMPLE, KIL}
2724 */
2725 type = *(*inst)++;
2726
2727 /* The actual opcode name */
2728 code = *(*inst)++;
2729
2730 /* Increment the correct count */
2731 switch (instClass) {
2732 case OP_ALU_INST:
2733 Program->NumAluInstructions++;
2734 break;
2735 case OP_TEX_INST:
2736 Program->NumTexInstructions++;
2737 break;
2738 }
2739
Jouk Jansen40322e12004-04-05 08:50:36 +00002740 switch (type) {
2741 case OP_ALU_VECTOR:
2742 switch (code) {
2743 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002744 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002745 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002746 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 break;
2748
2749 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002750 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002752 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002753 break;
2754
2755 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002756 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002757 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002758 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002759 break;
2760
2761 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002762 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002763 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002764 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002765 break;
2766
2767 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002768 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002769 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002770 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002771 break;
2772 }
2773
Keith Whitwell7c26b612005-04-21 14:46:57 +00002774 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002775 return 1;
2776
Keith Whitwell7c26b612005-04-21 14:46:57 +00002777 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002778 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002779 break;
2780
2781 case OP_ALU_SCALAR:
2782 switch (code) {
2783 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002784 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002786 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002787 break;
2788
2789 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002790 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002792 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 break;
2794
2795 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002796 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002798 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002799 break;
2800
2801 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002802 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002803 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002804 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002805 break;
2806
2807 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002808 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002809 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002810 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002811 break;
2812
2813 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002814 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002815 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002816 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002817 break;
2818
2819 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002820 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002821 case OP_SCS:
2822
Brian Paul7e807512005-11-05 17:10:45 +00002823 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002824 break;
2825 }
2826
Keith Whitwell7c26b612005-04-21 14:46:57 +00002827 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002828 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002829
2830 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002832 break;
2833
2834 case OP_ALU_BINSC:
2835 switch (code) {
2836 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002837 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002838 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002839 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002840 break;
2841 }
2842
Keith Whitwell7c26b612005-04-21 14:46:57 +00002843 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002844 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002845
Jouk Jansen40322e12004-04-05 08:50:36 +00002846 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002847 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002848 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002849 }
2850 break;
2851
2852
2853 case OP_ALU_BIN:
2854 switch (code) {
2855 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002856 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002857 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002858 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002859 break;
2860
2861 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002862 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002864 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002865 break;
2866
2867 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002868 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002869 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002870 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002871 break;
2872
2873 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002874 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002875 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002876 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002877 break;
2878
2879 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002880 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002881 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002882 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002883 break;
2884
2885 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002886 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002887 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002888 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002889 break;
2890
2891 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002892 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002893 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002894 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002895 break;
2896
2897 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002898 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002899 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002900 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002901 break;
2902
2903 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002904 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002905 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002906 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002907 break;
2908
2909 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002910 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002911 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002912 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002913 break;
2914
2915 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002916 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002917 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002918 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002919 break;
2920
2921 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002922 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002923 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002924 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002925 break;
2926 }
2927
Keith Whitwell7c26b612005-04-21 14:46:57 +00002928 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002929 return 1;
2930 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002931 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2932 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002933 }
2934 break;
2935
2936 case OP_ALU_TRI:
2937 switch (code) {
2938 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002939 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002940 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002941 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002942 break;
2943
2944 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002945 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002946 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002947 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002948 break;
2949
2950 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002951 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002952 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002953 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002954 break;
2955 }
2956
Keith Whitwell7c26b612005-04-21 14:46:57 +00002957 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002958 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002959
Jouk Jansen40322e12004-04-05 08:50:36 +00002960 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002961 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2962 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002963 }
2964 break;
2965
2966 case OP_ALU_SWZ:
2967 switch (code) {
2968 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002969 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002970 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002971 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002972 break;
2973 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002974 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002975 return 1;
2976
Keith Whitwell7c26b612005-04-21 14:46:57 +00002977 {
Brian Paul32df89e2005-10-29 18:26:43 +00002978 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002979 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002980 enum register_file file;
2981 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002982
Brian Paul32df89e2005-10-29 18:26:43 +00002983 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002984 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002985 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2986 fp->SrcReg[0].File = file;
2987 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002988 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002989 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2990 swizzle[1],
2991 swizzle[2],
2992 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002993 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002994 break;
2995
2996 case OP_TEX_SAMPLE:
2997 switch (code) {
2998 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002999 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003000 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00003001 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003002 break;
3003
3004 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003005 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003006 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003007 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003008 break;
3009
3010 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003011 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003012 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003013 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003014 break;
3015 }
3016
Keith Whitwell7c26b612005-04-21 14:46:57 +00003017 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003018 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003019
3020 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003021 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003022
3023 /* texImageUnit */
3024 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
3025 return 1;
3026 fp->TexSrcUnit = texcoord;
3027
3028 /* texTarget */
3029 switch (*(*inst)++) {
3030 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003031 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003032 break;
3033 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003034 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003035 break;
3036 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003037 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003038 break;
3039 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003040 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003041 break;
3042 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003043 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003044 break;
3045 case TEXTARGET_SHADOW1D:
3046 case TEXTARGET_SHADOW2D:
3047 case TEXTARGET_SHADOWRECT:
3048 /* TODO ARB_fragment_program_shadow code */
3049 break;
3050 }
Brian Paul7e807512005-11-05 17:10:45 +00003051 Program->TexturesUsed[texcoord] |= (1<<fp->TexSrcTarget);
Jouk Jansen40322e12004-04-05 08:50:36 +00003052 break;
3053
3054 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003055 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003056 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003057 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003058 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003059 break;
3060 }
3061
3062 return 0;
3063}
3064
Keith Whitwell7c26b612005-04-21 14:46:57 +00003065static GLuint
3066parse_vp_dst_reg(GLcontext * ctx, GLubyte ** inst,
3067 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003068 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003069{
Brian Paul7aebaf32005-10-30 21:23:23 +00003070 GLint mask;
3071 GLuint idx;
3072 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003073
3074 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3075 return 1;
3076
3077 reg->File = file;
3078 reg->Index = idx;
3079 reg->WriteMask = mask;
3080 return 0;
3081}
3082
3083/**
3084 * Handle the parsing out of a masked address register
3085 *
3086 * \param Index - The register index we write to
3087 * \param WriteMask - The mask controlling which components we write (1->write)
3088 *
3089 * \return 0 on sucess, 1 on error
3090 */
3091static GLuint
3092parse_vp_address_reg (GLcontext * ctx, GLubyte ** inst,
3093 struct var_cache **vc_head,
3094 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003095 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003096{
3097 GLint idx;
3098
3099 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3100 return 1;
3101
3102 /* This should be 0x8 */
3103 (*inst)++;
3104
3105 reg->File = PROGRAM_ADDRESS;
3106 reg->Index = idx;
3107
3108 /* Writemask of .x is implied */
3109 reg->WriteMask = 0x1;
3110 return 0;
3111}
3112
3113/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003114 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003115 */
3116static GLuint
Brian Paul32df89e2005-10-29 18:26:43 +00003117parse_vp_vector_src_reg(GLcontext * ctx, GLubyte ** inst,
3118 struct var_cache **vc_head,
3119 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003120 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003121{
Brian Paul7aebaf32005-10-30 21:23:23 +00003122 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003123 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003124 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003125 GLubyte swizzle[4];
3126 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003127
3128 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003129 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003130
3131 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003132 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003133 return 1;
3134
3135 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003136 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003137
Brian Paul32df89e2005-10-29 18:26:43 +00003138 reg->File = file;
3139 reg->Index = index;
3140 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3141 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003142 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003143 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003144 return 0;
3145}
3146
3147
3148static GLuint
3149parse_vp_scalar_src_reg (GLcontext * ctx, GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003150 struct var_cache **vc_head,
3151 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003152 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003153{
Brian Paul7aebaf32005-10-30 21:23:23 +00003154 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003155 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003156 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003157 GLubyte Swizzle[4];
3158 GLboolean IsRelOffset;
3159
3160 /* Grab the sign */
3161 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3162
3163 /* And the src reg */
3164 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3165 return 1;
3166
3167 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003168 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003169
3170 reg->File = File;
3171 reg->Index = Index;
3172 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003173 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003174 reg->RelAddr = IsRelOffset;
3175 return 0;
3176}
3177
3178
Jouk Jansen40322e12004-04-05 08:50:36 +00003179/**
3180 * This is a big mother that handles getting opcodes into the instruction
3181 * and handling the src & dst registers for vertex program instructions
3182 */
3183static GLuint
3184parse_vp_instruction (GLcontext * ctx, GLubyte ** inst,
3185 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003186 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003187{
3188 GLint a;
3189 GLubyte type, code;
3190
3191 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3192 type = *(*inst)++;
3193
3194 /* The actual opcode name */
3195 code = *(*inst)++;
3196
Brian Paul7e807512005-11-05 17:10:45 +00003197 _mesa_init_instruction(vp);
Jouk Jansen40322e12004-04-05 08:50:36 +00003198 /* Record the position in the program string for debugging */
3199 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003200
3201 switch (type) {
3202 /* XXX: */
3203 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003204 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003205
3206 /* Remember to set SrcReg.RelAddr; */
3207
3208 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003209 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003210 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003211
Jouk Jansen40322e12004-04-05 08:50:36 +00003212 vp->DstReg.File = PROGRAM_ADDRESS;
3213
3214 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003215 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003216 return 1;
3217
3218 break;
3219
3220 case OP_ALU_VECTOR:
3221 switch (code) {
3222 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003223 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003224 break;
3225 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003226 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003227 break;
3228 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003229 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003230 break;
3231 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003232 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003233 break;
3234 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003235 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003236 break;
3237 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003238
3239 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003240 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003241
3242 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003243 return 1;
3244 break;
3245
3246 case OP_ALU_SCALAR:
3247 switch (code) {
3248 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003249 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003250 break;
3251 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003252 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003253 break;
3254 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003255 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003256 break;
3257 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003258 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003259 break;
3260 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003261 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003262 break;
3263 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003264 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003265 break;
3266 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003267 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003268 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003269
3270 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003271 return 1;
3272 break;
3273
3274 case OP_ALU_BINSC:
3275 switch (code) {
3276 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003277 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003278 break;
3279 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003280 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003281 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003282
Jouk Jansen40322e12004-04-05 08:50:36 +00003283 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003284 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003285 return 1;
3286 }
3287 break;
3288
3289 case OP_ALU_BIN:
3290 switch (code) {
3291 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003292 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003293 break;
3294 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003295 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003296 break;
3297 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003298 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003299 break;
3300 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003301 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003302 break;
3303 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003304 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003305 break;
3306 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003307 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003308 break;
3309 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003310 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003311 break;
3312 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003313 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003314 break;
3315 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003316 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003317 break;
3318 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003319 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003320 break;
3321 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003322 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003323 break;
3324 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003325 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003326 break;
3327 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003328 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003329 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003330
Jouk Jansen40322e12004-04-05 08:50:36 +00003331 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003332 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003333 return 1;
3334 }
3335 break;
3336
3337 case OP_ALU_TRI:
3338 switch (code) {
3339 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003340 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003341 break;
3342 }
3343
Keith Whitwell7c26b612005-04-21 14:46:57 +00003344 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003345 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003346
Jouk Jansen40322e12004-04-05 08:50:36 +00003347 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003348 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003349 return 1;
3350 }
3351 break;
3352
3353 case OP_ALU_SWZ:
3354 switch (code) {
3355 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003356 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003357 break;
3358 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003359 {
Brian Paul32df89e2005-10-29 18:26:43 +00003360 GLubyte swizzle[4];
3361 GLubyte negateMask;
3362 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003363 enum register_file file;
3364 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003365
Keith Whitwell7c26b612005-04-21 14:46:57 +00003366 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3367 return 1;
3368
Brian Paul32df89e2005-10-29 18:26:43 +00003369 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003370 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003371 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3372 vp->SrcReg[0].File = file;
3373 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003374 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003375 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3376 swizzle[1],
3377 swizzle[2],
3378 swizzle[3]);
3379 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003380 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003381 break;
3382 }
3383 return 0;
3384}
3385
3386#if DEBUG_PARSING
3387
3388static GLvoid
3389print_state_token (GLint token)
3390{
3391 switch (token) {
3392 case STATE_MATERIAL:
3393 fprintf (stderr, "STATE_MATERIAL ");
3394 break;
3395 case STATE_LIGHT:
3396 fprintf (stderr, "STATE_LIGHT ");
3397 break;
3398
3399 case STATE_LIGHTMODEL_AMBIENT:
3400 fprintf (stderr, "STATE_AMBIENT ");
3401 break;
3402
3403 case STATE_LIGHTMODEL_SCENECOLOR:
3404 fprintf (stderr, "STATE_SCENECOLOR ");
3405 break;
3406
3407 case STATE_LIGHTPROD:
3408 fprintf (stderr, "STATE_LIGHTPROD ");
3409 break;
3410
3411 case STATE_TEXGEN:
3412 fprintf (stderr, "STATE_TEXGEN ");
3413 break;
3414
3415 case STATE_FOG_COLOR:
3416 fprintf (stderr, "STATE_FOG_COLOR ");
3417 break;
3418
3419 case STATE_FOG_PARAMS:
3420 fprintf (stderr, "STATE_FOG_PARAMS ");
3421 break;
3422
3423 case STATE_CLIPPLANE:
3424 fprintf (stderr, "STATE_CLIPPLANE ");
3425 break;
3426
3427 case STATE_POINT_SIZE:
3428 fprintf (stderr, "STATE_POINT_SIZE ");
3429 break;
3430
3431 case STATE_POINT_ATTENUATION:
3432 fprintf (stderr, "STATE_ATTENUATION ");
3433 break;
3434
3435 case STATE_MATRIX:
3436 fprintf (stderr, "STATE_MATRIX ");
3437 break;
3438
3439 case STATE_MODELVIEW:
3440 fprintf (stderr, "STATE_MODELVIEW ");
3441 break;
3442
3443 case STATE_PROJECTION:
3444 fprintf (stderr, "STATE_PROJECTION ");
3445 break;
3446
3447 case STATE_MVP:
3448 fprintf (stderr, "STATE_MVP ");
3449 break;
3450
3451 case STATE_TEXTURE:
3452 fprintf (stderr, "STATE_TEXTURE ");
3453 break;
3454
3455 case STATE_PROGRAM:
3456 fprintf (stderr, "STATE_PROGRAM ");
3457 break;
3458
3459 case STATE_MATRIX_INVERSE:
3460 fprintf (stderr, "STATE_INVERSE ");
3461 break;
3462
3463 case STATE_MATRIX_TRANSPOSE:
3464 fprintf (stderr, "STATE_TRANSPOSE ");
3465 break;
3466
3467 case STATE_MATRIX_INVTRANS:
3468 fprintf (stderr, "STATE_INVTRANS ");
3469 break;
3470
3471 case STATE_AMBIENT:
3472 fprintf (stderr, "STATE_AMBIENT ");
3473 break;
3474
3475 case STATE_DIFFUSE:
3476 fprintf (stderr, "STATE_DIFFUSE ");
3477 break;
3478
3479 case STATE_SPECULAR:
3480 fprintf (stderr, "STATE_SPECULAR ");
3481 break;
3482
3483 case STATE_EMISSION:
3484 fprintf (stderr, "STATE_EMISSION ");
3485 break;
3486
3487 case STATE_SHININESS:
3488 fprintf (stderr, "STATE_SHININESS ");
3489 break;
3490
3491 case STATE_HALF:
3492 fprintf (stderr, "STATE_HALF ");
3493 break;
3494
3495 case STATE_POSITION:
3496 fprintf (stderr, "STATE_POSITION ");
3497 break;
3498
3499 case STATE_ATTENUATION:
3500 fprintf (stderr, "STATE_ATTENUATION ");
3501 break;
3502
3503 case STATE_SPOT_DIRECTION:
3504 fprintf (stderr, "STATE_DIRECTION ");
3505 break;
3506
3507 case STATE_TEXGEN_EYE_S:
3508 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3509 break;
3510
3511 case STATE_TEXGEN_EYE_T:
3512 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3513 break;
3514
3515 case STATE_TEXGEN_EYE_R:
3516 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3517 break;
3518
3519 case STATE_TEXGEN_EYE_Q:
3520 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3521 break;
3522
3523 case STATE_TEXGEN_OBJECT_S:
3524 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3525 break;
3526
3527 case STATE_TEXGEN_OBJECT_T:
3528 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3529 break;
3530
3531 case STATE_TEXGEN_OBJECT_R:
3532 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3533 break;
3534
3535 case STATE_TEXGEN_OBJECT_Q:
3536 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3537 break;
3538
3539 case STATE_TEXENV_COLOR:
3540 fprintf (stderr, "STATE_TEXENV_COLOR ");
3541 break;
3542
3543 case STATE_DEPTH_RANGE:
3544 fprintf (stderr, "STATE_DEPTH_RANGE ");
3545 break;
3546
3547 case STATE_VERTEX_PROGRAM:
3548 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3549 break;
3550
3551 case STATE_FRAGMENT_PROGRAM:
3552 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3553 break;
3554
3555 case STATE_ENV:
3556 fprintf (stderr, "STATE_ENV ");
3557 break;
3558
3559 case STATE_LOCAL:
3560 fprintf (stderr, "STATE_LOCAL ");
3561 break;
3562
3563 }
3564 fprintf (stderr, "[%d] ", token);
3565}
3566
3567
3568static GLvoid
3569debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3570 struct arb_program *Program)
3571{
3572 struct var_cache *vc;
3573 GLint a, b;
3574
3575 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3576
3577 /* First of all, print out the contents of the var_cache */
3578 vc = vc_head;
3579 while (vc) {
3580 fprintf (stderr, "[%x]\n", vc);
3581 switch (vc->type) {
3582 case vt_none:
3583 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3584 break;
3585 case vt_attrib:
3586 fprintf (stderr, "ATTRIB %s\n", vc->name);
3587 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3588 break;
3589 case vt_param:
3590 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3591 vc->param_binding_begin, vc->param_binding_length);
3592 b = vc->param_binding_begin;
3593 for (a = 0; a < vc->param_binding_length; a++) {
3594 fprintf (stderr, "%s\n",
3595 Program->Parameters->Parameters[a + b].Name);
3596 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3597 print_state_token (Program->Parameters->Parameters[a + b].
3598 StateIndexes[0]);
3599 print_state_token (Program->Parameters->Parameters[a + b].
3600 StateIndexes[1]);
3601 print_state_token (Program->Parameters->Parameters[a + b].
3602 StateIndexes[2]);
3603 print_state_token (Program->Parameters->Parameters[a + b].
3604 StateIndexes[3]);
3605 print_state_token (Program->Parameters->Parameters[a + b].
3606 StateIndexes[4]);
3607 print_state_token (Program->Parameters->Parameters[a + b].
3608 StateIndexes[5]);
3609 }
3610 else
3611 fprintf (stderr, "%f %f %f %f\n",
3612 Program->Parameters->Parameters[a + b].Values[0],
3613 Program->Parameters->Parameters[a + b].Values[1],
3614 Program->Parameters->Parameters[a + b].Values[2],
3615 Program->Parameters->Parameters[a + b].Values[3]);
3616 }
3617 break;
3618 case vt_temp:
3619 fprintf (stderr, "TEMP %s\n", vc->name);
3620 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3621 break;
3622 case vt_output:
3623 fprintf (stderr, "OUTPUT %s\n", vc->name);
3624 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3625 break;
3626 case vt_alias:
3627 fprintf (stderr, "ALIAS %s\n", vc->name);
3628 fprintf (stderr, " binding: 0x%x (%s)\n",
3629 vc->alias_binding, vc->alias_binding->name);
3630 break;
3631 }
3632 vc = vc->next;
3633 }
3634}
3635
Brian Paul72030e02005-11-03 03:30:34 +00003636#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003637
3638
3639/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003640 * The main loop for parsing a fragment or vertex program
3641 *
Brian Paul72030e02005-11-03 03:30:34 +00003642 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003643 */
Brian Paul72030e02005-11-03 03:30:34 +00003644static GLint
Brian Paul8c41a142005-11-19 15:36:28 +00003645parse_instructions(GLcontext * ctx, GLubyte * inst, struct var_cache **vc_head,
Brian Paul45703642005-10-29 15:52:31 +00003646 struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003647{
Brian Paul72030e02005-11-03 03:30:34 +00003648 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3649 ? ctx->Const.FragmentProgram.MaxInstructions
3650 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003651 GLint err = 0;
3652
Brian Paul72030e02005-11-03 03:30:34 +00003653 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3654
Jouk Jansen40322e12004-04-05 08:50:36 +00003655 Program->MajorVersion = (GLuint) * inst++;
3656 Program->MinorVersion = (GLuint) * inst++;
3657
3658 while (*inst != END) {
3659 switch (*inst++) {
3660
3661 case OPTION:
3662 switch (*inst++) {
3663 case ARB_PRECISION_HINT_FASTEST:
3664 Program->PrecisionOption = GL_FASTEST;
3665 break;
3666
3667 case ARB_PRECISION_HINT_NICEST:
3668 Program->PrecisionOption = GL_NICEST;
3669 break;
3670
3671 case ARB_FOG_EXP:
3672 Program->FogOption = GL_EXP;
3673 break;
3674
3675 case ARB_FOG_EXP2:
3676 Program->FogOption = GL_EXP2;
3677 break;
3678
3679 case ARB_FOG_LINEAR:
3680 Program->FogOption = GL_LINEAR;
3681 break;
3682
3683 case ARB_POSITION_INVARIANT:
3684 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003685 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003686 break;
3687
3688 case ARB_FRAGMENT_PROGRAM_SHADOW:
3689 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3690 /* TODO ARB_fragment_program_shadow code */
3691 }
3692 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003693
3694 case ARB_DRAW_BUFFERS:
3695 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3696 /* do nothing for now */
3697 }
3698 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003699 }
3700 break;
3701
3702 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003703 /* check length */
3704 if (Program->Base.NumInstructions + 1 >= maxInst) {
3705 const char *msg = "Max instruction count exceeded";
3706 _mesa_set_program_error(ctx, Program->Position, msg);
3707 _mesa_error(ctx, GL_INVALID_OPERATION, msg);
3708 return 1;
3709 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003710 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003711 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003712 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003713 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003714 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003715 }
3716 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003718 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003719 }
3720
Brian Paul72030e02005-11-03 03:30:34 +00003721 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003722 Program->Base.NumInstructions++;
3723 break;
3724
3725 case DECLARATION:
3726 err = parse_declaration (ctx, &inst, vc_head, Program);
3727 break;
3728
3729 default:
3730 break;
3731 }
3732
3733 if (err)
3734 break;
3735 }
3736
3737 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003738 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003739 const GLuint numInst = Program->Base.NumInstructions;
Brian Paul8c41a142005-11-19 15:36:28 +00003740 _mesa_init_instruction(Program->Base.Instructions + numInst);
3741 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003742 /* YYY Wrong Position in program, whatever, at least not random -> crash
3743 Program->Position = parse_position (&inst);
3744 */
Brian Paul8c41a142005-11-19 15:36:28 +00003745 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003746 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003747 Program->Base.NumInstructions++;
3748
Brian Paul05051032005-11-01 04:36:33 +00003749 /*
3750 * Initialize native counts to logical counts. The device driver may
3751 * change them if program is translated into a hardware program.
3752 */
3753 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3754 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3755 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3756 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3757 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003758
Jouk Jansen40322e12004-04-05 08:50:36 +00003759 return err;
3760}
3761
Brian Paul7aebaf32005-10-30 21:23:23 +00003762
Jouk Jansen40322e12004-04-05 08:50:36 +00003763/* XXX temporary */
Brian Paula6c423d2004-08-25 15:59:48 +00003764__extension__ static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003765#include "grammar_syn.h"
3766;
3767
Brian Paul7aebaf32005-10-30 21:23:23 +00003768
Brian Paul8c41a142005-11-19 15:36:28 +00003769/**
3770 * Set a grammar parameter.
3771 * \param name the grammar parameter
3772 * \param value the new parameter value
3773 * \return 0 if OK, 1 if error
3774 */
3775static int
3776set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003777{
3778 char error_msg[300];
3779 GLint error_pos;
3780
Brian Paul8c41a142005-11-19 15:36:28 +00003781 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003782 return 0;
3783
3784 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3785 _mesa_set_program_error (ctx, error_pos, error_msg);
3786 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3787 return 1;
3788}
3789
Brian Paul8c41a142005-11-19 15:36:28 +00003790
3791/**
3792 * Enable support for the given language option in the parser.
3793 * \return 1 if OK, 0 if error
3794 */
3795static int
3796enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003797{
Brian Paul8c41a142005-11-19 15:36:28 +00003798 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003799}
3800
Brian Paul8c41a142005-11-19 15:36:28 +00003801
3802/**
3803 * Enable parser extensions based on which OpenGL extensions are supported
3804 * by this rendering context.
3805 *
3806 * \return GL_TRUE if OK, GL_FALSE if error.
3807 */
3808static GLboolean
3809enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003810{
Brian Paul8c41a142005-11-19 15:36:28 +00003811#if 0
3812 /* These are not supported at this time */
3813 if ((ctx->Extensions.ARB_vertex_blend ||
3814 ctx->Extensions.EXT_vertex_weighting)
3815 && !enable_ext(ctx, id, "point_parameters"))
3816 return GL_FALSE;
3817 if (ctx->Extensions.ARB_matrix_palette
3818 && !enable_ext(ctx, id, "matrix_palette"))
3819 return GL_FALSE;
3820 if (ctx->Extensions.ARB_fragment_program_shadow
3821 && !enable_ext(ctx, id, "fragment_program_shadow"))
3822 return GL_FALSE;
3823#endif
3824 if (ctx->Extensions.EXT_point_parameters
3825 && !enable_ext(ctx, id, "point_parameters"))
3826 return GL_FALSE;
3827 if (ctx->Extensions.EXT_secondary_color
3828 && !enable_ext(ctx, id, "secondary_color"))
3829 return GL_FALSE;
3830 if (ctx->Extensions.EXT_fog_coord
3831 && !enable_ext(ctx, id, "fog_coord"))
3832 return GL_FALSE;
3833 if (ctx->Extensions.NV_texture_rectangle
3834 && !enable_ext(ctx, id, "texture_rectangle"))
3835 return GL_FALSE;
3836 if (ctx->Extensions.ARB_draw_buffers
3837 && !enable_ext(ctx, id, "draw_buffers"))
3838 return GL_FALSE;
3839
3840 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003841}
3842
Brian Paul8c41a142005-11-19 15:36:28 +00003843
Jouk Jansen40322e12004-04-05 08:50:36 +00003844/**
3845 * This kicks everything off.
3846 *
3847 * \param ctx - The GL Context
3848 * \param str - The program string
3849 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003850 * \param program - The arb_program struct to return all the parsed info in
3851 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003852 */
Brian Paul8c41a142005-11-19 15:36:28 +00003853static GLboolean
3854_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3855 const GLubyte *str, GLsizei len,
3856 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003857{
3858 GLint a, err, error_pos;
3859 char error_msg[300];
3860 GLuint parsed_len;
3861 struct var_cache *vc_head;
3862 grammar arbprogram_syn_id;
3863 GLubyte *parsed, *inst;
3864 GLubyte *strz = NULL;
3865 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3866
Brian Paul8c41a142005-11-19 15:36:28 +00003867 /* set the program target before parsing */
3868 program->Base.Target = target;
3869
Brian Paul7f76b8f2004-09-10 01:05:39 +00003870 /* Reset error state */
3871 _mesa_set_program_error(ctx, -1, NULL);
3872
Brian Paul8c41a142005-11-19 15:36:28 +00003873 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003874 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003875 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003876 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003877 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003878
3879 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3880 if (grammar_syn_id == 0) {
3881 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003882 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003883 _mesa_set_program_error (ctx, error_pos, error_msg);
3884 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003885 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003886 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003887 }
3888
Brian Paul8c41a142005-11-19 15:36:28 +00003889 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3890 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003891
Brian Paul49a80ca2006-04-28 15:40:11 +00003892 /* 'parsed' is unused here */
3893 _mesa_free (parsed);
3894 parsed = NULL;
3895
Brian Paul45703642005-10-29 15:52:31 +00003896 /* NOTE: we can't destroy grammar_syn_id right here because
3897 * grammar_destroy() can reset the last error
3898 */
Brian Paul8c41a142005-11-19 15:36:28 +00003899 if (err) {
3900 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003901 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3902 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003903 _mesa_error (ctx, GL_INVALID_OPERATION,
3904 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003905 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003906 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003907 }
3908
3909 grammar_destroy (grammar_syn_id);
3910
3911 arbprogram_syn_is_ok = 1;
3912 }
3913
3914 /* create the grammar object */
3915 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3916 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003917 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003918 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3919 _mesa_set_program_error (ctx, error_pos, error_msg);
3920 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003921 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003922 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003923 }
3924
3925 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003926 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003927 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3928 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003929 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003930 }
3931
Brian Paul8c41a142005-11-19 15:36:28 +00003932 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3933 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003934 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003935 }
3936
3937 /* check for NULL character occurences */
3938 {
Brian Paul8c41a142005-11-19 15:36:28 +00003939 GLint i;
3940 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003941 if (str[i] == '\0') {
3942 _mesa_set_program_error (ctx, i, "invalid character");
Brian Paul8c41a142005-11-19 15:36:28 +00003943 _mesa_error (ctx, GL_INVALID_OPERATION,
3944 "glProgramStringARB(illegal character)");
Jouk Jansen40322e12004-04-05 08:50:36 +00003945 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003946 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003947 }
Brian Paul8c41a142005-11-19 15:36:28 +00003948 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003949 }
3950
3951 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003952 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003953 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003954 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3955 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003956 return GL_FALSE;
3957 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003958 _mesa_memcpy (strz, str, len);
3959 strz[len] = '\0';
3960
Michal Krolb80bc052004-10-21 14:09:54 +00003961 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003962 err = !grammar_fast_check(arbprogram_syn_id, strz,
3963 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003964
3965 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003966 if (err) {
3967 _mesa_free(strz);
Brian Paul49a80ca2006-04-28 15:40:11 +00003968 _mesa_free(parsed);
Jouk Jansen40322e12004-04-05 08:50:36 +00003969 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3970 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003971 _mesa_error (ctx, GL_INVALID_OPERATION,
3972 "glProgramStringARB(syntax error)");
Brian Paul5fe90292004-07-20 21:15:13 +00003973
3974 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003975#if DEBUG_PARSING
3976 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003977 int line, col;
3978 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003979 fprintf(stderr, "program: %s\n", (char *) strz);
3980 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
3981 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos, &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003982 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3983 } while (0)
3984#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003985
Jouk Jansen40322e12004-04-05 08:50:36 +00003986 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003987 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003988 }
3989
Jouk Jansen40322e12004-04-05 08:50:36 +00003990 grammar_destroy (arbprogram_syn_id);
3991
Brian Paul8c41a142005-11-19 15:36:28 +00003992 /*
3993 * Program string is syntactically correct at this point
3994 * Parse the tokenized version of the program now, generating
3995 * vertex/fragment program instructions.
3996 */
3997
Jouk Jansen40322e12004-04-05 08:50:36 +00003998 /* Initialize the arb_program struct */
3999 program->Base.String = strz;
Brian Paul8c41a142005-11-19 15:36:28 +00004000 program->Base.Instructions = (struct prog_instruction *)
4001 _mesa_malloc(MAX_INSTRUCTIONS * sizeof(struct prog_instruction));
Jouk Jansen40322e12004-04-05 08:50:36 +00004002 program->Base.NumInstructions =
4003 program->Base.NumTemporaries =
4004 program->Base.NumParameters =
4005 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00004006 program->Base.Parameters = _mesa_new_parameter_list ();
4007 program->Base.InputsRead = 0x0;
4008 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004009 program->Position = 0;
4010 program->MajorVersion = program->MinorVersion = 0;
4011 program->PrecisionOption = GL_DONT_CARE;
4012 program->FogOption = GL_NONE;
4013 program->HintPositionInvariant = GL_FALSE;
4014 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00004015 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00004016 program->NumAluInstructions =
4017 program->NumTexInstructions =
4018 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00004019 program->UsesKill = 0;
4020
Jouk Jansen40322e12004-04-05 08:50:36 +00004021 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00004022 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004023
4024 /* Start examining the tokens in the array */
4025 inst = parsed;
4026
4027 /* Check the grammer rev */
4028 if (*inst++ != REVISION) {
4029 _mesa_set_program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00004030 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul45cd2f92005-11-03 02:25:10 +00004031 "glProgramStringARB(Grammar version mismatch)");
Brian Paul45703642005-10-29 15:52:31 +00004032 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00004033 }
4034 else {
Michal Krolb80bc052004-10-21 14:09:54 +00004035 /* ignore program target */
4036 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00004037 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00004038 }
4039
4040 /*debug_variables(ctx, vc_head, program); */
4041
4042 /* We're done with the parsed binary array */
4043 var_cache_destroy (&vc_head);
4044
4045 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00004046
Brian Paul8c41a142005-11-19 15:36:28 +00004047 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4048 * to size [ap.Base.NumInstructions].
4049 */
4050 program->Base.Instructions = (struct prog_instruction *)
4051 _mesa_realloc(program->Base.Instructions,
4052 MAX_INSTRUCTIONS * sizeof(struct prog_instruction),/*orig*/
4053 program->Base.NumInstructions * sizeof(struct prog_instruction));
4054
Brian Paul45703642005-10-29 15:52:31 +00004055 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00004056}
Brian Paul8c41a142005-11-19 15:36:28 +00004057
4058
4059
4060void
4061_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4062 const GLvoid *str, GLsizei len,
4063 struct fragment_program *program)
4064{
4065 struct arb_program ap;
4066 GLuint i;
4067
4068 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004069 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004070 /* Error in the program. Just return. */
4071 return;
4072 }
4073
4074 /* Copy the relevant contents of the arb_program struct into the
4075 * fragment_program struct.
4076 */
4077 program->Base.String = ap.Base.String;
4078 program->Base.NumInstructions = ap.Base.NumInstructions;
4079 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4080 program->Base.NumParameters = ap.Base.NumParameters;
4081 program->Base.NumAttributes = ap.Base.NumAttributes;
4082 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4083 program->NumAluInstructions = ap.NumAluInstructions;
4084 program->NumTexInstructions = ap.NumTexInstructions;
4085 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004086 program->NumNativeAluInstructions = ap.NumAluInstructions;
4087 program->NumNativeTexInstructions = ap.NumTexInstructions;
4088 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004089 program->Base.InputsRead = ap.Base.InputsRead;
4090 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4091 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4092 program->TexturesUsed[i] = ap.TexturesUsed[i];
4093 program->FogOption = ap.FogOption;
4094
4095 if (program->Base.Instructions)
4096 _mesa_free(program->Base.Instructions);
4097 program->Base.Instructions = ap.Base.Instructions;
4098
4099 if (program->Base.Parameters)
4100 _mesa_free_parameter_list(program->Base.Parameters);
4101 program->Base.Parameters = ap.Base.Parameters;
4102
4103#if DEBUG_FP
4104 _mesa_print_program(&program.Base);
4105#endif
4106}
4107
4108
4109
4110/**
4111 * Parse the vertex program string. If success, update the given
4112 * vertex_program object with the new program. Else, leave the vertex_program
4113 * object unchanged.
4114 */
4115void
4116_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4117 const GLvoid *str, GLsizei len,
4118 struct vertex_program *program)
4119{
4120 struct arb_program ap;
4121
4122 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4123
Brian Paul95801792005-12-06 15:41:43 +00004124 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004125 /* Error in the program. Just return. */
4126 return;
4127 }
4128
4129 /* Copy the relevant contents of the arb_program struct into the
4130 * vertex_program struct.
4131 */
4132 program->Base.String = ap.Base.String;
4133 program->Base.NumInstructions = ap.Base.NumInstructions;
4134 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4135 program->Base.NumParameters = ap.Base.NumParameters;
4136 program->Base.NumAttributes = ap.Base.NumAttributes;
4137 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
4138 program->Base.InputsRead = ap.Base.InputsRead;
4139 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4140 program->IsPositionInvariant = ap.HintPositionInvariant;
4141
4142 if (program->Base.Instructions)
4143 _mesa_free(program->Base.Instructions);
4144 program->Base.Instructions = ap.Base.Instructions;
4145
4146 if (program->Base.Parameters)
4147 _mesa_free_parameter_list(program->Base.Parameters);
4148 program->Base.Parameters = ap.Base.Parameters;
4149
4150#if DEBUG_VP
4151 _mesa_print_program(&program->Base);
4152#endif
4153}