blob: a375805052d6999f3f50116b93781ee47ff79034 [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 Whitwellf29f2fc2005-05-10 13:56:23 +0000411 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000412 paramList->Parameters[n].Type = type;
413 if (values)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000414 COPY_4V(paramList->ParameterValues[n], values);
Michal Krol2861e732004-03-29 11:09:34 +0000415 return (GLint) n;
416 }
417}
418
419
420/**
421 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
422 * \return index of the new entry in the parameter list
423 */
424GLint
425_mesa_add_named_parameter(struct program_parameter_list *paramList,
426 const char *name, const GLfloat values[4])
427{
428 return add_parameter(paramList, name, values, NAMED_PARAMETER);
429}
430
431
432/**
433 * Add a new unnamed constant to the parameter list.
434 * \param paramList - the parameter list
435 * \param values - four float values
436 * \return index of the new parameter.
437 */
438GLint
439_mesa_add_named_constant(struct program_parameter_list *paramList,
440 const char *name, const GLfloat values[4])
441{
442 return add_parameter(paramList, name, values, CONSTANT);
443}
444
445
446/**
447 * Add a new unnamed constant to the parameter list.
448 * \param paramList - the parameter list
449 * \param values - four float values
450 * \return index of the new parameter.
451 */
452GLint
453_mesa_add_unnamed_constant(struct program_parameter_list *paramList,
454 const GLfloat values[4])
455{
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000456 return add_parameter(paramList, NULL, values, CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000457}
458
459
460/**
461 * Add a new state reference to the parameter list.
462 * \param paramList - the parameter list
463 * \param state - an array of 6 state tokens
464 *
465 * \return index of the new parameter.
466 */
467GLint
468_mesa_add_state_reference(struct program_parameter_list *paramList,
469 GLint *stateTokens)
470{
471 /* XXX Should we parse <stateString> here and produce the parameter's
472 * list of STATE_* tokens here, or in the parser?
473 */
474 GLint a, idx;
475
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000476 idx = add_parameter(paramList, NULL, NULL, STATE);
Michal Krol2861e732004-03-29 11:09:34 +0000477
478 for (a=0; a<6; a++)
479 paramList->Parameters[idx].StateIndexes[a] = (enum state_index) stateTokens[a];
480
481 return idx;
482}
483
484
485/**
486 * Lookup a parameter value by name in the given parameter list.
487 * \return pointer to the float[4] values.
488 */
489GLfloat *
490_mesa_lookup_parameter_value(struct program_parameter_list *paramList,
491 GLsizei nameLen, const char *name)
492{
493 GLuint i;
494
495 if (!paramList)
496 return NULL;
497
498 if (nameLen == -1) {
499 /* name is null-terminated */
500 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000501 if (paramList->Parameters[i].Name &&
502 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000503 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000504 }
505 }
506 else {
507 /* name is not null-terminated, use nameLen */
508 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000509 if (paramList->Parameters[i].Name &&
510 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000511 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000512 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000513 }
514 }
515 return NULL;
516}
517
518
519/**
520 * Lookup a parameter index by name in the given parameter list.
521 * \return index of parameter in the list.
522 */
523GLint
524_mesa_lookup_parameter_index(struct program_parameter_list *paramList,
525 GLsizei nameLen, const char *name)
526{
527 GLint i;
528
529 if (!paramList)
530 return -1;
531
532 if (nameLen == -1) {
533 /* name is null-terminated */
534 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000535 if (paramList->Parameters[i].Name &&
536 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Michal Krol2861e732004-03-29 11:09:34 +0000537 return i;
538 }
539 }
540 else {
541 /* name is not null-terminated, use nameLen */
542 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000543 if (paramList->Parameters[i].Name &&
544 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000545 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
546 return i;
547 }
548 }
549 return -1;
550}
551
552
553/**
554 * Use the list of tokens in the state[] array to find global GL state
555 * and return it in <value>. Usually, four values are returned in <value>
556 * but matrix queries may return as many as 16 values.
557 * This function is used for ARB vertex/fragment programs.
558 * The program parser will produce the state[] values.
559 */
560static void
561_mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
562 GLfloat *value)
563{
564 switch (state[0]) {
565 case STATE_MATERIAL:
566 {
567 /* state[1] is either 0=front or 1=back side */
568 const GLuint face = (GLuint) state[1];
569 /* state[2] is the material attribute */
570 switch (state[2]) {
571 case STATE_AMBIENT:
572 if (face == 0)
573 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
574 else
575 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
576 return;
577 case STATE_DIFFUSE:
578 if (face == 0)
579 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
580 else
581 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
582 return;
583 case STATE_SPECULAR:
584 if (face == 0)
585 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
586 else
587 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
588 return;
589 case STATE_EMISSION:
590 if (face == 0)
591 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
592 else
593 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
594 return;
595 case STATE_SHININESS:
596 if (face == 0)
597 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
598 else
599 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
600 value[1] = 0.0F;
601 value[2] = 0.0F;
602 value[3] = 1.0F;
603 return;
604 default:
605 _mesa_problem(ctx, "Invalid material state in fetch_state");
606 return;
607 }
Alan Hourihane22ae6332004-12-02 13:29:40 +0000608 }
Michal Krol2861e732004-03-29 11:09:34 +0000609 case STATE_LIGHT:
610 {
611 /* state[1] is the light number */
612 const GLuint ln = (GLuint) state[1];
613 /* state[2] is the light attribute */
614 switch (state[2]) {
615 case STATE_AMBIENT:
616 COPY_4V(value, ctx->Light.Light[ln].Ambient);
617 return;
618 case STATE_DIFFUSE:
619 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
620 return;
621 case STATE_SPECULAR:
622 COPY_4V(value, ctx->Light.Light[ln].Specular);
623 return;
624 case STATE_POSITION:
625 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
626 return;
627 case STATE_ATTENUATION:
628 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
629 value[1] = ctx->Light.Light[ln].LinearAttenuation;
630 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
631 value[3] = ctx->Light.Light[ln].SpotExponent;
632 return;
633 case STATE_SPOT_DIRECTION:
Brian Paul52bf0052005-04-20 23:47:03 +0000634 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
635 value[3] = ctx->Light.Light[ln]._CosCutoff;
Michal Krol2861e732004-03-29 11:09:34 +0000636 return;
637 case STATE_HALF:
638 {
639 GLfloat eye_z[] = {0, 0, 1};
640
641 /* Compute infinite half angle vector:
642 * half-vector = light_position + (0, 0, 1)
643 * and then normalize. w = 0
Keith Whitwell7c26b612005-04-21 14:46:57 +0000644 *
645 * light.EyePosition.w should be 0 for infinite lights.
Michal Krol2861e732004-03-29 11:09:34 +0000646 */
Keith Whitwell7c26b612005-04-21 14:46:57 +0000647 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
648 NORMALIZE_3FV(value);
649 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000650 }
651 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000652 case STATE_POSITION_NORMALIZED:
653 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
654 NORMALIZE_3FV( value );
655 return;
Michal Krol2861e732004-03-29 11:09:34 +0000656 default:
657 _mesa_problem(ctx, "Invalid light state in fetch_state");
658 return;
659 }
660 }
Michal Krol2861e732004-03-29 11:09:34 +0000661 case STATE_LIGHTMODEL_AMBIENT:
662 COPY_4V(value, ctx->Light.Model.Ambient);
663 return;
664 case STATE_LIGHTMODEL_SCENECOLOR:
665 if (state[1] == 0) {
666 /* front */
667 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000668 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000669 value[i] = ctx->Light.Model.Ambient[i]
670 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
671 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
672 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000673 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000674 }
675 else {
676 /* back */
677 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000678 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000679 value[i] = ctx->Light.Model.Ambient[i]
680 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
681 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
682 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000683 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000684 }
685 return;
686 case STATE_LIGHTPROD:
687 {
688 const GLuint ln = (GLuint) state[1];
689 const GLuint face = (GLuint) state[2];
690 GLint i;
691 ASSERT(face == 0 || face == 1);
692 switch (state[3]) {
693 case STATE_AMBIENT:
694 for (i = 0; i < 3; i++) {
695 value[i] = ctx->Light.Light[ln].Ambient[i] *
696 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
697 }
698 /* [3] = material alpha */
699 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
700 return;
701 case STATE_DIFFUSE:
702 for (i = 0; i < 3; i++) {
703 value[i] = ctx->Light.Light[ln].Diffuse[i] *
704 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
705 }
706 /* [3] = material alpha */
707 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
708 return;
709 case STATE_SPECULAR:
710 for (i = 0; i < 3; i++) {
711 value[i] = ctx->Light.Light[ln].Specular[i] *
712 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
713 }
714 /* [3] = material alpha */
715 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
716 return;
717 default:
718 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
719 return;
720 }
721 }
Michal Krol2861e732004-03-29 11:09:34 +0000722 case STATE_TEXGEN:
723 {
724 /* state[1] is the texture unit */
725 const GLuint unit = (GLuint) state[1];
726 /* state[2] is the texgen attribute */
727 switch (state[2]) {
728 case STATE_TEXGEN_EYE_S:
729 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
730 return;
731 case STATE_TEXGEN_EYE_T:
732 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
733 return;
734 case STATE_TEXGEN_EYE_R:
735 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
736 return;
737 case STATE_TEXGEN_EYE_Q:
738 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
739 return;
740 case STATE_TEXGEN_OBJECT_S:
741 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
742 return;
743 case STATE_TEXGEN_OBJECT_T:
744 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
745 return;
746 case STATE_TEXGEN_OBJECT_R:
747 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
748 return;
749 case STATE_TEXGEN_OBJECT_Q:
750 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
751 return;
752 default:
753 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
754 return;
755 }
756 }
Michal Krol2861e732004-03-29 11:09:34 +0000757 case STATE_TEXENV_COLOR:
758 {
759 /* state[1] is the texture unit */
760 const GLuint unit = (GLuint) state[1];
761 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
762 }
763 return;
764 case STATE_FOG_COLOR:
765 COPY_4V(value, ctx->Fog.Color);
766 return;
767 case STATE_FOG_PARAMS:
768 value[0] = ctx->Fog.Density;
769 value[1] = ctx->Fog.Start;
770 value[2] = ctx->Fog.End;
771 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
772 return;
773 case STATE_CLIPPLANE:
774 {
775 const GLuint plane = (GLuint) state[1];
776 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
777 }
778 return;
779 case STATE_POINT_SIZE:
780 value[0] = ctx->Point.Size;
781 value[1] = ctx->Point.MinSize;
782 value[2] = ctx->Point.MaxSize;
783 value[3] = ctx->Point.Threshold;
784 return;
785 case STATE_POINT_ATTENUATION:
786 value[0] = ctx->Point.Params[0];
787 value[1] = ctx->Point.Params[1];
788 value[2] = ctx->Point.Params[2];
789 value[3] = 1.0F;
790 return;
791 case STATE_MATRIX:
792 {
793 /* state[1] = modelview, projection, texture, etc. */
794 /* state[2] = which texture matrix or program matrix */
795 /* state[3] = first column to fetch */
796 /* state[4] = last column to fetch */
797 /* state[5] = transpose, inverse or invtrans */
798
799 const GLmatrix *matrix;
800 const enum state_index mat = state[1];
801 const GLuint index = (GLuint) state[2];
802 const GLuint first = (GLuint) state[3];
803 const GLuint last = (GLuint) state[4];
804 const enum state_index modifier = state[5];
805 const GLfloat *m;
806 GLuint row, i;
807 if (mat == STATE_MODELVIEW) {
808 matrix = ctx->ModelviewMatrixStack.Top;
809 }
810 else if (mat == STATE_PROJECTION) {
811 matrix = ctx->ProjectionMatrixStack.Top;
812 }
813 else if (mat == STATE_MVP) {
814 matrix = &ctx->_ModelProjectMatrix;
815 }
816 else if (mat == STATE_TEXTURE) {
817 matrix = ctx->TextureMatrixStack[index].Top;
818 }
819 else if (mat == STATE_PROGRAM) {
820 matrix = ctx->ProgramMatrixStack[index].Top;
821 }
822 else {
823 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
824 return;
825 }
826 if (modifier == STATE_MATRIX_INVERSE ||
827 modifier == STATE_MATRIX_INVTRANS) {
Keith Whitwell78803b22005-04-15 12:57:23 +0000828 /* Be sure inverse is up to date:
829 */
Jouk Jansen49b1d952005-04-18 13:05:24 +0000830 _math_matrix_analyse( (GLmatrix*) matrix );
Michal Krol2861e732004-03-29 11:09:34 +0000831 m = matrix->inv;
832 }
833 else {
834 m = matrix->m;
835 }
836 if (modifier == STATE_MATRIX_TRANSPOSE ||
837 modifier == STATE_MATRIX_INVTRANS) {
838 for (i = 0, row = first; row <= last; row++) {
839 value[i++] = m[row * 4 + 0];
840 value[i++] = m[row * 4 + 1];
841 value[i++] = m[row * 4 + 2];
842 value[i++] = m[row * 4 + 3];
843 }
844 }
845 else {
846 for (i = 0, row = first; row <= last; row++) {
847 value[i++] = m[row + 0];
848 value[i++] = m[row + 4];
849 value[i++] = m[row + 8];
850 value[i++] = m[row + 12];
851 }
852 }
853 }
854 return;
855 case STATE_DEPTH_RANGE:
Brian Paul824fdf02004-06-29 00:00:06 +0000856 value[0] = ctx->Viewport.Near; /* near */
857 value[1] = ctx->Viewport.Far; /* far */
858 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
859 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000860 return;
861 case STATE_FRAGMENT_PROGRAM:
862 {
863 /* state[1] = {STATE_ENV, STATE_LOCAL} */
864 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000865 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000866 switch (state[1]) {
867 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000868 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000869 break;
Michal Krol2861e732004-03-29 11:09:34 +0000870 case STATE_LOCAL:
871 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000872 break;
Michal Krol2861e732004-03-29 11:09:34 +0000873 default:
874 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
875 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000876 }
877 }
Michal Krol2861e732004-03-29 11:09:34 +0000878 return;
879
880 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000881 {
Michal Krol2861e732004-03-29 11:09:34 +0000882 /* state[1] = {STATE_ENV, STATE_LOCAL} */
883 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000884 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000885 switch (state[1]) {
886 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000887 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000888 break;
Michal Krol2861e732004-03-29 11:09:34 +0000889 case STATE_LOCAL:
890 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000891 break;
Michal Krol2861e732004-03-29 11:09:34 +0000892 default:
893 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
894 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000895 }
896 }
Michal Krol2861e732004-03-29 11:09:34 +0000897 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000898
899 case STATE_INTERNAL:
900 {
901 switch (state[1]) {
902 case STATE_NORMAL_SCALE:
903 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
904 break;
905 default:
906 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
907 return;
908 }
909 }
910 return;
911
Michal Krol2861e732004-03-29 11:09:34 +0000912 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000913 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000914 return;
915 }
916}
917
918
919/**
920 * Loop over all the parameters in a parameter list. If the parameter
921 * is a GL state reference, look up the current value of that state
922 * variable and put it into the parameter's Value[4] array.
923 * This would be called at glBegin time when using a fragment program.
924 */
925void
926_mesa_load_state_parameters(GLcontext *ctx,
927 struct program_parameter_list *paramList)
928{
929 GLuint i;
930
931 if (!paramList)
932 return;
933
934 for (i = 0; i < paramList->NumParameters; i++) {
935 if (paramList->Parameters[i].Type == STATE) {
Keith Whitwell7c26b612005-04-21 14:46:57 +0000936 _mesa_fetch_state(ctx,
937 paramList->Parameters[i].StateIndexes,
938 paramList->ParameterValues[i]);
Michal Krol2861e732004-03-29 11:09:34 +0000939 }
940 }
941}
942
943
944
945/**********************************************************************/
946/* API functions */
947/**********************************************************************/
948
949
950/**
951 * Bind a program (make it current)
952 * \note Called from the GL API dispatcher by both glBindProgramNV
953 * and glBindProgramARB.
954 */
955void GLAPIENTRY
956_mesa_BindProgram(GLenum target, GLuint id)
957{
958 struct program *prog;
959 GET_CURRENT_CONTEXT(ctx);
960 ASSERT_OUTSIDE_BEGIN_END(ctx);
961
962 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
963
964 if ((target == GL_VERTEX_PROGRAM_NV
965 && ctx->Extensions.NV_vertex_program) ||
966 (target == GL_VERTEX_PROGRAM_ARB
967 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000968 /*** Vertex program binding ***/
969 struct vertex_program *curProg = ctx->VertexProgram.Current;
970 if (curProg->Base.Id == id) {
971 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +0000972 return;
Brian Paul765f1a12004-09-14 22:28:27 +0000973 }
974 if (curProg->Base.Id != 0) {
975 /* decrement refcount on previously bound vertex program */
976 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000977 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +0000978 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +0000979 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +0000980 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +0000981 }
982 }
983 }
984 else if ((target == GL_FRAGMENT_PROGRAM_NV
985 && ctx->Extensions.NV_fragment_program) ||
986 (target == GL_FRAGMENT_PROGRAM_ARB
987 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000988 /*** Fragment program binding ***/
989 struct fragment_program *curProg = ctx->FragmentProgram.Current;
990 if (curProg->Base.Id == id) {
991 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +0000992 return;
Brian Paul765f1a12004-09-14 22:28:27 +0000993 }
994 if (curProg->Base.Id != 0) {
995 /* decrement refcount on previously bound fragment program */
996 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000997 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +0000998 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +0000999 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001000 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001001 }
1002 }
1003 }
1004 else {
1005 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1006 return;
1007 }
1008
1009 /* NOTE: binding to a non-existant program is not an error.
1010 * That's supposed to be caught in glBegin.
1011 */
1012 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001013 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001014 prog = NULL;
1015 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1016 prog = ctx->Shared->DefaultVertexProgram;
1017 else
1018 prog = ctx->Shared->DefaultFragmentProgram;
1019 }
1020 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001021 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001022 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001023 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001024 /* allocate a new program now */
1025 prog = ctx->Driver.NewProgram(ctx, target, id);
1026 if (!prog) {
1027 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1028 return;
1029 }
Michal Krol2861e732004-03-29 11:09:34 +00001030 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1031 }
Brian Paul765f1a12004-09-14 22:28:27 +00001032 else if (prog->Target != target) {
1033 _mesa_error(ctx, GL_INVALID_OPERATION,
1034 "glBindProgramNV/ARB(target mismatch)");
1035 return;
1036 }
Michal Krol2861e732004-03-29 11:09:34 +00001037 }
1038
1039 /* bind now */
1040 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1041 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1042 }
1043 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1044 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1045 }
1046
Brian Paul765f1a12004-09-14 22:28:27 +00001047 /* Never null pointers */
1048 ASSERT(ctx->VertexProgram.Current);
1049 ASSERT(ctx->FragmentProgram.Current);
1050
Michal Krol2861e732004-03-29 11:09:34 +00001051 if (prog)
1052 prog->RefCount++;
1053
1054 if (ctx->Driver.BindProgram)
1055 ctx->Driver.BindProgram(ctx, target, prog);
1056}
1057
1058
1059/**
1060 * Delete a list of programs.
1061 * \note Not compiled into display lists.
1062 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1063 */
1064void GLAPIENTRY
1065_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1066{
1067 GLint i;
1068 GET_CURRENT_CONTEXT(ctx);
1069 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1070
1071 if (n < 0) {
1072 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1073 return;
1074 }
1075
1076 for (i = 0; i < n; i++) {
1077 if (ids[i] != 0) {
1078 struct program *prog = (struct program *)
1079 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001080 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001081 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1082 }
1083 else if (prog) {
1084 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001085 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1086 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1087 if (ctx->VertexProgram.Current &&
1088 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1089 /* unbind this currently bound program */
1090 _mesa_BindProgram(prog->Target, 0);
1091 }
1092 }
1093 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1094 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1095 if (ctx->FragmentProgram.Current &&
1096 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1097 /* unbind this currently bound program */
1098 _mesa_BindProgram(prog->Target, 0);
1099 }
1100 }
1101 else {
1102 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1103 return;
1104 }
Brian Paulea2943e2005-01-20 04:02:02 +00001105 /* The ID is immediately available for re-use now */
1106 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1107 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001108 if (prog->RefCount <= 0) {
1109 ctx->Driver.DeleteProgram(ctx, prog);
1110 }
1111 }
Michal Krol2861e732004-03-29 11:09:34 +00001112 }
1113 }
1114}
1115
1116
1117/**
1118 * Generate a list of new program identifiers.
1119 * \note Not compiled into display lists.
1120 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1121 */
1122void GLAPIENTRY
1123_mesa_GenPrograms(GLsizei n, GLuint *ids)
1124{
1125 GLuint first;
1126 GLuint i;
1127 GET_CURRENT_CONTEXT(ctx);
1128 ASSERT_OUTSIDE_BEGIN_END(ctx);
1129
1130 if (n < 0) {
1131 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1132 return;
1133 }
1134
1135 if (!ids)
1136 return;
1137
1138 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1139
Brian Paul765f1a12004-09-14 22:28:27 +00001140 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001141 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001142 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001143 }
1144
1145 /* Return the program names */
1146 for (i = 0; i < (GLuint) n; i++) {
1147 ids[i] = first + i;
1148 }
1149}
1150
1151
1152/**
Brian Paul765f1a12004-09-14 22:28:27 +00001153 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001154 * \note Not compiled into display lists.
1155 * \note Called from both glIsProgramNV and glIsProgramARB.
1156 * \param id is the program identifier
1157 * \return GL_TRUE if id is a program, else GL_FALSE.
1158 */
1159GLboolean GLAPIENTRY
1160_mesa_IsProgram(GLuint id)
1161{
1162 GET_CURRENT_CONTEXT(ctx);
1163 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1164
1165 if (id == 0)
1166 return GL_FALSE;
1167
1168 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1169 return GL_TRUE;
1170 else
1171 return GL_FALSE;
1172}
1173
1174
1175
1176/**********************************************************************/
1177/* GL_MESA_program_debug extension */
1178/**********************************************************************/
1179
1180
1181/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001182GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001183glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1184 GLvoid *data)
1185{
1186 _mesa_ProgramCallbackMESA(target, callback, data);
1187}
1188
1189
1190void
1191_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1192 GLvoid *data)
1193{
1194 GET_CURRENT_CONTEXT(ctx);
1195
1196 switch (target) {
1197 case GL_FRAGMENT_PROGRAM_ARB:
1198 if (!ctx->Extensions.ARB_fragment_program) {
1199 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1200 return;
1201 }
1202 ctx->FragmentProgram.Callback = callback;
1203 ctx->FragmentProgram.CallbackData = data;
1204 break;
1205 case GL_FRAGMENT_PROGRAM_NV:
1206 if (!ctx->Extensions.NV_fragment_program) {
1207 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1208 return;
1209 }
1210 ctx->FragmentProgram.Callback = callback;
1211 ctx->FragmentProgram.CallbackData = data;
1212 break;
1213 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1214 if (!ctx->Extensions.ARB_vertex_program &&
1215 !ctx->Extensions.NV_vertex_program) {
1216 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1217 return;
1218 }
1219 ctx->VertexProgram.Callback = callback;
1220 ctx->VertexProgram.CallbackData = data;
1221 break;
1222 default:
1223 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1224 return;
1225 }
1226}
1227
1228
1229/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001230GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001231glGetProgramRegisterfvMESA(GLenum target,
1232 GLsizei len, const GLubyte *registerName,
1233 GLfloat *v)
1234{
1235 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1236}
1237
1238
1239void
1240_mesa_GetProgramRegisterfvMESA(GLenum target,
1241 GLsizei len, const GLubyte *registerName,
1242 GLfloat *v)
1243{
1244 char reg[1000];
1245 GET_CURRENT_CONTEXT(ctx);
1246
1247 /* We _should_ be inside glBegin/glEnd */
1248#if 0
1249 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1250 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1251 return;
1252 }
1253#endif
1254
1255 /* make null-terminated copy of registerName */
1256 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1257 _mesa_memcpy(reg, registerName, len);
1258 reg[len] = 0;
1259
1260 switch (target) {
1261 case GL_VERTEX_PROGRAM_NV:
1262 if (!ctx->Extensions.ARB_vertex_program &&
1263 !ctx->Extensions.NV_vertex_program) {
1264 _mesa_error(ctx, GL_INVALID_ENUM,
1265 "glGetProgramRegisterfvMESA(target)");
1266 return;
1267 }
Brian Paul6d460af2004-04-23 14:16:46 +00001268 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001269 _mesa_error(ctx, GL_INVALID_OPERATION,
1270 "glGetProgramRegisterfvMESA");
1271 return;
1272 }
1273 /* GL_NV_vertex_program */
1274 if (reg[0] == 'R') {
1275 /* Temp register */
1276 GLint i = _mesa_atoi(reg + 1);
1277 if (i >= (GLint)ctx->Const.MaxVertexProgramTemps) {
1278 _mesa_error(ctx, GL_INVALID_VALUE,
1279 "glGetProgramRegisterfvMESA(registerName)");
1280 return;
1281 }
1282 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1283 }
1284 else if (reg[0] == 'v' && reg[1] == '[') {
1285 /* Vertex Input attribute */
1286 GLuint i;
1287 for (i = 0; i < ctx->Const.MaxVertexProgramAttribs; i++) {
1288 const char *name = _mesa_nv_vertex_input_register_name(i);
1289 char number[10];
1290 sprintf(number, "%d", i);
1291 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1292 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1293 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1294 return;
1295 }
1296 }
1297 _mesa_error(ctx, GL_INVALID_VALUE,
1298 "glGetProgramRegisterfvMESA(registerName)");
1299 return;
1300 }
1301 else if (reg[0] == 'o' && reg[1] == '[') {
1302 /* Vertex output attribute */
1303 }
1304 /* GL_ARB_vertex_program */
1305 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1306
1307 }
1308 else {
1309 _mesa_error(ctx, GL_INVALID_VALUE,
1310 "glGetProgramRegisterfvMESA(registerName)");
1311 return;
1312 }
1313 break;
1314 case GL_FRAGMENT_PROGRAM_ARB:
1315 if (!ctx->Extensions.ARB_fragment_program) {
1316 _mesa_error(ctx, GL_INVALID_ENUM,
1317 "glGetProgramRegisterfvMESA(target)");
1318 return;
1319 }
Brian Paul6d460af2004-04-23 14:16:46 +00001320 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001321 _mesa_error(ctx, GL_INVALID_OPERATION,
1322 "glGetProgramRegisterfvMESA");
1323 return;
1324 }
1325 /* XXX to do */
1326 break;
1327 case GL_FRAGMENT_PROGRAM_NV:
1328 if (!ctx->Extensions.NV_fragment_program) {
1329 _mesa_error(ctx, GL_INVALID_ENUM,
1330 "glGetProgramRegisterfvMESA(target)");
1331 return;
1332 }
Brian Paul6d460af2004-04-23 14:16:46 +00001333 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001334 _mesa_error(ctx, GL_INVALID_OPERATION,
1335 "glGetProgramRegisterfvMESA");
1336 return;
1337 }
1338 if (reg[0] == 'R') {
1339 /* Temp register */
1340 GLint i = _mesa_atoi(reg + 1);
1341 if (i >= (GLint)ctx->Const.MaxFragmentProgramTemps) {
1342 _mesa_error(ctx, GL_INVALID_VALUE,
1343 "glGetProgramRegisterfvMESA(registerName)");
1344 return;
1345 }
1346 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1347 }
1348 else if (reg[0] == 'f' && reg[1] == '[') {
1349 /* Fragment input attribute */
1350 GLuint i;
1351 for (i = 0; i < ctx->Const.MaxFragmentProgramAttribs; i++) {
1352 const char *name = _mesa_nv_fragment_input_register_name(i);
1353 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1354 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1355 return;
1356 }
1357 }
1358 _mesa_error(ctx, GL_INVALID_VALUE,
1359 "glGetProgramRegisterfvMESA(registerName)");
1360 return;
1361 }
1362 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1363 /* Fragment output color */
1364 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR]);
1365 }
1366 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1367 /* Fragment output color */
1368 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLH]);
1369 }
1370 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1371 /* Fragment output depth */
1372 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR]);
1373 }
1374 else {
1375 /* try user-defined identifiers */
1376 const GLfloat *value = _mesa_lookup_parameter_value(
1377 ctx->FragmentProgram.Current->Parameters, -1, reg);
1378 if (value) {
1379 COPY_4V(v, value);
1380 }
1381 else {
1382 _mesa_error(ctx, GL_INVALID_VALUE,
1383 "glGetProgramRegisterfvMESA(registerName)");
1384 return;
1385 }
1386 }
1387 break;
1388 default:
1389 _mesa_error(ctx, GL_INVALID_ENUM,
1390 "glGetProgramRegisterfvMESA(target)");
1391 return;
1392 }
1393
1394}