blob: 7b9185a6a8173013a7340ccbfb0656d9df4d4cfb [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"
40#include "nvfragprog.h"
41#include "nvvertparse.h"
Brian Paul575700f2004-12-16 03:07:18 +000042#include "nvvertprog.h"
Roland Scheideggerf519a772005-09-02 01:11:53 +000043#include "atifragshader.h"
Michal Krol2861e732004-03-29 11:09:34 +000044
45
46/**********************************************************************/
47/* Utility functions */
48/**********************************************************************/
49
50
Brian Paul765f1a12004-09-14 22:28:27 +000051/* A pointer to this dummy program is put into the hash table when
52 * glGenPrograms is called.
53 */
Brian Paul9ca83922004-10-02 15:16:59 +000054struct program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000055
56
Michal Krol2861e732004-03-29 11:09:34 +000057/**
Brian Paul21841f02004-08-14 14:28:11 +000058 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000059 */
60void
61_mesa_init_program(GLcontext *ctx)
62{
63 GLuint i;
64
65 ctx->Program.ErrorPos = -1;
66 ctx->Program.ErrorString = _mesa_strdup("");
67
68#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
69 ctx->VertexProgram.Enabled = GL_FALSE;
70 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
71 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Michal Krol2861e732004-03-29 11:09:34 +000072 ctx->VertexProgram.Current = (struct vertex_program *) ctx->Shared->DefaultVertexProgram;
73 assert(ctx->VertexProgram.Current);
74 ctx->VertexProgram.Current->Base.RefCount++;
75 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
76 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
77 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
78 }
79#endif
80
81#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
82 ctx->FragmentProgram.Enabled = GL_FALSE;
83 ctx->FragmentProgram.Current = (struct fragment_program *) ctx->Shared->DefaultFragmentProgram;
84 assert(ctx->FragmentProgram.Current);
85 ctx->FragmentProgram.Current->Base.RefCount++;
86#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000087
88#if FEATURE_ATI_fragment_shader
89 ctx->ATIFragmentShader.Enabled = GL_FALSE;
90 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
91 assert(ctx->ATIFragmentShader.Current);
92 ctx->ATIFragmentShader.Current->Base.RefCount++;
93#endif
Michal Krol2861e732004-03-29 11:09:34 +000094}
95
96
97/**
Brian Paul21841f02004-08-14 14:28:11 +000098 * Free a context's vertex/fragment program state
99 */
100void
101_mesa_free_program_data(GLcontext *ctx)
102{
103#if FEATURE_NV_vertex_program
104 if (ctx->VertexProgram.Current) {
105 ctx->VertexProgram.Current->Base.RefCount--;
106 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
107 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
108 }
109#endif
110#if FEATURE_NV_fragment_program
111 if (ctx->FragmentProgram.Current) {
112 ctx->FragmentProgram.Current->Base.RefCount--;
113 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
114 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
115 }
116#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000117#if FEATURE_ATI_fragment_shader
118 if (ctx->ATIFragmentShader.Current) {
119 ctx->ATIFragmentShader.Current->Base.RefCount--;
120 if (ctx->ATIFragmentShader.Current->Base.RefCount <= 0)
121 ctx->Driver.DeleteProgram(ctx, &(ctx->ATIFragmentShader.Current->Base));
122 }
123#endif
Brian Paul21841f02004-08-14 14:28:11 +0000124 _mesa_free((void *) ctx->Program.ErrorString);
125}
126
127
128
129
130/**
Michal Krol2861e732004-03-29 11:09:34 +0000131 * Set the vertex/fragment program error state (position and error string).
132 * This is generally called from within the parsers.
133 */
134void
135_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
136{
137 ctx->Program.ErrorPos = pos;
138 _mesa_free((void *) ctx->Program.ErrorString);
139 if (!string)
140 string = "";
141 ctx->Program.ErrorString = _mesa_strdup(string);
142}
143
144
145/**
146 * Find the line number and column for 'pos' within 'string'.
147 * Return a copy of the line which contains 'pos'. Free the line with
148 * _mesa_free().
149 * \param string the program string
150 * \param pos the position within the string
151 * \param line returns the line number corresponding to 'pos'.
152 * \param col returns the column number corresponding to 'pos'.
153 * \return copy of the line containing 'pos'.
154 */
155const GLubyte *
156_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
157 GLint *line, GLint *col)
158{
159 const GLubyte *lineStart = string;
160 const GLubyte *p = string;
161 GLubyte *s;
162 int len;
163
164 *line = 1;
165
166 while (p != pos) {
167 if (*p == (GLubyte) '\n') {
168 (*line)++;
169 lineStart = p + 1;
170 }
171 p++;
172 }
173
174 *col = (pos - lineStart) + 1;
175
176 /* return copy of this line */
177 while (*p != 0 && *p != '\n')
178 p++;
179 len = p - lineStart;
180 s = (GLubyte *) _mesa_malloc(len + 1);
181 _mesa_memcpy(s, lineStart, len);
182 s[len] = 0;
183
184 return s;
185}
186
187
Brian Paul765f1a12004-09-14 22:28:27 +0000188/**
189 * Initialize a new vertex/fragment program object.
190 */
191static struct program *
192_mesa_init_program_struct( GLcontext *ctx, struct program *prog,
193 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000194{
Brian Paula6c423d2004-08-25 15:59:48 +0000195 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000196 if (prog) {
197 prog->Id = id;
198 prog->Target = target;
199 prog->Resident = GL_TRUE;
200 prog->RefCount = 1;
201 }
202
203 return prog;
204}
205
Brian Paul765f1a12004-09-14 22:28:27 +0000206
207/**
208 * Initialize a new fragment program object.
209 */
210struct program *
211_mesa_init_fragment_program( GLcontext *ctx, struct fragment_program *prog,
212 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000213{
214 if (prog)
215 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
216 else
217 return NULL;
218}
219
Brian Paul765f1a12004-09-14 22:28:27 +0000220
221/**
222 * Initialize a new vertex program object.
223 */
224struct program *
225_mesa_init_vertex_program( GLcontext *ctx, struct vertex_program *prog,
226 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000227{
228 if (prog)
229 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
230 else
231 return NULL;
232}
233
Dave Airlie7f752fe2004-12-19 03:06:59 +0000234/**
235 * Initialize a new ATI fragment shader object.
236 */
237struct program *
Brian Paulcdb65412005-01-11 15:56:47 +0000238_mesa_init_ati_fragment_shader( GLcontext *ctx,
239 struct ati_fragment_shader *prog,
240 GLenum target, GLuint id )
Dave Airlie7f752fe2004-12-19 03:06:59 +0000241{
242 if (prog)
243 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
244 else
245 return NULL;
246}
247
248
Michal Krol2861e732004-03-29 11:09:34 +0000249
250/**
251 * Allocate and initialize a new fragment/vertex program object but
252 * don't put it into the program hash table. Called via
253 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
254 * device driver function to implement OO deriviation with additional
255 * types not understood by this function.
256 *
257 * \param ctx context
258 * \param id program id/number
259 * \param target program target/type
260 * \return pointer to new program object
261 */
262struct program *
263_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
264{
265 switch (target) {
266 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
267 return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program),
268 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000269 case GL_FRAGMENT_PROGRAM_NV:
270 case GL_FRAGMENT_PROGRAM_ARB:
271 return _mesa_init_fragment_program( ctx, CALLOC_STRUCT(fragment_program),
272 target, id );
Dave Airlie7f752fe2004-12-19 03:06:59 +0000273 case GL_FRAGMENT_SHADER_ATI:
274 return _mesa_init_ati_fragment_shader( ctx, CALLOC_STRUCT(ati_fragment_shader),
275 target, id );
276
Michal Krol2861e732004-03-29 11:09:34 +0000277 default:
278 _mesa_problem(ctx, "bad target in _mesa_new_program");
279 return NULL;
280 }
281}
282
283
284/**
285 * Delete a program and remove it from the hash table, ignoring the
286 * reference count.
287 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
288 * by a device driver function.
289 */
290void
291_mesa_delete_program(GLcontext *ctx, struct program *prog)
292{
Brian Paula6c423d2004-08-25 15:59:48 +0000293 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000294 ASSERT(prog);
295
296 if (prog->String)
297 _mesa_free(prog->String);
298 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
299 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
300 struct vertex_program *vprog = (struct vertex_program *) prog;
Brian Paul575700f2004-12-16 03:07:18 +0000301 if (vprog->Instructions) {
302 GLuint i;
303 for (i = 0; i < vprog->Base.NumInstructions; i++) {
304 if (vprog->Instructions[i].Data)
305 _mesa_free(vprog->Instructions[i].Data);
306 }
Michal Krol2861e732004-03-29 11:09:34 +0000307 _mesa_free(vprog->Instructions);
Brian Paul575700f2004-12-16 03:07:18 +0000308 }
Brian Paul21841f02004-08-14 14:28:11 +0000309 if (vprog->Parameters)
310 _mesa_free_parameter_list(vprog->Parameters);
Michal Krol2861e732004-03-29 11:09:34 +0000311 }
312 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
313 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
314 struct fragment_program *fprog = (struct fragment_program *) prog;
Brian Paul2a5afe32004-12-18 16:18:00 +0000315 if (fprog->Instructions) {
316 GLuint i;
317 for (i = 0; i < fprog->Base.NumInstructions; i++) {
318 if (fprog->Instructions[i].Data)
319 _mesa_free(fprog->Instructions[i].Data);
320 }
Michal Krol2861e732004-03-29 11:09:34 +0000321 _mesa_free(fprog->Instructions);
Brian Paul2a5afe32004-12-18 16:18:00 +0000322 }
Brian Paul21841f02004-08-14 14:28:11 +0000323 if (fprog->Parameters)
Michal Krol2861e732004-03-29 11:09:34 +0000324 _mesa_free_parameter_list(fprog->Parameters);
Michal Krol2861e732004-03-29 11:09:34 +0000325 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000326 else if (prog->Target == GL_FRAGMENT_SHADER_ATI) {
327 struct ati_fragment_shader *atifs = (struct ati_fragment_shader *)prog;
Roland Scheideggerf519a772005-09-02 01:11:53 +0000328 GLuint i;
329 for (i = 0; i < MAX_NUM_PASSES_ATI; i++) {
330 if (atifs->Instructions[i])
331 _mesa_free(atifs->Instructions[i]);
332 if (atifs->SetupInst[i])
333 _mesa_free(atifs->SetupInst[i]);
334 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000335 }
336
Michal Krol2861e732004-03-29 11:09:34 +0000337 _mesa_free(prog);
338}
339
340
341
342/**********************************************************************/
343/* Program parameter functions */
344/**********************************************************************/
345
346struct program_parameter_list *
347_mesa_new_parameter_list(void)
348{
349 return (struct program_parameter_list *)
350 _mesa_calloc(sizeof(struct program_parameter_list));
351}
352
353
354/**
355 * Free a parameter list and all its parameters
356 */
357void
358_mesa_free_parameter_list(struct program_parameter_list *paramList)
359{
360 _mesa_free_parameters(paramList);
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000361 _mesa_free(paramList->Parameters);
Keith Whitwell9899f582005-06-08 21:57:45 +0000362 if (paramList->ParameterValues)
363 ALIGN_FREE(paramList->ParameterValues);
Michal Krol2861e732004-03-29 11:09:34 +0000364 _mesa_free(paramList);
365}
366
367
368/**
369 * Free all the parameters in the given list, but don't free the
370 * paramList structure itself.
371 */
372void
373_mesa_free_parameters(struct program_parameter_list *paramList)
374{
375 GLuint i;
376 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000377 if (paramList->Parameters[i].Name)
378 _mesa_free((void *) paramList->Parameters[i].Name);
Michal Krol2861e732004-03-29 11:09:34 +0000379 }
Michal Krol2861e732004-03-29 11:09:34 +0000380 paramList->NumParameters = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000381}
382
383
384/**
385 * Helper function used by the functions below.
Brian Paul16241622005-11-03 02:26:47 +0000386 * \return index of new parameter in the list, or -1 if error (out of mem)
Michal Krol2861e732004-03-29 11:09:34 +0000387 */
388static GLint
389add_parameter(struct program_parameter_list *paramList,
390 const char *name, const GLfloat values[4],
391 enum parameter_type type)
392{
393 const GLuint n = paramList->NumParameters;
394
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000395 if (n == paramList->Size) {
Keith Whitwell9899f582005-06-08 21:57:45 +0000396 GLfloat (*tmp)[4];
397
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000398 paramList->Size *= 2;
399 if (!paramList->Size)
Keith Whitwell9899f582005-06-08 21:57:45 +0000400 paramList->Size = 8;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000401
402 paramList->Parameters = (struct program_parameter *)
403 _mesa_realloc(paramList->Parameters,
404 n * sizeof(struct program_parameter),
405 paramList->Size * sizeof(struct program_parameter));
Keith Whitwell9899f582005-06-08 21:57:45 +0000406
407 tmp = paramList->ParameterValues;
408 paramList->ParameterValues = ALIGN_MALLOC(paramList->Size * 4 * sizeof(GLfloat), 16);
409 if (tmp) {
410 _mesa_memcpy(paramList->ParameterValues, tmp,
411 n * 4 * sizeof(GLfloat));
412 ALIGN_FREE(tmp);
413 }
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000414 }
Keith Whitwell7c26b612005-04-21 14:46:57 +0000415
416 if (!paramList->Parameters ||
417 !paramList->ParameterValues) {
Michal Krol2861e732004-03-29 11:09:34 +0000418 /* out of memory */
419 paramList->NumParameters = 0;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000420 paramList->Size = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000421 return -1;
422 }
423 else {
424 paramList->NumParameters = n + 1;
Keith Whitwella42fe192005-05-10 18:22:19 +0000425
426 _mesa_memset(&paramList->Parameters[n], 0,
427 sizeof(struct program_parameter));
428
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000429 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000430 paramList->Parameters[n].Type = type;
431 if (values)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000432 COPY_4V(paramList->ParameterValues[n], values);
Michal Krol2861e732004-03-29 11:09:34 +0000433 return (GLint) n;
434 }
435}
436
437
438/**
439 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
440 * \return index of the new entry in the parameter list
441 */
442GLint
443_mesa_add_named_parameter(struct program_parameter_list *paramList,
444 const char *name, const GLfloat values[4])
445{
446 return add_parameter(paramList, name, values, NAMED_PARAMETER);
447}
448
449
450/**
451 * Add a new unnamed constant to the parameter list.
452 * \param paramList - the parameter list
453 * \param values - four float values
454 * \return index of the new parameter.
455 */
456GLint
457_mesa_add_named_constant(struct program_parameter_list *paramList,
458 const char *name, const GLfloat values[4])
459{
460 return add_parameter(paramList, name, values, CONSTANT);
461}
462
463
464/**
465 * Add a new unnamed constant to the parameter list.
466 * \param paramList - the parameter list
467 * \param values - four float values
468 * \return index of the new parameter.
469 */
470GLint
471_mesa_add_unnamed_constant(struct program_parameter_list *paramList,
472 const GLfloat values[4])
473{
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000474 return add_parameter(paramList, NULL, values, CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000475}
476
477
478/**
479 * Add a new state reference to the parameter list.
480 * \param paramList - the parameter list
481 * \param state - an array of 6 state tokens
482 *
483 * \return index of the new parameter.
484 */
485GLint
486_mesa_add_state_reference(struct program_parameter_list *paramList,
Brian Paul16241622005-11-03 02:26:47 +0000487 const GLint *stateTokens)
Michal Krol2861e732004-03-29 11:09:34 +0000488{
Brian Paul16241622005-11-03 02:26:47 +0000489 /* XXX we should probably search the current parameter list to see if
490 * the new state reference is already present.
Michal Krol2861e732004-03-29 11:09:34 +0000491 */
Brian Paul16241622005-11-03 02:26:47 +0000492 GLint index;
Michal Krol2861e732004-03-29 11:09:34 +0000493
Brian Paul16241622005-11-03 02:26:47 +0000494 index = add_parameter(paramList, NULL, NULL, STATE);
495 if (index >= 0) {
496 GLuint i;
497 for (i = 0; i < 6; i++)
498 paramList->Parameters[index].StateIndexes[i]
499 = (enum state_index) stateTokens[i];
500 }
Michal Krol2861e732004-03-29 11:09:34 +0000501
Brian Paul16241622005-11-03 02:26:47 +0000502 return index;
Michal Krol2861e732004-03-29 11:09:34 +0000503}
504
505
506/**
507 * Lookup a parameter value by name in the given parameter list.
508 * \return pointer to the float[4] values.
509 */
510GLfloat *
511_mesa_lookup_parameter_value(struct program_parameter_list *paramList,
512 GLsizei nameLen, const char *name)
513{
514 GLuint i;
515
516 if (!paramList)
517 return NULL;
518
519 if (nameLen == -1) {
520 /* name is null-terminated */
521 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000522 if (paramList->Parameters[i].Name &&
523 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000524 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000525 }
526 }
527 else {
528 /* name is not null-terminated, use nameLen */
529 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000530 if (paramList->Parameters[i].Name &&
531 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000532 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000533 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000534 }
535 }
536 return NULL;
537}
538
539
540/**
541 * Lookup a parameter index by name in the given parameter list.
542 * \return index of parameter in the list.
543 */
544GLint
545_mesa_lookup_parameter_index(struct program_parameter_list *paramList,
546 GLsizei nameLen, const char *name)
547{
548 GLint i;
549
550 if (!paramList)
551 return -1;
552
553 if (nameLen == -1) {
554 /* name is null-terminated */
555 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000556 if (paramList->Parameters[i].Name &&
557 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Michal Krol2861e732004-03-29 11:09:34 +0000558 return i;
559 }
560 }
561 else {
562 /* name is not null-terminated, use nameLen */
563 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000564 if (paramList->Parameters[i].Name &&
565 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000566 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
567 return i;
568 }
569 }
570 return -1;
571}
572
573
574/**
575 * Use the list of tokens in the state[] array to find global GL state
576 * and return it in <value>. Usually, four values are returned in <value>
577 * but matrix queries may return as many as 16 values.
578 * This function is used for ARB vertex/fragment programs.
579 * The program parser will produce the state[] values.
580 */
581static void
582_mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
583 GLfloat *value)
584{
585 switch (state[0]) {
586 case STATE_MATERIAL:
587 {
588 /* state[1] is either 0=front or 1=back side */
589 const GLuint face = (GLuint) state[1];
590 /* state[2] is the material attribute */
591 switch (state[2]) {
592 case STATE_AMBIENT:
593 if (face == 0)
594 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
595 else
596 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
597 return;
598 case STATE_DIFFUSE:
599 if (face == 0)
600 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
601 else
602 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
603 return;
604 case STATE_SPECULAR:
605 if (face == 0)
606 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
607 else
608 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
609 return;
610 case STATE_EMISSION:
611 if (face == 0)
612 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
613 else
614 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
615 return;
616 case STATE_SHININESS:
617 if (face == 0)
618 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
619 else
620 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
621 value[1] = 0.0F;
622 value[2] = 0.0F;
623 value[3] = 1.0F;
624 return;
625 default:
626 _mesa_problem(ctx, "Invalid material state in fetch_state");
627 return;
628 }
Alan Hourihane22ae6332004-12-02 13:29:40 +0000629 }
Michal Krol2861e732004-03-29 11:09:34 +0000630 case STATE_LIGHT:
631 {
632 /* state[1] is the light number */
633 const GLuint ln = (GLuint) state[1];
634 /* state[2] is the light attribute */
635 switch (state[2]) {
636 case STATE_AMBIENT:
637 COPY_4V(value, ctx->Light.Light[ln].Ambient);
638 return;
639 case STATE_DIFFUSE:
640 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
641 return;
642 case STATE_SPECULAR:
643 COPY_4V(value, ctx->Light.Light[ln].Specular);
644 return;
645 case STATE_POSITION:
646 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
647 return;
648 case STATE_ATTENUATION:
649 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
650 value[1] = ctx->Light.Light[ln].LinearAttenuation;
651 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
652 value[3] = ctx->Light.Light[ln].SpotExponent;
653 return;
654 case STATE_SPOT_DIRECTION:
Brian Paul52bf0052005-04-20 23:47:03 +0000655 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
656 value[3] = ctx->Light.Light[ln]._CosCutoff;
Michal Krol2861e732004-03-29 11:09:34 +0000657 return;
658 case STATE_HALF:
659 {
660 GLfloat eye_z[] = {0, 0, 1};
661
662 /* Compute infinite half angle vector:
663 * half-vector = light_position + (0, 0, 1)
664 * and then normalize. w = 0
Keith Whitwell7c26b612005-04-21 14:46:57 +0000665 *
666 * light.EyePosition.w should be 0 for infinite lights.
Michal Krol2861e732004-03-29 11:09:34 +0000667 */
Keith Whitwell7c26b612005-04-21 14:46:57 +0000668 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
669 NORMALIZE_3FV(value);
670 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000671 }
672 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000673 case STATE_POSITION_NORMALIZED:
674 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
675 NORMALIZE_3FV( value );
676 return;
Michal Krol2861e732004-03-29 11:09:34 +0000677 default:
678 _mesa_problem(ctx, "Invalid light state in fetch_state");
679 return;
680 }
681 }
Michal Krol2861e732004-03-29 11:09:34 +0000682 case STATE_LIGHTMODEL_AMBIENT:
683 COPY_4V(value, ctx->Light.Model.Ambient);
684 return;
685 case STATE_LIGHTMODEL_SCENECOLOR:
686 if (state[1] == 0) {
687 /* front */
688 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000689 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000690 value[i] = ctx->Light.Model.Ambient[i]
691 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
692 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
693 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000694 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000695 }
696 else {
697 /* back */
698 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000699 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000700 value[i] = ctx->Light.Model.Ambient[i]
701 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
702 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
703 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000704 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000705 }
706 return;
707 case STATE_LIGHTPROD:
708 {
709 const GLuint ln = (GLuint) state[1];
710 const GLuint face = (GLuint) state[2];
711 GLint i;
712 ASSERT(face == 0 || face == 1);
713 switch (state[3]) {
714 case STATE_AMBIENT:
715 for (i = 0; i < 3; i++) {
716 value[i] = ctx->Light.Light[ln].Ambient[i] *
717 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
718 }
719 /* [3] = material alpha */
720 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
721 return;
722 case STATE_DIFFUSE:
723 for (i = 0; i < 3; i++) {
724 value[i] = ctx->Light.Light[ln].Diffuse[i] *
725 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
726 }
727 /* [3] = material alpha */
728 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
729 return;
730 case STATE_SPECULAR:
731 for (i = 0; i < 3; i++) {
732 value[i] = ctx->Light.Light[ln].Specular[i] *
733 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
734 }
735 /* [3] = material alpha */
736 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
737 return;
738 default:
739 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
740 return;
741 }
742 }
Michal Krol2861e732004-03-29 11:09:34 +0000743 case STATE_TEXGEN:
744 {
745 /* state[1] is the texture unit */
746 const GLuint unit = (GLuint) state[1];
747 /* state[2] is the texgen attribute */
748 switch (state[2]) {
749 case STATE_TEXGEN_EYE_S:
750 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
751 return;
752 case STATE_TEXGEN_EYE_T:
753 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
754 return;
755 case STATE_TEXGEN_EYE_R:
756 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
757 return;
758 case STATE_TEXGEN_EYE_Q:
759 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
760 return;
761 case STATE_TEXGEN_OBJECT_S:
762 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
763 return;
764 case STATE_TEXGEN_OBJECT_T:
765 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
766 return;
767 case STATE_TEXGEN_OBJECT_R:
768 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
769 return;
770 case STATE_TEXGEN_OBJECT_Q:
771 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
772 return;
773 default:
774 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
775 return;
776 }
777 }
Michal Krol2861e732004-03-29 11:09:34 +0000778 case STATE_TEXENV_COLOR:
779 {
780 /* state[1] is the texture unit */
781 const GLuint unit = (GLuint) state[1];
782 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
783 }
784 return;
785 case STATE_FOG_COLOR:
786 COPY_4V(value, ctx->Fog.Color);
787 return;
788 case STATE_FOG_PARAMS:
789 value[0] = ctx->Fog.Density;
790 value[1] = ctx->Fog.Start;
791 value[2] = ctx->Fog.End;
792 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
793 return;
794 case STATE_CLIPPLANE:
795 {
796 const GLuint plane = (GLuint) state[1];
797 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
798 }
799 return;
800 case STATE_POINT_SIZE:
801 value[0] = ctx->Point.Size;
802 value[1] = ctx->Point.MinSize;
803 value[2] = ctx->Point.MaxSize;
804 value[3] = ctx->Point.Threshold;
805 return;
806 case STATE_POINT_ATTENUATION:
807 value[0] = ctx->Point.Params[0];
808 value[1] = ctx->Point.Params[1];
809 value[2] = ctx->Point.Params[2];
810 value[3] = 1.0F;
811 return;
812 case STATE_MATRIX:
813 {
814 /* state[1] = modelview, projection, texture, etc. */
815 /* state[2] = which texture matrix or program matrix */
816 /* state[3] = first column to fetch */
817 /* state[4] = last column to fetch */
818 /* state[5] = transpose, inverse or invtrans */
819
820 const GLmatrix *matrix;
821 const enum state_index mat = state[1];
822 const GLuint index = (GLuint) state[2];
823 const GLuint first = (GLuint) state[3];
824 const GLuint last = (GLuint) state[4];
825 const enum state_index modifier = state[5];
826 const GLfloat *m;
827 GLuint row, i;
828 if (mat == STATE_MODELVIEW) {
829 matrix = ctx->ModelviewMatrixStack.Top;
830 }
831 else if (mat == STATE_PROJECTION) {
832 matrix = ctx->ProjectionMatrixStack.Top;
833 }
834 else if (mat == STATE_MVP) {
835 matrix = &ctx->_ModelProjectMatrix;
836 }
837 else if (mat == STATE_TEXTURE) {
838 matrix = ctx->TextureMatrixStack[index].Top;
839 }
840 else if (mat == STATE_PROGRAM) {
841 matrix = ctx->ProgramMatrixStack[index].Top;
842 }
843 else {
844 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
845 return;
846 }
847 if (modifier == STATE_MATRIX_INVERSE ||
848 modifier == STATE_MATRIX_INVTRANS) {
Keith Whitwell78803b22005-04-15 12:57:23 +0000849 /* Be sure inverse is up to date:
850 */
Jouk Jansen49b1d952005-04-18 13:05:24 +0000851 _math_matrix_analyse( (GLmatrix*) matrix );
Michal Krol2861e732004-03-29 11:09:34 +0000852 m = matrix->inv;
853 }
854 else {
855 m = matrix->m;
856 }
857 if (modifier == STATE_MATRIX_TRANSPOSE ||
858 modifier == STATE_MATRIX_INVTRANS) {
859 for (i = 0, row = first; row <= last; row++) {
860 value[i++] = m[row * 4 + 0];
861 value[i++] = m[row * 4 + 1];
862 value[i++] = m[row * 4 + 2];
863 value[i++] = m[row * 4 + 3];
864 }
865 }
866 else {
867 for (i = 0, row = first; row <= last; row++) {
868 value[i++] = m[row + 0];
869 value[i++] = m[row + 4];
870 value[i++] = m[row + 8];
871 value[i++] = m[row + 12];
872 }
873 }
874 }
875 return;
876 case STATE_DEPTH_RANGE:
Brian Paul824fdf02004-06-29 00:00:06 +0000877 value[0] = ctx->Viewport.Near; /* near */
878 value[1] = ctx->Viewport.Far; /* far */
879 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
880 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000881 return;
882 case STATE_FRAGMENT_PROGRAM:
883 {
884 /* state[1] = {STATE_ENV, STATE_LOCAL} */
885 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000886 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000887 switch (state[1]) {
888 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000889 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000890 break;
Michal Krol2861e732004-03-29 11:09:34 +0000891 case STATE_LOCAL:
892 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000893 break;
Michal Krol2861e732004-03-29 11:09:34 +0000894 default:
895 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
896 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000897 }
898 }
Michal Krol2861e732004-03-29 11:09:34 +0000899 return;
900
901 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000902 {
Michal Krol2861e732004-03-29 11:09:34 +0000903 /* state[1] = {STATE_ENV, STATE_LOCAL} */
904 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000905 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000906 switch (state[1]) {
907 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000908 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000909 break;
Michal Krol2861e732004-03-29 11:09:34 +0000910 case STATE_LOCAL:
911 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000912 break;
Michal Krol2861e732004-03-29 11:09:34 +0000913 default:
914 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
915 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000916 }
917 }
Michal Krol2861e732004-03-29 11:09:34 +0000918 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000919
920 case STATE_INTERNAL:
921 {
922 switch (state[1]) {
923 case STATE_NORMAL_SCALE:
924 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
925 break;
926 default:
927 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
928 return;
929 }
930 }
931 return;
932
Michal Krol2861e732004-03-29 11:09:34 +0000933 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000934 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000935 return;
936 }
937}
938
939
940/**
941 * Loop over all the parameters in a parameter list. If the parameter
942 * is a GL state reference, look up the current value of that state
943 * variable and put it into the parameter's Value[4] array.
944 * This would be called at glBegin time when using a fragment program.
945 */
946void
947_mesa_load_state_parameters(GLcontext *ctx,
948 struct program_parameter_list *paramList)
949{
950 GLuint i;
951
952 if (!paramList)
953 return;
954
955 for (i = 0; i < paramList->NumParameters; i++) {
956 if (paramList->Parameters[i].Type == STATE) {
Keith Whitwell7c26b612005-04-21 14:46:57 +0000957 _mesa_fetch_state(ctx,
958 paramList->Parameters[i].StateIndexes,
959 paramList->ParameterValues[i]);
Michal Krol2861e732004-03-29 11:09:34 +0000960 }
961 }
962}
963
964
965
966/**********************************************************************/
967/* API functions */
968/**********************************************************************/
969
970
971/**
972 * Bind a program (make it current)
973 * \note Called from the GL API dispatcher by both glBindProgramNV
974 * and glBindProgramARB.
975 */
976void GLAPIENTRY
977_mesa_BindProgram(GLenum target, GLuint id)
978{
979 struct program *prog;
980 GET_CURRENT_CONTEXT(ctx);
981 ASSERT_OUTSIDE_BEGIN_END(ctx);
982
983 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
984
985 if ((target == GL_VERTEX_PROGRAM_NV
986 && ctx->Extensions.NV_vertex_program) ||
987 (target == GL_VERTEX_PROGRAM_ARB
988 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000989 /*** Vertex program binding ***/
990 struct vertex_program *curProg = ctx->VertexProgram.Current;
991 if (curProg->Base.Id == id) {
992 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +0000993 return;
Brian Paul765f1a12004-09-14 22:28:27 +0000994 }
995 if (curProg->Base.Id != 0) {
996 /* decrement refcount on previously bound vertex program */
997 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000998 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +0000999 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001000 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001001 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001002 }
1003 }
1004 }
1005 else if ((target == GL_FRAGMENT_PROGRAM_NV
1006 && ctx->Extensions.NV_fragment_program) ||
1007 (target == GL_FRAGMENT_PROGRAM_ARB
1008 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001009 /*** Fragment program binding ***/
1010 struct fragment_program *curProg = ctx->FragmentProgram.Current;
1011 if (curProg->Base.Id == id) {
1012 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001013 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001014 }
1015 if (curProg->Base.Id != 0) {
1016 /* decrement refcount on previously bound fragment program */
1017 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001018 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001019 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001020 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001021 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001022 }
1023 }
1024 }
1025 else {
1026 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1027 return;
1028 }
1029
1030 /* NOTE: binding to a non-existant program is not an error.
1031 * That's supposed to be caught in glBegin.
1032 */
1033 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001034 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001035 prog = NULL;
1036 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1037 prog = ctx->Shared->DefaultVertexProgram;
1038 else
1039 prog = ctx->Shared->DefaultFragmentProgram;
1040 }
1041 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001042 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001043 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001044 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001045 /* allocate a new program now */
1046 prog = ctx->Driver.NewProgram(ctx, target, id);
1047 if (!prog) {
1048 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1049 return;
1050 }
Michal Krol2861e732004-03-29 11:09:34 +00001051 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1052 }
Brian Paul765f1a12004-09-14 22:28:27 +00001053 else if (prog->Target != target) {
1054 _mesa_error(ctx, GL_INVALID_OPERATION,
1055 "glBindProgramNV/ARB(target mismatch)");
1056 return;
1057 }
Michal Krol2861e732004-03-29 11:09:34 +00001058 }
1059
1060 /* bind now */
1061 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1062 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1063 }
1064 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1065 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1066 }
1067
Brian Paul765f1a12004-09-14 22:28:27 +00001068 /* Never null pointers */
1069 ASSERT(ctx->VertexProgram.Current);
1070 ASSERT(ctx->FragmentProgram.Current);
1071
Michal Krol2861e732004-03-29 11:09:34 +00001072 if (prog)
1073 prog->RefCount++;
1074
1075 if (ctx->Driver.BindProgram)
1076 ctx->Driver.BindProgram(ctx, target, prog);
1077}
1078
1079
1080/**
1081 * Delete a list of programs.
1082 * \note Not compiled into display lists.
1083 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1084 */
1085void GLAPIENTRY
1086_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1087{
1088 GLint i;
1089 GET_CURRENT_CONTEXT(ctx);
1090 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1091
1092 if (n < 0) {
1093 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1094 return;
1095 }
1096
1097 for (i = 0; i < n; i++) {
1098 if (ids[i] != 0) {
1099 struct program *prog = (struct program *)
1100 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001101 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001102 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1103 }
1104 else if (prog) {
1105 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001106 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1107 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1108 if (ctx->VertexProgram.Current &&
1109 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1110 /* unbind this currently bound program */
1111 _mesa_BindProgram(prog->Target, 0);
1112 }
1113 }
1114 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1115 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1116 if (ctx->FragmentProgram.Current &&
1117 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1118 /* unbind this currently bound program */
1119 _mesa_BindProgram(prog->Target, 0);
1120 }
1121 }
1122 else {
1123 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1124 return;
1125 }
Brian Paulea2943e2005-01-20 04:02:02 +00001126 /* The ID is immediately available for re-use now */
1127 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1128 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001129 if (prog->RefCount <= 0) {
1130 ctx->Driver.DeleteProgram(ctx, prog);
1131 }
1132 }
Michal Krol2861e732004-03-29 11:09:34 +00001133 }
1134 }
1135}
1136
1137
1138/**
1139 * Generate a list of new program identifiers.
1140 * \note Not compiled into display lists.
1141 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1142 */
1143void GLAPIENTRY
1144_mesa_GenPrograms(GLsizei n, GLuint *ids)
1145{
1146 GLuint first;
1147 GLuint i;
1148 GET_CURRENT_CONTEXT(ctx);
1149 ASSERT_OUTSIDE_BEGIN_END(ctx);
1150
1151 if (n < 0) {
1152 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1153 return;
1154 }
1155
1156 if (!ids)
1157 return;
1158
1159 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1160
Brian Paul765f1a12004-09-14 22:28:27 +00001161 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001162 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001163 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001164 }
1165
1166 /* Return the program names */
1167 for (i = 0; i < (GLuint) n; i++) {
1168 ids[i] = first + i;
1169 }
1170}
1171
1172
1173/**
Brian Paul765f1a12004-09-14 22:28:27 +00001174 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001175 * \note Not compiled into display lists.
1176 * \note Called from both glIsProgramNV and glIsProgramARB.
1177 * \param id is the program identifier
1178 * \return GL_TRUE if id is a program, else GL_FALSE.
1179 */
1180GLboolean GLAPIENTRY
1181_mesa_IsProgram(GLuint id)
1182{
1183 GET_CURRENT_CONTEXT(ctx);
1184 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1185
1186 if (id == 0)
1187 return GL_FALSE;
1188
1189 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1190 return GL_TRUE;
1191 else
1192 return GL_FALSE;
1193}
1194
1195
1196
1197/**********************************************************************/
1198/* GL_MESA_program_debug extension */
1199/**********************************************************************/
1200
1201
1202/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001203GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001204glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1205 GLvoid *data)
1206{
1207 _mesa_ProgramCallbackMESA(target, callback, data);
1208}
1209
1210
1211void
1212_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1213 GLvoid *data)
1214{
1215 GET_CURRENT_CONTEXT(ctx);
1216
1217 switch (target) {
1218 case GL_FRAGMENT_PROGRAM_ARB:
1219 if (!ctx->Extensions.ARB_fragment_program) {
1220 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1221 return;
1222 }
1223 ctx->FragmentProgram.Callback = callback;
1224 ctx->FragmentProgram.CallbackData = data;
1225 break;
1226 case GL_FRAGMENT_PROGRAM_NV:
1227 if (!ctx->Extensions.NV_fragment_program) {
1228 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1229 return;
1230 }
1231 ctx->FragmentProgram.Callback = callback;
1232 ctx->FragmentProgram.CallbackData = data;
1233 break;
1234 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1235 if (!ctx->Extensions.ARB_vertex_program &&
1236 !ctx->Extensions.NV_vertex_program) {
1237 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1238 return;
1239 }
1240 ctx->VertexProgram.Callback = callback;
1241 ctx->VertexProgram.CallbackData = data;
1242 break;
1243 default:
1244 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1245 return;
1246 }
1247}
1248
1249
1250/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001251GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001252glGetProgramRegisterfvMESA(GLenum target,
1253 GLsizei len, const GLubyte *registerName,
1254 GLfloat *v)
1255{
1256 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1257}
1258
1259
1260void
1261_mesa_GetProgramRegisterfvMESA(GLenum target,
1262 GLsizei len, const GLubyte *registerName,
1263 GLfloat *v)
1264{
1265 char reg[1000];
1266 GET_CURRENT_CONTEXT(ctx);
1267
1268 /* We _should_ be inside glBegin/glEnd */
1269#if 0
1270 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1271 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1272 return;
1273 }
1274#endif
1275
1276 /* make null-terminated copy of registerName */
1277 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1278 _mesa_memcpy(reg, registerName, len);
1279 reg[len] = 0;
1280
1281 switch (target) {
1282 case GL_VERTEX_PROGRAM_NV:
1283 if (!ctx->Extensions.ARB_vertex_program &&
1284 !ctx->Extensions.NV_vertex_program) {
1285 _mesa_error(ctx, GL_INVALID_ENUM,
1286 "glGetProgramRegisterfvMESA(target)");
1287 return;
1288 }
Brian Paul6d460af2004-04-23 14:16:46 +00001289 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001290 _mesa_error(ctx, GL_INVALID_OPERATION,
1291 "glGetProgramRegisterfvMESA");
1292 return;
1293 }
1294 /* GL_NV_vertex_program */
1295 if (reg[0] == 'R') {
1296 /* Temp register */
1297 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001298 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001299 _mesa_error(ctx, GL_INVALID_VALUE,
1300 "glGetProgramRegisterfvMESA(registerName)");
1301 return;
1302 }
1303 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1304 }
1305 else if (reg[0] == 'v' && reg[1] == '[') {
1306 /* Vertex Input attribute */
1307 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001308 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001309 const char *name = _mesa_nv_vertex_input_register_name(i);
1310 char number[10];
Brian Paulaa206952005-09-16 18:14:24 +00001311 _mesa_sprintf(number, "%d", i);
Michal Krol2861e732004-03-29 11:09:34 +00001312 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1313 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1314 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1315 return;
1316 }
1317 }
1318 _mesa_error(ctx, GL_INVALID_VALUE,
1319 "glGetProgramRegisterfvMESA(registerName)");
1320 return;
1321 }
1322 else if (reg[0] == 'o' && reg[1] == '[') {
1323 /* Vertex output attribute */
1324 }
1325 /* GL_ARB_vertex_program */
1326 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1327
1328 }
1329 else {
1330 _mesa_error(ctx, GL_INVALID_VALUE,
1331 "glGetProgramRegisterfvMESA(registerName)");
1332 return;
1333 }
1334 break;
1335 case GL_FRAGMENT_PROGRAM_ARB:
1336 if (!ctx->Extensions.ARB_fragment_program) {
1337 _mesa_error(ctx, GL_INVALID_ENUM,
1338 "glGetProgramRegisterfvMESA(target)");
1339 return;
1340 }
Brian Paul6d460af2004-04-23 14:16:46 +00001341 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001342 _mesa_error(ctx, GL_INVALID_OPERATION,
1343 "glGetProgramRegisterfvMESA");
1344 return;
1345 }
1346 /* XXX to do */
1347 break;
1348 case GL_FRAGMENT_PROGRAM_NV:
1349 if (!ctx->Extensions.NV_fragment_program) {
1350 _mesa_error(ctx, GL_INVALID_ENUM,
1351 "glGetProgramRegisterfvMESA(target)");
1352 return;
1353 }
Brian Paul6d460af2004-04-23 14:16:46 +00001354 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001355 _mesa_error(ctx, GL_INVALID_OPERATION,
1356 "glGetProgramRegisterfvMESA");
1357 return;
1358 }
1359 if (reg[0] == 'R') {
1360 /* Temp register */
1361 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001362 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001363 _mesa_error(ctx, GL_INVALID_VALUE,
1364 "glGetProgramRegisterfvMESA(registerName)");
1365 return;
1366 }
1367 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1368 }
1369 else if (reg[0] == 'f' && reg[1] == '[') {
1370 /* Fragment input attribute */
1371 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001372 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001373 const char *name = _mesa_nv_fragment_input_register_name(i);
1374 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1375 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1376 return;
1377 }
1378 }
1379 _mesa_error(ctx, GL_INVALID_VALUE,
1380 "glGetProgramRegisterfvMESA(registerName)");
1381 return;
1382 }
1383 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1384 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00001385 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLR]);
Michal Krol2861e732004-03-29 11:09:34 +00001386 }
1387 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1388 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00001389 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLH]);
Michal Krol2861e732004-03-29 11:09:34 +00001390 }
1391 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1392 /* Fragment output depth */
Brian Paul90ebb582005-11-02 18:06:12 +00001393 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_DEPR]);
Michal Krol2861e732004-03-29 11:09:34 +00001394 }
1395 else {
1396 /* try user-defined identifiers */
1397 const GLfloat *value = _mesa_lookup_parameter_value(
1398 ctx->FragmentProgram.Current->Parameters, -1, reg);
1399 if (value) {
1400 COPY_4V(v, value);
1401 }
1402 else {
1403 _mesa_error(ctx, GL_INVALID_VALUE,
1404 "glGetProgramRegisterfvMESA(registerName)");
1405 return;
1406 }
1407 }
1408 break;
1409 default:
1410 _mesa_error(ctx, GL_INVALID_ENUM,
1411 "glGetProgramRegisterfvMESA(target)");
1412 return;
1413 }
1414
1415}