blob: a3a75c3b0a6f74570b46ab8e2774f2ec55a8344a [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brianab31a3a2007-09-13 11:41:49 -06003 * Version: 7.1
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Brian7d2b6a02008-03-27 16:17:37 -06005 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
Jouk Jansen40322e12004-04-05 08:50:36 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#define DEBUG_PARSING 0
26
27/**
28 * \file arbprogparse.c
29 * ARB_*_program parser core
30 * \author Karl Rasche
31 */
32
Brianc223c6b2007-07-04 13:15:20 -060033#include "main/glheader.h"
34#include "main/imports.h"
Brian Paulbbd28712008-09-18 12:26:54 -060035#include "main/context.h"
36#include "main/macros.h"
37#include "main/mtypes.h"
Brianc223c6b2007-07-04 13:15:20 -060038#include "shader/grammar/grammar_mesa.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000039#include "arbprogparse.h"
Brian Paul32df89e2005-10-29 18:26:43 +000040#include "program.h"
Brianc0551f02006-12-14 15:02:37 -070041#include "prog_parameter.h"
42#include "prog_statevars.h"
Brianc0551f02006-12-14 15:02:37 -070043#include "prog_instruction.h"
Brian Paul8c41a142005-11-19 15:36:28 +000044
45
Brian Paul6211a142006-08-24 23:37:13 +000046/* For ARB programs, use the NV instruction limits */
47#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
48 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000049
50
51/**
52 * This is basically a union of the vertex_program and fragment_program
53 * structs that we can use to parse the program into
54 *
55 * XXX we can probably get rid of this entirely someday.
56 */
57struct arb_program
58{
Brian Paul122629f2006-07-20 16:49:57 +000059 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000060
61 GLuint Position; /* Just used for error reporting while parsing */
62 GLuint MajorVersion;
63 GLuint MinorVersion;
64
65 /* ARB_vertex_progmra options */
66 GLboolean HintPositionInvariant;
67
68 /* ARB_fragment_progmra options */
69 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
70 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
71
72 /* ARB_fragment_program specifics */
73 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
Ian Romanick7b559a92007-06-07 13:58:50 -070074 GLbitfield ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +000075 GLuint NumAluInstructions;
76 GLuint NumTexInstructions;
77 GLuint NumTexIndirections;
78
79 GLboolean UsesKill;
80};
81
Ian Romanick9bdfee32005-07-18 12:31:24 +000082
Brian Paula6c423d2004-08-25 15:59:48 +000083
Jouk Jansen40322e12004-04-05 08:50:36 +000084/* TODO:
85 * Fragment Program Stuff:
86 * -----------------------------------------------------
87 *
88 * - things from Michal's email
89 * + overflow on atoi
90 * + not-overflowing floats (don't use parse_integer..)
91 * + can remove range checking in arbparse.c
92 *
93 * - check all limits of number of various variables
94 * + parameters
95 *
96 * - test! test! test!
97 *
98 * Vertex Program Stuff:
99 * -----------------------------------------------------
100 * - Optimize param array usage and count limits correctly, see spec,
101 * section 2.14.3.7
102 * + Record if an array is reference absolutly or relatively (or both)
103 * + For absolute arrays, store a bitmap of accesses
104 * + For single parameters, store an access flag
105 * + After parsing, make a parameter cleanup and merging pass, where
106 * relative arrays are layed out first, followed by abs arrays, and
107 * finally single state.
108 * + Remap offsets for param src and dst registers
109 * + Now we can properly count parameter usage
110 *
111 * - Multiple state binding errors in param arrays (see spec, just before
112 * section 2.14.3.3)
113 * - grep for XXX
114 *
115 * Mesa Stuff
116 * -----------------------------------------------------
117 * - User clipping planes vs. PositionInvariant
118 * - Is it sufficient to just multiply by the mvp to transform in the
119 * PositionInvariant case? Or do we need something more involved?
120 *
121 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
122 * - fetch state listed in program_parameters list
123 * + WTF should this go???
124 * + currently in nvvertexec.c and s_nvfragprog.c
125 *
126 * - allow for multiple address registers (and fetch address regs properly)
127 *
128 * Cosmetic Stuff
129 * -----------------------------------------------------
130 * - remove any leftover unused grammer.c stuff (dict_ ?)
131 * - fix grammer.c error handling so its not static
132 * - #ifdef around stuff pertaining to extentions
133 *
134 * Outstanding Questions:
135 * -----------------------------------------------------
136 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
137 * what gets hacked off because of this:
138 * + VERTEX_ATTRIB_MATRIXINDEX
139 * + VERTEX_ATTRIB_WEIGHT
140 * + MATRIX_MODELVIEW
141 * + MATRIX_PALETTE
142 *
143 * - When can we fetch env/local params from their own register files, and
144 * when to we have to fetch them into the main state register file?
145 * (think arrays)
146 *
147 * Grammar Changes:
148 * -----------------------------------------------------
149 */
150
151/* Changes since moving the file to shader directory
152
1532004-III-4 ------------------------------------------------------------
154- added #include "grammar_mesa.h"
155- removed grammar specific code part (it resides now in grammar.c)
156- added GL_ARB_fragment_program_shadow tokens
157- modified #include "arbparse_syn.h"
158- major changes inside _mesa_parse_arb_program()
159- check the program string for '\0' characters
160- copy the program string to a one-byte-longer location to have
161 it null-terminated
162- position invariance test (not writing to result.position) moved
163 to syntax part
164*/
165
166typedef GLubyte *production;
167
Brian Paul11a54c32006-11-15 19:54:25 +0000168
Jouk Jansen40322e12004-04-05 08:50:36 +0000169/**
170 * This is the text describing the rules to parse the grammar
171 */
Brian Paul11a54c32006-11-15 19:54:25 +0000172LONGSTRING static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000173#include "arbprogram_syn.h"
174;
175
176/**
177 * These should match up with the values defined in arbprogram.syn
178 */
179
180/*
181 Changes:
182 - changed and merged V_* and F_* opcode values to OP_*.
183 - added GL_ARB_fragment_program_shadow specific tokens (michal)
184*/
Ian Romanickbb372f12007-05-16 15:34:22 -0700185#define REVISION 0x0a
Jouk Jansen40322e12004-04-05 08:50:36 +0000186
187/* program type */
188#define FRAGMENT_PROGRAM 0x01
189#define VERTEX_PROGRAM 0x02
190
191/* program section */
192#define OPTION 0x01
193#define INSTRUCTION 0x02
194#define DECLARATION 0x03
195#define END 0x04
196
Michal Krolb80bc052004-10-21 14:09:54 +0000197/* GL_ARB_fragment_program option */
198#define ARB_PRECISION_HINT_FASTEST 0x00
199#define ARB_PRECISION_HINT_NICEST 0x01
200#define ARB_FOG_EXP 0x02
201#define ARB_FOG_EXP2 0x03
202#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000203
Michal Krolb80bc052004-10-21 14:09:54 +0000204/* GL_ARB_vertex_program option */
205#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000206
Michal Krolb80bc052004-10-21 14:09:54 +0000207/* GL_ARB_fragment_program_shadow option */
208#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000209
Michal Krolb80bc052004-10-21 14:09:54 +0000210/* GL_ARB_draw_buffers option */
211#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000212
Ian Romanickbb372f12007-05-16 15:34:22 -0700213/* GL_MESA_texture_array option */
214#define MESA_TEXTURE_ARRAY 0x08
215
Jouk Jansen40322e12004-04-05 08:50:36 +0000216/* GL_ARB_fragment_program instruction class */
217#define OP_ALU_INST 0x00
218#define OP_TEX_INST 0x01
219
220/* GL_ARB_vertex_program instruction class */
221/* OP_ALU_INST */
222
223/* GL_ARB_fragment_program instruction type */
224#define OP_ALU_VECTOR 0x00
225#define OP_ALU_SCALAR 0x01
226#define OP_ALU_BINSC 0x02
227#define OP_ALU_BIN 0x03
228#define OP_ALU_TRI 0x04
229#define OP_ALU_SWZ 0x05
230#define OP_TEX_SAMPLE 0x06
231#define OP_TEX_KIL 0x07
232
233/* GL_ARB_vertex_program instruction type */
234#define OP_ALU_ARL 0x08
235/* OP_ALU_VECTOR */
236/* OP_ALU_SCALAR */
237/* OP_ALU_BINSC */
238/* OP_ALU_BIN */
239/* OP_ALU_TRI */
240/* OP_ALU_SWZ */
241
242/* GL_ARB_fragment_program instruction code */
243#define OP_ABS 0x00
244#define OP_ABS_SAT 0x1B
245#define OP_FLR 0x09
246#define OP_FLR_SAT 0x26
247#define OP_FRC 0x0A
248#define OP_FRC_SAT 0x27
249#define OP_LIT 0x0C
250#define OP_LIT_SAT 0x2A
251#define OP_MOV 0x11
252#define OP_MOV_SAT 0x30
253#define OP_COS 0x1F
254#define OP_COS_SAT 0x20
255#define OP_EX2 0x07
256#define OP_EX2_SAT 0x25
257#define OP_LG2 0x0B
258#define OP_LG2_SAT 0x29
259#define OP_RCP 0x14
260#define OP_RCP_SAT 0x33
261#define OP_RSQ 0x15
262#define OP_RSQ_SAT 0x34
263#define OP_SIN 0x38
264#define OP_SIN_SAT 0x39
265#define OP_SCS 0x35
266#define OP_SCS_SAT 0x36
267#define OP_POW 0x13
268#define OP_POW_SAT 0x32
269#define OP_ADD 0x01
270#define OP_ADD_SAT 0x1C
271#define OP_DP3 0x03
272#define OP_DP3_SAT 0x21
273#define OP_DP4 0x04
274#define OP_DP4_SAT 0x22
275#define OP_DPH 0x05
276#define OP_DPH_SAT 0x23
277#define OP_DST 0x06
278#define OP_DST_SAT 0x24
279#define OP_MAX 0x0F
280#define OP_MAX_SAT 0x2E
281#define OP_MIN 0x10
282#define OP_MIN_SAT 0x2F
283#define OP_MUL 0x12
284#define OP_MUL_SAT 0x31
285#define OP_SGE 0x16
286#define OP_SGE_SAT 0x37
287#define OP_SLT 0x17
288#define OP_SLT_SAT 0x3A
289#define OP_SUB 0x18
290#define OP_SUB_SAT 0x3B
291#define OP_XPD 0x1A
292#define OP_XPD_SAT 0x43
293#define OP_CMP 0x1D
294#define OP_CMP_SAT 0x1E
295#define OP_LRP 0x2B
296#define OP_LRP_SAT 0x2C
297#define OP_MAD 0x0E
298#define OP_MAD_SAT 0x2D
299#define OP_SWZ 0x19
300#define OP_SWZ_SAT 0x3C
301#define OP_TEX 0x3D
302#define OP_TEX_SAT 0x3E
303#define OP_TXB 0x3F
304#define OP_TXB_SAT 0x40
305#define OP_TXP 0x41
306#define OP_TXP_SAT 0x42
307#define OP_KIL 0x28
308
309/* GL_ARB_vertex_program instruction code */
310#define OP_ARL 0x02
311/* OP_ABS */
312/* OP_FLR */
313/* OP_FRC */
314/* OP_LIT */
315/* OP_MOV */
316/* OP_EX2 */
317#define OP_EXP 0x08
318/* OP_LG2 */
319#define OP_LOG 0x0D
320/* OP_RCP */
321/* OP_RSQ */
322/* OP_POW */
323/* OP_ADD */
324/* OP_DP3 */
325/* OP_DP4 */
326/* OP_DPH */
327/* OP_DST */
328/* OP_MAX */
329/* OP_MIN */
330/* OP_MUL */
331/* OP_SGE */
332/* OP_SLT */
333/* OP_SUB */
334/* OP_XPD */
335/* OP_MAD */
336/* OP_SWZ */
337
338/* fragment attribute binding */
339#define FRAGMENT_ATTRIB_COLOR 0x01
340#define FRAGMENT_ATTRIB_TEXCOORD 0x02
341#define FRAGMENT_ATTRIB_FOGCOORD 0x03
342#define FRAGMENT_ATTRIB_POSITION 0x04
343
344/* vertex attribute binding */
345#define VERTEX_ATTRIB_POSITION 0x01
346#define VERTEX_ATTRIB_WEIGHT 0x02
347#define VERTEX_ATTRIB_NORMAL 0x03
348#define VERTEX_ATTRIB_COLOR 0x04
349#define VERTEX_ATTRIB_FOGCOORD 0x05
350#define VERTEX_ATTRIB_TEXCOORD 0x06
351#define VERTEX_ATTRIB_MATRIXINDEX 0x07
352#define VERTEX_ATTRIB_GENERIC 0x08
353
354/* fragment result binding */
355#define FRAGMENT_RESULT_COLOR 0x01
356#define FRAGMENT_RESULT_DEPTH 0x02
357
358/* vertex result binding */
359#define VERTEX_RESULT_POSITION 0x01
360#define VERTEX_RESULT_COLOR 0x02
361#define VERTEX_RESULT_FOGCOORD 0x03
362#define VERTEX_RESULT_POINTSIZE 0x04
363#define VERTEX_RESULT_TEXCOORD 0x05
364
365/* texture target */
366#define TEXTARGET_1D 0x01
367#define TEXTARGET_2D 0x02
368#define TEXTARGET_3D 0x03
369#define TEXTARGET_RECT 0x04
370#define TEXTARGET_CUBE 0x05
371/* GL_ARB_fragment_program_shadow */
372#define TEXTARGET_SHADOW1D 0x06
373#define TEXTARGET_SHADOW2D 0x07
374#define TEXTARGET_SHADOWRECT 0x08
Ian Romanickbb372f12007-05-16 15:34:22 -0700375/* GL_MESA_texture_array */
376#define TEXTARGET_1D_ARRAY 0x09
377#define TEXTARGET_2D_ARRAY 0x0a
Ian Romanick69358e72007-06-05 09:24:27 -0700378#define TEXTARGET_SHADOW1D_ARRAY 0x0b
379#define TEXTARGET_SHADOW2D_ARRAY 0x0c
Jouk Jansen40322e12004-04-05 08:50:36 +0000380
381/* face type */
382#define FACE_FRONT 0x00
383#define FACE_BACK 0x01
384
385/* color type */
386#define COLOR_PRIMARY 0x00
387#define COLOR_SECONDARY 0x01
388
389/* component */
390#define COMPONENT_X 0x00
391#define COMPONENT_Y 0x01
392#define COMPONENT_Z 0x02
393#define COMPONENT_W 0x03
394#define COMPONENT_0 0x04
395#define COMPONENT_1 0x05
396
397/* array index type */
398#define ARRAY_INDEX_ABSOLUTE 0x00
399#define ARRAY_INDEX_RELATIVE 0x01
400
401/* matrix name */
402#define MATRIX_MODELVIEW 0x01
403#define MATRIX_PROJECTION 0x02
404#define MATRIX_MVP 0x03
405#define MATRIX_TEXTURE 0x04
406#define MATRIX_PALETTE 0x05
407#define MATRIX_PROGRAM 0x06
408
409/* matrix modifier */
410#define MATRIX_MODIFIER_IDENTITY 0x00
411#define MATRIX_MODIFIER_INVERSE 0x01
412#define MATRIX_MODIFIER_TRANSPOSE 0x02
413#define MATRIX_MODIFIER_INVTRANS 0x03
414
415/* constant type */
416#define CONSTANT_SCALAR 0x01
417#define CONSTANT_VECTOR 0x02
418
419/* program param type */
420#define PROGRAM_PARAM_ENV 0x01
421#define PROGRAM_PARAM_LOCAL 0x02
422
423/* register type */
424#define REGISTER_ATTRIB 0x01
425#define REGISTER_PARAM 0x02
426#define REGISTER_RESULT 0x03
427#define REGISTER_ESTABLISHED_NAME 0x04
428
429/* param binding */
430#define PARAM_NULL 0x00
431#define PARAM_ARRAY_ELEMENT 0x01
432#define PARAM_STATE_ELEMENT 0x02
433#define PARAM_PROGRAM_ELEMENT 0x03
434#define PARAM_PROGRAM_ELEMENTS 0x04
435#define PARAM_CONSTANT 0x05
436
437/* param state property */
438#define STATE_MATERIAL_PARSER 0x01
439#define STATE_LIGHT_PARSER 0x02
440#define STATE_LIGHT_MODEL 0x03
441#define STATE_LIGHT_PROD 0x04
442#define STATE_FOG 0x05
443#define STATE_MATRIX_ROWS 0x06
444/* GL_ARB_fragment_program */
445#define STATE_TEX_ENV 0x07
446#define STATE_DEPTH 0x08
447/* GL_ARB_vertex_program */
448#define STATE_TEX_GEN 0x09
449#define STATE_CLIP_PLANE 0x0A
450#define STATE_POINT 0x0B
451
452/* state material property */
453#define MATERIAL_AMBIENT 0x01
454#define MATERIAL_DIFFUSE 0x02
455#define MATERIAL_SPECULAR 0x03
456#define MATERIAL_EMISSION 0x04
457#define MATERIAL_SHININESS 0x05
458
459/* state light property */
460#define LIGHT_AMBIENT 0x01
461#define LIGHT_DIFFUSE 0x02
462#define LIGHT_SPECULAR 0x03
463#define LIGHT_POSITION 0x04
464#define LIGHT_ATTENUATION 0x05
465#define LIGHT_HALF 0x06
466#define LIGHT_SPOT_DIRECTION 0x07
467
468/* state light model property */
469#define LIGHT_MODEL_AMBIENT 0x01
470#define LIGHT_MODEL_SCENECOLOR 0x02
471
472/* state light product property */
473#define LIGHT_PROD_AMBIENT 0x01
474#define LIGHT_PROD_DIFFUSE 0x02
475#define LIGHT_PROD_SPECULAR 0x03
476
477/* state texture environment property */
478#define TEX_ENV_COLOR 0x01
479
480/* state texture generation coord property */
481#define TEX_GEN_EYE 0x01
482#define TEX_GEN_OBJECT 0x02
483
484/* state fog property */
485#define FOG_COLOR 0x01
486#define FOG_PARAMS 0x02
487
488/* state depth property */
489#define DEPTH_RANGE 0x01
490
491/* state point parameters property */
492#define POINT_SIZE 0x01
493#define POINT_ATTENUATION 0x02
494
495/* declaration */
496#define ATTRIB 0x01
497#define PARAM 0x02
498#define TEMP 0x03
499#define OUTPUT 0x04
500#define ALIAS 0x05
501/* GL_ARB_vertex_program */
502#define ADDRESS 0x06
503
504/*-----------------------------------------------------------------------
505 * From here on down is the semantic checking portion
506 *
507 */
508
509/**
510 * Variable Table Handling functions
511 */
512typedef enum
513{
514 vt_none,
515 vt_address,
516 vt_attrib,
517 vt_param,
518 vt_temp,
519 vt_output,
520 vt_alias
521} var_type;
522
523
Brian Paul7aebaf32005-10-30 21:23:23 +0000524/**
525 * Setting an explicit field for each of the binding properties is a bit
526 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000527 */
528struct var_cache
529{
Brian Paul6a769d92006-04-28 15:42:15 +0000530 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000531 var_type type;
532 GLuint address_binding; /* The index of the address register we should
533 * be using */
534 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000535 GLuint attrib_is_generic; /* If the attrib was specified through a generic
536 * vertex attrib */
537 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000538 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000539 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
540 * that this is aliased to */
541 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
542 * PROGRAM_ENV_PARAM} */
543 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
544 * the tokens representing our bound state (or constants)
545 * start */
546 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
547 * we take up with our state tokens or constants. Note that
548 * this is _not_ the same as the number of param registers
549 * we eventually use */
550 struct var_cache *next;
551};
552
553static GLvoid
554var_cache_create (struct var_cache **va)
555{
556 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
557 if (*va) {
558 (**va).name = NULL;
559 (**va).type = vt_none;
560 (**va).attrib_binding = ~0;
561 (**va).attrib_is_generic = 0;
562 (**va).temp_binding = ~0;
563 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000564 (**va).param_binding_type = ~0;
565 (**va).param_binding_begin = ~0;
566 (**va).param_binding_length = ~0;
567 (**va).alias_binding = NULL;
568 (**va).next = NULL;
569 }
570}
571
572static GLvoid
573var_cache_destroy (struct var_cache **va)
574{
575 if (*va) {
576 var_cache_destroy (&(**va).next);
577 _mesa_free (*va);
578 *va = NULL;
579 }
580}
581
582static GLvoid
583var_cache_append (struct var_cache **va, struct var_cache *nv)
584{
585 if (*va)
586 var_cache_append (&(**va).next, nv);
587 else
588 *va = nv;
589}
590
591static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000592var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000593{
Brian Paul0a360cf2005-01-17 01:21:03 +0000594 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000595
596 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000597 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000598 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000599 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000600 return va;
601 }
602
603 va = va->next;
604 }
605
606 return NULL;
607}
608
Brian Paula088f162006-09-05 23:08:51 +0000609
610
611/**
612 * Called when an error is detected while parsing/compiling a program.
613 * Sets the ctx->Program.ErrorString field to descript and records a
614 * GL_INVALID_OPERATION error.
615 * \param position position of error in program string
616 * \param descrip verbose error description
617 */
618static void
619program_error(GLcontext *ctx, GLint position, const char *descrip)
620{
621 if (descrip) {
622 const char *prefix = "glProgramString(", *suffix = ")";
623 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
624 _mesa_strlen(prefix) +
625 _mesa_strlen(suffix) + 1);
626 if (str) {
627 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
628 _mesa_error(ctx, GL_INVALID_OPERATION, str);
629 _mesa_free(str);
630 }
631 }
632 _mesa_set_program_error(ctx, position, descrip);
633}
634
635
Brianab31a3a2007-09-13 11:41:49 -0600636/**
637 * As above, but with an extra string parameter for more info.
638 */
639static void
640program_error2(GLcontext *ctx, GLint position, const char *descrip,
641 const char *var)
642{
643 if (descrip) {
644 const char *prefix = "glProgramString(", *suffix = ")";
645 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
646 _mesa_strlen(": ") +
647 _mesa_strlen(var) +
648 _mesa_strlen(prefix) +
649 _mesa_strlen(suffix) + 1);
650 if (str) {
651 _mesa_sprintf(str, "%s%s: %s%s", prefix, descrip, var, suffix);
652 _mesa_error(ctx, GL_INVALID_OPERATION, str);
653 _mesa_free(str);
654 }
655 }
656 {
657 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
658 _mesa_strlen(": ") +
659 _mesa_strlen(var) + 1);
660 if (str) {
661 _mesa_sprintf(str, "%s: %s", descrip, var);
662 }
663 _mesa_set_program_error(ctx, position, str);
664 if (str) {
665 _mesa_free(str);
666 }
667 }
668}
669
670
Brian Paula088f162006-09-05 23:08:51 +0000671
Jouk Jansen40322e12004-04-05 08:50:36 +0000672/**
673 * constructs an integer from 4 GLubytes in LE format
674 */
675static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000676parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000677{
678 GLuint value;
679
680 value = (GLuint) (*(*inst)++);
681 value += (GLuint) (*(*inst)++) * 0x100;
682 value += (GLuint) (*(*inst)++) * 0x10000;
683 value += (GLuint) (*(*inst)++) * 0x1000000;
684
685 return value;
686}
687
688/**
689 * This will, given a string, lookup the string as a variable name in the
690 * var cache. If the name is found, the var cache node corresponding to the
691 * var name is returned. If it is not found, a new entry is allocated
692 *
693 * \param I Points into the binary array where the string identifier begins
694 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
695 * \return The location on the var_cache corresponding the the string starting at I
696 */
697static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000698parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000699 struct arb_program *Program, GLuint * found)
700{
Brian Paula088f162006-09-05 23:08:51 +0000701 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000702 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000703 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000704
705 *inst += _mesa_strlen ((char *) i) + 1;
706
707 va = var_cache_find (*vc_head, i);
708
709 if (va) {
710 *found = 1;
711 return va;
712 }
713
714 *found = 0;
715 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000716 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000717
718 var_cache_append (vc_head, va);
719
720 return va;
721}
722
723static char *
Brian Paula088f162006-09-05 23:08:51 +0000724parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000725{
Brian Paula088f162006-09-05 23:08:51 +0000726 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000727 (void) Program;
728
Jouk Jansen40322e12004-04-05 08:50:36 +0000729 *inst += _mesa_strlen ((char *) i) + 1;
730
731 return (char *) i;
732}
733
734/**
Brian Paul05908952004-06-08 15:20:23 +0000735 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000736 */
Brian Paul05908952004-06-08 15:20:23 +0000737static GLint
Brian Paula088f162006-09-05 23:08:51 +0000738parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000739{
740 /*return *(*inst)++ != '+'; */
741
742 if (**inst == '-') {
743 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000744 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000745 }
746 else if (**inst == '+') {
747 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000748 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000749 }
750
Brian Paul05908952004-06-08 15:20:23 +0000751 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000752}
753
754/**
755 * parses and returns signed integer
756 */
757static GLint
Brian Paula088f162006-09-05 23:08:51 +0000758parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000759{
760 GLint sign;
761 GLint value;
762
763 /* check if *inst points to '+' or '-'
764 * if yes, grab the sign and increment *inst
765 */
766 sign = parse_sign (inst);
767
768 /* now check if *inst points to 0
769 * if yes, increment the *inst and return the default value
770 */
771 if (**inst == 0) {
772 (*inst)++;
773 return 0;
774 }
775
776 /* parse the integer as you normally would do it */
777 value = _mesa_atoi (parse_string_without_adding (inst, Program));
778
779 /* now, after terminating 0 there is a position
780 * to parse it - parse_position()
781 */
782 Program->Position = parse_position (inst);
783
Brian Paul05908952004-06-08 15:20:23 +0000784 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000785}
786
787/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000788 Accumulate this string of digits, and return them as
789 a large integer represented in floating point (for range).
790 If scale is not NULL, also accumulates a power-of-ten
791 integer scale factor that represents the number of digits
792 in the string.
793*/
794static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000795parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000796{
797 GLdouble value = 0.0;
798 GLdouble oscale = 1.0;
799
800 if (**inst == 0) { /* this string of digits is empty-- do nothing */
801 (*inst)++;
802 }
803 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000804 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000805 GLubyte digit = *((*inst)++);
806 value = value * 10.0 + (GLint) (digit - '0');
807 oscale *= 10.0;
808 }
809 assert(**inst == 0); /* integer string should end with 0 */
810 (*inst)++; /* skip over terminating 0 */
811 Program->Position = parse_position(inst); /* skip position (from integer) */
812 }
813 if (scale)
814 *scale = oscale;
815 return value;
816}
817
818/**
819 Parse an unsigned floating-point number from this stream of tokenized
820 characters. Example floating-point formats supported:
821 12.34
822 12
823 0.34
824 .34
825 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000826 */
827static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000828parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000829{
Brian Paul1ff8f502005-02-16 15:08:29 +0000830 GLint exponent;
831 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000832
Brian Paul1ff8f502005-02-16 15:08:29 +0000833 whole = parse_float_string(inst, Program, 0);
834 fraction = parse_float_string(inst, Program, &fracScale);
835
836 /* Parse signed exponent */
837 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000838
Brian Paul1ff8f502005-02-16 15:08:29 +0000839 /* Assemble parts of floating-point number: */
840 return (GLfloat) ((whole + fraction / fracScale) *
841 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000842}
843
844
845/**
846 */
847static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000848parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000849{
Brian Paul05908952004-06-08 15:20:23 +0000850 GLint sign = parse_sign (inst);
851 GLfloat value = parse_float (inst, Program);
852 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000853}
854
855/**
856 * This picks out a constant value from the parsed array. The constant vector is r
857 * returned in the *values array, which should be of length 4.
858 *
859 * \param values - The 4 component vector with the constant value in it
860 */
861static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000862parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000863 GLboolean use)
864{
865 GLuint components, i;
866
867
868 switch (*(*inst)++) {
869 case CONSTANT_SCALAR:
870 if (use == GL_TRUE) {
871 values[0] =
872 values[1] =
873 values[2] = values[3] = parse_float (inst, Program);
874 }
875 else {
876 values[0] =
877 values[1] =
878 values[2] = values[3] = parse_signed_float (inst, Program);
879 }
880
881 break;
882 case CONSTANT_VECTOR:
883 values[0] = values[1] = values[2] = 0;
884 values[3] = 1;
885 components = *(*inst)++;
886 for (i = 0; i < components; i++) {
887 values[i] = parse_signed_float (inst, Program);
888 }
889 break;
890 }
891}
892
893/**
894 * \param offset The offset from the address register that we should
895 * address
896 *
897 * \return 0 on sucess, 1 on error
898 */
899static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000900parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
901 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000902{
Brian Paul6a769d92006-04-28 15:42:15 +0000903 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000904 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000905 return 0;
906}
907
908/**
909 * \param color 0 if color type is primary, 1 if color type is secondary
910 * \return 0 on sucess, 1 on error
911 */
912static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000913parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000914 GLint * color)
915{
Brian Paula6c423d2004-08-25 15:59:48 +0000916 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000917 *color = *(*inst)++ != COLOR_PRIMARY;
918 return 0;
919}
920
921/**
922 * Get an integer corresponding to a generic vertex attribute.
923 *
924 * \return 0 on sucess, 1 on error
925 */
926static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000927parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000928 struct arb_program *Program, GLuint *attrib)
929{
Brian Paulbd997cd2004-07-20 21:12:56 +0000930 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000931
Michal Krolbb38cad2006-04-11 11:41:11 +0000932 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000933 {
Brian Paula088f162006-09-05 23:08:51 +0000934 program_error(ctx, Program->Position,
935 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000936 return 1;
937 }
938
Brian Paulbd997cd2004-07-20 21:12:56 +0000939 *attrib = (GLuint) i;
940
Jouk Jansen40322e12004-04-05 08:50:36 +0000941 return 0;
942}
943
944
945/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000946 * \param color The index of the color buffer to write into
947 * \return 0 on sucess, 1 on error
948 */
949static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000950parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000951 struct arb_program *Program, GLuint * color)
952{
953 GLint i = parse_integer (inst, Program);
954
955 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000956 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000957 return 1;
958 }
959
960 *color = (GLuint) i;
961 return 0;
962}
963
964
965/**
Ian Romanick2549c262009-01-14 10:05:40 -0800966 * Validate the index of a texture coordinate
967 *
Jouk Jansen40322e12004-04-05 08:50:36 +0000968 * \param coord The texture unit index
969 * \return 0 on sucess, 1 on error
970 */
971static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000972parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000973 struct arb_program *Program, GLuint * coord)
974{
Brian Paulbd997cd2004-07-20 21:12:56 +0000975 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000976
Ian Romanick2549c262009-01-14 10:05:40 -0800977 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureCoordUnits)) {
978 program_error(ctx, Program->Position, "Invalid texture coordinate index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000979 return 1;
980 }
981
Brian Paulbd997cd2004-07-20 21:12:56 +0000982 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000983 return 0;
984}
985
Ian Romanick2549c262009-01-14 10:05:40 -0800986
987/**
988 * Validate the index of a texture image unit
989 *
990 * \param coord The texture unit index
991 * \return 0 on sucess, 1 on error
992 */
993static GLuint
994parse_teximage_num (GLcontext * ctx, const GLubyte ** inst,
995 struct arb_program *Program, GLuint * coord)
996{
997 GLint i = parse_integer (inst, Program);
998
999 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureImageUnits)) {
1000 program_error(ctx, Program->Position, "Invalid texture image index");
1001 return 1;
1002 }
1003
1004 *coord = (GLuint) i;
1005 return 0;
1006}
1007
1008
Jouk Jansen40322e12004-04-05 08:50:36 +00001009/**
1010 * \param coord The weight index
1011 * \return 0 on sucess, 1 on error
1012 */
1013static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001014parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001015 GLint * coord)
1016{
1017 *coord = parse_integer (inst, Program);
1018
1019 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +00001020 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +00001021 return 1;
1022 }
1023
1024 return 0;
1025}
1026
1027/**
1028 * \param coord The clip plane index
1029 * \return 0 on sucess, 1 on error
1030 */
1031static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001032parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001033 struct arb_program *Program, GLint * coord)
1034{
1035 *coord = parse_integer (inst, Program);
1036
1037 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +00001038 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +00001039 return 1;
1040 }
1041
1042 return 0;
1043}
1044
1045
1046/**
1047 * \return 0 on front face, 1 on back face
1048 */
1049static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001050parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +00001051{
1052 switch (*(*inst)++) {
1053 case FACE_FRONT:
1054 return 0;
1055
1056 case FACE_BACK:
1057 return 1;
1058 }
1059 return 0;
1060}
1061
1062
1063/**
1064 * Given a matrix and a modifier token on the binary array, return tokens
1065 * that _mesa_fetch_state() [program.c] can understand.
1066 *
1067 * \param matrix - the matrix we are talking about
1068 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1069 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1070 * \return 0 on sucess, 1 on failure
1071 */
1072static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001073parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001074 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1075{
1076 GLubyte mat = *(*inst)++;
1077
1078 *matrix_idx = 0;
1079
1080 switch (mat) {
1081 case MATRIX_MODELVIEW:
Brian65319522007-02-21 11:08:21 -07001082 *matrix = STATE_MODELVIEW_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001083 *matrix_idx = parse_integer (inst, Program);
1084 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001085 program_error(ctx, Program->Position,
1086 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001087 return 1;
1088 }
1089 break;
1090
1091 case MATRIX_PROJECTION:
Brian65319522007-02-21 11:08:21 -07001092 *matrix = STATE_PROJECTION_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001093 break;
1094
1095 case MATRIX_MVP:
Brian65319522007-02-21 11:08:21 -07001096 *matrix = STATE_MVP_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001097 break;
1098
1099 case MATRIX_TEXTURE:
Brian65319522007-02-21 11:08:21 -07001100 *matrix = STATE_TEXTURE_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001101 *matrix_idx = parse_integer (inst, Program);
1102 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001103 program_error(ctx, Program->Position, "Invalid Texture Unit");
1104 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001105 return 1;
1106 }
1107 break;
1108
1109 /* This is not currently supported (ARB_matrix_palette) */
1110 case MATRIX_PALETTE:
1111 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001112 program_error(ctx, Program->Position,
1113 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001114 return 1;
1115 break;
1116
1117 case MATRIX_PROGRAM:
Brian65319522007-02-21 11:08:21 -07001118 *matrix = STATE_PROGRAM_MATRIX;
Jouk Jansen40322e12004-04-05 08:50:36 +00001119 *matrix_idx = parse_integer (inst, Program);
1120 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001121 program_error(ctx, Program->Position, "Invalid Program Matrix");
1122 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001123 return 1;
1124 }
1125 break;
1126 }
1127
1128 switch (*(*inst)++) {
1129 case MATRIX_MODIFIER_IDENTITY:
1130 *matrix_modifier = 0;
1131 break;
1132 case MATRIX_MODIFIER_INVERSE:
1133 *matrix_modifier = STATE_MATRIX_INVERSE;
1134 break;
1135 case MATRIX_MODIFIER_TRANSPOSE:
1136 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1137 break;
1138 case MATRIX_MODIFIER_INVTRANS:
1139 *matrix_modifier = STATE_MATRIX_INVTRANS;
1140 break;
1141 }
1142
1143 return 0;
1144}
1145
1146
1147/**
1148 * This parses a state string (rather, the binary version of it) into
1149 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1150 *
1151 * \param inst - the start in the binary arry to start working from
1152 * \param state_tokens - the storage for the 6-token state description
1153 * \return - 0 on sucess, 1 on error
1154 */
1155static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001156parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001157 struct arb_program *Program,
1158 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001159{
Brian Paul4ca0af12008-07-09 08:35:50 -06001160 GLubyte token = *(*inst)++;
1161
1162 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001163 case STATE_MATERIAL_PARSER:
1164 state_tokens[0] = STATE_MATERIAL;
1165 state_tokens[1] = parse_face_type (inst);
1166 switch (*(*inst)++) {
1167 case MATERIAL_AMBIENT:
1168 state_tokens[2] = STATE_AMBIENT;
1169 break;
1170 case MATERIAL_DIFFUSE:
1171 state_tokens[2] = STATE_DIFFUSE;
1172 break;
1173 case MATERIAL_SPECULAR:
1174 state_tokens[2] = STATE_SPECULAR;
1175 break;
1176 case MATERIAL_EMISSION:
1177 state_tokens[2] = STATE_EMISSION;
1178 break;
1179 case MATERIAL_SHININESS:
1180 state_tokens[2] = STATE_SHININESS;
1181 break;
1182 }
1183 break;
1184
1185 case STATE_LIGHT_PARSER:
1186 state_tokens[0] = STATE_LIGHT;
1187 state_tokens[1] = parse_integer (inst, Program);
1188
1189 /* Check the value of state_tokens[1] against the # of lights */
1190 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001191 program_error(ctx, Program->Position, "Invalid Light Number");
1192 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001193 return 1;
1194 }
1195
1196 switch (*(*inst)++) {
1197 case LIGHT_AMBIENT:
1198 state_tokens[2] = STATE_AMBIENT;
1199 break;
1200 case LIGHT_DIFFUSE:
1201 state_tokens[2] = STATE_DIFFUSE;
1202 break;
1203 case LIGHT_SPECULAR:
1204 state_tokens[2] = STATE_SPECULAR;
1205 break;
1206 case LIGHT_POSITION:
1207 state_tokens[2] = STATE_POSITION;
1208 break;
1209 case LIGHT_ATTENUATION:
1210 state_tokens[2] = STATE_ATTENUATION;
1211 break;
1212 case LIGHT_HALF:
Brianf958aab2007-02-21 15:23:11 -07001213 state_tokens[2] = STATE_HALF_VECTOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001214 break;
1215 case LIGHT_SPOT_DIRECTION:
1216 state_tokens[2] = STATE_SPOT_DIRECTION;
1217 break;
1218 }
1219 break;
1220
1221 case STATE_LIGHT_MODEL:
1222 switch (*(*inst)++) {
1223 case LIGHT_MODEL_AMBIENT:
1224 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1225 break;
1226 case LIGHT_MODEL_SCENECOLOR:
1227 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1228 state_tokens[1] = parse_face_type (inst);
1229 break;
1230 }
1231 break;
1232
1233 case STATE_LIGHT_PROD:
1234 state_tokens[0] = STATE_LIGHTPROD;
1235 state_tokens[1] = parse_integer (inst, Program);
1236
1237 /* Check the value of state_tokens[1] against the # of lights */
1238 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001239 program_error(ctx, Program->Position, "Invalid Light Number");
1240 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001241 return 1;
1242 }
1243
1244 state_tokens[2] = parse_face_type (inst);
1245 switch (*(*inst)++) {
1246 case LIGHT_PROD_AMBIENT:
1247 state_tokens[3] = STATE_AMBIENT;
1248 break;
1249 case LIGHT_PROD_DIFFUSE:
1250 state_tokens[3] = STATE_DIFFUSE;
1251 break;
1252 case LIGHT_PROD_SPECULAR:
1253 state_tokens[3] = STATE_SPECULAR;
1254 break;
1255 }
1256 break;
1257
1258
1259 case STATE_FOG:
1260 switch (*(*inst)++) {
1261 case FOG_COLOR:
Brianf1390a32007-02-23 17:11:01 -07001262 state_tokens[0] = STATE_FOG_COLOR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001263 break;
1264 case FOG_PARAMS:
Brianf1390a32007-02-23 17:11:01 -07001265 state_tokens[0] = STATE_FOG_PARAMS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001266 break;
1267 }
1268 break;
1269
1270 case STATE_TEX_ENV:
1271 state_tokens[1] = parse_integer (inst, Program);
1272 switch (*(*inst)++) {
1273 case TEX_ENV_COLOR:
1274 state_tokens[0] = STATE_TEXENV_COLOR;
1275 break;
1276 }
1277 break;
1278
1279 case STATE_TEX_GEN:
1280 {
1281 GLuint type, coord;
1282
1283 state_tokens[0] = STATE_TEXGEN;
1284 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1285
1286 if (parse_texcoord_num (ctx, inst, Program, &coord))
1287 return 1;
1288 state_tokens[1] = coord;
1289
1290 /* EYE or OBJECT */
Briand799b7a2007-09-13 11:29:00 -06001291 type = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001292
1293 /* 0 - s, 1 - t, 2 - r, 3 - q */
Briand799b7a2007-09-13 11:29:00 -06001294 coord = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001295
1296 if (type == TEX_GEN_EYE) {
1297 switch (coord) {
1298 case COMPONENT_X:
1299 state_tokens[2] = STATE_TEXGEN_EYE_S;
1300 break;
1301 case COMPONENT_Y:
1302 state_tokens[2] = STATE_TEXGEN_EYE_T;
1303 break;
1304 case COMPONENT_Z:
1305 state_tokens[2] = STATE_TEXGEN_EYE_R;
1306 break;
1307 case COMPONENT_W:
1308 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1309 break;
Briand799b7a2007-09-13 11:29:00 -06001310 default:
1311 _mesa_problem(ctx, "bad texgen component in "
1312 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001313 }
1314 }
1315 else {
1316 switch (coord) {
1317 case COMPONENT_X:
1318 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1319 break;
1320 case COMPONENT_Y:
1321 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1322 break;
1323 case COMPONENT_Z:
1324 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1325 break;
1326 case COMPONENT_W:
1327 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1328 break;
Briand799b7a2007-09-13 11:29:00 -06001329 default:
1330 _mesa_problem(ctx, "bad texgen component in "
1331 "parse_state_single_item()");
Jouk Jansen40322e12004-04-05 08:50:36 +00001332 }
1333 }
1334 }
1335 break;
1336
1337 case STATE_DEPTH:
1338 switch (*(*inst)++) {
1339 case DEPTH_RANGE:
1340 state_tokens[0] = STATE_DEPTH_RANGE;
1341 break;
1342 }
1343 break;
1344
1345 case STATE_CLIP_PLANE:
1346 state_tokens[0] = STATE_CLIPPLANE;
Brianaa9d22a2007-02-23 11:21:03 -07001347 if (parse_clipplane_num (ctx, inst, Program,
1348 (GLint *) &state_tokens[1]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001349 return 1;
1350 break;
1351
1352 case STATE_POINT:
Briand799b7a2007-09-13 11:29:00 -06001353 switch (*(*inst)++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001354 case POINT_SIZE:
Brian776bc9c2007-02-22 09:29:46 -07001355 state_tokens[0] = STATE_POINT_SIZE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001356 break;
1357
1358 case POINT_ATTENUATION:
Brian776bc9c2007-02-22 09:29:46 -07001359 state_tokens[0] = STATE_POINT_ATTENUATION;
Jouk Jansen40322e12004-04-05 08:50:36 +00001360 break;
1361 }
1362 break;
1363
1364 /* XXX: I think this is the correct format for a matrix row */
1365 case STATE_MATRIX_ROWS:
Brianaa9d22a2007-02-23 11:21:03 -07001366 if (parse_matrix(ctx, inst, Program,
1367 (GLint *) &state_tokens[0],
1368 (GLint *) &state_tokens[1],
1369 (GLint *) &state_tokens[4]))
Jouk Jansen40322e12004-04-05 08:50:36 +00001370 return 1;
1371
Brian65319522007-02-21 11:08:21 -07001372 state_tokens[2] = parse_integer (inst, Program); /* The first row to grab */
Jouk Jansen40322e12004-04-05 08:50:36 +00001373
1374 if ((**inst) != 0) { /* Either the last row, 0 */
Brian65319522007-02-21 11:08:21 -07001375 state_tokens[3] = parse_integer (inst, Program);
1376 if (state_tokens[3] < state_tokens[2]) {
Brian Paula088f162006-09-05 23:08:51 +00001377 program_error(ctx, Program->Position,
1378 "Second matrix index less than the first");
1379 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001380 return 1;
1381 }
1382 }
1383 else {
Brian65319522007-02-21 11:08:21 -07001384 state_tokens[3] = state_tokens[2];
Jouk Jansen40322e12004-04-05 08:50:36 +00001385 (*inst)++;
1386 }
1387 break;
1388 }
1389
1390 return 0;
1391}
1392
1393/**
1394 * This parses a state string (rather, the binary version of it) into
1395 * a 6-token similar for the state fetching code in program.c
1396 *
1397 * One might ask, why fetch these parameters into just like you fetch
1398 * state when they are already stored in other places?
1399 *
1400 * Because of array offsets -> We can stick env/local parameters in the
1401 * middle of a parameter array and then index someplace into the array
1402 * when we execute.
1403 *
1404 * One optimization might be to only do this for the cases where the
1405 * env/local parameters end up inside of an array, and leave the
1406 * single parameters (or arrays of pure env/local pareameters) in their
1407 * respective register files.
1408 *
1409 * For ENV parameters, the format is:
1410 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1411 * state_tokens[1] = STATE_ENV
1412 * state_tokens[2] = the parameter index
1413 *
1414 * for LOCAL parameters, the format is:
1415 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1416 * state_tokens[1] = STATE_LOCAL
1417 * state_tokens[2] = the parameter index
1418 *
1419 * \param inst - the start in the binary arry to start working from
1420 * \param state_tokens - the storage for the 6-token state description
1421 * \return - 0 on sucess, 1 on failure
1422 */
1423static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001424parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Brianaa9d22a2007-02-23 11:21:03 -07001425 struct arb_program *Program,
1426 gl_state_index state_tokens[STATE_LENGTH])
Jouk Jansen40322e12004-04-05 08:50:36 +00001427{
1428 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1429 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1430 else
1431 state_tokens[0] = STATE_VERTEX_PROGRAM;
1432
1433
1434 switch (*(*inst)++) {
1435 case PROGRAM_PARAM_ENV:
1436 state_tokens[1] = STATE_ENV;
1437 state_tokens[2] = parse_integer (inst, Program);
1438
1439 /* Check state_tokens[2] against the number of ENV parameters available */
1440 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001441 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001442 ||
1443 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001444 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001445 program_error(ctx, Program->Position,
1446 "Invalid Program Env Parameter");
1447 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001448 return 1;
1449 }
1450
1451 break;
1452
1453 case PROGRAM_PARAM_LOCAL:
1454 state_tokens[1] = STATE_LOCAL;
1455 state_tokens[2] = parse_integer (inst, Program);
1456
1457 /* Check state_tokens[2] against the number of LOCAL parameters available */
1458 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001459 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001460 ||
1461 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001462 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001463 program_error(ctx, Program->Position,
1464 "Invalid Program Local Parameter");
1465 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001466 return 1;
1467 }
1468 break;
1469 }
1470
1471 return 0;
1472}
1473
1474/**
1475 * For ARB_vertex_program, programs are not allowed to use both an explicit
1476 * vertex attribute and a generic vertex attribute corresponding to the same
1477 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1478 *
1479 * This will walk our var_cache and make sure that nobody does anything fishy.
1480 *
1481 * \return 0 on sucess, 1 on error
1482 */
1483static GLuint
1484generic_attrib_check(struct var_cache *vc_head)
1485{
1486 int a;
1487 struct var_cache *curr;
1488 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1489 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1490
1491 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1492 explicitAttrib[a] = GL_FALSE;
1493 genericAttrib[a] = GL_FALSE;
1494 }
1495
1496 curr = vc_head;
1497 while (curr) {
1498 if (curr->type == vt_attrib) {
1499 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001500 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001501 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001502 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001503 }
1504
1505 curr = curr->next;
1506 }
1507
1508 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1509 if ((explicitAttrib[a]) && (genericAttrib[a]))
1510 return 1;
1511 }
1512
1513 return 0;
1514}
1515
1516/**
1517 * This will handle the binding side of an ATTRIB var declaration
1518 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001519 * \param inputReg returns the input register index, one of the
1520 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001521 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001522 */
1523static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001524parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001525 struct arb_program *Program,
1526 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001527{
Jouk Jansen40322e12004-04-05 08:50:36 +00001528 GLint err = 0;
1529
1530 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001531
Jouk Jansen40322e12004-04-05 08:50:36 +00001532 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1533 switch (*(*inst)++) {
1534 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001535 {
1536 GLint coord;
1537 err = parse_color_type (ctx, inst, Program, &coord);
1538 *inputReg = FRAG_ATTRIB_COL0 + coord;
1539 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001540 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001541 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001542 {
Brian8fa6f732007-02-01 09:24:41 -07001543 GLuint texcoord = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001544 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1545 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1546 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001547 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001548 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001549 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001550 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001551 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001552 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001553 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001554 default:
1555 err = 1;
1556 break;
1557 }
1558 }
1559 else {
1560 switch (*(*inst)++) {
1561 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001562 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001563 break;
1564
1565 case VERTEX_ATTRIB_WEIGHT:
1566 {
1567 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001568 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001569 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001570#if 1
1571 /* hack for Warcraft (see bug 8060) */
1572 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001573 break;
Brian Paul3a557502006-09-05 23:15:29 +00001574#else
Brian Paula088f162006-09-05 23:08:51 +00001575 program_error(ctx, Program->Position,
1576 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001577 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001578#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001579 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001580
1581 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001582 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001583 break;
1584
1585 case VERTEX_ATTRIB_COLOR:
1586 {
1587 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001588 err = parse_color_type (ctx, inst, Program, &color);
1589 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001590 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001591 }
1592 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001593 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001594 }
1595 }
1596 break;
1597
1598 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001599 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001600 break;
1601
1602 case VERTEX_ATTRIB_TEXCOORD:
1603 {
Brian8fa6f732007-02-01 09:24:41 -07001604 GLuint unit = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001605 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001606 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001607 }
1608 break;
1609
Jouk Jansen40322e12004-04-05 08:50:36 +00001610 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001611 /* Not supported at this time */
1612 {
1613 const char *msg = "ARB_palette_matrix not supported";
1614 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001615 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001616 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001618
1619 case VERTEX_ATTRIB_GENERIC:
1620 {
1621 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001622 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001623 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001625 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1626 * attributes do not alias the conventional vertex
1627 * attributes.
1628 */
1629 if (attrib > 0)
1630 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001631 else
1632 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001633 }
1634 }
1635 break;
1636
1637 default:
1638 err = 1;
1639 break;
1640 }
1641 }
1642
Jouk Jansen40322e12004-04-05 08:50:36 +00001643 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001644 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001645 }
1646
Jouk Jansen40322e12004-04-05 08:50:36 +00001647 return err;
1648}
1649
Brian Paul7aebaf32005-10-30 21:23:23 +00001650
Jouk Jansen40322e12004-04-05 08:50:36 +00001651/**
1652 * This translates between a binary token for an output variable type
1653 * and the mesa token for the same thing.
1654 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001655 * \param inst The parsed tokens
1656 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001657 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001658 */
1659static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001660parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001661 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001662{
Brian Paul7aebaf32005-10-30 21:23:23 +00001663 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001664
Brian Paul7aebaf32005-10-30 21:23:23 +00001665 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001666 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001667 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001668 GLuint out_color;
1669
Brian Paulbe76b7f2004-10-04 14:40:05 +00001670 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001671 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001672 */
1673 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001674 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001675 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001676 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001677 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001678 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1679 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001680 }
1681 break;
1682
1683 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001684 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001685 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001686 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001687 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001688 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001689 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001690 GLint color_type;
1691 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001692 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1693 if (err)
1694 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001695
Jouk Jansen40322e12004-04-05 08:50:36 +00001696 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001697 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001698 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001699 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001700 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001701 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001702 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001703 }
1704 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001705 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001706 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001707 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001708 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001709 }
1710 /* primary color */
1711 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001712 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001713 }
1714 }
1715 }
1716 break;
1717
1718 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001719 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001720 break;
1721
1722 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001723 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001724 break;
1725
1726 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001727 {
1728 GLuint unit;
1729 if (parse_texcoord_num (ctx, inst, Program, &unit))
1730 return 1;
1731 *outputReg = VERT_RESULT_TEX0 + unit;
1732 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001733 break;
1734 }
1735
Brian Paulde997602005-11-12 17:53:14 +00001736 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001737
1738 return 0;
1739}
1740
Brian Paul7aebaf32005-10-30 21:23:23 +00001741
Jouk Jansen40322e12004-04-05 08:50:36 +00001742/**
1743 * This handles the declaration of ATTRIB variables
1744 *
1745 * XXX: Still needs
1746 * parse_vert_attrib_binding(), or something like that
1747 *
1748 * \return 0 on sucess, 1 on error
1749 */
1750static GLint
Brian Paula088f162006-09-05 23:08:51 +00001751parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001752 struct arb_program *Program)
1753{
1754 GLuint found;
Jouk Jansen40322e12004-04-05 08:50:36 +00001755 struct var_cache *attrib_var;
1756
1757 attrib_var = parse_string (inst, vc_head, Program, &found);
1758 Program->Position = parse_position (inst);
1759 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06001760 program_error2(ctx, Program->Position,
1761 "Duplicate variable declaration",
1762 (char *) attrib_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001763 return 1;
1764 }
1765
1766 attrib_var->type = vt_attrib;
1767
Brian Paul7aebaf32005-10-30 21:23:23 +00001768 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001769 &attrib_var->attrib_is_generic))
1770 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001771
Brian Paul7aebaf32005-10-30 21:23:23 +00001772 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001773 program_error(ctx, Program->Position,
1774 "Cannot use both a generic vertex attribute "
1775 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001776 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001777 }
1778
1779 Program->Base.NumAttributes++;
1780 return 0;
1781}
1782
1783/**
1784 * \param use -- TRUE if we're called when declaring implicit parameters,
1785 * FALSE if we're declaraing variables. This has to do with
1786 * if we get a signed or unsigned float for scalar constants
1787 */
1788static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001789parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001790 struct var_cache *param_var,
1791 struct arb_program *Program, GLboolean use)
1792{
1793 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001794 GLuint err = 0;
Roland Scheidegger8dc18842007-11-29 03:08:18 +01001795 gl_state_index state_tokens[STATE_LENGTH] = {0, 0, 0, 0, 0};
Jouk Jansen40322e12004-04-05 08:50:36 +00001796 GLfloat const_values[4];
1797
Brian Paul4ca0af12008-07-09 08:35:50 -06001798 GLubyte token = *(*inst)++;
1799
1800 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001801 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001802 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1803 return 1;
1804
1805 /* If we adding STATE_MATRIX that has multiple rows, we need to
1806 * unroll it and call _mesa_add_state_reference() for each row
1807 */
Brian65319522007-02-21 11:08:21 -07001808 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
1809 state_tokens[0] == STATE_PROJECTION_MATRIX ||
1810 state_tokens[0] == STATE_MVP_MATRIX ||
1811 state_tokens[0] == STATE_TEXTURE_MATRIX ||
1812 state_tokens[0] == STATE_PROGRAM_MATRIX)
1813 && (state_tokens[2] != state_tokens[3])) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001814 GLint row;
Brian65319522007-02-21 11:08:21 -07001815 const GLint first_row = state_tokens[2];
1816 const GLint last_row = state_tokens[3];
Jouk Jansen40322e12004-04-05 08:50:36 +00001817
1818 for (row = first_row; row <= last_row; row++) {
Brian65319522007-02-21 11:08:21 -07001819 state_tokens[2] = state_tokens[3] = row;
Jouk Jansen40322e12004-04-05 08:50:36 +00001820
Brian Paulde997602005-11-12 17:53:14 +00001821 idx = _mesa_add_state_reference(Program->Base.Parameters,
1822 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001823 if (param_var->param_binding_begin == ~0U)
1824 param_var->param_binding_begin = idx;
1825 param_var->param_binding_length++;
1826 Program->Base.NumParameters++;
1827 }
1828 }
1829 else {
Brian Paulde997602005-11-12 17:53:14 +00001830 idx = _mesa_add_state_reference(Program->Base.Parameters,
1831 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001832 if (param_var->param_binding_begin == ~0U)
1833 param_var->param_binding_begin = idx;
1834 param_var->param_binding_length++;
1835 Program->Base.NumParameters++;
1836 }
1837 break;
1838
1839 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001840 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1841 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001842 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001843 if (param_var->param_binding_begin == ~0U)
1844 param_var->param_binding_begin = idx;
1845 param_var->param_binding_length++;
1846 Program->Base.NumParameters++;
1847
1848 /* Check if there is more: 0 -> we're done, else its an integer */
1849 if (**inst) {
1850 GLuint out_of_range, new_idx;
1851 GLuint start_idx = state_tokens[2] + 1;
1852 GLuint end_idx = parse_integer (inst, Program);
1853
1854 out_of_range = 0;
1855 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1856 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001857 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001858 || ((state_tokens[1] == STATE_LOCAL)
1859 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001860 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001861 out_of_range = 1;
1862 }
1863 else {
1864 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001865 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001866 || ((state_tokens[1] == STATE_LOCAL)
1867 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001868 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001869 out_of_range = 1;
1870 }
1871 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001872 program_error(ctx, Program->Position,
1873 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001874 return 1;
1875 }
1876
1877 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1878 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001879 idx = _mesa_add_state_reference(Program->Base.Parameters,
1880 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001881 param_var->param_binding_length++;
1882 Program->Base.NumParameters++;
1883 }
1884 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001885 else {
1886 (*inst)++;
1887 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001888 break;
1889
1890 case PARAM_CONSTANT:
Brian978145a2008-03-27 16:00:15 -06001891 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
Jouk Jansen40322e12004-04-05 08:50:36 +00001892 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001893 idx = _mesa_add_named_constant(Program->Base.Parameters,
1894 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001895 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001896 if (param_var->param_binding_begin == ~0U)
1897 param_var->param_binding_begin = idx;
Brian978145a2008-03-27 16:00:15 -06001898 param_var->param_binding_type = PROGRAM_CONSTANT;
Jouk Jansen40322e12004-04-05 08:50:36 +00001899 param_var->param_binding_length++;
1900 Program->Base.NumParameters++;
1901 break;
1902
1903 default:
Brian Paula088f162006-09-05 23:08:51 +00001904 program_error(ctx, Program->Position,
1905 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001906 return 1;
1907 }
1908
1909 /* Make sure we haven't blown past our parameter limits */
1910 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1911 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001912 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001913 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1914 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001915 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001916 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001917 return 1;
1918 }
1919
1920 return err;
1921}
1922
Brian Paul7aebaf32005-10-30 21:23:23 +00001923
Jouk Jansen40322e12004-04-05 08:50:36 +00001924/**
1925 * This picks out PARAM program parameter bindings.
1926 *
1927 * XXX: This needs to be stressed & tested
1928 *
1929 * \return 0 on sucess, 1 on error
1930 */
1931static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001932parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001933 struct arb_program *Program)
1934{
Brian Paulbd997cd2004-07-20 21:12:56 +00001935 GLuint found, err;
1936 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001937 struct var_cache *param_var;
1938
1939 err = 0;
1940 param_var = parse_string (inst, vc_head, Program, &found);
1941 Program->Position = parse_position (inst);
1942
1943 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06001944 program_error2(ctx, Program->Position,
1945 "Duplicate variable declaration",
1946 (char *) param_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00001947 return 1;
1948 }
1949
1950 specified_length = parse_integer (inst, Program);
1951
1952 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001953 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001954 return 1;
1955 }
1956
1957 param_var->type = vt_param;
1958 param_var->param_binding_length = 0;
1959
1960 /* Right now, everything is shoved into the main state register file.
1961 *
1962 * In the future, it would be nice to leave things ENV/LOCAL params
1963 * in their respective register files, if possible
1964 */
1965 param_var->param_binding_type = PROGRAM_STATE_VAR;
1966
1967 /* Remember to:
1968 * * - add each guy to the parameter list
1969 * * - increment the param_var->param_binding_len
1970 * * - store the param_var->param_binding_begin for the first one
1971 * * - compare the actual len to the specified len at the end
1972 */
1973 while (**inst != PARAM_NULL) {
1974 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1975 return 1;
1976 }
1977
1978 /* Test array length here! */
1979 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001980 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001981 program_error(ctx, Program->Position,
1982 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001983 }
1984 }
1985
1986 (*inst)++;
1987
1988 return 0;
1989}
1990
1991/**
1992 *
1993 */
1994static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001995parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001996 struct arb_program *Program, struct var_cache **new_var)
1997{
1998 struct var_cache *param_var;
1999
2000 /* First, insert a dummy entry into the var_cache */
2001 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00002002 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00002003 param_var->type = vt_param;
2004
2005 param_var->param_binding_length = 0;
2006 /* Don't fill in binding_begin; We use the default value of -1
2007 * to tell if its already initialized, elsewhere.
2008 *
2009 * param_var->param_binding_begin = 0;
2010 */
2011 param_var->param_binding_type = PROGRAM_STATE_VAR;
2012
2013 var_cache_append (vc_head, param_var);
2014
2015 /* Then fill it with juicy parameter goodness */
2016 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
2017 return 1;
2018
2019 *new_var = param_var;
2020
2021 return 0;
2022}
2023
2024
2025/**
2026 * This handles the declaration of TEMP variables
2027 *
2028 * \return 0 on sucess, 1 on error
2029 */
2030static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002031parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002032 struct arb_program *Program)
2033{
2034 GLuint found;
2035 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002036
2037 while (**inst != 0) {
2038 temp_var = parse_string (inst, vc_head, Program, &found);
2039 Program->Position = parse_position (inst);
2040 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002041 program_error2(ctx, Program->Position,
2042 "Duplicate variable declaration",
2043 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002044 return 1;
2045 }
2046
2047 temp_var->type = vt_temp;
2048
2049 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
2050 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002051 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00002052 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
2053 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00002054 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00002055 program_error(ctx, Program->Position,
2056 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00002057 return 1;
2058 }
2059
2060 temp_var->temp_binding = Program->Base.NumTemporaries;
2061 Program->Base.NumTemporaries++;
2062 }
2063 (*inst)++;
2064
2065 return 0;
2066}
2067
2068/**
2069 * This handles variables of the OUTPUT variety
2070 *
2071 * \return 0 on sucess, 1 on error
2072 */
2073static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002074parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002075 struct arb_program *Program)
2076{
2077 GLuint found;
2078 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002079 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002080
2081 output_var = parse_string (inst, vc_head, Program, &found);
2082 Program->Position = parse_position (inst);
2083 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002084 program_error2(ctx, Program->Position,
2085 "Duplicate variable declaration",
2086 (char *) output_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002087 return 1;
2088 }
2089
2090 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002091
2092 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2093 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002094}
2095
2096/**
2097 * This handles variables of the ALIAS kind
2098 *
2099 * \return 0 on sucess, 1 on error
2100 */
2101static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002102parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002103 struct arb_program *Program)
2104{
2105 GLuint found;
2106 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002107
2108 temp_var = parse_string (inst, vc_head, Program, &found);
2109 Program->Position = parse_position (inst);
2110
2111 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002112 program_error2(ctx, Program->Position,
2113 "Duplicate variable declaration",
2114 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002115 return 1;
2116 }
2117
2118 temp_var->type = vt_alias;
2119 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2120 Program->Position = parse_position (inst);
2121
2122 if (!found)
2123 {
Brianab31a3a2007-09-13 11:41:49 -06002124 program_error2(ctx, Program->Position,
2125 "Undefined alias value",
2126 (char *) temp_var->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002127 return 1;
2128 }
2129
2130 return 0;
2131}
2132
2133/**
2134 * This handles variables of the ADDRESS kind
2135 *
2136 * \return 0 on sucess, 1 on error
2137 */
2138static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002139parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002140 struct arb_program *Program)
2141{
2142 GLuint found;
2143 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002144
2145 while (**inst != 0) {
2146 temp_var = parse_string (inst, vc_head, Program, &found);
2147 Program->Position = parse_position (inst);
2148 if (found) {
Brianab31a3a2007-09-13 11:41:49 -06002149 program_error2(ctx, Program->Position,
2150 "Duplicate variable declaration",
2151 (char *) temp_var->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002152 return 1;
2153 }
2154
2155 temp_var->type = vt_address;
2156
2157 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002158 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002159 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002160 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002161 return 1;
2162 }
2163
2164 temp_var->address_binding = Program->Base.NumAddressRegs;
2165 Program->Base.NumAddressRegs++;
2166 }
2167 (*inst)++;
2168
2169 return 0;
2170}
2171
2172/**
2173 * Parse a program declaration
2174 *
2175 * \return 0 on sucess, 1 on error
2176 */
2177static GLint
Brian Paula088f162006-09-05 23:08:51 +00002178parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002179 struct arb_program *Program)
2180{
2181 GLint err = 0;
2182
2183 switch (*(*inst)++) {
2184 case ADDRESS:
2185 err = parse_address (ctx, inst, vc_head, Program);
2186 break;
2187
2188 case ALIAS:
2189 err = parse_alias (ctx, inst, vc_head, Program);
2190 break;
2191
2192 case ATTRIB:
2193 err = parse_attrib (ctx, inst, vc_head, Program);
2194 break;
2195
2196 case OUTPUT:
2197 err = parse_output (ctx, inst, vc_head, Program);
2198 break;
2199
2200 case PARAM:
2201 err = parse_param (ctx, inst, vc_head, Program);
2202 break;
2203
2204 case TEMP:
2205 err = parse_temp (ctx, inst, vc_head, Program);
2206 break;
2207 }
2208
2209 return err;
2210}
2211
2212/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002213 * Handle the parsing out of a masked destination register, either for a
2214 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002215 *
2216 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002217 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002218 * position invariant
2219 *
2220 * \param File - The register file we write to
2221 * \param Index - The register index we write to
2222 * \param WriteMask - The mask controlling which components we write (1->write)
2223 *
2224 * \return 0 on sucess, 1 on error
2225 */
2226static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002227parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002228 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002229 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002230{
Brian Paul7aebaf32005-10-30 21:23:23 +00002231 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002232 struct var_cache *dst;
2233
2234 /* We either have a result register specified, or a
2235 * variable that may or may not be writable
2236 */
2237 switch (*(*inst)++) {
2238 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002239 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002240 return 1;
2241 *File = PROGRAM_OUTPUT;
2242 break;
2243
2244 case REGISTER_ESTABLISHED_NAME:
2245 dst = parse_string (inst, vc_head, Program, &result);
2246 Program->Position = parse_position (inst);
2247
2248 /* If the name has never been added to our symbol table, we're hosed */
2249 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002250 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002251 return 1;
2252 }
2253
2254 switch (dst->type) {
2255 case vt_output:
2256 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002257 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002258 break;
2259
2260 case vt_temp:
2261 *File = PROGRAM_TEMPORARY;
2262 *Index = dst->temp_binding;
2263 break;
2264
2265 /* If the var type is not vt_output or vt_temp, no go */
2266 default:
Brian Paula088f162006-09-05 23:08:51 +00002267 program_error(ctx, Program->Position,
2268 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002269 return 1;
2270 }
2271 break;
2272
2273 default:
Brian Paula088f162006-09-05 23:08:51 +00002274 program_error(ctx, Program->Position,
2275 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002276 return 1;
2277 }
2278
2279
2280 /* Position invariance test */
2281 /* This test is done now in syntax portion - when position invariance OPTION
2282 is specified, "result.position" rule is disabled so there is no way
2283 to write the position
2284 */
2285 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2286 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002287 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002288 "Vertex program specified position invariance and wrote vertex position");
2289 }*/
2290
2291 /* And then the mask.
2292 * w,a -> bit 0
2293 * z,b -> bit 1
2294 * y,g -> bit 2
2295 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002296 *
2297 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002298 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002299 tmp = (GLint) *(*inst)++;
2300 *WriteMask = (((tmp>>3) & 0x1) |
2301 ((tmp>>1) & 0x2) |
2302 ((tmp<<1) & 0x4) |
2303 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002304
2305 return 0;
2306}
2307
2308
2309/**
2310 * Handle the parsing of a address register
2311 *
2312 * \param Index - The register index we write to
2313 *
2314 * \return 0 on sucess, 1 on error
2315 */
2316static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002317parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002318 struct var_cache **vc_head,
2319 struct arb_program *Program, GLint * Index)
2320{
2321 struct var_cache *dst;
2322 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002323
2324 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002325
2326 dst = parse_string (inst, vc_head, Program, &result);
2327 Program->Position = parse_position (inst);
2328
2329 /* If the name has never been added to our symbol table, we're hosed */
2330 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002331 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002332 return 1;
2333 }
2334
2335 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002336 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002337 return 1;
2338 }
2339
2340 return 0;
2341}
2342
Brian Paul8e8fa632005-07-01 02:03:33 +00002343#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002344/**
2345 * Handle the parsing out of a masked address register
2346 *
2347 * \param Index - The register index we write to
2348 * \param WriteMask - The mask controlling which components we write (1->write)
2349 *
2350 * \return 0 on sucess, 1 on error
2351 */
2352static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002353parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002354 struct var_cache **vc_head,
2355 struct arb_program *Program, GLint * Index,
2356 GLboolean * WriteMask)
2357{
2358 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2359 return 1;
2360
2361 /* This should be 0x8 */
2362 (*inst)++;
2363
2364 /* Writemask of .x is implied */
2365 WriteMask[0] = 1;
2366 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2367
2368 return 0;
2369}
Brian Paul8e8fa632005-07-01 02:03:33 +00002370#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002371
2372/**
2373 * Parse out a swizzle mask.
2374 *
Brian Paul32df89e2005-10-29 18:26:43 +00002375 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002376 *
2377 * The len parameter allows us to grab 4 components for a vector
2378 * swizzle, or just 1 component for a scalar src register selection
2379 */
Brian Paul32df89e2005-10-29 18:26:43 +00002380static void
Brian Paula088f162006-09-05 23:08:51 +00002381parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002382{
Brian Paul32df89e2005-10-29 18:26:43 +00002383 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002384
Brian Paul32df89e2005-10-29 18:26:43 +00002385 for (i = 0; i < 4; i++)
2386 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387
Brian Paul32df89e2005-10-29 18:26:43 +00002388 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002389 switch (*(*inst)++) {
2390 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002391 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002392 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002393 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002394 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002395 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002396 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002397 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002398 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002399 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002400 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002401 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002402 default:
2403 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2404 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002405 }
2406 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002407}
2408
Jouk Jansen40322e12004-04-05 08:50:36 +00002409
Brian Paul32df89e2005-10-29 18:26:43 +00002410/**
2411 * Parse an extended swizzle mask which is a sequence of
2412 * four x/y/z/w/0/1 tokens.
2413 * \return swizzle four swizzle values
2414 * \return negateMask four element bitfield
2415 */
2416static void
Brian Paula088f162006-09-05 23:08:51 +00002417parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002418 GLubyte *negateMask)
2419{
2420 GLint i;
2421
2422 *negateMask = 0x0;
2423 for (i = 0; i < 4; i++) {
2424 GLubyte swz;
2425 if (parse_sign(inst) == -1)
2426 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002427
2428 swz = *(*inst)++;
2429
2430 switch (swz) {
2431 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002432 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002433 break;
2434 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002435 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002436 break;
2437 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002438 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002439 break;
2440 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002441 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002442 break;
2443 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002444 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002445 break;
2446 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002447 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002448 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002449 default:
2450 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2451 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002452 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002453 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002454}
2455
2456
2457static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002458parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2459 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002460 struct arb_program *Program,
2461 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002462 GLboolean *IsRelOffset )
2463{
2464 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002465 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002466 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002467
Keith Whitwell7c26b612005-04-21 14:46:57 +00002468 *IsRelOffset = 0;
2469
Jouk Jansen40322e12004-04-05 08:50:36 +00002470 /* And the binding for the src */
2471 switch (*(*inst)++) {
2472 case REGISTER_ATTRIB:
2473 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002474 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002475 return 1;
2476 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002477 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002478
2479 /* We need to insert a dummy variable into the var_cache so we can
2480 * catch generic vertex attrib aliasing errors
2481 */
2482 var_cache_create(&src);
2483 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002484 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002485 src->attrib_binding = binding;
2486 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002487 var_cache_append(vc_head, src);
2488 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002489 program_error(ctx, Program->Position,
2490 "Cannot use both a generic vertex attribute "
2491 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002492 return 1;
2493 }
2494 break;
2495
2496 case REGISTER_PARAM:
2497 switch (**inst) {
2498 case PARAM_ARRAY_ELEMENT:
2499 (*inst)++;
2500 src = parse_string (inst, vc_head, Program, &found);
2501 Program->Position = parse_position (inst);
2502
2503 if (!found) {
Brianab31a3a2007-09-13 11:41:49 -06002504 program_error2(ctx, Program->Position,
2505 "Undefined variable",
2506 (char *) src->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00002507 return 1;
2508 }
2509
Brian Paul95801792005-12-06 15:41:43 +00002510 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002511
2512 switch (*(*inst)++) {
2513 case ARRAY_INDEX_ABSOLUTE:
2514 offset = parse_integer (inst, Program);
2515
2516 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002517 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002518 program_error(ctx, Program->Position,
2519 "Index out of range");
2520 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002521 return 1;
2522 }
2523
2524 *Index = src->param_binding_begin + offset;
2525 break;
2526
2527 case ARRAY_INDEX_RELATIVE:
2528 {
2529 GLint addr_reg_idx, rel_off;
2530
2531 /* First, grab the address regiseter */
2532 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2533 return 1;
2534
2535 /* And the .x */
2536 ((*inst)++);
2537 ((*inst)++);
2538 ((*inst)++);
2539 ((*inst)++);
2540
2541 /* Then the relative offset */
2542 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2543
2544 /* And store it properly */
2545 *Index = src->param_binding_begin + rel_off;
2546 *IsRelOffset = 1;
2547 }
2548 break;
2549 }
2550 break;
2551
2552 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002553 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2554 return 1;
2555
Brian Paul95801792005-12-06 15:41:43 +00002556 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002557 *Index = src->param_binding_begin;
2558 break;
2559 }
2560 break;
2561
2562 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002563 src = parse_string (inst, vc_head, Program, &found);
2564 Program->Position = parse_position (inst);
2565
2566 /* If the name has never been added to our symbol table, we're hosed */
2567 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002568 program_error(ctx, Program->Position,
2569 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002570 return 1;
2571 }
2572
2573 switch (src->type) {
2574 case vt_attrib:
2575 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002576 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002577 break;
2578
2579 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2580 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002581 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002582 *Index = src->param_binding_begin;
2583 break;
2584
2585 case vt_temp:
2586 *File = PROGRAM_TEMPORARY;
2587 *Index = src->temp_binding;
2588 break;
2589
2590 /* If the var type is vt_output no go */
2591 default:
Brian Paula088f162006-09-05 23:08:51 +00002592 program_error(ctx, Program->Position,
2593 "destination register is read only");
2594 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002595 return 1;
2596 }
2597 break;
2598
2599 default:
Brian Paula088f162006-09-05 23:08:51 +00002600 program_error(ctx, Program->Position,
2601 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002602 return 1;
2603 }
2604
Markus Amslerf2b91422008-03-17 08:35:27 -06002605 /* Add attributes to InputsRead only if they are used the program.
2606 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2607 if (*File == PROGRAM_INPUT)
2608 Program->Base.InputsRead |= (1 << *Index);
2609
Jouk Jansen40322e12004-04-05 08:50:36 +00002610 return 0;
2611}
2612
Brian7d2b6a02008-03-27 16:17:37 -06002613
Jouk Jansen40322e12004-04-05 08:50:36 +00002614/**
Brian7d2b6a02008-03-27 16:17:37 -06002615 * Parse vertex/fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002616 */
2617static GLuint
Brian7d2b6a02008-03-27 16:17:37 -06002618parse_vector_src_reg(GLcontext *ctx, const GLubyte **inst,
2619 struct var_cache **vc_head,
2620 struct arb_program *program,
2621 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002622{
Brian Paul7aebaf32005-10-30 21:23:23 +00002623 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002624 GLint index;
Brian7d2b6a02008-03-27 16:17:37 -06002625 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002626 GLubyte swizzle[4];
2627 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002628
Jouk Jansen40322e12004-04-05 08:50:36 +00002629 /* Grab the sign */
Brian7d2b6a02008-03-27 16:17:37 -06002630 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002631
2632 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002633 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002634 return 1;
2635
2636 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002637 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002638
Brian Paul32df89e2005-10-29 18:26:43 +00002639 reg->File = file;
2640 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002641 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Brian7d2b6a02008-03-27 16:17:37 -06002642 reg->NegateBase = negateMask;
2643 reg->RelAddr = isRelOffset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002644 return 0;
2645}
2646
Jouk Jansen40322e12004-04-05 08:50:36 +00002647
Brian Paulb7fc1c32006-08-30 23:38:03 +00002648/**
Brian7d2b6a02008-03-27 16:17:37 -06002649 * Parse vertex/fragment program scalar source register.
2650 */
2651static GLuint
2652parse_scalar_src_reg(GLcontext *ctx, const GLubyte **inst,
2653 struct var_cache **vc_head,
2654 struct arb_program *program,
2655 struct prog_src_register *reg)
2656{
2657 enum register_file file;
2658 GLint index;
2659 GLubyte negateMask;
2660 GLubyte swizzle[4];
2661 GLboolean isRelOffset;
2662
2663 /* Grab the sign */
2664 negateMask = (parse_sign (inst) == -1) ? NEGATE_XYZW : NEGATE_NONE;
2665
2666 /* And the src reg */
2667 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
2668 return 1;
2669
2670 /* finally, the swizzle */
2671 parse_swizzle_mask(inst, swizzle, 1);
2672
2673 reg->File = file;
2674 reg->Index = index;
2675 reg->Swizzle = (swizzle[0] << 0);
2676 reg->NegateBase = negateMask;
2677 reg->RelAddr = isRelOffset;
2678 return 0;
2679}
2680
2681
2682/**
2683 * Parse vertex/fragment program destination register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002684 * \return 1 if error, 0 if no error.
2685 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002686static GLuint
Brian7d2b6a02008-03-27 16:17:37 -06002687parse_dst_reg(GLcontext * ctx, const GLubyte ** inst,
2688 struct var_cache **vc_head, struct arb_program *program,
2689 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002690{
Brian Paul7aebaf32005-10-30 21:23:23 +00002691 GLint mask;
2692 GLuint idx;
2693 enum register_file file;
2694
Brian7d2b6a02008-03-27 16:17:37 -06002695 if (parse_masked_dst_reg (ctx, inst, vc_head, program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002696 return 1;
2697
Keith Whitwell7c26b612005-04-21 14:46:57 +00002698 reg->File = file;
2699 reg->Index = idx;
2700 reg->WriteMask = mask;
2701 return 0;
2702}
2703
2704
Brian Paul7aebaf32005-10-30 21:23:23 +00002705/**
Jouk Jansen40322e12004-04-05 08:50:36 +00002706 * This is a big mother that handles getting opcodes into the instruction
2707 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002708 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002709 */
2710static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002711parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002712 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002713 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002714{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002715 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002716 GLuint texcoord;
2717 GLubyte instClass, type, code;
2718 GLboolean rel;
Ian Romanick7b559a92007-06-07 13:58:50 -07002719 GLuint shadow_tex = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002720
Brian Pauld6272e02006-10-29 18:03:16 +00002721 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002722
2723 /* Record the position in the program string for debugging */
2724 fp->StringPos = Program->Position;
2725
2726 /* OP_ALU_INST or OP_TEX_INST */
2727 instClass = *(*inst)++;
2728
2729 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2730 * OP_TEX_{SAMPLE, KIL}
2731 */
2732 type = *(*inst)++;
2733
2734 /* The actual opcode name */
2735 code = *(*inst)++;
2736
2737 /* Increment the correct count */
2738 switch (instClass) {
2739 case OP_ALU_INST:
2740 Program->NumAluInstructions++;
2741 break;
2742 case OP_TEX_INST:
2743 Program->NumTexInstructions++;
2744 break;
2745 }
2746
Jouk Jansen40322e12004-04-05 08:50:36 +00002747 switch (type) {
2748 case OP_ALU_VECTOR:
2749 switch (code) {
2750 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002751 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002752 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002753 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002754 break;
2755
2756 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002757 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002758 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002759 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002760 break;
2761
2762 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002763 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002764 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002765 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002766 break;
2767
2768 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002769 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002770 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002771 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002772 break;
2773
2774 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002775 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002776 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002777 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002778 break;
2779 }
2780
Brian7d2b6a02008-03-27 16:17:37 -06002781 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 return 1;
2783
Brian7d2b6a02008-03-27 16:17:37 -06002784 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002785 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002786 break;
2787
2788 case OP_ALU_SCALAR:
2789 switch (code) {
2790 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002791 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002792 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002793 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002794 break;
2795
2796 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002797 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002798 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002799 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002800 break;
2801
2802 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002803 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002804 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002805 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002806 break;
2807
2808 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002809 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002810 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002811 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002812 break;
2813
2814 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002815 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002816 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002817 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002818 break;
2819
2820 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002821 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002822 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002823 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002824 break;
2825
2826 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002827 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002828 case OP_SCS:
2829
Brian Paul7e807512005-11-05 17:10:45 +00002830 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002831 break;
2832 }
2833
Brian7d2b6a02008-03-27 16:17:37 -06002834 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002836
Brian7d2b6a02008-03-27 16:17:37 -06002837 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002838 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002839 break;
2840
2841 case OP_ALU_BINSC:
2842 switch (code) {
2843 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002844 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002846 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 break;
2848 }
2849
Brian7d2b6a02008-03-27 16:17:37 -06002850 if (parse_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002851 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002852
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002854 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002855 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002856 }
2857 break;
2858
2859
2860 case OP_ALU_BIN:
2861 switch (code) {
2862 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002863 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002864 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002865 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002866 break;
2867
2868 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002869 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002870 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002871 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002872 break;
2873
2874 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002875 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002876 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002877 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002878 break;
2879
2880 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002881 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002882 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002883 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002884 break;
2885
2886 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002887 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002888 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002889 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002890 break;
2891
2892 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002893 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002894 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002895 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002896 break;
2897
2898 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002899 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002900 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002901 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002902 break;
2903
2904 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002905 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002906 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002907 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002908 break;
2909
2910 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002911 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002912 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002913 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002914 break;
2915
2916 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002917 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002918 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002919 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002920 break;
2921
2922 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002923 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002924 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002925 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002926 break;
2927
2928 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002929 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002930 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002931 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002932 break;
2933 }
2934
Brian7d2b6a02008-03-27 16:17:37 -06002935 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 return 1;
2937 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002938 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002939 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002940 }
2941 break;
2942
2943 case OP_ALU_TRI:
2944 switch (code) {
2945 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002946 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002947 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002948 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002949 break;
2950
2951 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002952 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002953 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002954 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002955 break;
2956
2957 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002958 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002959 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002960 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002961 break;
2962 }
2963
Brian7d2b6a02008-03-27 16:17:37 -06002964 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002965 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002966
Jouk Jansen40322e12004-04-05 08:50:36 +00002967 for (a = 0; a < 3; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06002968 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002969 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002970 }
2971 break;
2972
2973 case OP_ALU_SWZ:
2974 switch (code) {
2975 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002976 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002977 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002978 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002979 break;
2980 }
Brian7d2b6a02008-03-27 16:17:37 -06002981 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002982 return 1;
2983
Keith Whitwell7c26b612005-04-21 14:46:57 +00002984 {
Brian Paul32df89e2005-10-29 18:26:43 +00002985 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002986 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002987 enum register_file file;
2988 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002989
Brian Paul32df89e2005-10-29 18:26:43 +00002990 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002991 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002992 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2993 fp->SrcReg[0].File = file;
2994 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002995 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002996 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2997 swizzle[1],
2998 swizzle[2],
2999 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003000 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003001 break;
3002
3003 case OP_TEX_SAMPLE:
3004 switch (code) {
3005 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003006 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003007 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00003008 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003009 break;
3010
3011 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003012 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003013 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00003014 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003015 break;
3016
3017 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00003018 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003019 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00003020 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003021 break;
3022 }
3023
Brian7d2b6a02008-03-27 16:17:37 -06003024 if (parse_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003025 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003026
Brian7d2b6a02008-03-27 16:17:37 -06003027 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003028 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003029
3030 /* texImageUnit */
Ian Romanick2549c262009-01-14 10:05:40 -08003031 if (parse_teximage_num (ctx, inst, Program, &texcoord))
Jouk Jansen40322e12004-04-05 08:50:36 +00003032 return 1;
3033 fp->TexSrcUnit = texcoord;
3034
3035 /* texTarget */
3036 switch (*(*inst)++) {
Ian Romanick7b559a92007-06-07 13:58:50 -07003037 case TEXTARGET_SHADOW1D:
3038 shadow_tex = 1 << texcoord;
3039 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003040 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00003041 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003042 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003043 case TEXTARGET_SHADOW2D:
3044 shadow_tex = 1 << texcoord;
3045 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003046 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00003047 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003048 break;
3049 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00003050 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003051 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003052 case TEXTARGET_SHADOWRECT:
3053 shadow_tex = 1 << texcoord;
3054 /* FALLTHROUGH */
Jouk Jansen40322e12004-04-05 08:50:36 +00003055 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00003056 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003057 break;
3058 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00003059 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003060 break;
Ian Romanick69358e72007-06-05 09:24:27 -07003061 case TEXTARGET_SHADOW1D_ARRAY:
Ian Romanick7b559a92007-06-07 13:58:50 -07003062 shadow_tex = 1 << texcoord;
3063 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003064 case TEXTARGET_1D_ARRAY:
3065 fp->TexSrcTarget = TEXTURE_1D_ARRAY_INDEX;
3066 break;
Ian Romanick7b559a92007-06-07 13:58:50 -07003067 case TEXTARGET_SHADOW2D_ARRAY:
3068 shadow_tex = 1 << texcoord;
3069 /* FALLTHROUGH */
Ian Romanickbb372f12007-05-16 15:34:22 -07003070 case TEXTARGET_2D_ARRAY:
3071 fp->TexSrcTarget = TEXTURE_2D_ARRAY_INDEX;
3072 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003073 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003074
3075 /* Don't test the first time a particular sampler is seen. Each time
3076 * after that, make sure the shadow state is the same.
3077 */
3078 if ((_mesa_bitcount(Program->TexturesUsed[texcoord]) > 0)
3079 && ((Program->ShadowSamplers & (1 << texcoord)) != shadow_tex)) {
3080 program_error(ctx, Program->Position,
3081 "texture image unit used for shadow sampling and non-shadow sampling");
3082 return 1;
3083 }
3084
Brian Paulb7fc1c32006-08-30 23:38:03 +00003085 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
3086 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3087 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00003088 program_error(ctx, Program->Position,
3089 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00003090 return 1;
3091 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003092
3093
3094 Program->ShadowSamplers |= shadow_tex;
Jouk Jansen40322e12004-04-05 08:50:36 +00003095 break;
3096
3097 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00003098 Program->UsesKill = 1;
Brian7d2b6a02008-03-27 16:17:37 -06003099 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003100 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00003101 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003102 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003103 default:
3104 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3105 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003106 }
3107
3108 return 0;
3109}
3110
Keith Whitwell7c26b612005-04-21 14:46:57 +00003111
3112/**
3113 * Handle the parsing out of a masked address register
3114 *
3115 * \param Index - The register index we write to
3116 * \param WriteMask - The mask controlling which components we write (1->write)
3117 *
3118 * \return 0 on sucess, 1 on error
3119 */
3120static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003121parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003122 struct var_cache **vc_head,
3123 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003124 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003125{
3126 GLint idx;
3127
3128 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3129 return 1;
3130
3131 /* This should be 0x8 */
3132 (*inst)++;
3133
3134 reg->File = PROGRAM_ADDRESS;
3135 reg->Index = idx;
3136
3137 /* Writemask of .x is implied */
3138 reg->WriteMask = 0x1;
3139 return 0;
3140}
3141
Keith Whitwell7c26b612005-04-21 14:46:57 +00003142
Jouk Jansen40322e12004-04-05 08:50:36 +00003143/**
3144 * This is a big mother that handles getting opcodes into the instruction
3145 * and handling the src & dst registers for vertex program instructions
3146 */
3147static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003148parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003149 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003150 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003151{
3152 GLint a;
3153 GLubyte type, code;
3154
3155 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3156 type = *(*inst)++;
3157
3158 /* The actual opcode name */
3159 code = *(*inst)++;
3160
Brian Pauld6272e02006-10-29 18:03:16 +00003161 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003162 /* Record the position in the program string for debugging */
3163 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003164
3165 switch (type) {
3166 /* XXX: */
3167 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003168 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003169
3170 /* Remember to set SrcReg.RelAddr; */
3171
3172 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003173 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003174 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003175
Jouk Jansen40322e12004-04-05 08:50:36 +00003176 vp->DstReg.File = PROGRAM_ADDRESS;
3177
3178 /* Get a scalar src register */
Brian7d2b6a02008-03-27 16:17:37 -06003179 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003180 return 1;
3181
3182 break;
3183
3184 case OP_ALU_VECTOR:
3185 switch (code) {
3186 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003187 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003188 break;
3189 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003190 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003191 break;
3192 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003193 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003194 break;
3195 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003196 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003197 break;
3198 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003199 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003200 break;
3201 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003202
Brian7d2b6a02008-03-27 16:17:37 -06003203 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003204 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003205
Brian7d2b6a02008-03-27 16:17:37 -06003206 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003207 return 1;
3208 break;
3209
3210 case OP_ALU_SCALAR:
3211 switch (code) {
3212 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003213 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003214 break;
3215 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003216 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003217 break;
3218 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003219 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003220 break;
3221 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003222 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003223 break;
3224 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003225 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003226 break;
3227 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003228 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003229 break;
3230 }
Brian7d2b6a02008-03-27 16:17:37 -06003231 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003232 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003233
Brian7d2b6a02008-03-27 16:17:37 -06003234 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003235 return 1;
3236 break;
3237
3238 case OP_ALU_BINSC:
3239 switch (code) {
3240 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003241 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003242 break;
3243 }
Brian7d2b6a02008-03-27 16:17:37 -06003244 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003246
Jouk Jansen40322e12004-04-05 08:50:36 +00003247 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003248 if (parse_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003249 return 1;
3250 }
3251 break;
3252
3253 case OP_ALU_BIN:
3254 switch (code) {
3255 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003256 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003257 break;
3258 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003259 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003260 break;
3261 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003262 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003263 break;
3264 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003265 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003266 break;
3267 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003268 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003269 break;
3270 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003271 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003272 break;
3273 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003274 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003275 break;
3276 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003277 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003278 break;
3279 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003280 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003281 break;
3282 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003283 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003284 break;
3285 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003286 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003287 break;
3288 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003289 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003290 break;
3291 }
Brian7d2b6a02008-03-27 16:17:37 -06003292 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003293 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003294
Jouk Jansen40322e12004-04-05 08:50:36 +00003295 for (a = 0; a < 2; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003296 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003297 return 1;
3298 }
3299 break;
3300
3301 case OP_ALU_TRI:
3302 switch (code) {
3303 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003304 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003305 break;
3306 }
3307
Brian7d2b6a02008-03-27 16:17:37 -06003308 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003309 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003310
Jouk Jansen40322e12004-04-05 08:50:36 +00003311 for (a = 0; a < 3; a++) {
Brian7d2b6a02008-03-27 16:17:37 -06003312 if (parse_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003313 return 1;
3314 }
3315 break;
3316
3317 case OP_ALU_SWZ:
3318 switch (code) {
3319 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003320 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003321 break;
3322 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003323 {
Brian Paul32df89e2005-10-29 18:26:43 +00003324 GLubyte swizzle[4];
3325 GLubyte negateMask;
3326 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003327 enum register_file file;
3328 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003329
Brian7d2b6a02008-03-27 16:17:37 -06003330 if (parse_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003331 return 1;
3332
Brian Paul32df89e2005-10-29 18:26:43 +00003333 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003334 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003335 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3336 vp->SrcReg[0].File = file;
3337 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003338 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003339 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3340 swizzle[1],
3341 swizzle[2],
3342 swizzle[3]);
3343 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003344 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003345 break;
3346 }
3347 return 0;
3348}
3349
3350#if DEBUG_PARSING
3351
3352static GLvoid
Jouk Jansen40322e12004-04-05 08:50:36 +00003353debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3354 struct arb_program *Program)
3355{
3356 struct var_cache *vc;
3357 GLint a, b;
3358
Brianb618ac82007-02-22 09:39:25 -07003359 fprintf (stderr, "debug_variables, vc_head: %p\n", (void*) vc_head);
Jouk Jansen40322e12004-04-05 08:50:36 +00003360
3361 /* First of all, print out the contents of the var_cache */
3362 vc = vc_head;
3363 while (vc) {
Brianb618ac82007-02-22 09:39:25 -07003364 fprintf (stderr, "[%p]\n", (void*) vc);
Jouk Jansen40322e12004-04-05 08:50:36 +00003365 switch (vc->type) {
3366 case vt_none:
3367 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3368 break;
3369 case vt_attrib:
3370 fprintf (stderr, "ATTRIB %s\n", vc->name);
3371 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3372 break;
3373 case vt_param:
3374 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3375 vc->param_binding_begin, vc->param_binding_length);
3376 b = vc->param_binding_begin;
3377 for (a = 0; a < vc->param_binding_length; a++) {
3378 fprintf (stderr, "%s\n",
Brianb618ac82007-02-22 09:39:25 -07003379 Program->Base.Parameters->Parameters[a + b].Name);
3380 if (Program->Base.Parameters->Parameters[a + b].Type == PROGRAM_STATE_VAR) {
3381 const char *s;
3382 s = _mesa_program_state_string(Program->Base.Parameters->Parameters
3383 [a + b].StateIndexes);
3384 fprintf(stderr, "%s\n", s);
3385 _mesa_free((char *) s);
Jouk Jansen40322e12004-04-05 08:50:36 +00003386 }
3387 else
3388 fprintf (stderr, "%f %f %f %f\n",
Brianb618ac82007-02-22 09:39:25 -07003389 Program->Base.Parameters->ParameterValues[a + b][0],
3390 Program->Base.Parameters->ParameterValues[a + b][1],
3391 Program->Base.Parameters->ParameterValues[a + b][2],
3392 Program->Base.Parameters->ParameterValues[a + b][3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003393 }
3394 break;
3395 case vt_temp:
3396 fprintf (stderr, "TEMP %s\n", vc->name);
3397 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3398 break;
3399 case vt_output:
3400 fprintf (stderr, "OUTPUT %s\n", vc->name);
3401 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3402 break;
3403 case vt_alias:
3404 fprintf (stderr, "ALIAS %s\n", vc->name);
Brianb618ac82007-02-22 09:39:25 -07003405 fprintf (stderr, " binding: 0x%p (%s)\n",
3406 (void*) vc->alias_binding, vc->alias_binding->name);
Jouk Jansen40322e12004-04-05 08:50:36 +00003407 break;
Brianb618ac82007-02-22 09:39:25 -07003408 default:
3409 /* nothing */
3410 ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003411 }
3412 vc = vc->next;
3413 }
3414}
3415
Brian Paul72030e02005-11-03 03:30:34 +00003416#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003417
3418
3419/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003420 * The main loop for parsing a fragment or vertex program
3421 *
Brian Paul72030e02005-11-03 03:30:34 +00003422 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003423 */
Brian Paul72030e02005-11-03 03:30:34 +00003424static GLint
Brian Paula088f162006-09-05 23:08:51 +00003425parse_instructions(GLcontext * ctx, const GLubyte * inst,
3426 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003427{
Brian Paul72030e02005-11-03 03:30:34 +00003428 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3429 ? ctx->Const.FragmentProgram.MaxInstructions
3430 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003431 GLint err = 0;
3432
Brian Paul72030e02005-11-03 03:30:34 +00003433 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3434
Jouk Jansen40322e12004-04-05 08:50:36 +00003435 Program->MajorVersion = (GLuint) * inst++;
3436 Program->MinorVersion = (GLuint) * inst++;
3437
3438 while (*inst != END) {
3439 switch (*inst++) {
3440
3441 case OPTION:
3442 switch (*inst++) {
3443 case ARB_PRECISION_HINT_FASTEST:
3444 Program->PrecisionOption = GL_FASTEST;
3445 break;
3446
3447 case ARB_PRECISION_HINT_NICEST:
3448 Program->PrecisionOption = GL_NICEST;
3449 break;
3450
3451 case ARB_FOG_EXP:
3452 Program->FogOption = GL_EXP;
3453 break;
3454
3455 case ARB_FOG_EXP2:
3456 Program->FogOption = GL_EXP2;
3457 break;
3458
3459 case ARB_FOG_LINEAR:
3460 Program->FogOption = GL_LINEAR;
3461 break;
3462
3463 case ARB_POSITION_INVARIANT:
3464 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003465 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003466 break;
3467
3468 case ARB_FRAGMENT_PROGRAM_SHADOW:
3469 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3470 /* TODO ARB_fragment_program_shadow code */
3471 }
3472 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003473
3474 case ARB_DRAW_BUFFERS:
3475 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3476 /* do nothing for now */
3477 }
3478 break;
Ian Romanickbb372f12007-05-16 15:34:22 -07003479
3480 case MESA_TEXTURE_ARRAY:
3481 /* do nothing for now */
3482 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003483 }
3484 break;
3485
3486 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003487 /* check length */
3488 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003489 program_error(ctx, Program->Position,
3490 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003491 return 1;
3492 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003493 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003494 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003495 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003496 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003497 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003498 }
3499 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003500 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003501 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003502 }
3503
Brian Paul72030e02005-11-03 03:30:34 +00003504 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003505 Program->Base.NumInstructions++;
3506 break;
3507
3508 case DECLARATION:
3509 err = parse_declaration (ctx, &inst, vc_head, Program);
3510 break;
3511
3512 default:
3513 break;
3514 }
3515
3516 if (err)
3517 break;
3518 }
3519
3520 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003521 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003522 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003523 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003524 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003525 /* YYY Wrong Position in program, whatever, at least not random -> crash
3526 Program->Position = parse_position (&inst);
3527 */
Brian Paul8c41a142005-11-19 15:36:28 +00003528 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003529 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003530 Program->Base.NumInstructions++;
3531
Brian Paul05051032005-11-01 04:36:33 +00003532 /*
3533 * Initialize native counts to logical counts. The device driver may
3534 * change them if program is translated into a hardware program.
3535 */
3536 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3537 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3538 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3539 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3540 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003541
Jouk Jansen40322e12004-04-05 08:50:36 +00003542 return err;
3543}
3544
Brian Paul7aebaf32005-10-30 21:23:23 +00003545
Jouk Jansen40322e12004-04-05 08:50:36 +00003546/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003547LONGSTRING static char core_grammar_text[] =
Brianc223c6b2007-07-04 13:15:20 -06003548#include "shader/grammar/grammar_syn.h"
Jouk Jansen40322e12004-04-05 08:50:36 +00003549;
3550
Brian Paul7aebaf32005-10-30 21:23:23 +00003551
Brian Paul8c41a142005-11-19 15:36:28 +00003552/**
3553 * Set a grammar parameter.
3554 * \param name the grammar parameter
3555 * \param value the new parameter value
3556 * \return 0 if OK, 1 if error
3557 */
3558static int
3559set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003560{
3561 char error_msg[300];
3562 GLint error_pos;
3563
Brian Paul8c41a142005-11-19 15:36:28 +00003564 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003565 return 0;
3566
3567 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3568 _mesa_set_program_error (ctx, error_pos, error_msg);
3569 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3570 return 1;
3571}
3572
Brian Paul8c41a142005-11-19 15:36:28 +00003573
3574/**
3575 * Enable support for the given language option in the parser.
3576 * \return 1 if OK, 0 if error
3577 */
3578static int
3579enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003580{
Brian Paul8c41a142005-11-19 15:36:28 +00003581 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003582}
3583
Brian Paul8c41a142005-11-19 15:36:28 +00003584
3585/**
3586 * Enable parser extensions based on which OpenGL extensions are supported
3587 * by this rendering context.
3588 *
3589 * \return GL_TRUE if OK, GL_FALSE if error.
3590 */
3591static GLboolean
3592enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003593{
Brian Paul8c41a142005-11-19 15:36:28 +00003594#if 0
3595 /* These are not supported at this time */
3596 if ((ctx->Extensions.ARB_vertex_blend ||
3597 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003598 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003599 return GL_FALSE;
3600 if (ctx->Extensions.ARB_matrix_palette
3601 && !enable_ext(ctx, id, "matrix_palette"))
3602 return GL_FALSE;
Ian Romanick7b559a92007-06-07 13:58:50 -07003603#endif
Brian Paul8c41a142005-11-19 15:36:28 +00003604 if (ctx->Extensions.ARB_fragment_program_shadow
3605 && !enable_ext(ctx, id, "fragment_program_shadow"))
3606 return GL_FALSE;
Brian Paul8c41a142005-11-19 15:36:28 +00003607 if (ctx->Extensions.EXT_point_parameters
3608 && !enable_ext(ctx, id, "point_parameters"))
3609 return GL_FALSE;
3610 if (ctx->Extensions.EXT_secondary_color
3611 && !enable_ext(ctx, id, "secondary_color"))
3612 return GL_FALSE;
3613 if (ctx->Extensions.EXT_fog_coord
3614 && !enable_ext(ctx, id, "fog_coord"))
3615 return GL_FALSE;
3616 if (ctx->Extensions.NV_texture_rectangle
3617 && !enable_ext(ctx, id, "texture_rectangle"))
3618 return GL_FALSE;
3619 if (ctx->Extensions.ARB_draw_buffers
3620 && !enable_ext(ctx, id, "draw_buffers"))
3621 return GL_FALSE;
Ian Romanickbb372f12007-05-16 15:34:22 -07003622 if (ctx->Extensions.MESA_texture_array
3623 && !enable_ext(ctx, id, "texture_array"))
3624 return GL_FALSE;
Brian Paul3a557502006-09-05 23:15:29 +00003625#if 1
3626 /* hack for Warcraft (see bug 8060) */
3627 enable_ext(ctx, id, "vertex_blend");
3628#endif
3629
Brian Paul8c41a142005-11-19 15:36:28 +00003630 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003631}
3632
Brian Paul8c41a142005-11-19 15:36:28 +00003633
Jouk Jansen40322e12004-04-05 08:50:36 +00003634/**
3635 * This kicks everything off.
3636 *
3637 * \param ctx - The GL Context
3638 * \param str - The program string
3639 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003640 * \param program - The arb_program struct to return all the parsed info in
3641 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003642 */
Brian Paul8c41a142005-11-19 15:36:28 +00003643static GLboolean
3644_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3645 const GLubyte *str, GLsizei len,
3646 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003647{
3648 GLint a, err, error_pos;
3649 char error_msg[300];
3650 GLuint parsed_len;
3651 struct var_cache *vc_head;
3652 grammar arbprogram_syn_id;
3653 GLubyte *parsed, *inst;
3654 GLubyte *strz = NULL;
3655 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3656
Brian Paul8c41a142005-11-19 15:36:28 +00003657 /* set the program target before parsing */
3658 program->Base.Target = target;
3659
Brian Paul7f76b8f2004-09-10 01:05:39 +00003660 /* Reset error state */
3661 _mesa_set_program_error(ctx, -1, NULL);
3662
Brian Paul8c41a142005-11-19 15:36:28 +00003663 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003664 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003665 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003666 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003667 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003668
3669 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3670 if (grammar_syn_id == 0) {
3671 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003672 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003673 _mesa_set_program_error (ctx, error_pos, error_msg);
3674 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003675 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003676 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003677 }
3678
Brian Paul8c41a142005-11-19 15:36:28 +00003679 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3680 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003681
Brian Paul49a80ca2006-04-28 15:40:11 +00003682 /* 'parsed' is unused here */
3683 _mesa_free (parsed);
3684 parsed = NULL;
3685
Brian Paul45703642005-10-29 15:52:31 +00003686 /* NOTE: we can't destroy grammar_syn_id right here because
3687 * grammar_destroy() can reset the last error
3688 */
Brian Paul8c41a142005-11-19 15:36:28 +00003689 if (err) {
3690 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003691 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3692 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003693 _mesa_error (ctx, GL_INVALID_OPERATION,
3694 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003695 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003696 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003697 }
3698
3699 grammar_destroy (grammar_syn_id);
3700
3701 arbprogram_syn_is_ok = 1;
3702 }
3703
3704 /* create the grammar object */
3705 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3706 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003707 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003708 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3709 _mesa_set_program_error (ctx, error_pos, error_msg);
3710 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003711 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003712 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003713 }
3714
3715 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003716 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003717 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3718 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003719 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003720 }
3721
Brian Paul8c41a142005-11-19 15:36:28 +00003722 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3723 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003724 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003725 }
3726
3727 /* check for NULL character occurences */
3728 {
Brian Paul8c41a142005-11-19 15:36:28 +00003729 GLint i;
3730 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003731 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003732 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003733 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003734 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003735 }
Brian Paul8c41a142005-11-19 15:36:28 +00003736 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003737 }
3738
3739 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003740 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003741 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003742 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3743 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003744 return GL_FALSE;
3745 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003746 _mesa_memcpy (strz, str, len);
3747 strz[len] = '\0';
3748
Michal Krolb80bc052004-10-21 14:09:54 +00003749 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003750 err = !grammar_fast_check(arbprogram_syn_id, strz,
3751 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003752
3753 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003754 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003755 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3756 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003757
Brian Paulb3aefd12005-09-19 20:12:32 +00003758#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003759 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003760 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003761 int line, col;
3762 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003763 fprintf(stderr, "program: %s\n", (char *) strz);
Eric Anholtb039b782008-01-15 15:09:21 -08003764 fprintf(stderr, "Error Pos: %d\n", ctx->Program.ErrorPos);
3765 s = (char *) _mesa_find_line_column(strz, strz+ctx->Program.ErrorPos,
Brian Paula088f162006-09-05 23:08:51 +00003766 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003767 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
Eric Anholtb039b782008-01-15 15:09:21 -08003768 } while (0);
Brian Paulb3aefd12005-09-19 20:12:32 +00003769#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003770
Brian Paula088f162006-09-05 23:08:51 +00003771 _mesa_free(strz);
3772 _mesa_free(parsed);
3773
Jouk Jansen40322e12004-04-05 08:50:36 +00003774 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003775 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003776 }
3777
Jouk Jansen40322e12004-04-05 08:50:36 +00003778 grammar_destroy (arbprogram_syn_id);
3779
Brian Paul8c41a142005-11-19 15:36:28 +00003780 /*
3781 * Program string is syntactically correct at this point
3782 * Parse the tokenized version of the program now, generating
3783 * vertex/fragment program instructions.
3784 */
3785
Jouk Jansen40322e12004-04-05 08:50:36 +00003786 /* Initialize the arb_program struct */
3787 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003788 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003789 program->Base.NumInstructions =
3790 program->Base.NumTemporaries =
3791 program->Base.NumParameters =
3792 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003793 program->Base.Parameters = _mesa_new_parameter_list ();
3794 program->Base.InputsRead = 0x0;
3795 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003796 program->Position = 0;
3797 program->MajorVersion = program->MinorVersion = 0;
3798 program->PrecisionOption = GL_DONT_CARE;
3799 program->FogOption = GL_NONE;
3800 program->HintPositionInvariant = GL_FALSE;
3801 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003802 program->TexturesUsed[a] = 0x0;
Ian Romanick7b559a92007-06-07 13:58:50 -07003803 program->ShadowSamplers = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003804 program->NumAluInstructions =
3805 program->NumTexInstructions =
3806 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003807 program->UsesKill = 0;
3808
Jouk Jansen40322e12004-04-05 08:50:36 +00003809 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003810 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003811
3812 /* Start examining the tokens in the array */
3813 inst = parsed;
3814
3815 /* Check the grammer rev */
3816 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003817 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003818 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003819 }
3820 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003821 /* ignore program target */
3822 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003823 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003824 }
3825
3826 /*debug_variables(ctx, vc_head, program); */
3827
3828 /* We're done with the parsed binary array */
3829 var_cache_destroy (&vc_head);
3830
3831 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003832
Brian Paul8c41a142005-11-19 15:36:28 +00003833 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3834 * to size [ap.Base.NumInstructions].
3835 */
Brian Paula7543902006-08-24 21:58:32 +00003836 program->Base.Instructions
3837 = _mesa_realloc_instructions(program->Base.Instructions,
3838 MAX_INSTRUCTIONS,
3839 program->Base.NumInstructions);
3840
Brian Paul45703642005-10-29 15:52:31 +00003841 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003842}
Brian Paul8c41a142005-11-19 15:36:28 +00003843
3844
3845
3846void
3847_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
3848 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003849 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003850{
3851 struct arb_program ap;
3852 GLuint i;
3853
3854 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00003855 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00003856 /* Error in the program. Just return. */
3857 return;
3858 }
3859
3860 /* Copy the relevant contents of the arb_program struct into the
3861 * fragment_program struct.
3862 */
3863 program->Base.String = ap.Base.String;
3864 program->Base.NumInstructions = ap.Base.NumInstructions;
3865 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3866 program->Base.NumParameters = ap.Base.NumParameters;
3867 program->Base.NumAttributes = ap.Base.NumAttributes;
3868 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003869 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3870 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3871 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3872 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3873 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -07003874 program->Base.NumAluInstructions = ap.Base.NumAluInstructions;
3875 program->Base.NumTexInstructions = ap.Base.NumTexInstructions;
3876 program->Base.NumTexIndirections = ap.Base.NumTexIndirections;
3877 program->Base.NumNativeAluInstructions = ap.Base.NumAluInstructions;
3878 program->Base.NumNativeTexInstructions = ap.Base.NumTexInstructions;
3879 program->Base.NumNativeTexIndirections = ap.Base.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00003880 program->Base.InputsRead = ap.Base.InputsRead;
3881 program->Base.OutputsWritten = ap.Base.OutputsWritten;
Brian Paul1af2b142008-05-14 16:44:48 -06003882 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
Brianc9db2232007-01-04 17:22:19 -07003883 program->Base.TexturesUsed[i] = ap.TexturesUsed[i];
Brian Paul1af2b142008-05-14 16:44:48 -06003884 if (ap.TexturesUsed[i])
3885 program->Base.SamplersUsed |= (1 << i);
3886 }
Ian Romanick7b559a92007-06-07 13:58:50 -07003887 program->Base.ShadowSamplers = ap.ShadowSamplers;
Brian Paul8c41a142005-11-19 15:36:28 +00003888 program->FogOption = ap.FogOption;
Keith Whitwell7ecdfb22007-03-04 21:47:05 +00003889 program->UsesKill = ap.UsesKill;
Brian Paul8c41a142005-11-19 15:36:28 +00003890
Brian Paulf8baad22008-10-06 12:29:29 -06003891 if (program->FogOption)
3892 program->Base.InputsRead |= FRAG_BIT_FOGC;
3893
Brian Paul8c41a142005-11-19 15:36:28 +00003894 if (program->Base.Instructions)
3895 _mesa_free(program->Base.Instructions);
3896 program->Base.Instructions = ap.Base.Instructions;
3897
3898 if (program->Base.Parameters)
3899 _mesa_free_parameter_list(program->Base.Parameters);
3900 program->Base.Parameters = ap.Base.Parameters;
3901
3902#if DEBUG_FP
Zack Rusin74964ff2008-06-10 16:59:44 -04003903 _mesa_printf("____________Fragment program %u ________\n", program->Base.Id);
Brian Paul11a54c32006-11-15 19:54:25 +00003904 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00003905#endif
3906}
3907
3908
3909
3910/**
3911 * Parse the vertex program string. If success, update the given
3912 * vertex_program object with the new program. Else, leave the vertex_program
3913 * object unchanged.
3914 */
3915void
3916_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
3917 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00003918 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00003919{
3920 struct arb_program ap;
3921
3922 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
3923
Brian Paul95801792005-12-06 15:41:43 +00003924 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian3075f262008-02-20 08:53:41 -07003925 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
Brian Paul8c41a142005-11-19 15:36:28 +00003926 return;
3927 }
3928
3929 /* Copy the relevant contents of the arb_program struct into the
3930 * vertex_program struct.
3931 */
3932 program->Base.String = ap.Base.String;
3933 program->Base.NumInstructions = ap.Base.NumInstructions;
3934 program->Base.NumTemporaries = ap.Base.NumTemporaries;
3935 program->Base.NumParameters = ap.Base.NumParameters;
3936 program->Base.NumAttributes = ap.Base.NumAttributes;
3937 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00003938 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
3939 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
3940 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
3941 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
3942 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00003943 program->Base.InputsRead = ap.Base.InputsRead;
3944 program->Base.OutputsWritten = ap.Base.OutputsWritten;
3945 program->IsPositionInvariant = ap.HintPositionInvariant;
3946
3947 if (program->Base.Instructions)
3948 _mesa_free(program->Base.Instructions);
3949 program->Base.Instructions = ap.Base.Instructions;
3950
3951 if (program->Base.Parameters)
3952 _mesa_free_parameter_list(program->Base.Parameters);
3953 program->Base.Parameters = ap.Base.Parameters;
3954
3955#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01003956 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00003957 _mesa_print_program(&program->Base);
3958#endif
3959}