New functions for cloning programs and parameter lists.
[mesa.git] / src / mesa / shader / program.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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 /**
26 * \file program.c
27 * Vertex and fragment program support functions.
28 * \author Brian Paul
29 */
30
31
32 /**
33 * \mainpage Mesa vertex and fragment program module
34 *
35 * This module or directory contains most of the code for vertex and
36 * fragment programs and shaders, including state management, parsers,
37 * and (some) software routines for executing programs
38 */
39
40 #ifndef PROGRAM_H
41 #define PROGRAM_H
42
43 #include "mtypes.h"
44
45
46 /* for GL_ARB_v_p and GL_ARB_f_p SWZ instruction */
47 #define SWIZZLE_X 0
48 #define SWIZZLE_Y 1
49 #define SWIZZLE_Z 2
50 #define SWIZZLE_W 3
51 #define SWIZZLE_ZERO 4 /* keep these values together: KW */
52 #define SWIZZLE_ONE 5 /* keep these values together: KW */
53
54 #define MAKE_SWIZZLE4(a,b,c,d) (((a)<<0) | ((b)<<3) | ((c)<<6) | ((d)<<9))
55 #define SWIZZLE_NOOP MAKE_SWIZZLE4(0,1,2,3)
56 #define GET_SWZ(swz, idx) (((swz) >> ((idx)*3)) & 0x7)
57 #define GET_BIT(msk, idx) (((msk) >> (idx)) & 0x1)
58
59
60 #define WRITEMASK_X 0x1
61 #define WRITEMASK_Y 0x2
62 #define WRITEMASK_XY 0x3
63 #define WRITEMASK_Z 0x4
64 #define WRITEMASK_XZ 0x5
65 #define WRITEMASK_YZ 0x6
66 #define WRITEMASK_XYZ 0x7
67 #define WRITEMASK_W 0x8
68 #define WRITEMASK_XW 0x9
69 #define WRITEMASK_YW 0xa
70 #define WRITEMASK_XYW 0xb
71 #define WRITEMASK_ZW 0xc
72 #define WRITEMASK_XZW 0xd
73 #define WRITEMASK_YZW 0xe
74 #define WRITEMASK_XYZW 0xf
75
76
77 extern struct gl_program _mesa_DummyProgram;
78
79
80 /*
81 * Internal functions
82 */
83
84 extern void
85 _mesa_init_program(GLcontext *ctx);
86
87 extern void
88 _mesa_free_program_data(GLcontext *ctx);
89
90 extern void
91 _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string);
92
93 extern const GLubyte *
94 _mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
95 GLint *line, GLint *col);
96
97
98 extern struct gl_program *
99 _mesa_init_vertex_program(GLcontext *ctx,
100 struct gl_vertex_program *prog,
101 GLenum target, GLuint id);
102
103 extern struct gl_program *
104 _mesa_init_fragment_program(GLcontext *ctx,
105 struct gl_fragment_program *prog,
106 GLenum target, GLuint id);
107
108 extern struct gl_program *
109 _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id);
110
111 extern void
112 _mesa_delete_program(GLcontext *ctx, struct gl_program *prog);
113
114 extern struct gl_program *
115 _mesa_lookup_program(GLcontext *ctx, GLuint id);
116
117 extern struct prog_instruction *
118 _mesa_alloc_instructions(GLuint numInst);
119
120 extern struct prog_instruction *
121 _mesa_realloc_instructions(struct prog_instruction *oldInst,
122 GLuint numOldInst, GLuint numNewInst);
123
124 extern struct gl_program *
125 _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog);
126
127
128 /**
129 * Used for describing GL state referenced from inside ARB vertex and
130 * fragment programs.
131 * A string such as "state.light[0].ambient" gets translated into a
132 * sequence of tokens such as [ STATE_LIGHT, 0, STATE_AMBIENT ].
133 */
134 typedef enum gl_state_index_ {
135 STATE_MATERIAL = 100, /* start at 100 so small ints are seen as ints */
136
137 STATE_LIGHT,
138 STATE_LIGHTMODEL_AMBIENT,
139 STATE_LIGHTMODEL_SCENECOLOR,
140 STATE_LIGHTPROD,
141
142 STATE_TEXGEN,
143
144 STATE_FOG_COLOR,
145 STATE_FOG_PARAMS,
146
147 STATE_CLIPPLANE,
148
149 STATE_POINT_SIZE,
150 STATE_POINT_ATTENUATION,
151
152 STATE_MATRIX,
153 STATE_MODELVIEW,
154 STATE_PROJECTION,
155 STATE_MVP,
156 STATE_TEXTURE,
157 STATE_PROGRAM,
158 STATE_MATRIX_INVERSE,
159 STATE_MATRIX_TRANSPOSE,
160 STATE_MATRIX_INVTRANS,
161
162 STATE_AMBIENT,
163 STATE_DIFFUSE,
164 STATE_SPECULAR,
165 STATE_EMISSION,
166 STATE_SHININESS,
167 STATE_HALF,
168
169 STATE_POSITION,
170 STATE_ATTENUATION,
171 STATE_SPOT_DIRECTION,
172
173 STATE_TEXGEN_EYE_S,
174 STATE_TEXGEN_EYE_T,
175 STATE_TEXGEN_EYE_R,
176 STATE_TEXGEN_EYE_Q,
177 STATE_TEXGEN_OBJECT_S,
178 STATE_TEXGEN_OBJECT_T,
179 STATE_TEXGEN_OBJECT_R,
180 STATE_TEXGEN_OBJECT_Q,
181
182 STATE_TEXENV_COLOR,
183
184 STATE_DEPTH_RANGE,
185
186 STATE_VERTEX_PROGRAM,
187 STATE_FRAGMENT_PROGRAM,
188
189 STATE_ENV,
190 STATE_LOCAL,
191
192 STATE_INTERNAL, /* Mesa additions */
193 STATE_NORMAL_SCALE,
194 STATE_TEXRECT_SCALE,
195 STATE_POSITION_NORMALIZED, /* normalized light position */
196 STATE_INTERNAL_DRIVER /* first available state index for drivers (must be last) */
197 } gl_state_index;
198
199
200
201 /**
202 * Named program parameters
203 * Used for NV_fragment_program "DEFINE"d constants and "DECLARE"d parameters,
204 * and ARB_fragment_program global state references. For the later, Name
205 * might be "state.light[0].diffuse", for example.
206 */
207 struct gl_program_parameter
208 {
209 const char *Name; /**< Null-terminated string */
210 enum register_file Type; /**< PROGRAM_NAMED_PARAM, CONSTANT or STATE_VAR */
211 GLuint Size; /**< Number of components (1..4) */
212 /**
213 * A sequence of STATE_* tokens and integers to identify GL state.
214 */
215 gl_state_index StateIndexes[6];
216 };
217
218
219 /**
220 * A list of the above program_parameter instances.
221 */
222 struct gl_program_parameter_list
223 {
224 GLuint Size; /**< allocated size of Parameters, ParameterValues */
225 GLuint NumParameters; /**< number of parameters in arrays */
226 struct gl_program_parameter *Parameters; /**< Array [Size] */
227 GLfloat (*ParameterValues)[4]; /**< Array [Size] of GLfloat[4] */
228 GLbitfield StateFlags; /**< _NEW_* flags indicating which state changes
229 might invalidate ParameterValues[] */
230 };
231
232
233 /*
234 * Program parameter functions
235 */
236
237 extern struct gl_program_parameter_list *
238 _mesa_new_parameter_list(void);
239
240 extern void
241 _mesa_free_parameter_list(struct gl_program_parameter_list *paramList);
242
243 extern struct gl_program_parameter_list *
244 _mesa_clone_parameter_list(const struct gl_program_parameter_list *list);
245
246 extern GLint
247 _mesa_add_parameter(struct gl_program_parameter_list *paramList,
248 const char *name, const GLfloat values[4], GLuint size,
249 enum register_file type);
250
251 extern GLint
252 _mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
253 const char *name, const GLfloat values[4]);
254
255 extern GLint
256 _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
257 const char *name, const GLfloat values[4],
258 GLuint size);
259
260 extern GLint
261 _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
262 const GLfloat values[4], GLuint size,
263 GLuint *swizzleOut);
264
265 extern GLint
266 _mesa_add_uniform(struct gl_program_parameter_list *paramList,
267 const char *name, GLuint size);
268
269 extern GLint
270 _mesa_add_varying(struct gl_program_parameter_list *paramList,
271 const char *name, GLuint size);
272
273 extern GLint
274 _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
275 const GLint *stateTokens);
276
277 extern GLfloat *
278 _mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
279 GLsizei nameLen, const char *name);
280
281 extern GLint
282 _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
283 GLsizei nameLen, const char *name);
284
285 extern GLboolean
286 _mesa_lookup_parameter_constant(const struct gl_program_parameter_list *paramList,
287 const GLfloat v[], GLsizei vSize,
288 GLint *posOut, GLuint *swizzleOut);
289
290 extern void
291 _mesa_load_state_parameters(GLcontext *ctx,
292 struct gl_program_parameter_list *paramList);
293
294 extern void
295 _mesa_print_instruction(const struct prog_instruction *inst);
296
297 void
298 _mesa_print_alu_instruction(const struct prog_instruction *inst,
299 const char *opcode_string,
300 GLuint numRegs);
301
302 extern void
303 _mesa_print_program(const struct gl_program *prog);
304
305 extern void
306 _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog);
307
308
309 /*
310 * API functions common to ARB/NV_vertex/fragment_program
311 */
312
313 extern void GLAPIENTRY
314 _mesa_BindProgram(GLenum target, GLuint id);
315
316 extern void GLAPIENTRY
317 _mesa_DeletePrograms(GLsizei n, const GLuint *ids);
318
319 extern void GLAPIENTRY
320 _mesa_GenPrograms(GLsizei n, GLuint *ids);
321
322
323
324 /*
325 * GL_MESA_program_debug
326 */
327
328 extern void
329 _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
330 GLvoid *data);
331
332 extern void
333 _mesa_GetProgramRegisterfvMESA(GLenum target, GLsizei len,
334 const GLubyte *registerName, GLfloat *v);
335
336
337 #endif /* PROGRAM_H */