blob: 1e2d34a3079a8b54f98e237ba49e4aba434d93e2 [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian Paul765f1a12004-09-14 22:28:27 +00003 * Version: 6.2
Michal Krol2861e732004-03-29 11:09:34 +00004 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/**
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"
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;
327 if (atifs->Instructions)
328 _mesa_free(atifs->Instructions);
329 }
330
Michal Krol2861e732004-03-29 11:09:34 +0000331 _mesa_free(prog);
332}
333
334
335
336/**********************************************************************/
337/* Program parameter functions */
338/**********************************************************************/
339
340struct program_parameter_list *
341_mesa_new_parameter_list(void)
342{
343 return (struct program_parameter_list *)
344 _mesa_calloc(sizeof(struct program_parameter_list));
345}
346
347
348/**
349 * Free a parameter list and all its parameters
350 */
351void
352_mesa_free_parameter_list(struct program_parameter_list *paramList)
353{
354 _mesa_free_parameters(paramList);
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000355 _mesa_free(paramList->Parameters);
356 _mesa_free(paramList->ParameterValues);
Michal Krol2861e732004-03-29 11:09:34 +0000357 _mesa_free(paramList);
358}
359
360
361/**
362 * Free all the parameters in the given list, but don't free the
363 * paramList structure itself.
364 */
365void
366_mesa_free_parameters(struct program_parameter_list *paramList)
367{
368 GLuint i;
369 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000370 if (paramList->Parameters[i].Name)
371 _mesa_free((void *) paramList->Parameters[i].Name);
Michal Krol2861e732004-03-29 11:09:34 +0000372 }
Michal Krol2861e732004-03-29 11:09:34 +0000373 paramList->NumParameters = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000374}
375
376
377/**
378 * Helper function used by the functions below.
379 */
380static GLint
381add_parameter(struct program_parameter_list *paramList,
382 const char *name, const GLfloat values[4],
383 enum parameter_type type)
384{
385 const GLuint n = paramList->NumParameters;
386
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000387 if (n == paramList->Size) {
388 paramList->Size *= 2;
389 if (!paramList->Size)
390 paramList->Size = 4;
391
392 paramList->Parameters = (struct program_parameter *)
393 _mesa_realloc(paramList->Parameters,
394 n * sizeof(struct program_parameter),
395 paramList->Size * sizeof(struct program_parameter));
396 paramList->ParameterValues = (GLfloat (*)[4])
397 _mesa_realloc(paramList->ParameterValues,
398 n * 4 * sizeof(GLfloat),
399 paramList->Size * 4 * sizeof(GLfloat));
400 }
Keith Whitwell7c26b612005-04-21 14:46:57 +0000401
402 if (!paramList->Parameters ||
403 !paramList->ParameterValues) {
Michal Krol2861e732004-03-29 11:09:34 +0000404 /* out of memory */
405 paramList->NumParameters = 0;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000406 paramList->Size = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000407 return -1;
408 }
409 else {
410 paramList->NumParameters = n + 1;
Keith Whitwella42fe192005-05-10 18:22:19 +0000411
412 _mesa_memset(&paramList->Parameters[n], 0,
413 sizeof(struct program_parameter));
414
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000415 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000416 paramList->Parameters[n].Type = type;
417 if (values)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000418 COPY_4V(paramList->ParameterValues[n], values);
Michal Krol2861e732004-03-29 11:09:34 +0000419 return (GLint) n;
420 }
421}
422
423
424/**
425 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
426 * \return index of the new entry in the parameter list
427 */
428GLint
429_mesa_add_named_parameter(struct program_parameter_list *paramList,
430 const char *name, const GLfloat values[4])
431{
432 return add_parameter(paramList, name, values, NAMED_PARAMETER);
433}
434
435
436/**
437 * Add a new unnamed constant to the parameter list.
438 * \param paramList - the parameter list
439 * \param values - four float values
440 * \return index of the new parameter.
441 */
442GLint
443_mesa_add_named_constant(struct program_parameter_list *paramList,
444 const char *name, const GLfloat values[4])
445{
446 return add_parameter(paramList, name, values, CONSTANT);
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_unnamed_constant(struct program_parameter_list *paramList,
458 const GLfloat values[4])
459{
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000460 return add_parameter(paramList, NULL, values, CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000461}
462
463
464/**
465 * Add a new state reference to the parameter list.
466 * \param paramList - the parameter list
467 * \param state - an array of 6 state tokens
468 *
469 * \return index of the new parameter.
470 */
471GLint
472_mesa_add_state_reference(struct program_parameter_list *paramList,
473 GLint *stateTokens)
474{
475 /* XXX Should we parse <stateString> here and produce the parameter's
476 * list of STATE_* tokens here, or in the parser?
477 */
478 GLint a, idx;
479
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000480 idx = add_parameter(paramList, NULL, NULL, STATE);
Michal Krol2861e732004-03-29 11:09:34 +0000481
482 for (a=0; a<6; a++)
483 paramList->Parameters[idx].StateIndexes[a] = (enum state_index) stateTokens[a];
484
485 return idx;
486}
487
488
489/**
490 * Lookup a parameter value by name in the given parameter list.
491 * \return pointer to the float[4] values.
492 */
493GLfloat *
494_mesa_lookup_parameter_value(struct program_parameter_list *paramList,
495 GLsizei nameLen, const char *name)
496{
497 GLuint i;
498
499 if (!paramList)
500 return NULL;
501
502 if (nameLen == -1) {
503 /* name is null-terminated */
504 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000505 if (paramList->Parameters[i].Name &&
506 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000507 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000508 }
509 }
510 else {
511 /* name is not null-terminated, use nameLen */
512 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000513 if (paramList->Parameters[i].Name &&
514 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000515 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000516 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000517 }
518 }
519 return NULL;
520}
521
522
523/**
524 * Lookup a parameter index by name in the given parameter list.
525 * \return index of parameter in the list.
526 */
527GLint
528_mesa_lookup_parameter_index(struct program_parameter_list *paramList,
529 GLsizei nameLen, const char *name)
530{
531 GLint i;
532
533 if (!paramList)
534 return -1;
535
536 if (nameLen == -1) {
537 /* name is null-terminated */
538 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000539 if (paramList->Parameters[i].Name &&
540 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Michal Krol2861e732004-03-29 11:09:34 +0000541 return i;
542 }
543 }
544 else {
545 /* name is not null-terminated, use nameLen */
546 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000547 if (paramList->Parameters[i].Name &&
548 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000549 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
550 return i;
551 }
552 }
553 return -1;
554}
555
556
557/**
558 * Use the list of tokens in the state[] array to find global GL state
559 * and return it in <value>. Usually, four values are returned in <value>
560 * but matrix queries may return as many as 16 values.
561 * This function is used for ARB vertex/fragment programs.
562 * The program parser will produce the state[] values.
563 */
564static void
565_mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
566 GLfloat *value)
567{
568 switch (state[0]) {
569 case STATE_MATERIAL:
570 {
571 /* state[1] is either 0=front or 1=back side */
572 const GLuint face = (GLuint) state[1];
573 /* state[2] is the material attribute */
574 switch (state[2]) {
575 case STATE_AMBIENT:
576 if (face == 0)
577 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
578 else
579 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
580 return;
581 case STATE_DIFFUSE:
582 if (face == 0)
583 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
584 else
585 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
586 return;
587 case STATE_SPECULAR:
588 if (face == 0)
589 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
590 else
591 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
592 return;
593 case STATE_EMISSION:
594 if (face == 0)
595 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
596 else
597 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
598 return;
599 case STATE_SHININESS:
600 if (face == 0)
601 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
602 else
603 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
604 value[1] = 0.0F;
605 value[2] = 0.0F;
606 value[3] = 1.0F;
607 return;
608 default:
609 _mesa_problem(ctx, "Invalid material state in fetch_state");
610 return;
611 }
Alan Hourihane22ae6332004-12-02 13:29:40 +0000612 }
Michal Krol2861e732004-03-29 11:09:34 +0000613 case STATE_LIGHT:
614 {
615 /* state[1] is the light number */
616 const GLuint ln = (GLuint) state[1];
617 /* state[2] is the light attribute */
618 switch (state[2]) {
619 case STATE_AMBIENT:
620 COPY_4V(value, ctx->Light.Light[ln].Ambient);
621 return;
622 case STATE_DIFFUSE:
623 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
624 return;
625 case STATE_SPECULAR:
626 COPY_4V(value, ctx->Light.Light[ln].Specular);
627 return;
628 case STATE_POSITION:
629 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
630 return;
631 case STATE_ATTENUATION:
632 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
633 value[1] = ctx->Light.Light[ln].LinearAttenuation;
634 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
635 value[3] = ctx->Light.Light[ln].SpotExponent;
636 return;
637 case STATE_SPOT_DIRECTION:
Brian Paul52bf0052005-04-20 23:47:03 +0000638 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
639 value[3] = ctx->Light.Light[ln]._CosCutoff;
Michal Krol2861e732004-03-29 11:09:34 +0000640 return;
641 case STATE_HALF:
642 {
643 GLfloat eye_z[] = {0, 0, 1};
644
645 /* Compute infinite half angle vector:
646 * half-vector = light_position + (0, 0, 1)
647 * and then normalize. w = 0
Keith Whitwell7c26b612005-04-21 14:46:57 +0000648 *
649 * light.EyePosition.w should be 0 for infinite lights.
Michal Krol2861e732004-03-29 11:09:34 +0000650 */
Keith Whitwell7c26b612005-04-21 14:46:57 +0000651 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
652 NORMALIZE_3FV(value);
653 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000654 }
655 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000656 case STATE_POSITION_NORMALIZED:
657 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
658 NORMALIZE_3FV( value );
659 return;
Michal Krol2861e732004-03-29 11:09:34 +0000660 default:
661 _mesa_problem(ctx, "Invalid light state in fetch_state");
662 return;
663 }
664 }
Michal Krol2861e732004-03-29 11:09:34 +0000665 case STATE_LIGHTMODEL_AMBIENT:
666 COPY_4V(value, ctx->Light.Model.Ambient);
667 return;
668 case STATE_LIGHTMODEL_SCENECOLOR:
669 if (state[1] == 0) {
670 /* front */
671 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000672 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000673 value[i] = ctx->Light.Model.Ambient[i]
674 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
675 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
676 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000677 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000678 }
679 else {
680 /* back */
681 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000682 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000683 value[i] = ctx->Light.Model.Ambient[i]
684 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
685 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
686 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000687 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000688 }
689 return;
690 case STATE_LIGHTPROD:
691 {
692 const GLuint ln = (GLuint) state[1];
693 const GLuint face = (GLuint) state[2];
694 GLint i;
695 ASSERT(face == 0 || face == 1);
696 switch (state[3]) {
697 case STATE_AMBIENT:
698 for (i = 0; i < 3; i++) {
699 value[i] = ctx->Light.Light[ln].Ambient[i] *
700 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
701 }
702 /* [3] = material alpha */
703 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
704 return;
705 case STATE_DIFFUSE:
706 for (i = 0; i < 3; i++) {
707 value[i] = ctx->Light.Light[ln].Diffuse[i] *
708 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
709 }
710 /* [3] = material alpha */
711 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
712 return;
713 case STATE_SPECULAR:
714 for (i = 0; i < 3; i++) {
715 value[i] = ctx->Light.Light[ln].Specular[i] *
716 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
717 }
718 /* [3] = material alpha */
719 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
720 return;
721 default:
722 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
723 return;
724 }
725 }
Michal Krol2861e732004-03-29 11:09:34 +0000726 case STATE_TEXGEN:
727 {
728 /* state[1] is the texture unit */
729 const GLuint unit = (GLuint) state[1];
730 /* state[2] is the texgen attribute */
731 switch (state[2]) {
732 case STATE_TEXGEN_EYE_S:
733 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
734 return;
735 case STATE_TEXGEN_EYE_T:
736 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
737 return;
738 case STATE_TEXGEN_EYE_R:
739 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
740 return;
741 case STATE_TEXGEN_EYE_Q:
742 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
743 return;
744 case STATE_TEXGEN_OBJECT_S:
745 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
746 return;
747 case STATE_TEXGEN_OBJECT_T:
748 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
749 return;
750 case STATE_TEXGEN_OBJECT_R:
751 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
752 return;
753 case STATE_TEXGEN_OBJECT_Q:
754 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
755 return;
756 default:
757 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
758 return;
759 }
760 }
Michal Krol2861e732004-03-29 11:09:34 +0000761 case STATE_TEXENV_COLOR:
762 {
763 /* state[1] is the texture unit */
764 const GLuint unit = (GLuint) state[1];
765 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
766 }
767 return;
768 case STATE_FOG_COLOR:
769 COPY_4V(value, ctx->Fog.Color);
770 return;
771 case STATE_FOG_PARAMS:
772 value[0] = ctx->Fog.Density;
773 value[1] = ctx->Fog.Start;
774 value[2] = ctx->Fog.End;
775 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
776 return;
777 case STATE_CLIPPLANE:
778 {
779 const GLuint plane = (GLuint) state[1];
780 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
781 }
782 return;
783 case STATE_POINT_SIZE:
784 value[0] = ctx->Point.Size;
785 value[1] = ctx->Point.MinSize;
786 value[2] = ctx->Point.MaxSize;
787 value[3] = ctx->Point.Threshold;
788 return;
789 case STATE_POINT_ATTENUATION:
790 value[0] = ctx->Point.Params[0];
791 value[1] = ctx->Point.Params[1];
792 value[2] = ctx->Point.Params[2];
793 value[3] = 1.0F;
794 return;
795 case STATE_MATRIX:
796 {
797 /* state[1] = modelview, projection, texture, etc. */
798 /* state[2] = which texture matrix or program matrix */
799 /* state[3] = first column to fetch */
800 /* state[4] = last column to fetch */
801 /* state[5] = transpose, inverse or invtrans */
802
803 const GLmatrix *matrix;
804 const enum state_index mat = state[1];
805 const GLuint index = (GLuint) state[2];
806 const GLuint first = (GLuint) state[3];
807 const GLuint last = (GLuint) state[4];
808 const enum state_index modifier = state[5];
809 const GLfloat *m;
810 GLuint row, i;
811 if (mat == STATE_MODELVIEW) {
812 matrix = ctx->ModelviewMatrixStack.Top;
813 }
814 else if (mat == STATE_PROJECTION) {
815 matrix = ctx->ProjectionMatrixStack.Top;
816 }
817 else if (mat == STATE_MVP) {
818 matrix = &ctx->_ModelProjectMatrix;
819 }
820 else if (mat == STATE_TEXTURE) {
821 matrix = ctx->TextureMatrixStack[index].Top;
822 }
823 else if (mat == STATE_PROGRAM) {
824 matrix = ctx->ProgramMatrixStack[index].Top;
825 }
826 else {
827 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
828 return;
829 }
830 if (modifier == STATE_MATRIX_INVERSE ||
831 modifier == STATE_MATRIX_INVTRANS) {
Keith Whitwell78803b22005-04-15 12:57:23 +0000832 /* Be sure inverse is up to date:
833 */
Jouk Jansen49b1d952005-04-18 13:05:24 +0000834 _math_matrix_analyse( (GLmatrix*) matrix );
Michal Krol2861e732004-03-29 11:09:34 +0000835 m = matrix->inv;
836 }
837 else {
838 m = matrix->m;
839 }
840 if (modifier == STATE_MATRIX_TRANSPOSE ||
841 modifier == STATE_MATRIX_INVTRANS) {
842 for (i = 0, row = first; row <= last; row++) {
843 value[i++] = m[row * 4 + 0];
844 value[i++] = m[row * 4 + 1];
845 value[i++] = m[row * 4 + 2];
846 value[i++] = m[row * 4 + 3];
847 }
848 }
849 else {
850 for (i = 0, row = first; row <= last; row++) {
851 value[i++] = m[row + 0];
852 value[i++] = m[row + 4];
853 value[i++] = m[row + 8];
854 value[i++] = m[row + 12];
855 }
856 }
857 }
858 return;
859 case STATE_DEPTH_RANGE:
Brian Paul824fdf02004-06-29 00:00:06 +0000860 value[0] = ctx->Viewport.Near; /* near */
861 value[1] = ctx->Viewport.Far; /* far */
862 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
863 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000864 return;
865 case STATE_FRAGMENT_PROGRAM:
866 {
867 /* state[1] = {STATE_ENV, STATE_LOCAL} */
868 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000869 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000870 switch (state[1]) {
871 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000872 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000873 break;
Michal Krol2861e732004-03-29 11:09:34 +0000874 case STATE_LOCAL:
875 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000876 break;
Michal Krol2861e732004-03-29 11:09:34 +0000877 default:
878 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
879 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000880 }
881 }
Michal Krol2861e732004-03-29 11:09:34 +0000882 return;
883
884 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000885 {
Michal Krol2861e732004-03-29 11:09:34 +0000886 /* state[1] = {STATE_ENV, STATE_LOCAL} */
887 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000888 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000889 switch (state[1]) {
890 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000891 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000892 break;
Michal Krol2861e732004-03-29 11:09:34 +0000893 case STATE_LOCAL:
894 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000895 break;
Michal Krol2861e732004-03-29 11:09:34 +0000896 default:
897 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
898 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000899 }
900 }
Michal Krol2861e732004-03-29 11:09:34 +0000901 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000902
903 case STATE_INTERNAL:
904 {
905 switch (state[1]) {
906 case STATE_NORMAL_SCALE:
907 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
908 break;
909 default:
910 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
911 return;
912 }
913 }
914 return;
915
Michal Krol2861e732004-03-29 11:09:34 +0000916 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000917 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000918 return;
919 }
920}
921
922
923/**
924 * Loop over all the parameters in a parameter list. If the parameter
925 * is a GL state reference, look up the current value of that state
926 * variable and put it into the parameter's Value[4] array.
927 * This would be called at glBegin time when using a fragment program.
928 */
929void
930_mesa_load_state_parameters(GLcontext *ctx,
931 struct program_parameter_list *paramList)
932{
933 GLuint i;
934
935 if (!paramList)
936 return;
937
938 for (i = 0; i < paramList->NumParameters; i++) {
939 if (paramList->Parameters[i].Type == STATE) {
Keith Whitwell7c26b612005-04-21 14:46:57 +0000940 _mesa_fetch_state(ctx,
941 paramList->Parameters[i].StateIndexes,
942 paramList->ParameterValues[i]);
Michal Krol2861e732004-03-29 11:09:34 +0000943 }
944 }
945}
946
947
948
949/**********************************************************************/
950/* API functions */
951/**********************************************************************/
952
953
954/**
955 * Bind a program (make it current)
956 * \note Called from the GL API dispatcher by both glBindProgramNV
957 * and glBindProgramARB.
958 */
959void GLAPIENTRY
960_mesa_BindProgram(GLenum target, GLuint id)
961{
962 struct program *prog;
963 GET_CURRENT_CONTEXT(ctx);
964 ASSERT_OUTSIDE_BEGIN_END(ctx);
965
966 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
967
968 if ((target == GL_VERTEX_PROGRAM_NV
969 && ctx->Extensions.NV_vertex_program) ||
970 (target == GL_VERTEX_PROGRAM_ARB
971 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000972 /*** Vertex program binding ***/
973 struct vertex_program *curProg = ctx->VertexProgram.Current;
974 if (curProg->Base.Id == id) {
975 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +0000976 return;
Brian Paul765f1a12004-09-14 22:28:27 +0000977 }
978 if (curProg->Base.Id != 0) {
979 /* decrement refcount on previously bound vertex program */
980 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000981 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +0000982 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +0000983 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +0000984 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +0000985 }
986 }
987 }
988 else if ((target == GL_FRAGMENT_PROGRAM_NV
989 && ctx->Extensions.NV_fragment_program) ||
990 (target == GL_FRAGMENT_PROGRAM_ARB
991 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000992 /*** Fragment program binding ***/
993 struct fragment_program *curProg = ctx->FragmentProgram.Current;
994 if (curProg->Base.Id == id) {
995 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +0000996 return;
Brian Paul765f1a12004-09-14 22:28:27 +0000997 }
998 if (curProg->Base.Id != 0) {
999 /* decrement refcount on previously bound fragment program */
1000 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001001 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001002 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001003 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001004 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001005 }
1006 }
1007 }
1008 else {
1009 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1010 return;
1011 }
1012
1013 /* NOTE: binding to a non-existant program is not an error.
1014 * That's supposed to be caught in glBegin.
1015 */
1016 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001017 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001018 prog = NULL;
1019 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1020 prog = ctx->Shared->DefaultVertexProgram;
1021 else
1022 prog = ctx->Shared->DefaultFragmentProgram;
1023 }
1024 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001025 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001026 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001027 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001028 /* allocate a new program now */
1029 prog = ctx->Driver.NewProgram(ctx, target, id);
1030 if (!prog) {
1031 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1032 return;
1033 }
Michal Krol2861e732004-03-29 11:09:34 +00001034 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1035 }
Brian Paul765f1a12004-09-14 22:28:27 +00001036 else if (prog->Target != target) {
1037 _mesa_error(ctx, GL_INVALID_OPERATION,
1038 "glBindProgramNV/ARB(target mismatch)");
1039 return;
1040 }
Michal Krol2861e732004-03-29 11:09:34 +00001041 }
1042
1043 /* bind now */
1044 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1045 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1046 }
1047 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1048 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1049 }
1050
Brian Paul765f1a12004-09-14 22:28:27 +00001051 /* Never null pointers */
1052 ASSERT(ctx->VertexProgram.Current);
1053 ASSERT(ctx->FragmentProgram.Current);
1054
Michal Krol2861e732004-03-29 11:09:34 +00001055 if (prog)
1056 prog->RefCount++;
1057
1058 if (ctx->Driver.BindProgram)
1059 ctx->Driver.BindProgram(ctx, target, prog);
1060}
1061
1062
1063/**
1064 * Delete a list of programs.
1065 * \note Not compiled into display lists.
1066 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1067 */
1068void GLAPIENTRY
1069_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1070{
1071 GLint i;
1072 GET_CURRENT_CONTEXT(ctx);
1073 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1074
1075 if (n < 0) {
1076 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1077 return;
1078 }
1079
1080 for (i = 0; i < n; i++) {
1081 if (ids[i] != 0) {
1082 struct program *prog = (struct program *)
1083 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001084 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001085 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1086 }
1087 else if (prog) {
1088 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001089 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1090 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1091 if (ctx->VertexProgram.Current &&
1092 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1093 /* unbind this currently bound program */
1094 _mesa_BindProgram(prog->Target, 0);
1095 }
1096 }
1097 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1098 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1099 if (ctx->FragmentProgram.Current &&
1100 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1101 /* unbind this currently bound program */
1102 _mesa_BindProgram(prog->Target, 0);
1103 }
1104 }
1105 else {
1106 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1107 return;
1108 }
Brian Paulea2943e2005-01-20 04:02:02 +00001109 /* The ID is immediately available for re-use now */
1110 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1111 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001112 if (prog->RefCount <= 0) {
1113 ctx->Driver.DeleteProgram(ctx, prog);
1114 }
1115 }
Michal Krol2861e732004-03-29 11:09:34 +00001116 }
1117 }
1118}
1119
1120
1121/**
1122 * Generate a list of new program identifiers.
1123 * \note Not compiled into display lists.
1124 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1125 */
1126void GLAPIENTRY
1127_mesa_GenPrograms(GLsizei n, GLuint *ids)
1128{
1129 GLuint first;
1130 GLuint i;
1131 GET_CURRENT_CONTEXT(ctx);
1132 ASSERT_OUTSIDE_BEGIN_END(ctx);
1133
1134 if (n < 0) {
1135 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1136 return;
1137 }
1138
1139 if (!ids)
1140 return;
1141
1142 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1143
Brian Paul765f1a12004-09-14 22:28:27 +00001144 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001145 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001146 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001147 }
1148
1149 /* Return the program names */
1150 for (i = 0; i < (GLuint) n; i++) {
1151 ids[i] = first + i;
1152 }
1153}
1154
1155
1156/**
Brian Paul765f1a12004-09-14 22:28:27 +00001157 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001158 * \note Not compiled into display lists.
1159 * \note Called from both glIsProgramNV and glIsProgramARB.
1160 * \param id is the program identifier
1161 * \return GL_TRUE if id is a program, else GL_FALSE.
1162 */
1163GLboolean GLAPIENTRY
1164_mesa_IsProgram(GLuint id)
1165{
1166 GET_CURRENT_CONTEXT(ctx);
1167 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1168
1169 if (id == 0)
1170 return GL_FALSE;
1171
1172 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1173 return GL_TRUE;
1174 else
1175 return GL_FALSE;
1176}
1177
1178
1179
1180/**********************************************************************/
1181/* GL_MESA_program_debug extension */
1182/**********************************************************************/
1183
1184
1185/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001186GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001187glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1188 GLvoid *data)
1189{
1190 _mesa_ProgramCallbackMESA(target, callback, data);
1191}
1192
1193
1194void
1195_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1196 GLvoid *data)
1197{
1198 GET_CURRENT_CONTEXT(ctx);
1199
1200 switch (target) {
1201 case GL_FRAGMENT_PROGRAM_ARB:
1202 if (!ctx->Extensions.ARB_fragment_program) {
1203 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1204 return;
1205 }
1206 ctx->FragmentProgram.Callback = callback;
1207 ctx->FragmentProgram.CallbackData = data;
1208 break;
1209 case GL_FRAGMENT_PROGRAM_NV:
1210 if (!ctx->Extensions.NV_fragment_program) {
1211 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1212 return;
1213 }
1214 ctx->FragmentProgram.Callback = callback;
1215 ctx->FragmentProgram.CallbackData = data;
1216 break;
1217 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1218 if (!ctx->Extensions.ARB_vertex_program &&
1219 !ctx->Extensions.NV_vertex_program) {
1220 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1221 return;
1222 }
1223 ctx->VertexProgram.Callback = callback;
1224 ctx->VertexProgram.CallbackData = data;
1225 break;
1226 default:
1227 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1228 return;
1229 }
1230}
1231
1232
1233/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001234GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001235glGetProgramRegisterfvMESA(GLenum target,
1236 GLsizei len, const GLubyte *registerName,
1237 GLfloat *v)
1238{
1239 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1240}
1241
1242
1243void
1244_mesa_GetProgramRegisterfvMESA(GLenum target,
1245 GLsizei len, const GLubyte *registerName,
1246 GLfloat *v)
1247{
1248 char reg[1000];
1249 GET_CURRENT_CONTEXT(ctx);
1250
1251 /* We _should_ be inside glBegin/glEnd */
1252#if 0
1253 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1254 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1255 return;
1256 }
1257#endif
1258
1259 /* make null-terminated copy of registerName */
1260 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1261 _mesa_memcpy(reg, registerName, len);
1262 reg[len] = 0;
1263
1264 switch (target) {
1265 case GL_VERTEX_PROGRAM_NV:
1266 if (!ctx->Extensions.ARB_vertex_program &&
1267 !ctx->Extensions.NV_vertex_program) {
1268 _mesa_error(ctx, GL_INVALID_ENUM,
1269 "glGetProgramRegisterfvMESA(target)");
1270 return;
1271 }
Brian Paul6d460af2004-04-23 14:16:46 +00001272 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001273 _mesa_error(ctx, GL_INVALID_OPERATION,
1274 "glGetProgramRegisterfvMESA");
1275 return;
1276 }
1277 /* GL_NV_vertex_program */
1278 if (reg[0] == 'R') {
1279 /* Temp register */
1280 GLint i = _mesa_atoi(reg + 1);
1281 if (i >= (GLint)ctx->Const.MaxVertexProgramTemps) {
1282 _mesa_error(ctx, GL_INVALID_VALUE,
1283 "glGetProgramRegisterfvMESA(registerName)");
1284 return;
1285 }
1286 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1287 }
1288 else if (reg[0] == 'v' && reg[1] == '[') {
1289 /* Vertex Input attribute */
1290 GLuint i;
1291 for (i = 0; i < ctx->Const.MaxVertexProgramAttribs; i++) {
1292 const char *name = _mesa_nv_vertex_input_register_name(i);
1293 char number[10];
1294 sprintf(number, "%d", i);
1295 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1296 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1297 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1298 return;
1299 }
1300 }
1301 _mesa_error(ctx, GL_INVALID_VALUE,
1302 "glGetProgramRegisterfvMESA(registerName)");
1303 return;
1304 }
1305 else if (reg[0] == 'o' && reg[1] == '[') {
1306 /* Vertex output attribute */
1307 }
1308 /* GL_ARB_vertex_program */
1309 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1310
1311 }
1312 else {
1313 _mesa_error(ctx, GL_INVALID_VALUE,
1314 "glGetProgramRegisterfvMESA(registerName)");
1315 return;
1316 }
1317 break;
1318 case GL_FRAGMENT_PROGRAM_ARB:
1319 if (!ctx->Extensions.ARB_fragment_program) {
1320 _mesa_error(ctx, GL_INVALID_ENUM,
1321 "glGetProgramRegisterfvMESA(target)");
1322 return;
1323 }
Brian Paul6d460af2004-04-23 14:16:46 +00001324 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001325 _mesa_error(ctx, GL_INVALID_OPERATION,
1326 "glGetProgramRegisterfvMESA");
1327 return;
1328 }
1329 /* XXX to do */
1330 break;
1331 case GL_FRAGMENT_PROGRAM_NV:
1332 if (!ctx->Extensions.NV_fragment_program) {
1333 _mesa_error(ctx, GL_INVALID_ENUM,
1334 "glGetProgramRegisterfvMESA(target)");
1335 return;
1336 }
Brian Paul6d460af2004-04-23 14:16:46 +00001337 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001338 _mesa_error(ctx, GL_INVALID_OPERATION,
1339 "glGetProgramRegisterfvMESA");
1340 return;
1341 }
1342 if (reg[0] == 'R') {
1343 /* Temp register */
1344 GLint i = _mesa_atoi(reg + 1);
1345 if (i >= (GLint)ctx->Const.MaxFragmentProgramTemps) {
1346 _mesa_error(ctx, GL_INVALID_VALUE,
1347 "glGetProgramRegisterfvMESA(registerName)");
1348 return;
1349 }
1350 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1351 }
1352 else if (reg[0] == 'f' && reg[1] == '[') {
1353 /* Fragment input attribute */
1354 GLuint i;
1355 for (i = 0; i < ctx->Const.MaxFragmentProgramAttribs; i++) {
1356 const char *name = _mesa_nv_fragment_input_register_name(i);
1357 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1358 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1359 return;
1360 }
1361 }
1362 _mesa_error(ctx, GL_INVALID_VALUE,
1363 "glGetProgramRegisterfvMESA(registerName)");
1364 return;
1365 }
1366 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1367 /* Fragment output color */
1368 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR]);
1369 }
1370 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1371 /* Fragment output color */
1372 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLH]);
1373 }
1374 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1375 /* Fragment output depth */
1376 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR]);
1377 }
1378 else {
1379 /* try user-defined identifiers */
1380 const GLfloat *value = _mesa_lookup_parameter_value(
1381 ctx->FragmentProgram.Current->Parameters, -1, reg);
1382 if (value) {
1383 COPY_4V(v, value);
1384 }
1385 else {
1386 _mesa_error(ctx, GL_INVALID_VALUE,
1387 "glGetProgramRegisterfvMESA(registerName)");
1388 return;
1389 }
1390 }
1391 break;
1392 default:
1393 _mesa_error(ctx, GL_INVALID_ENUM,
1394 "glGetProgramRegisterfvMESA(target)");
1395 return;
1396 }
1397
1398}