blob: 45d99c974659fd5d9b259d348ab174f648a96aef [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian Paul16241622005-11-03 02:26:47 +00003 * Version: 6.5
Michal Krol2861e732004-03-29 11:09:34 +00004 *
Brian Paul16241622005-11-03 02:26:47 +00005 * Copyright (C) 1999-2005 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
32#include "glheader.h"
33#include "context.h"
34#include "hash.h"
35#include "imports.h"
36#include "macros.h"
37#include "mtypes.h"
38#include "program.h"
39#include "nvfragparse.h"
Brian Paul7e807512005-11-05 17:10:45 +000040#include "program_instruction.h"
Michal Krol2861e732004-03-29 11:09:34 +000041#include "nvvertparse.h"
Roland Scheideggerf519a772005-09-02 01:11:53 +000042#include "atifragshader.h"
Michal Krol2861e732004-03-29 11:09:34 +000043
44
45/**********************************************************************/
46/* Utility functions */
47/**********************************************************************/
48
49
Brian Paul765f1a12004-09-14 22:28:27 +000050/* A pointer to this dummy program is put into the hash table when
51 * glGenPrograms is called.
52 */
Brian Paul9ca83922004-10-02 15:16:59 +000053struct program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000054
55
Michal Krol2861e732004-03-29 11:09:34 +000056/**
Brian Paul21841f02004-08-14 14:28:11 +000057 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000058 */
59void
60_mesa_init_program(GLcontext *ctx)
61{
62 GLuint i;
63
64 ctx->Program.ErrorPos = -1;
65 ctx->Program.ErrorString = _mesa_strdup("");
66
67#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
68 ctx->VertexProgram.Enabled = GL_FALSE;
69 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
70 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Michal Krol2861e732004-03-29 11:09:34 +000071 ctx->VertexProgram.Current = (struct vertex_program *) ctx->Shared->DefaultVertexProgram;
72 assert(ctx->VertexProgram.Current);
73 ctx->VertexProgram.Current->Base.RefCount++;
74 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
75 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
76 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
77 }
78#endif
79
80#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
81 ctx->FragmentProgram.Enabled = GL_FALSE;
82 ctx->FragmentProgram.Current = (struct fragment_program *) ctx->Shared->DefaultFragmentProgram;
83 assert(ctx->FragmentProgram.Current);
84 ctx->FragmentProgram.Current->Base.RefCount++;
85#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000086
87#if FEATURE_ATI_fragment_shader
88 ctx->ATIFragmentShader.Enabled = GL_FALSE;
89 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
90 assert(ctx->ATIFragmentShader.Current);
91 ctx->ATIFragmentShader.Current->Base.RefCount++;
92#endif
Michal Krol2861e732004-03-29 11:09:34 +000093}
94
95
96/**
Brian Paul21841f02004-08-14 14:28:11 +000097 * Free a context's vertex/fragment program state
98 */
99void
100_mesa_free_program_data(GLcontext *ctx)
101{
102#if FEATURE_NV_vertex_program
103 if (ctx->VertexProgram.Current) {
104 ctx->VertexProgram.Current->Base.RefCount--;
105 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
106 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
107 }
108#endif
109#if FEATURE_NV_fragment_program
110 if (ctx->FragmentProgram.Current) {
111 ctx->FragmentProgram.Current->Base.RefCount--;
112 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
113 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
114 }
115#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000116#if FEATURE_ATI_fragment_shader
117 if (ctx->ATIFragmentShader.Current) {
118 ctx->ATIFragmentShader.Current->Base.RefCount--;
119 if (ctx->ATIFragmentShader.Current->Base.RefCount <= 0)
120 ctx->Driver.DeleteProgram(ctx, &(ctx->ATIFragmentShader.Current->Base));
121 }
122#endif
Brian Paul21841f02004-08-14 14:28:11 +0000123 _mesa_free((void *) ctx->Program.ErrorString);
124}
125
126
127
128
129/**
Michal Krol2861e732004-03-29 11:09:34 +0000130 * Set the vertex/fragment program error state (position and error string).
131 * This is generally called from within the parsers.
132 */
133void
134_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
135{
136 ctx->Program.ErrorPos = pos;
137 _mesa_free((void *) ctx->Program.ErrorString);
138 if (!string)
139 string = "";
140 ctx->Program.ErrorString = _mesa_strdup(string);
141}
142
143
144/**
145 * Find the line number and column for 'pos' within 'string'.
146 * Return a copy of the line which contains 'pos'. Free the line with
147 * _mesa_free().
148 * \param string the program string
149 * \param pos the position within the string
150 * \param line returns the line number corresponding to 'pos'.
151 * \param col returns the column number corresponding to 'pos'.
152 * \return copy of the line containing 'pos'.
153 */
154const GLubyte *
155_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
156 GLint *line, GLint *col)
157{
158 const GLubyte *lineStart = string;
159 const GLubyte *p = string;
160 GLubyte *s;
161 int len;
162
163 *line = 1;
164
165 while (p != pos) {
166 if (*p == (GLubyte) '\n') {
167 (*line)++;
168 lineStart = p + 1;
169 }
170 p++;
171 }
172
173 *col = (pos - lineStart) + 1;
174
175 /* return copy of this line */
176 while (*p != 0 && *p != '\n')
177 p++;
178 len = p - lineStart;
179 s = (GLubyte *) _mesa_malloc(len + 1);
180 _mesa_memcpy(s, lineStart, len);
181 s[len] = 0;
182
183 return s;
184}
185
186
Brian Paul765f1a12004-09-14 22:28:27 +0000187/**
188 * Initialize a new vertex/fragment program object.
189 */
190static struct program *
191_mesa_init_program_struct( GLcontext *ctx, struct program *prog,
192 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000193{
Brian Paula6c423d2004-08-25 15:59:48 +0000194 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000195 if (prog) {
196 prog->Id = id;
197 prog->Target = target;
198 prog->Resident = GL_TRUE;
199 prog->RefCount = 1;
200 }
201
202 return prog;
203}
204
Brian Paul765f1a12004-09-14 22:28:27 +0000205
206/**
207 * Initialize a new fragment program object.
208 */
209struct program *
210_mesa_init_fragment_program( GLcontext *ctx, struct fragment_program *prog,
211 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000212{
213 if (prog)
214 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
215 else
216 return NULL;
217}
218
Brian Paul765f1a12004-09-14 22:28:27 +0000219
220/**
221 * Initialize a new vertex program object.
222 */
223struct program *
224_mesa_init_vertex_program( GLcontext *ctx, struct vertex_program *prog,
225 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000226{
227 if (prog)
228 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
229 else
230 return NULL;
231}
232
Dave Airlie7f752fe2004-12-19 03:06:59 +0000233/**
234 * Initialize a new ATI fragment shader object.
235 */
236struct program *
Brian Paulcdb65412005-01-11 15:56:47 +0000237_mesa_init_ati_fragment_shader( GLcontext *ctx,
238 struct ati_fragment_shader *prog,
239 GLenum target, GLuint id )
Dave Airlie7f752fe2004-12-19 03:06:59 +0000240{
241 if (prog)
242 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
243 else
244 return NULL;
245}
246
247
Michal Krol2861e732004-03-29 11:09:34 +0000248
249/**
250 * Allocate and initialize a new fragment/vertex program object but
251 * don't put it into the program hash table. Called via
252 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
253 * device driver function to implement OO deriviation with additional
254 * types not understood by this function.
255 *
256 * \param ctx context
257 * \param id program id/number
258 * \param target program target/type
259 * \return pointer to new program object
260 */
261struct program *
262_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
263{
264 switch (target) {
265 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
266 return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program),
267 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000268 case GL_FRAGMENT_PROGRAM_NV:
269 case GL_FRAGMENT_PROGRAM_ARB:
270 return _mesa_init_fragment_program( ctx, CALLOC_STRUCT(fragment_program),
271 target, id );
Dave Airlie7f752fe2004-12-19 03:06:59 +0000272 case GL_FRAGMENT_SHADER_ATI:
273 return _mesa_init_ati_fragment_shader( ctx, CALLOC_STRUCT(ati_fragment_shader),
274 target, id );
275
Michal Krol2861e732004-03-29 11:09:34 +0000276 default:
277 _mesa_problem(ctx, "bad target in _mesa_new_program");
278 return NULL;
279 }
280}
281
282
283/**
284 * Delete a program and remove it from the hash table, ignoring the
285 * reference count.
286 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
287 * by a device driver function.
288 */
289void
290_mesa_delete_program(GLcontext *ctx, struct program *prog)
291{
Brian Paula6c423d2004-08-25 15:59:48 +0000292 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000293 ASSERT(prog);
294
295 if (prog->String)
296 _mesa_free(prog->String);
297 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
298 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
299 struct vertex_program *vprog = (struct vertex_program *) prog;
Brian Paul575700f2004-12-16 03:07:18 +0000300 if (vprog->Instructions) {
301 GLuint i;
302 for (i = 0; i < vprog->Base.NumInstructions; i++) {
303 if (vprog->Instructions[i].Data)
304 _mesa_free(vprog->Instructions[i].Data);
305 }
Michal Krol2861e732004-03-29 11:09:34 +0000306 _mesa_free(vprog->Instructions);
Brian Paul575700f2004-12-16 03:07:18 +0000307 }
Brian Paul21841f02004-08-14 14:28:11 +0000308 if (vprog->Parameters)
309 _mesa_free_parameter_list(vprog->Parameters);
Michal Krol2861e732004-03-29 11:09:34 +0000310 }
311 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
312 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
313 struct fragment_program *fprog = (struct fragment_program *) prog;
Brian Paul2a5afe32004-12-18 16:18:00 +0000314 if (fprog->Instructions) {
315 GLuint i;
316 for (i = 0; i < fprog->Base.NumInstructions; i++) {
317 if (fprog->Instructions[i].Data)
318 _mesa_free(fprog->Instructions[i].Data);
319 }
Michal Krol2861e732004-03-29 11:09:34 +0000320 _mesa_free(fprog->Instructions);
Brian Paul2a5afe32004-12-18 16:18:00 +0000321 }
Brian Paul21841f02004-08-14 14:28:11 +0000322 if (fprog->Parameters)
Michal Krol2861e732004-03-29 11:09:34 +0000323 _mesa_free_parameter_list(fprog->Parameters);
Michal Krol2861e732004-03-29 11:09:34 +0000324 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000325 else if (prog->Target == GL_FRAGMENT_SHADER_ATI) {
326 struct ati_fragment_shader *atifs = (struct ati_fragment_shader *)prog;
Roland Scheideggerf519a772005-09-02 01:11:53 +0000327 GLuint i;
328 for (i = 0; i < MAX_NUM_PASSES_ATI; i++) {
329 if (atifs->Instructions[i])
330 _mesa_free(atifs->Instructions[i]);
331 if (atifs->SetupInst[i])
332 _mesa_free(atifs->SetupInst[i]);
333 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000334 }
335
Michal Krol2861e732004-03-29 11:09:34 +0000336 _mesa_free(prog);
337}
338
339
340
341/**********************************************************************/
342/* Program parameter functions */
343/**********************************************************************/
344
345struct program_parameter_list *
346_mesa_new_parameter_list(void)
347{
348 return (struct program_parameter_list *)
349 _mesa_calloc(sizeof(struct program_parameter_list));
350}
351
352
353/**
354 * Free a parameter list and all its parameters
355 */
356void
357_mesa_free_parameter_list(struct program_parameter_list *paramList)
358{
359 _mesa_free_parameters(paramList);
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000360 _mesa_free(paramList->Parameters);
Keith Whitwell9899f582005-06-08 21:57:45 +0000361 if (paramList->ParameterValues)
362 ALIGN_FREE(paramList->ParameterValues);
Michal Krol2861e732004-03-29 11:09:34 +0000363 _mesa_free(paramList);
364}
365
366
367/**
368 * Free all the parameters in the given list, but don't free the
369 * paramList structure itself.
370 */
371void
372_mesa_free_parameters(struct program_parameter_list *paramList)
373{
374 GLuint i;
375 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000376 if (paramList->Parameters[i].Name)
377 _mesa_free((void *) paramList->Parameters[i].Name);
Michal Krol2861e732004-03-29 11:09:34 +0000378 }
Michal Krol2861e732004-03-29 11:09:34 +0000379 paramList->NumParameters = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000380}
381
382
383/**
384 * Helper function used by the functions below.
Brian Paul16241622005-11-03 02:26:47 +0000385 * \return index of new parameter in the list, or -1 if error (out of mem)
Michal Krol2861e732004-03-29 11:09:34 +0000386 */
387static GLint
388add_parameter(struct program_parameter_list *paramList,
389 const char *name, const GLfloat values[4],
Brian Paul613e1ad2005-11-05 02:15:21 +0000390 enum register_file type)
Michal Krol2861e732004-03-29 11:09:34 +0000391{
392 const GLuint n = paramList->NumParameters;
393
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000394 if (n == paramList->Size) {
Keith Whitwell9899f582005-06-08 21:57:45 +0000395 GLfloat (*tmp)[4];
396
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000397 paramList->Size *= 2;
398 if (!paramList->Size)
Keith Whitwell9899f582005-06-08 21:57:45 +0000399 paramList->Size = 8;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000400
401 paramList->Parameters = (struct program_parameter *)
402 _mesa_realloc(paramList->Parameters,
403 n * sizeof(struct program_parameter),
404 paramList->Size * sizeof(struct program_parameter));
Keith Whitwell9899f582005-06-08 21:57:45 +0000405
406 tmp = paramList->ParameterValues;
407 paramList->ParameterValues = ALIGN_MALLOC(paramList->Size * 4 * sizeof(GLfloat), 16);
408 if (tmp) {
409 _mesa_memcpy(paramList->ParameterValues, tmp,
410 n * 4 * sizeof(GLfloat));
411 ALIGN_FREE(tmp);
412 }
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000413 }
Keith Whitwell7c26b612005-04-21 14:46:57 +0000414
415 if (!paramList->Parameters ||
416 !paramList->ParameterValues) {
Michal Krol2861e732004-03-29 11:09:34 +0000417 /* out of memory */
418 paramList->NumParameters = 0;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000419 paramList->Size = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000420 return -1;
421 }
422 else {
423 paramList->NumParameters = n + 1;
Keith Whitwella42fe192005-05-10 18:22:19 +0000424
425 _mesa_memset(&paramList->Parameters[n], 0,
426 sizeof(struct program_parameter));
427
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000428 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000429 paramList->Parameters[n].Type = type;
430 if (values)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000431 COPY_4V(paramList->ParameterValues[n], values);
Michal Krol2861e732004-03-29 11:09:34 +0000432 return (GLint) n;
433 }
434}
435
436
437/**
438 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
439 * \return index of the new entry in the parameter list
440 */
441GLint
442_mesa_add_named_parameter(struct program_parameter_list *paramList,
443 const char *name, const GLfloat values[4])
444{
Brian Paul613e1ad2005-11-05 02:15:21 +0000445 return add_parameter(paramList, name, values, PROGRAM_NAMED_PARAM);
Michal Krol2861e732004-03-29 11:09:34 +0000446}
447
448
449/**
450 * Add a new unnamed constant to the parameter list.
451 * \param paramList - the parameter list
452 * \param values - four float values
453 * \return index of the new parameter.
454 */
455GLint
456_mesa_add_named_constant(struct program_parameter_list *paramList,
457 const char *name, const GLfloat values[4])
458{
Brian Paul613e1ad2005-11-05 02:15:21 +0000459 return add_parameter(paramList, name, values, PROGRAM_CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000460}
461
462
463/**
464 * Add a new unnamed constant to the parameter list.
465 * \param paramList - the parameter list
466 * \param values - four float values
467 * \return index of the new parameter.
468 */
469GLint
470_mesa_add_unnamed_constant(struct program_parameter_list *paramList,
471 const GLfloat values[4])
472{
Brian Paul613e1ad2005-11-05 02:15:21 +0000473 return add_parameter(paramList, NULL, values, PROGRAM_CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000474}
475
476
477/**
478 * Add a new state reference to the parameter list.
479 * \param paramList - the parameter list
480 * \param state - an array of 6 state tokens
481 *
482 * \return index of the new parameter.
483 */
484GLint
485_mesa_add_state_reference(struct program_parameter_list *paramList,
Brian Paul16241622005-11-03 02:26:47 +0000486 const GLint *stateTokens)
Michal Krol2861e732004-03-29 11:09:34 +0000487{
Brian Paul16241622005-11-03 02:26:47 +0000488 /* XXX we should probably search the current parameter list to see if
489 * the new state reference is already present.
Michal Krol2861e732004-03-29 11:09:34 +0000490 */
Brian Paul16241622005-11-03 02:26:47 +0000491 GLint index;
Michal Krol2861e732004-03-29 11:09:34 +0000492
Brian Paul613e1ad2005-11-05 02:15:21 +0000493 index = add_parameter(paramList, NULL, NULL, PROGRAM_STATE_VAR);
Brian Paul16241622005-11-03 02:26:47 +0000494 if (index >= 0) {
495 GLuint i;
496 for (i = 0; i < 6; i++)
497 paramList->Parameters[index].StateIndexes[i]
498 = (enum state_index) stateTokens[i];
499 }
Michal Krol2861e732004-03-29 11:09:34 +0000500
Brian Paul16241622005-11-03 02:26:47 +0000501 return index;
Michal Krol2861e732004-03-29 11:09:34 +0000502}
503
504
505/**
506 * Lookup a parameter value by name in the given parameter list.
507 * \return pointer to the float[4] values.
508 */
509GLfloat *
510_mesa_lookup_parameter_value(struct program_parameter_list *paramList,
511 GLsizei nameLen, const char *name)
512{
513 GLuint i;
514
515 if (!paramList)
516 return NULL;
517
518 if (nameLen == -1) {
519 /* name is null-terminated */
520 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000521 if (paramList->Parameters[i].Name &&
522 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000523 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000524 }
525 }
526 else {
527 /* name is not null-terminated, use nameLen */
528 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000529 if (paramList->Parameters[i].Name &&
530 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000531 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000532 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000533 }
534 }
535 return NULL;
536}
537
538
539/**
540 * Lookup a parameter index by name in the given parameter list.
541 * \return index of parameter in the list.
542 */
543GLint
544_mesa_lookup_parameter_index(struct program_parameter_list *paramList,
545 GLsizei nameLen, const char *name)
546{
547 GLint i;
548
549 if (!paramList)
550 return -1;
551
552 if (nameLen == -1) {
553 /* name is null-terminated */
554 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000555 if (paramList->Parameters[i].Name &&
556 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Michal Krol2861e732004-03-29 11:09:34 +0000557 return i;
558 }
559 }
560 else {
561 /* name is not null-terminated, use nameLen */
562 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000563 if (paramList->Parameters[i].Name &&
564 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000565 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
566 return i;
567 }
568 }
569 return -1;
570}
571
572
573/**
574 * Use the list of tokens in the state[] array to find global GL state
575 * and return it in <value>. Usually, four values are returned in <value>
576 * but matrix queries may return as many as 16 values.
577 * This function is used for ARB vertex/fragment programs.
578 * The program parser will produce the state[] values.
579 */
580static void
581_mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
582 GLfloat *value)
583{
584 switch (state[0]) {
585 case STATE_MATERIAL:
586 {
587 /* state[1] is either 0=front or 1=back side */
588 const GLuint face = (GLuint) state[1];
589 /* state[2] is the material attribute */
590 switch (state[2]) {
591 case STATE_AMBIENT:
592 if (face == 0)
593 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
594 else
595 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
596 return;
597 case STATE_DIFFUSE:
598 if (face == 0)
599 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
600 else
601 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
602 return;
603 case STATE_SPECULAR:
604 if (face == 0)
605 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
606 else
607 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
608 return;
609 case STATE_EMISSION:
610 if (face == 0)
611 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
612 else
613 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
614 return;
615 case STATE_SHININESS:
616 if (face == 0)
617 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
618 else
619 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
620 value[1] = 0.0F;
621 value[2] = 0.0F;
622 value[3] = 1.0F;
623 return;
624 default:
625 _mesa_problem(ctx, "Invalid material state in fetch_state");
626 return;
627 }
Alan Hourihane22ae6332004-12-02 13:29:40 +0000628 }
Michal Krol2861e732004-03-29 11:09:34 +0000629 case STATE_LIGHT:
630 {
631 /* state[1] is the light number */
632 const GLuint ln = (GLuint) state[1];
633 /* state[2] is the light attribute */
634 switch (state[2]) {
635 case STATE_AMBIENT:
636 COPY_4V(value, ctx->Light.Light[ln].Ambient);
637 return;
638 case STATE_DIFFUSE:
639 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
640 return;
641 case STATE_SPECULAR:
642 COPY_4V(value, ctx->Light.Light[ln].Specular);
643 return;
644 case STATE_POSITION:
645 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
646 return;
647 case STATE_ATTENUATION:
648 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
649 value[1] = ctx->Light.Light[ln].LinearAttenuation;
650 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
651 value[3] = ctx->Light.Light[ln].SpotExponent;
652 return;
653 case STATE_SPOT_DIRECTION:
Brian Paul52bf0052005-04-20 23:47:03 +0000654 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
655 value[3] = ctx->Light.Light[ln]._CosCutoff;
Michal Krol2861e732004-03-29 11:09:34 +0000656 return;
657 case STATE_HALF:
658 {
659 GLfloat eye_z[] = {0, 0, 1};
660
661 /* Compute infinite half angle vector:
662 * half-vector = light_position + (0, 0, 1)
663 * and then normalize. w = 0
Keith Whitwell7c26b612005-04-21 14:46:57 +0000664 *
665 * light.EyePosition.w should be 0 for infinite lights.
Michal Krol2861e732004-03-29 11:09:34 +0000666 */
Keith Whitwell7c26b612005-04-21 14:46:57 +0000667 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
668 NORMALIZE_3FV(value);
669 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000670 }
671 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000672 case STATE_POSITION_NORMALIZED:
673 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
674 NORMALIZE_3FV( value );
675 return;
Michal Krol2861e732004-03-29 11:09:34 +0000676 default:
677 _mesa_problem(ctx, "Invalid light state in fetch_state");
678 return;
679 }
680 }
Michal Krol2861e732004-03-29 11:09:34 +0000681 case STATE_LIGHTMODEL_AMBIENT:
682 COPY_4V(value, ctx->Light.Model.Ambient);
683 return;
684 case STATE_LIGHTMODEL_SCENECOLOR:
685 if (state[1] == 0) {
686 /* front */
687 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000688 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000689 value[i] = ctx->Light.Model.Ambient[i]
690 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
691 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
692 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000693 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000694 }
695 else {
696 /* back */
697 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000698 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000699 value[i] = ctx->Light.Model.Ambient[i]
700 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
701 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
702 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000703 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000704 }
705 return;
706 case STATE_LIGHTPROD:
707 {
708 const GLuint ln = (GLuint) state[1];
709 const GLuint face = (GLuint) state[2];
710 GLint i;
711 ASSERT(face == 0 || face == 1);
712 switch (state[3]) {
713 case STATE_AMBIENT:
714 for (i = 0; i < 3; i++) {
715 value[i] = ctx->Light.Light[ln].Ambient[i] *
716 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
717 }
718 /* [3] = material alpha */
719 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
720 return;
721 case STATE_DIFFUSE:
722 for (i = 0; i < 3; i++) {
723 value[i] = ctx->Light.Light[ln].Diffuse[i] *
724 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
725 }
726 /* [3] = material alpha */
727 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
728 return;
729 case STATE_SPECULAR:
730 for (i = 0; i < 3; i++) {
731 value[i] = ctx->Light.Light[ln].Specular[i] *
732 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
733 }
734 /* [3] = material alpha */
735 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
736 return;
737 default:
738 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
739 return;
740 }
741 }
Michal Krol2861e732004-03-29 11:09:34 +0000742 case STATE_TEXGEN:
743 {
744 /* state[1] is the texture unit */
745 const GLuint unit = (GLuint) state[1];
746 /* state[2] is the texgen attribute */
747 switch (state[2]) {
748 case STATE_TEXGEN_EYE_S:
749 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
750 return;
751 case STATE_TEXGEN_EYE_T:
752 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
753 return;
754 case STATE_TEXGEN_EYE_R:
755 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
756 return;
757 case STATE_TEXGEN_EYE_Q:
758 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
759 return;
760 case STATE_TEXGEN_OBJECT_S:
761 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
762 return;
763 case STATE_TEXGEN_OBJECT_T:
764 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
765 return;
766 case STATE_TEXGEN_OBJECT_R:
767 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
768 return;
769 case STATE_TEXGEN_OBJECT_Q:
770 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
771 return;
772 default:
773 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
774 return;
775 }
776 }
Michal Krol2861e732004-03-29 11:09:34 +0000777 case STATE_TEXENV_COLOR:
778 {
779 /* state[1] is the texture unit */
780 const GLuint unit = (GLuint) state[1];
781 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
782 }
783 return;
784 case STATE_FOG_COLOR:
785 COPY_4V(value, ctx->Fog.Color);
786 return;
787 case STATE_FOG_PARAMS:
788 value[0] = ctx->Fog.Density;
789 value[1] = ctx->Fog.Start;
790 value[2] = ctx->Fog.End;
791 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
792 return;
793 case STATE_CLIPPLANE:
794 {
795 const GLuint plane = (GLuint) state[1];
796 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
797 }
798 return;
799 case STATE_POINT_SIZE:
800 value[0] = ctx->Point.Size;
801 value[1] = ctx->Point.MinSize;
802 value[2] = ctx->Point.MaxSize;
803 value[3] = ctx->Point.Threshold;
804 return;
805 case STATE_POINT_ATTENUATION:
806 value[0] = ctx->Point.Params[0];
807 value[1] = ctx->Point.Params[1];
808 value[2] = ctx->Point.Params[2];
809 value[3] = 1.0F;
810 return;
811 case STATE_MATRIX:
812 {
813 /* state[1] = modelview, projection, texture, etc. */
814 /* state[2] = which texture matrix or program matrix */
815 /* state[3] = first column to fetch */
816 /* state[4] = last column to fetch */
817 /* state[5] = transpose, inverse or invtrans */
818
819 const GLmatrix *matrix;
820 const enum state_index mat = state[1];
821 const GLuint index = (GLuint) state[2];
822 const GLuint first = (GLuint) state[3];
823 const GLuint last = (GLuint) state[4];
824 const enum state_index modifier = state[5];
825 const GLfloat *m;
826 GLuint row, i;
827 if (mat == STATE_MODELVIEW) {
828 matrix = ctx->ModelviewMatrixStack.Top;
829 }
830 else if (mat == STATE_PROJECTION) {
831 matrix = ctx->ProjectionMatrixStack.Top;
832 }
833 else if (mat == STATE_MVP) {
834 matrix = &ctx->_ModelProjectMatrix;
835 }
836 else if (mat == STATE_TEXTURE) {
837 matrix = ctx->TextureMatrixStack[index].Top;
838 }
839 else if (mat == STATE_PROGRAM) {
840 matrix = ctx->ProgramMatrixStack[index].Top;
841 }
842 else {
843 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
844 return;
845 }
846 if (modifier == STATE_MATRIX_INVERSE ||
847 modifier == STATE_MATRIX_INVTRANS) {
Keith Whitwell78803b22005-04-15 12:57:23 +0000848 /* Be sure inverse is up to date:
849 */
Jouk Jansen49b1d952005-04-18 13:05:24 +0000850 _math_matrix_analyse( (GLmatrix*) matrix );
Michal Krol2861e732004-03-29 11:09:34 +0000851 m = matrix->inv;
852 }
853 else {
854 m = matrix->m;
855 }
856 if (modifier == STATE_MATRIX_TRANSPOSE ||
857 modifier == STATE_MATRIX_INVTRANS) {
858 for (i = 0, row = first; row <= last; row++) {
859 value[i++] = m[row * 4 + 0];
860 value[i++] = m[row * 4 + 1];
861 value[i++] = m[row * 4 + 2];
862 value[i++] = m[row * 4 + 3];
863 }
864 }
865 else {
866 for (i = 0, row = first; row <= last; row++) {
867 value[i++] = m[row + 0];
868 value[i++] = m[row + 4];
869 value[i++] = m[row + 8];
870 value[i++] = m[row + 12];
871 }
872 }
873 }
874 return;
875 case STATE_DEPTH_RANGE:
Brian Paul824fdf02004-06-29 00:00:06 +0000876 value[0] = ctx->Viewport.Near; /* near */
877 value[1] = ctx->Viewport.Far; /* far */
878 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
879 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000880 return;
881 case STATE_FRAGMENT_PROGRAM:
882 {
883 /* state[1] = {STATE_ENV, STATE_LOCAL} */
884 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000885 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000886 switch (state[1]) {
887 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000888 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000889 break;
Michal Krol2861e732004-03-29 11:09:34 +0000890 case STATE_LOCAL:
891 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000892 break;
Michal Krol2861e732004-03-29 11:09:34 +0000893 default:
894 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
895 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000896 }
897 }
Michal Krol2861e732004-03-29 11:09:34 +0000898 return;
899
900 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000901 {
Michal Krol2861e732004-03-29 11:09:34 +0000902 /* state[1] = {STATE_ENV, STATE_LOCAL} */
903 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000904 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000905 switch (state[1]) {
906 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000907 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000908 break;
Michal Krol2861e732004-03-29 11:09:34 +0000909 case STATE_LOCAL:
910 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000911 break;
Michal Krol2861e732004-03-29 11:09:34 +0000912 default:
913 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
914 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000915 }
916 }
Michal Krol2861e732004-03-29 11:09:34 +0000917 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000918
919 case STATE_INTERNAL:
920 {
921 switch (state[1]) {
922 case STATE_NORMAL_SCALE:
923 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
924 break;
925 default:
926 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
927 return;
928 }
929 }
930 return;
931
Michal Krol2861e732004-03-29 11:09:34 +0000932 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000933 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000934 return;
935 }
936}
937
938
939/**
940 * Loop over all the parameters in a parameter list. If the parameter
941 * is a GL state reference, look up the current value of that state
942 * variable and put it into the parameter's Value[4] array.
943 * This would be called at glBegin time when using a fragment program.
944 */
945void
946_mesa_load_state_parameters(GLcontext *ctx,
947 struct program_parameter_list *paramList)
948{
949 GLuint i;
950
951 if (!paramList)
952 return;
953
954 for (i = 0; i < paramList->NumParameters; i++) {
Brian Paul613e1ad2005-11-05 02:15:21 +0000955 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
Keith Whitwell7c26b612005-04-21 14:46:57 +0000956 _mesa_fetch_state(ctx,
957 paramList->Parameters[i].StateIndexes,
958 paramList->ParameterValues[i]);
Michal Krol2861e732004-03-29 11:09:34 +0000959 }
960 }
961}
962
963
Brian Paul1fcdaf12005-11-05 19:12:36 +0000964/**
965 * Basic info about each instruction
966 */
967struct instruction_info
968{
969 enum prog_opcode Opcode;
970 const char *Name;
971 GLuint NumSrcRegs;
972};
973
974/**
975 * Instruction info
976 * \note Opcode should equal array index!
977 */
978static const struct instruction_info InstInfo[MAX_OPCODE] = {
979 { OPCODE_ABS, "ABS", 1 },
980 { OPCODE_ADD, "ADD", 2 },
981 { OPCODE_ARL, "ARL", 1 },
982 { OPCODE_CMP, "CMP", 3 },
983 { OPCODE_COS, "COS", 1 },
984 { OPCODE_DDX, "DDX", 1 },
985 { OPCODE_DDY, "DDY", 1 },
986 { OPCODE_DP3, "DP3", 2 },
987 { OPCODE_DP4, "DP4", 2 },
988 { OPCODE_DPH, "DPH", 2 },
989 { OPCODE_DST, "DST", 2 },
990 { OPCODE_END, "END", 0 },
991 { OPCODE_EX2, "EX2", 1 },
992 { OPCODE_EXP, "EXP", 1 },
993 { OPCODE_FLR, "FLR", 1 },
994 { OPCODE_FRC, "FRC", 1 },
995 { OPCODE_KIL, "KIL", 1 },
996 { OPCODE_KIL_NV, "KIL", 0 },
997 { OPCODE_LG2, "LG2", 1 },
998 { OPCODE_LIT, "LIT", 1 },
999 { OPCODE_LOG, "LOG", 1 },
1000 { OPCODE_LRP, "LRP", 3 },
1001 { OPCODE_MAD, "MAD", 3 },
1002 { OPCODE_MAX, "MAX", 2 },
1003 { OPCODE_MIN, "MIN", 2 },
1004 { OPCODE_MOV, "MOV", 1 },
1005 { OPCODE_MUL, "MUL", 2 },
1006 { OPCODE_PK2H, "PK2H", 1 },
1007 { OPCODE_PK2US, "PK2US", 1 },
1008 { OPCODE_PK4B, "PK4B", 1 },
1009 { OPCODE_PK4UB, "PK4UB", 1 },
1010 { OPCODE_POW, "POW", 2 },
1011 { OPCODE_PRINT, "PRINT", 1 },
1012 { OPCODE_RCC, "RCC", 1 },
1013 { OPCODE_RCP, "RCP", 1 },
1014 { OPCODE_RFL, "RFL", 1 },
1015 { OPCODE_RSQ, "RSQ", 1 },
1016 { OPCODE_SCS, "SCS", 1 },
1017 { OPCODE_SEQ, "SEQ", 2 },
1018 { OPCODE_SFL, "SFL", 0 },
1019 { OPCODE_SGE, "SGE", 2 },
1020 { OPCODE_SGT, "SGT", 2 },
1021 { OPCODE_SIN, "SIN", 1 },
1022 { OPCODE_SLE, "SLE", 2 },
1023 { OPCODE_SLT, "SLT", 2 },
1024 { OPCODE_SNE, "SNE", 2 },
1025 { OPCODE_STR, "STR", 0 },
1026 { OPCODE_SUB, "SUB", 2 },
1027 { OPCODE_SWZ, "SWZ", 1 },
1028 { OPCODE_TEX, "TEX", 1 },
1029 { OPCODE_TXB, "TXB", 1 },
1030 { OPCODE_TXD, "TXD", 3 },
1031 { OPCODE_TXP, "TXP", 1 },
1032 { OPCODE_TXP_NV, "TXP", 1 },
1033 { OPCODE_UP2H, "UP2H", 1 },
1034 { OPCODE_UP2US, "UP2US", 1 },
1035 { OPCODE_UP4B, "UP4B", 1 },
1036 { OPCODE_UP4UB, "UP4UB", 1 },
1037 { OPCODE_X2D, "X2D", 3 },
1038 { OPCODE_XPD, "XPD", 2 }
1039};
1040
1041
1042/**
1043 * Return the number of src registers for the given instruction/opcode.
1044 */
1045GLuint
1046_mesa_num_inst_src_regs(enum prog_opcode opcode)
1047{
1048 GLuint i;
1049#ifdef DEBUG
1050 for (i = 0; i < MAX_OPCODE; i++) {
1051 ASSERT(i == InstInfo[i].Opcode);
1052 }
1053#endif
1054 for (i = 0; i < MAX_OPCODE; i++) {
1055 if (InstInfo[i].Opcode == opcode) {
1056 return InstInfo[i].NumSrcRegs;
1057 }
1058 }
1059 _mesa_problem(NULL, "invalid opcode in _mesa_num_inst_src_regs");
1060 return 0;
1061}
1062
1063
1064/**
1065 * Return string name for given program opcode.
1066 */
1067const char *
1068_mesa_opcode_string(enum prog_opcode opcode)
1069{
1070 ASSERT(opcode < MAX_OPCODE);
1071 return InstInfo[opcode].Name;
1072}
1073
Michal Krol2861e732004-03-29 11:09:34 +00001074
1075/**********************************************************************/
1076/* API functions */
1077/**********************************************************************/
1078
1079
1080/**
1081 * Bind a program (make it current)
1082 * \note Called from the GL API dispatcher by both glBindProgramNV
1083 * and glBindProgramARB.
1084 */
1085void GLAPIENTRY
1086_mesa_BindProgram(GLenum target, GLuint id)
1087{
1088 struct program *prog;
1089 GET_CURRENT_CONTEXT(ctx);
1090 ASSERT_OUTSIDE_BEGIN_END(ctx);
1091
1092 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1093
1094 if ((target == GL_VERTEX_PROGRAM_NV
1095 && ctx->Extensions.NV_vertex_program) ||
1096 (target == GL_VERTEX_PROGRAM_ARB
1097 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001098 /*** Vertex program binding ***/
1099 struct vertex_program *curProg = ctx->VertexProgram.Current;
1100 if (curProg->Base.Id == id) {
1101 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001102 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001103 }
1104 if (curProg->Base.Id != 0) {
1105 /* decrement refcount on previously bound vertex program */
1106 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001107 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001108 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001109 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001110 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001111 }
1112 }
1113 }
1114 else if ((target == GL_FRAGMENT_PROGRAM_NV
1115 && ctx->Extensions.NV_fragment_program) ||
1116 (target == GL_FRAGMENT_PROGRAM_ARB
1117 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001118 /*** Fragment program binding ***/
1119 struct fragment_program *curProg = ctx->FragmentProgram.Current;
1120 if (curProg->Base.Id == id) {
1121 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001122 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001123 }
1124 if (curProg->Base.Id != 0) {
1125 /* decrement refcount on previously bound fragment program */
1126 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001127 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001128 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001129 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001130 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001131 }
1132 }
1133 }
1134 else {
1135 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1136 return;
1137 }
1138
1139 /* NOTE: binding to a non-existant program is not an error.
1140 * That's supposed to be caught in glBegin.
1141 */
1142 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001143 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001144 prog = NULL;
1145 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1146 prog = ctx->Shared->DefaultVertexProgram;
1147 else
1148 prog = ctx->Shared->DefaultFragmentProgram;
1149 }
1150 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001151 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001152 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001153 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001154 /* allocate a new program now */
1155 prog = ctx->Driver.NewProgram(ctx, target, id);
1156 if (!prog) {
1157 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1158 return;
1159 }
Michal Krol2861e732004-03-29 11:09:34 +00001160 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1161 }
Brian Paul765f1a12004-09-14 22:28:27 +00001162 else if (prog->Target != target) {
1163 _mesa_error(ctx, GL_INVALID_OPERATION,
1164 "glBindProgramNV/ARB(target mismatch)");
1165 return;
1166 }
Michal Krol2861e732004-03-29 11:09:34 +00001167 }
1168
1169 /* bind now */
1170 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1171 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1172 }
1173 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1174 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1175 }
1176
Brian Paul765f1a12004-09-14 22:28:27 +00001177 /* Never null pointers */
1178 ASSERT(ctx->VertexProgram.Current);
1179 ASSERT(ctx->FragmentProgram.Current);
1180
Michal Krol2861e732004-03-29 11:09:34 +00001181 if (prog)
1182 prog->RefCount++;
1183
1184 if (ctx->Driver.BindProgram)
1185 ctx->Driver.BindProgram(ctx, target, prog);
1186}
1187
1188
1189/**
1190 * Delete a list of programs.
1191 * \note Not compiled into display lists.
1192 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1193 */
1194void GLAPIENTRY
1195_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1196{
1197 GLint i;
1198 GET_CURRENT_CONTEXT(ctx);
1199 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1200
1201 if (n < 0) {
1202 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1203 return;
1204 }
1205
1206 for (i = 0; i < n; i++) {
1207 if (ids[i] != 0) {
1208 struct program *prog = (struct program *)
1209 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001210 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001211 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1212 }
1213 else if (prog) {
1214 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001215 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1216 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1217 if (ctx->VertexProgram.Current &&
1218 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1219 /* unbind this currently bound program */
1220 _mesa_BindProgram(prog->Target, 0);
1221 }
1222 }
1223 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1224 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1225 if (ctx->FragmentProgram.Current &&
1226 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1227 /* unbind this currently bound program */
1228 _mesa_BindProgram(prog->Target, 0);
1229 }
1230 }
1231 else {
1232 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1233 return;
1234 }
Brian Paulea2943e2005-01-20 04:02:02 +00001235 /* The ID is immediately available for re-use now */
1236 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1237 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001238 if (prog->RefCount <= 0) {
1239 ctx->Driver.DeleteProgram(ctx, prog);
1240 }
1241 }
Michal Krol2861e732004-03-29 11:09:34 +00001242 }
1243 }
1244}
1245
1246
1247/**
1248 * Generate a list of new program identifiers.
1249 * \note Not compiled into display lists.
1250 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1251 */
1252void GLAPIENTRY
1253_mesa_GenPrograms(GLsizei n, GLuint *ids)
1254{
1255 GLuint first;
1256 GLuint i;
1257 GET_CURRENT_CONTEXT(ctx);
1258 ASSERT_OUTSIDE_BEGIN_END(ctx);
1259
1260 if (n < 0) {
1261 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1262 return;
1263 }
1264
1265 if (!ids)
1266 return;
1267
1268 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1269
Brian Paul765f1a12004-09-14 22:28:27 +00001270 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001271 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001272 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001273 }
1274
1275 /* Return the program names */
1276 for (i = 0; i < (GLuint) n; i++) {
1277 ids[i] = first + i;
1278 }
1279}
1280
1281
1282/**
Brian Paul765f1a12004-09-14 22:28:27 +00001283 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001284 * \note Not compiled into display lists.
1285 * \note Called from both glIsProgramNV and glIsProgramARB.
1286 * \param id is the program identifier
1287 * \return GL_TRUE if id is a program, else GL_FALSE.
1288 */
1289GLboolean GLAPIENTRY
1290_mesa_IsProgram(GLuint id)
1291{
1292 GET_CURRENT_CONTEXT(ctx);
1293 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1294
1295 if (id == 0)
1296 return GL_FALSE;
1297
1298 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1299 return GL_TRUE;
1300 else
1301 return GL_FALSE;
1302}
1303
1304
1305
1306/**********************************************************************/
1307/* GL_MESA_program_debug extension */
1308/**********************************************************************/
1309
1310
1311/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001312GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001313glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1314 GLvoid *data)
1315{
1316 _mesa_ProgramCallbackMESA(target, callback, data);
1317}
1318
1319
1320void
1321_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1322 GLvoid *data)
1323{
1324 GET_CURRENT_CONTEXT(ctx);
1325
1326 switch (target) {
1327 case GL_FRAGMENT_PROGRAM_ARB:
1328 if (!ctx->Extensions.ARB_fragment_program) {
1329 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1330 return;
1331 }
1332 ctx->FragmentProgram.Callback = callback;
1333 ctx->FragmentProgram.CallbackData = data;
1334 break;
1335 case GL_FRAGMENT_PROGRAM_NV:
1336 if (!ctx->Extensions.NV_fragment_program) {
1337 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1338 return;
1339 }
1340 ctx->FragmentProgram.Callback = callback;
1341 ctx->FragmentProgram.CallbackData = data;
1342 break;
1343 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1344 if (!ctx->Extensions.ARB_vertex_program &&
1345 !ctx->Extensions.NV_vertex_program) {
1346 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1347 return;
1348 }
1349 ctx->VertexProgram.Callback = callback;
1350 ctx->VertexProgram.CallbackData = data;
1351 break;
1352 default:
1353 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1354 return;
1355 }
1356}
1357
1358
1359/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001360GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001361glGetProgramRegisterfvMESA(GLenum target,
1362 GLsizei len, const GLubyte *registerName,
1363 GLfloat *v)
1364{
1365 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1366}
1367
1368
1369void
1370_mesa_GetProgramRegisterfvMESA(GLenum target,
1371 GLsizei len, const GLubyte *registerName,
1372 GLfloat *v)
1373{
1374 char reg[1000];
1375 GET_CURRENT_CONTEXT(ctx);
1376
1377 /* We _should_ be inside glBegin/glEnd */
1378#if 0
1379 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1380 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1381 return;
1382 }
1383#endif
1384
1385 /* make null-terminated copy of registerName */
1386 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1387 _mesa_memcpy(reg, registerName, len);
1388 reg[len] = 0;
1389
1390 switch (target) {
1391 case GL_VERTEX_PROGRAM_NV:
1392 if (!ctx->Extensions.ARB_vertex_program &&
1393 !ctx->Extensions.NV_vertex_program) {
1394 _mesa_error(ctx, GL_INVALID_ENUM,
1395 "glGetProgramRegisterfvMESA(target)");
1396 return;
1397 }
Brian Paul6d460af2004-04-23 14:16:46 +00001398 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001399 _mesa_error(ctx, GL_INVALID_OPERATION,
1400 "glGetProgramRegisterfvMESA");
1401 return;
1402 }
1403 /* GL_NV_vertex_program */
1404 if (reg[0] == 'R') {
1405 /* Temp register */
1406 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001407 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001408 _mesa_error(ctx, GL_INVALID_VALUE,
1409 "glGetProgramRegisterfvMESA(registerName)");
1410 return;
1411 }
1412 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1413 }
1414 else if (reg[0] == 'v' && reg[1] == '[') {
1415 /* Vertex Input attribute */
1416 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001417 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001418 const char *name = _mesa_nv_vertex_input_register_name(i);
1419 char number[10];
Brian Paulaa206952005-09-16 18:14:24 +00001420 _mesa_sprintf(number, "%d", i);
Michal Krol2861e732004-03-29 11:09:34 +00001421 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1422 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1423 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1424 return;
1425 }
1426 }
1427 _mesa_error(ctx, GL_INVALID_VALUE,
1428 "glGetProgramRegisterfvMESA(registerName)");
1429 return;
1430 }
1431 else if (reg[0] == 'o' && reg[1] == '[') {
1432 /* Vertex output attribute */
1433 }
1434 /* GL_ARB_vertex_program */
1435 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1436
1437 }
1438 else {
1439 _mesa_error(ctx, GL_INVALID_VALUE,
1440 "glGetProgramRegisterfvMESA(registerName)");
1441 return;
1442 }
1443 break;
1444 case GL_FRAGMENT_PROGRAM_ARB:
1445 if (!ctx->Extensions.ARB_fragment_program) {
1446 _mesa_error(ctx, GL_INVALID_ENUM,
1447 "glGetProgramRegisterfvMESA(target)");
1448 return;
1449 }
Brian Paul6d460af2004-04-23 14:16:46 +00001450 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001451 _mesa_error(ctx, GL_INVALID_OPERATION,
1452 "glGetProgramRegisterfvMESA");
1453 return;
1454 }
1455 /* XXX to do */
1456 break;
1457 case GL_FRAGMENT_PROGRAM_NV:
1458 if (!ctx->Extensions.NV_fragment_program) {
1459 _mesa_error(ctx, GL_INVALID_ENUM,
1460 "glGetProgramRegisterfvMESA(target)");
1461 return;
1462 }
Brian Paul6d460af2004-04-23 14:16:46 +00001463 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001464 _mesa_error(ctx, GL_INVALID_OPERATION,
1465 "glGetProgramRegisterfvMESA");
1466 return;
1467 }
1468 if (reg[0] == 'R') {
1469 /* Temp register */
1470 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001471 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001472 _mesa_error(ctx, GL_INVALID_VALUE,
1473 "glGetProgramRegisterfvMESA(registerName)");
1474 return;
1475 }
1476 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1477 }
1478 else if (reg[0] == 'f' && reg[1] == '[') {
1479 /* Fragment input attribute */
1480 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001481 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001482 const char *name = _mesa_nv_fragment_input_register_name(i);
1483 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1484 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1485 return;
1486 }
1487 }
1488 _mesa_error(ctx, GL_INVALID_VALUE,
1489 "glGetProgramRegisterfvMESA(registerName)");
1490 return;
1491 }
1492 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1493 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00001494 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLR]);
Michal Krol2861e732004-03-29 11:09:34 +00001495 }
1496 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1497 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00001498 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLH]);
Michal Krol2861e732004-03-29 11:09:34 +00001499 }
1500 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1501 /* Fragment output depth */
Brian Paul90ebb582005-11-02 18:06:12 +00001502 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_DEPR]);
Michal Krol2861e732004-03-29 11:09:34 +00001503 }
1504 else {
1505 /* try user-defined identifiers */
1506 const GLfloat *value = _mesa_lookup_parameter_value(
1507 ctx->FragmentProgram.Current->Parameters, -1, reg);
1508 if (value) {
1509 COPY_4V(v, value);
1510 }
1511 else {
1512 _mesa_error(ctx, GL_INVALID_VALUE,
1513 "glGetProgramRegisterfvMESA(registerName)");
1514 return;
1515 }
1516 }
1517 break;
1518 default:
1519 _mesa_error(ctx, GL_INVALID_ENUM,
1520 "glGetProgramRegisterfvMESA(target)");
1521 return;
1522 }
1523
1524}