7b9abec1c948344e715ff15fc4c5585d74892149
[mesa.git] / src / mesa / shader / arbfragparse.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.2
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
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_FP 0
26
27 /**
28 * \file arbfragparse.c
29 * ARB_fragment_program parser.
30 * \author Karl Rasche
31 */
32
33 #include "glheader.h"
34 #include "context.h"
35 #include "imports.h"
36 #include "macros.h"
37 #include "mtypes.h"
38 #include "program.h"
39 #include "arbprogparse.h"
40 #include "arbfragparse.h"
41
42 void
43 _mesa_debug_fp_inst(GLint num, struct fp_instruction *fp)
44 {
45 GLint a;
46
47 static const char *opcode_string[] = {
48 "ABS", /* ARB_f_p only */
49 "ADD",
50 "CMP", /* ARB_f_p only */
51 "COS",
52 "DDX", /* NV_f_p only */
53 "DDY", /* NV_f_p only */
54 "DP3",
55 "DP4",
56 "DPH", /* ARB_f_p only */
57 "DST",
58 "END", /* private opcode */
59 "EX2",
60 "FLR",
61 "FRC",
62 "KIL", /* ARB_f_p only */
63 "KIL_NV", /* NV_f_p only */
64 "LG2",
65 "LIT",
66 "LRP",
67 "MAD",
68 "MAX",
69 "MIN",
70 "MOV",
71 "MUL",
72 "PK2H", /* NV_f_p only */
73 "PK2US", /* NV_f_p only */
74 "PK4B", /* NV_f_p only */
75 "PK4UB", /* NV_f_p only */
76 "POW",
77 "PRINT", /* Mesa only */
78 "RCP",
79 "RFL", /* NV_f_p only */
80 "RSQ",
81 "SCS", /* ARB_f_p only */
82 "SEQ", /* NV_f_p only */
83 "SFL", /* NV_f_p only */
84 "SGE", /* NV_f_p only */
85 "SGT", /* NV_f_p only */
86 "SIN",
87 "SLE", /* NV_f_p only */
88 "SLT",
89 "SNE", /* NV_f_p only */
90 "STR", /* NV_f_p only */
91 "SUB",
92 "SWZ", /* ARB_f_p only */
93 "TEX",
94 "TXB", /* ARB_f_p only */
95 "TXD", /* NV_f_p only */
96 "TXP", /* ARB_f_p only */
97 "TXP_NV", /* NV_f_p only */
98 "UP2H", /* NV_f_p only */
99 "UP2US", /* NV_f_p only */
100 "UP4B", /* NV_f_p only */
101 "UP4UB", /* NV_f_p only */
102 "X2D", /* NV_f_p only - 2d mat mul */
103 "XPD", /* ARB_f_p only - cross product */
104 };
105
106 static const char *file_string[] = {
107 "TEMP",
108 "INPUT",
109 "OUTPUT",
110 "LOCAL",
111 "ENV",
112 "NAMED",
113 "STATE",
114 "WRITE_ONLY",
115 "ADDR"
116 };
117
118 static const char swz[] = "xyzw01??";
119
120 for (a=0; a<num; a++) {
121 _mesa_printf("%s", opcode_string[fp[a].Opcode]);
122
123 if (fp[a].Saturate)
124 _mesa_printf("_SAT");
125
126 if (fp[a].DstReg.File != 0xf) {
127 if (fp[a].DstReg.WriteMask != 0xf)
128 _mesa_printf(" %s[%d].%s%s%s%s ", file_string[fp[a].DstReg.File], fp[a].DstReg.Index,
129 GET_BIT(fp[a].DstReg.WriteMask, 0) ? "x" : "",
130 GET_BIT(fp[a].DstReg.WriteMask, 1) ? "y" : "",
131 GET_BIT(fp[a].DstReg.WriteMask, 2) ? "z" : "",
132 GET_BIT(fp[a].DstReg.WriteMask, 3) ? "w" : "");
133 else
134 _mesa_printf(" %s[%d] ", file_string[fp[a].DstReg.File], fp[a].DstReg.Index);
135 }
136
137 /* Examine each bit of negatebase here as this may be a SWZ instruction
138 */
139 if (fp[a].SrcReg[0].File != 0xf) {
140 if (fp[a].SrcReg[0].Swizzle != SWIZZLE_NOOP ||
141 fp[a].SrcReg[0].NegateBase)
142 _mesa_printf("%s[%d].%s%c%s%c%s%c%s%c ", file_string[fp[a].SrcReg[0].File], fp[a].SrcReg[0].Index,
143 GET_BIT(fp[a].SrcReg[0].NegateBase, 0) ? "-" : "",
144 swz[GET_SWZ(fp[a].SrcReg[0].Swizzle, 0)],
145 GET_BIT(fp[a].SrcReg[0].NegateBase, 0) ? "-" : "",
146 swz[GET_SWZ(fp[a].SrcReg[0].Swizzle, 1)],
147 GET_BIT(fp[a].SrcReg[0].NegateBase, 0) ? "-" : "",
148 swz[GET_SWZ(fp[a].SrcReg[0].Swizzle, 2)],
149 GET_BIT(fp[a].SrcReg[0].NegateBase, 0) ? "-" : "",
150 swz[GET_SWZ(fp[a].SrcReg[0].Swizzle, 3)]);
151 else
152 _mesa_printf("%s[%d] ", file_string[fp[a].SrcReg[0].File], fp[a].SrcReg[0].Index);
153 }
154
155 if (fp[a].SrcReg[1].File != 0xf) {
156 if (fp[a].SrcReg[1].Swizzle != SWIZZLE_NOOP ||
157 fp[a].SrcReg[1].NegateBase)
158 _mesa_printf("%s[%d].%s%c%c%c%c ", file_string[fp[a].SrcReg[1].File], fp[a].SrcReg[1].Index,
159 fp[a].SrcReg[1].NegateBase ? "-" : "",
160 swz[GET_SWZ(fp[a].SrcReg[1].Swizzle, 0)],
161 swz[GET_SWZ(fp[a].SrcReg[1].Swizzle, 1)],
162 swz[GET_SWZ(fp[a].SrcReg[1].Swizzle, 2)],
163 swz[GET_SWZ(fp[a].SrcReg[1].Swizzle, 3)]);
164 else
165 _mesa_printf("%s[%d] ", file_string[fp[a].SrcReg[1].File], fp[a].SrcReg[1].Index);
166 }
167
168 if (fp[a].SrcReg[2].File != 0xf) {
169 if (fp[a].SrcReg[2].Swizzle != SWIZZLE_NOOP ||
170 fp[a].SrcReg[2].NegateBase)
171 _mesa_printf("%s[%d].%s%c%c%c%c ", file_string[fp[a].SrcReg[2].File], fp[a].SrcReg[2].Index,
172 fp[a].SrcReg[1].NegateBase ? "-" : "",
173 swz[GET_SWZ(fp[a].SrcReg[2].Swizzle, 0)],
174 swz[GET_SWZ(fp[a].SrcReg[2].Swizzle, 1)],
175 swz[GET_SWZ(fp[a].SrcReg[2].Swizzle, 2)],
176 swz[GET_SWZ(fp[a].SrcReg[2].Swizzle, 3)]);
177 else
178 _mesa_printf("%s[%d] ", file_string[fp[a].SrcReg[2].File], fp[a].SrcReg[2].Index);
179 }
180
181 _mesa_printf("\n");
182 }
183 }
184
185
186 void
187 _mesa_parse_arb_fragment_program(GLcontext * ctx, GLenum target,
188 const GLubyte * str, GLsizei len,
189 struct fragment_program *program)
190 {
191 GLuint i;
192 struct arb_program ap;
193 (void) target;
194
195 /* set the program target before parsing */
196 ap.Base.Target = GL_FRAGMENT_PROGRAM_ARB;
197
198 if (!_mesa_parse_arb_program(ctx, str, len, &ap)) {
199 /* Error in the program. Just return. */
200 return;
201 }
202
203 /* Copy the relevant contents of the arb_program struct into the
204 * fragment_program struct.
205 */
206 program->Base.String = ap.Base.String;
207 program->Base.NumInstructions = ap.Base.NumInstructions;
208 program->Base.NumTemporaries = ap.Base.NumTemporaries;
209 program->Base.NumParameters = ap.Base.NumParameters;
210 program->Base.NumAttributes = ap.Base.NumAttributes;
211 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
212 program->NumAluInstructions = ap.NumAluInstructions;
213 program->NumTexInstructions = ap.NumTexInstructions;
214 program->NumTexIndirections = ap.NumTexIndirections;
215
216 program->InputsRead = ap.InputsRead;
217 program->OutputsWritten = ap.OutputsWritten;
218 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
219 program->TexturesUsed[i] = ap.TexturesUsed[i];
220
221 if (program->Parameters) {
222 /* free previous program's parameters */
223 _mesa_free_parameter_list(program->Parameters);
224 }
225 program->Parameters = ap.Parameters;
226 program->FogOption = ap.FogOption;
227
228 #if DEBUG_FP
229 _mesa_debug_fp_inst(ap.Base.NumInstructions, ap.FPInstructions);
230 #endif
231
232 program->Instructions = ap.FPInstructions;
233 }