blob: a3f50b931079064ce428d7b05a5c350d99c79762 [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian Paulf49c0d02006-11-02 16:20:29 +00003 * Version: 6.5.2
Michal Krol2861e732004-03-29 11:09:34 +00004 *
Brian Paul0c1cbd52006-05-24 03:01:58 +00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
Michal Krol2861e732004-03-29 11:09:34 +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/**
26 * \file program.c
27 * Vertex and fragment program support functions.
28 * \author Brian Paul
29 */
30
31
Brian Paul253204f2004-09-10 00:45:12 +000032/**
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
Michal Krol2861e732004-03-29 11:09:34 +000040#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
Keith Whitwell7c26b612005-04-21 14:46:57 +000054#define MAKE_SWIZZLE4(a,b,c,d) (((a)<<0) | ((b)<<3) | ((c)<<6) | ((d)<<9))
Keith Whitwell7c26b612005-04-21 14:46:57 +000055#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
Michal Krol2861e732004-03-29 11:09:34 +000076
Brian Paul122629f2006-07-20 16:49:57 +000077extern struct gl_program _mesa_DummyProgram;
Brian Paul9ca83922004-10-02 15:16:59 +000078
79
Michal Krol2861e732004-03-29 11:09:34 +000080/*
81 * Internal functions
82 */
83
84extern void
85_mesa_init_program(GLcontext *ctx);
86
87extern void
Brian Paul21841f02004-08-14 14:28:11 +000088_mesa_free_program_data(GLcontext *ctx);
89
90extern void
Michal Krol2861e732004-03-29 11:09:34 +000091_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string);
92
93extern const GLubyte *
94_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
95 GLint *line, GLint *col);
96
97
Brian Paul122629f2006-07-20 16:49:57 +000098extern struct gl_program *
Brian Paulcdb65412005-01-11 15:56:47 +000099_mesa_init_vertex_program(GLcontext *ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000100 struct gl_vertex_program *prog,
Brian Paulcdb65412005-01-11 15:56:47 +0000101 GLenum target, GLuint id);
Michal Krol2861e732004-03-29 11:09:34 +0000102
Brian Paul122629f2006-07-20 16:49:57 +0000103extern struct gl_program *
Brian Paulcdb65412005-01-11 15:56:47 +0000104_mesa_init_fragment_program(GLcontext *ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000105 struct gl_fragment_program *prog,
Brian Paulcdb65412005-01-11 15:56:47 +0000106 GLenum target, GLuint id);
107
Brian Paul122629f2006-07-20 16:49:57 +0000108extern struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000109_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id);
110
111extern void
Brian Paul122629f2006-07-20 16:49:57 +0000112_mesa_delete_program(GLcontext *ctx, struct gl_program *prog);
Michal Krol2861e732004-03-29 11:09:34 +0000113
Brian Paul4d12a052006-08-23 23:10:14 +0000114extern struct gl_program *
115_mesa_lookup_program(GLcontext *ctx, GLuint id);
Michal Krol2861e732004-03-29 11:09:34 +0000116
Brian Paul3b9b8de2006-08-24 21:57:36 +0000117extern struct prog_instruction *
Brian Paul9983a272006-08-25 15:13:25 +0000118_mesa_alloc_instructions(GLuint numInst);
119
120extern struct prog_instruction *
Brian Paul3b9b8de2006-08-24 21:57:36 +0000121_mesa_realloc_instructions(struct prog_instruction *oldInst,
122 GLuint numOldInst, GLuint numNewInst);
123
Michal Krol2861e732004-03-29 11:09:34 +0000124
Brian Paulfd4395b2005-11-05 03:02:28 +0000125/**
Michal Krol2861e732004-03-29 11:09:34 +0000126 * Used for describing GL state referenced from inside ARB vertex and
127 * fragment programs.
128 * A string such as "state.light[0].ambient" gets translated into a
129 * sequence of tokens such as [ STATE_LIGHT, 0, STATE_AMBIENT ].
130 */
Brianfe1d01c2006-12-13 14:54:47 -0700131typedef enum gl_state_index_ {
132 STATE_MATERIAL = 100, /* start at 100 so small ints are seen as ints */
Michal Krol2861e732004-03-29 11:09:34 +0000133
134 STATE_LIGHT,
135 STATE_LIGHTMODEL_AMBIENT,
136 STATE_LIGHTMODEL_SCENECOLOR,
137 STATE_LIGHTPROD,
138
139 STATE_TEXGEN,
140
141 STATE_FOG_COLOR,
142 STATE_FOG_PARAMS,
143
144 STATE_CLIPPLANE,
145
146 STATE_POINT_SIZE,
147 STATE_POINT_ATTENUATION,
148
149 STATE_MATRIX,
150 STATE_MODELVIEW,
151 STATE_PROJECTION,
152 STATE_MVP,
153 STATE_TEXTURE,
154 STATE_PROGRAM,
155 STATE_MATRIX_INVERSE,
156 STATE_MATRIX_TRANSPOSE,
157 STATE_MATRIX_INVTRANS,
158
159 STATE_AMBIENT,
160 STATE_DIFFUSE,
161 STATE_SPECULAR,
162 STATE_EMISSION,
163 STATE_SHININESS,
164 STATE_HALF,
165
166 STATE_POSITION,
167 STATE_ATTENUATION,
168 STATE_SPOT_DIRECTION,
169
170 STATE_TEXGEN_EYE_S,
171 STATE_TEXGEN_EYE_T,
172 STATE_TEXGEN_EYE_R,
173 STATE_TEXGEN_EYE_Q,
174 STATE_TEXGEN_OBJECT_S,
175 STATE_TEXGEN_OBJECT_T,
176 STATE_TEXGEN_OBJECT_R,
177 STATE_TEXGEN_OBJECT_Q,
178
179 STATE_TEXENV_COLOR,
180
181 STATE_DEPTH_RANGE,
182
183 STATE_VERTEX_PROGRAM,
184 STATE_FRAGMENT_PROGRAM,
185
186 STATE_ENV,
Keith Whitwell7c26b612005-04-21 14:46:57 +0000187 STATE_LOCAL,
188
189 STATE_INTERNAL, /* Mesa additions */
190 STATE_NORMAL_SCALE,
Keith Whitwell187f2cf2006-09-20 14:30:22 +0000191 STATE_TEXRECT_SCALE,
Brian Paula4806c92006-11-17 15:23:32 +0000192 STATE_POSITION_NORMALIZED, /* normalized light position */
193 STATE_INTERNAL_DRIVER /* first available state index for drivers (must be last) */
Brianfe1d01c2006-12-13 14:54:47 -0700194} gl_state_index;
Michal Krol2861e732004-03-29 11:09:34 +0000195
196
197
Brian Paul613e1ad2005-11-05 02:15:21 +0000198/**
Michal Krol2861e732004-03-29 11:09:34 +0000199 * Named program parameters
200 * Used for NV_fragment_program "DEFINE"d constants and "DECLARE"d parameters,
201 * and ARB_fragment_program global state references. For the later, Name
202 * might be "state.light[0].diffuse", for example.
203 */
Brian Paul122629f2006-07-20 16:49:57 +0000204struct gl_program_parameter
Michal Krol2861e732004-03-29 11:09:34 +0000205{
Brian Paul0c1cbd52006-05-24 03:01:58 +0000206 const char *Name; /**< Null-terminated string */
207 enum register_file Type; /**< PROGRAM_NAMED_PARAM, CONSTANT or STATE_VAR */
Brianfe1d01c2006-12-13 14:54:47 -0700208 /**
209 * A sequence of STATE_* tokens and integers to identify GL state.
210 */
211 gl_state_index StateIndexes[6];
Michal Krol2861e732004-03-29 11:09:34 +0000212};
213
214
Brian Paul0c1cbd52006-05-24 03:01:58 +0000215/**
216 * A list of the above program_parameter instances.
217 */
Brian Paul122629f2006-07-20 16:49:57 +0000218struct gl_program_parameter_list
Michal Krol2861e732004-03-29 11:09:34 +0000219{
Brian Paul0c1cbd52006-05-24 03:01:58 +0000220 GLuint Size; /**< allocated size of Parameters, ParameterValues */
221 GLuint NumParameters; /**< number of parameters in arrays */
Brian Paul122629f2006-07-20 16:49:57 +0000222 struct gl_program_parameter *Parameters; /**< Array [Size] */
Brian Paul0c1cbd52006-05-24 03:01:58 +0000223 GLfloat (*ParameterValues)[4]; /**< Array [Size] of GLfloat[4] */
224 GLbitfield StateFlags; /**< _NEW_* flags indicating which state changes
225 might invalidate ParameterValues[] */
Michal Krol2861e732004-03-29 11:09:34 +0000226};
227
228
229/*
230 * Program parameter functions
231 */
232
Brian Paul122629f2006-07-20 16:49:57 +0000233extern struct gl_program_parameter_list *
Michal Krol2861e732004-03-29 11:09:34 +0000234_mesa_new_parameter_list(void);
235
236extern void
Brian Paul122629f2006-07-20 16:49:57 +0000237_mesa_free_parameter_list(struct gl_program_parameter_list *paramList);
Michal Krol2861e732004-03-29 11:09:34 +0000238
Michal Krol2861e732004-03-29 11:09:34 +0000239extern GLint
Brian Paul122629f2006-07-20 16:49:57 +0000240_mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
Michal Krol2861e732004-03-29 11:09:34 +0000241 const char *name, const GLfloat values[4]);
242
243extern GLint
Brian Paul122629f2006-07-20 16:49:57 +0000244_mesa_add_named_constant(struct gl_program_parameter_list *paramList,
Brian Paul0c6723a2006-11-15 23:38:02 +0000245 const char *name, const GLfloat values[4],
246 GLuint size);
Michal Krol2861e732004-03-29 11:09:34 +0000247
248extern GLint
Brian Paul122629f2006-07-20 16:49:57 +0000249_mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
Brianfe1d01c2006-12-13 14:54:47 -0700250 const GLfloat values[4], GLuint size,
251 GLuint *swizzleOut);
252
253extern GLint
254_mesa_add_uniform(struct gl_program_parameter_list *paramList,
255 const char *name, GLuint size);
256
257extern GLint
258_mesa_add_varying(struct gl_program_parameter_list *paramList,
259 const char *name, GLuint size);
Michal Krol2861e732004-03-29 11:09:34 +0000260
261extern GLint
Brian Paul122629f2006-07-20 16:49:57 +0000262_mesa_add_state_reference(struct gl_program_parameter_list *paramList,
Brian Paul16241622005-11-03 02:26:47 +0000263 const GLint *stateTokens);
Michal Krol2861e732004-03-29 11:09:34 +0000264
265extern GLfloat *
Brian Paul699a33e2006-11-15 23:19:52 +0000266_mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
Michal Krol2861e732004-03-29 11:09:34 +0000267 GLsizei nameLen, const char *name);
268
269extern GLint
Brian Paul699a33e2006-11-15 23:19:52 +0000270_mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
Michal Krol2861e732004-03-29 11:09:34 +0000271 GLsizei nameLen, const char *name);
272
Brian Paul699a33e2006-11-15 23:19:52 +0000273extern GLboolean
274_mesa_lookup_parameter_constant(const struct gl_program_parameter_list *paramList,
275 const GLfloat v[], GLsizei vSize,
Brianfe1d01c2006-12-13 14:54:47 -0700276 GLint *posOut, GLuint *swizzleOut);
Brian Paul699a33e2006-11-15 23:19:52 +0000277
Michal Krol2861e732004-03-29 11:09:34 +0000278extern void
279_mesa_load_state_parameters(GLcontext *ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000280 struct gl_program_parameter_list *paramList);
Michal Krol2861e732004-03-29 11:09:34 +0000281
Brian Paul30d6a4b2005-11-05 20:18:18 +0000282extern void
Brian Paulde997602005-11-12 17:53:14 +0000283_mesa_print_instruction(const struct prog_instruction *inst);
284
Keith Whitwell56588102006-09-20 12:57:54 +0000285void
286_mesa_print_alu_instruction(const struct prog_instruction *inst,
287 const char *opcode_string,
288 GLuint numRegs);
289
Brian Paulde997602005-11-12 17:53:14 +0000290extern void
Brian Paul122629f2006-07-20 16:49:57 +0000291_mesa_print_program(const struct gl_program *prog);
Brian Paulde997602005-11-12 17:53:14 +0000292
293extern void
Brian Paul122629f2006-07-20 16:49:57 +0000294_mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog);
Brian Paul30d6a4b2005-11-05 20:18:18 +0000295
296
Michal Krol2861e732004-03-29 11:09:34 +0000297/*
Brian Paulfd4395b2005-11-05 03:02:28 +0000298 * API functions common to ARB/NV_vertex/fragment_program
Michal Krol2861e732004-03-29 11:09:34 +0000299 */
300
301extern void GLAPIENTRY
302_mesa_BindProgram(GLenum target, GLuint id);
303
304extern void GLAPIENTRY
305_mesa_DeletePrograms(GLsizei n, const GLuint *ids);
306
307extern void GLAPIENTRY
308_mesa_GenPrograms(GLsizei n, GLuint *ids);
309
Michal Krol2861e732004-03-29 11:09:34 +0000310
311
312/*
313 * GL_MESA_program_debug
314 */
315
316extern void
317_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
318 GLvoid *data);
319
320extern void
321_mesa_GetProgramRegisterfvMESA(GLenum target, GLsizei len,
322 const GLubyte *registerName, GLfloat *v);
323
324
325#endif /* PROGRAM_H */