blob: bc8bc1bcfa3383f02d955f9418c0b5d2f51342b9 [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);
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000358 paramList->Parameters = NULL;
359 paramList->ParameterValues = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000360}
361
362
363/**
364 * Free all the parameters in the given list, but don't free the
365 * paramList structure itself.
366 */
367void
368_mesa_free_parameters(struct program_parameter_list *paramList)
369{
370 GLuint i;
371 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000372 if (paramList->Parameters[i].Name)
373 _mesa_free((void *) paramList->Parameters[i].Name);
Michal Krol2861e732004-03-29 11:09:34 +0000374 }
Michal Krol2861e732004-03-29 11:09:34 +0000375 paramList->NumParameters = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000376}
377
378
379/**
380 * Helper function used by the functions below.
381 */
382static GLint
383add_parameter(struct program_parameter_list *paramList,
384 const char *name, const GLfloat values[4],
385 enum parameter_type type)
386{
387 const GLuint n = paramList->NumParameters;
388
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000389 if (n == paramList->Size) {
390 paramList->Size *= 2;
391 if (!paramList->Size)
392 paramList->Size = 4;
393
394 paramList->Parameters = (struct program_parameter *)
395 _mesa_realloc(paramList->Parameters,
396 n * sizeof(struct program_parameter),
397 paramList->Size * sizeof(struct program_parameter));
398 paramList->ParameterValues = (GLfloat (*)[4])
399 _mesa_realloc(paramList->ParameterValues,
400 n * 4 * sizeof(GLfloat),
401 paramList->Size * 4 * sizeof(GLfloat));
402 }
Keith Whitwell7c26b612005-04-21 14:46:57 +0000403
404 if (!paramList->Parameters ||
405 !paramList->ParameterValues) {
Michal Krol2861e732004-03-29 11:09:34 +0000406 /* out of memory */
407 paramList->NumParameters = 0;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000408 paramList->Size = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000409 return -1;
410 }
411 else {
412 paramList->NumParameters = n + 1;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000413 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000414 paramList->Parameters[n].Type = type;
415 if (values)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000416 COPY_4V(paramList->ParameterValues[n], values);
Michal Krol2861e732004-03-29 11:09:34 +0000417 return (GLint) n;
418 }
419}
420
421
422/**
423 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
424 * \return index of the new entry in the parameter list
425 */
426GLint
427_mesa_add_named_parameter(struct program_parameter_list *paramList,
428 const char *name, const GLfloat values[4])
429{
430 return add_parameter(paramList, name, values, NAMED_PARAMETER);
431}
432
433
434/**
435 * Add a new unnamed constant to the parameter list.
436 * \param paramList - the parameter list
437 * \param values - four float values
438 * \return index of the new parameter.
439 */
440GLint
441_mesa_add_named_constant(struct program_parameter_list *paramList,
442 const char *name, const GLfloat values[4])
443{
444 return add_parameter(paramList, name, values, CONSTANT);
445}
446
447
448/**
449 * Add a new unnamed constant to the parameter list.
450 * \param paramList - the parameter list
451 * \param values - four float values
452 * \return index of the new parameter.
453 */
454GLint
455_mesa_add_unnamed_constant(struct program_parameter_list *paramList,
456 const GLfloat values[4])
457{
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000458 return add_parameter(paramList, NULL, values, CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000459}
460
461
462/**
463 * Add a new state reference to the parameter list.
464 * \param paramList - the parameter list
465 * \param state - an array of 6 state tokens
466 *
467 * \return index of the new parameter.
468 */
469GLint
470_mesa_add_state_reference(struct program_parameter_list *paramList,
471 GLint *stateTokens)
472{
473 /* XXX Should we parse <stateString> here and produce the parameter's
474 * list of STATE_* tokens here, or in the parser?
475 */
476 GLint a, idx;
477
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000478 idx = add_parameter(paramList, NULL, NULL, STATE);
Michal Krol2861e732004-03-29 11:09:34 +0000479
480 for (a=0; a<6; a++)
481 paramList->Parameters[idx].StateIndexes[a] = (enum state_index) stateTokens[a];
482
483 return idx;
484}
485
486
487/**
488 * Lookup a parameter value by name in the given parameter list.
489 * \return pointer to the float[4] values.
490 */
491GLfloat *
492_mesa_lookup_parameter_value(struct program_parameter_list *paramList,
493 GLsizei nameLen, const char *name)
494{
495 GLuint i;
496
497 if (!paramList)
498 return NULL;
499
500 if (nameLen == -1) {
501 /* name is null-terminated */
502 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000503 if (paramList->Parameters[i].Name &&
504 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000505 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000506 }
507 }
508 else {
509 /* name is not null-terminated, use nameLen */
510 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000511 if (paramList->Parameters[i].Name &&
512 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000513 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000514 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000515 }
516 }
517 return NULL;
518}
519
520
521/**
522 * Lookup a parameter index by name in the given parameter list.
523 * \return index of parameter in the list.
524 */
525GLint
526_mesa_lookup_parameter_index(struct program_parameter_list *paramList,
527 GLsizei nameLen, const char *name)
528{
529 GLint i;
530
531 if (!paramList)
532 return -1;
533
534 if (nameLen == -1) {
535 /* name is null-terminated */
536 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000537 if (paramList->Parameters[i].Name &&
538 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Michal Krol2861e732004-03-29 11:09:34 +0000539 return i;
540 }
541 }
542 else {
543 /* name is not null-terminated, use nameLen */
544 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000545 if (paramList->Parameters[i].Name &&
546 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000547 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
548 return i;
549 }
550 }
551 return -1;
552}
553
554
555/**
556 * Use the list of tokens in the state[] array to find global GL state
557 * and return it in <value>. Usually, four values are returned in <value>
558 * but matrix queries may return as many as 16 values.
559 * This function is used for ARB vertex/fragment programs.
560 * The program parser will produce the state[] values.
561 */
562static void
563_mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
564 GLfloat *value)
565{
566 switch (state[0]) {
567 case STATE_MATERIAL:
568 {
569 /* state[1] is either 0=front or 1=back side */
570 const GLuint face = (GLuint) state[1];
571 /* state[2] is the material attribute */
572 switch (state[2]) {
573 case STATE_AMBIENT:
574 if (face == 0)
575 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
576 else
577 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
578 return;
579 case STATE_DIFFUSE:
580 if (face == 0)
581 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
582 else
583 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
584 return;
585 case STATE_SPECULAR:
586 if (face == 0)
587 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
588 else
589 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
590 return;
591 case STATE_EMISSION:
592 if (face == 0)
593 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
594 else
595 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
596 return;
597 case STATE_SHININESS:
598 if (face == 0)
599 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
600 else
601 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
602 value[1] = 0.0F;
603 value[2] = 0.0F;
604 value[3] = 1.0F;
605 return;
606 default:
607 _mesa_problem(ctx, "Invalid material state in fetch_state");
608 return;
609 }
Alan Hourihane22ae6332004-12-02 13:29:40 +0000610 }
Michal Krol2861e732004-03-29 11:09:34 +0000611 case STATE_LIGHT:
612 {
613 /* state[1] is the light number */
614 const GLuint ln = (GLuint) state[1];
615 /* state[2] is the light attribute */
616 switch (state[2]) {
617 case STATE_AMBIENT:
618 COPY_4V(value, ctx->Light.Light[ln].Ambient);
619 return;
620 case STATE_DIFFUSE:
621 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
622 return;
623 case STATE_SPECULAR:
624 COPY_4V(value, ctx->Light.Light[ln].Specular);
625 return;
626 case STATE_POSITION:
627 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
628 return;
629 case STATE_ATTENUATION:
630 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
631 value[1] = ctx->Light.Light[ln].LinearAttenuation;
632 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
633 value[3] = ctx->Light.Light[ln].SpotExponent;
634 return;
635 case STATE_SPOT_DIRECTION:
Brian Paul52bf0052005-04-20 23:47:03 +0000636 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
637 value[3] = ctx->Light.Light[ln]._CosCutoff;
Michal Krol2861e732004-03-29 11:09:34 +0000638 return;
639 case STATE_HALF:
640 {
641 GLfloat eye_z[] = {0, 0, 1};
642
643 /* Compute infinite half angle vector:
644 * half-vector = light_position + (0, 0, 1)
645 * and then normalize. w = 0
Keith Whitwell7c26b612005-04-21 14:46:57 +0000646 *
647 * light.EyePosition.w should be 0 for infinite lights.
Michal Krol2861e732004-03-29 11:09:34 +0000648 */
Keith Whitwell7c26b612005-04-21 14:46:57 +0000649 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
650 NORMALIZE_3FV(value);
651 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000652 }
653 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000654 case STATE_POSITION_NORMALIZED:
655 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
656 NORMALIZE_3FV( value );
657 return;
Michal Krol2861e732004-03-29 11:09:34 +0000658 default:
659 _mesa_problem(ctx, "Invalid light state in fetch_state");
660 return;
661 }
662 }
Michal Krol2861e732004-03-29 11:09:34 +0000663 case STATE_LIGHTMODEL_AMBIENT:
664 COPY_4V(value, ctx->Light.Model.Ambient);
665 return;
666 case STATE_LIGHTMODEL_SCENECOLOR:
667 if (state[1] == 0) {
668 /* front */
669 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000670 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000671 value[i] = ctx->Light.Model.Ambient[i]
672 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
673 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
674 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000675 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000676 }
677 else {
678 /* back */
679 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000680 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000681 value[i] = ctx->Light.Model.Ambient[i]
682 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
683 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
684 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000685 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000686 }
687 return;
688 case STATE_LIGHTPROD:
689 {
690 const GLuint ln = (GLuint) state[1];
691 const GLuint face = (GLuint) state[2];
692 GLint i;
693 ASSERT(face == 0 || face == 1);
694 switch (state[3]) {
695 case STATE_AMBIENT:
696 for (i = 0; i < 3; i++) {
697 value[i] = ctx->Light.Light[ln].Ambient[i] *
698 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
699 }
700 /* [3] = material alpha */
701 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
702 return;
703 case STATE_DIFFUSE:
704 for (i = 0; i < 3; i++) {
705 value[i] = ctx->Light.Light[ln].Diffuse[i] *
706 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
707 }
708 /* [3] = material alpha */
709 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
710 return;
711 case STATE_SPECULAR:
712 for (i = 0; i < 3; i++) {
713 value[i] = ctx->Light.Light[ln].Specular[i] *
714 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
715 }
716 /* [3] = material alpha */
717 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
718 return;
719 default:
720 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
721 return;
722 }
723 }
Michal Krol2861e732004-03-29 11:09:34 +0000724 case STATE_TEXGEN:
725 {
726 /* state[1] is the texture unit */
727 const GLuint unit = (GLuint) state[1];
728 /* state[2] is the texgen attribute */
729 switch (state[2]) {
730 case STATE_TEXGEN_EYE_S:
731 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
732 return;
733 case STATE_TEXGEN_EYE_T:
734 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
735 return;
736 case STATE_TEXGEN_EYE_R:
737 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
738 return;
739 case STATE_TEXGEN_EYE_Q:
740 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
741 return;
742 case STATE_TEXGEN_OBJECT_S:
743 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
744 return;
745 case STATE_TEXGEN_OBJECT_T:
746 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
747 return;
748 case STATE_TEXGEN_OBJECT_R:
749 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
750 return;
751 case STATE_TEXGEN_OBJECT_Q:
752 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
753 return;
754 default:
755 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
756 return;
757 }
758 }
Michal Krol2861e732004-03-29 11:09:34 +0000759 case STATE_TEXENV_COLOR:
760 {
761 /* state[1] is the texture unit */
762 const GLuint unit = (GLuint) state[1];
763 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
764 }
765 return;
766 case STATE_FOG_COLOR:
767 COPY_4V(value, ctx->Fog.Color);
768 return;
769 case STATE_FOG_PARAMS:
770 value[0] = ctx->Fog.Density;
771 value[1] = ctx->Fog.Start;
772 value[2] = ctx->Fog.End;
773 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
774 return;
775 case STATE_CLIPPLANE:
776 {
777 const GLuint plane = (GLuint) state[1];
778 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
779 }
780 return;
781 case STATE_POINT_SIZE:
782 value[0] = ctx->Point.Size;
783 value[1] = ctx->Point.MinSize;
784 value[2] = ctx->Point.MaxSize;
785 value[3] = ctx->Point.Threshold;
786 return;
787 case STATE_POINT_ATTENUATION:
788 value[0] = ctx->Point.Params[0];
789 value[1] = ctx->Point.Params[1];
790 value[2] = ctx->Point.Params[2];
791 value[3] = 1.0F;
792 return;
793 case STATE_MATRIX:
794 {
795 /* state[1] = modelview, projection, texture, etc. */
796 /* state[2] = which texture matrix or program matrix */
797 /* state[3] = first column to fetch */
798 /* state[4] = last column to fetch */
799 /* state[5] = transpose, inverse or invtrans */
800
801 const GLmatrix *matrix;
802 const enum state_index mat = state[1];
803 const GLuint index = (GLuint) state[2];
804 const GLuint first = (GLuint) state[3];
805 const GLuint last = (GLuint) state[4];
806 const enum state_index modifier = state[5];
807 const GLfloat *m;
808 GLuint row, i;
809 if (mat == STATE_MODELVIEW) {
810 matrix = ctx->ModelviewMatrixStack.Top;
811 }
812 else if (mat == STATE_PROJECTION) {
813 matrix = ctx->ProjectionMatrixStack.Top;
814 }
815 else if (mat == STATE_MVP) {
816 matrix = &ctx->_ModelProjectMatrix;
817 }
818 else if (mat == STATE_TEXTURE) {
819 matrix = ctx->TextureMatrixStack[index].Top;
820 }
821 else if (mat == STATE_PROGRAM) {
822 matrix = ctx->ProgramMatrixStack[index].Top;
823 }
824 else {
825 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
826 return;
827 }
828 if (modifier == STATE_MATRIX_INVERSE ||
829 modifier == STATE_MATRIX_INVTRANS) {
Keith Whitwell78803b22005-04-15 12:57:23 +0000830 /* Be sure inverse is up to date:
831 */
Jouk Jansen49b1d952005-04-18 13:05:24 +0000832 _math_matrix_analyse( (GLmatrix*) matrix );
Michal Krol2861e732004-03-29 11:09:34 +0000833 m = matrix->inv;
834 }
835 else {
836 m = matrix->m;
837 }
838 if (modifier == STATE_MATRIX_TRANSPOSE ||
839 modifier == STATE_MATRIX_INVTRANS) {
840 for (i = 0, row = first; row <= last; row++) {
841 value[i++] = m[row * 4 + 0];
842 value[i++] = m[row * 4 + 1];
843 value[i++] = m[row * 4 + 2];
844 value[i++] = m[row * 4 + 3];
845 }
846 }
847 else {
848 for (i = 0, row = first; row <= last; row++) {
849 value[i++] = m[row + 0];
850 value[i++] = m[row + 4];
851 value[i++] = m[row + 8];
852 value[i++] = m[row + 12];
853 }
854 }
855 }
856 return;
857 case STATE_DEPTH_RANGE:
Brian Paul824fdf02004-06-29 00:00:06 +0000858 value[0] = ctx->Viewport.Near; /* near */
859 value[1] = ctx->Viewport.Far; /* far */
860 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
861 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000862 return;
863 case STATE_FRAGMENT_PROGRAM:
864 {
865 /* state[1] = {STATE_ENV, STATE_LOCAL} */
866 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000867 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000868 switch (state[1]) {
869 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000870 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000871 break;
Michal Krol2861e732004-03-29 11:09:34 +0000872 case STATE_LOCAL:
873 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000874 break;
Michal Krol2861e732004-03-29 11:09:34 +0000875 default:
876 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
877 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000878 }
879 }
Michal Krol2861e732004-03-29 11:09:34 +0000880 return;
881
882 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000883 {
Michal Krol2861e732004-03-29 11:09:34 +0000884 /* state[1] = {STATE_ENV, STATE_LOCAL} */
885 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000886 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000887 switch (state[1]) {
888 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000889 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000890 break;
Michal Krol2861e732004-03-29 11:09:34 +0000891 case STATE_LOCAL:
892 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000893 break;
Michal Krol2861e732004-03-29 11:09:34 +0000894 default:
895 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
896 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000897 }
898 }
Michal Krol2861e732004-03-29 11:09:34 +0000899 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000900
901 case STATE_INTERNAL:
902 {
903 switch (state[1]) {
904 case STATE_NORMAL_SCALE:
905 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
906 break;
907 default:
908 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
909 return;
910 }
911 }
912 return;
913
Michal Krol2861e732004-03-29 11:09:34 +0000914 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000915 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000916 return;
917 }
918}
919
920
921/**
922 * Loop over all the parameters in a parameter list. If the parameter
923 * is a GL state reference, look up the current value of that state
924 * variable and put it into the parameter's Value[4] array.
925 * This would be called at glBegin time when using a fragment program.
926 */
927void
928_mesa_load_state_parameters(GLcontext *ctx,
929 struct program_parameter_list *paramList)
930{
931 GLuint i;
932
933 if (!paramList)
934 return;
935
936 for (i = 0; i < paramList->NumParameters; i++) {
937 if (paramList->Parameters[i].Type == STATE) {
Keith Whitwell7c26b612005-04-21 14:46:57 +0000938 _mesa_fetch_state(ctx,
939 paramList->Parameters[i].StateIndexes,
940 paramList->ParameterValues[i]);
Michal Krol2861e732004-03-29 11:09:34 +0000941 }
942 }
943}
944
945
946
947/**********************************************************************/
948/* API functions */
949/**********************************************************************/
950
951
952/**
953 * Bind a program (make it current)
954 * \note Called from the GL API dispatcher by both glBindProgramNV
955 * and glBindProgramARB.
956 */
957void GLAPIENTRY
958_mesa_BindProgram(GLenum target, GLuint id)
959{
960 struct program *prog;
961 GET_CURRENT_CONTEXT(ctx);
962 ASSERT_OUTSIDE_BEGIN_END(ctx);
963
964 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
965
966 if ((target == GL_VERTEX_PROGRAM_NV
967 && ctx->Extensions.NV_vertex_program) ||
968 (target == GL_VERTEX_PROGRAM_ARB
969 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000970 /*** Vertex program binding ***/
971 struct vertex_program *curProg = ctx->VertexProgram.Current;
972 if (curProg->Base.Id == id) {
973 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +0000974 return;
Brian Paul765f1a12004-09-14 22:28:27 +0000975 }
976 if (curProg->Base.Id != 0) {
977 /* decrement refcount on previously bound vertex program */
978 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000979 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +0000980 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +0000981 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +0000982 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +0000983 }
984 }
985 }
986 else if ((target == GL_FRAGMENT_PROGRAM_NV
987 && ctx->Extensions.NV_fragment_program) ||
988 (target == GL_FRAGMENT_PROGRAM_ARB
989 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000990 /*** Fragment program binding ***/
991 struct fragment_program *curProg = ctx->FragmentProgram.Current;
992 if (curProg->Base.Id == id) {
993 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +0000994 return;
Brian Paul765f1a12004-09-14 22:28:27 +0000995 }
996 if (curProg->Base.Id != 0) {
997 /* decrement refcount on previously bound fragment program */
998 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000999 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001000 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001001 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001002 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001003 }
1004 }
1005 }
1006 else {
1007 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1008 return;
1009 }
1010
1011 /* NOTE: binding to a non-existant program is not an error.
1012 * That's supposed to be caught in glBegin.
1013 */
1014 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001015 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001016 prog = NULL;
1017 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1018 prog = ctx->Shared->DefaultVertexProgram;
1019 else
1020 prog = ctx->Shared->DefaultFragmentProgram;
1021 }
1022 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001023 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001024 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001025 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001026 /* allocate a new program now */
1027 prog = ctx->Driver.NewProgram(ctx, target, id);
1028 if (!prog) {
1029 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1030 return;
1031 }
Michal Krol2861e732004-03-29 11:09:34 +00001032 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1033 }
Brian Paul765f1a12004-09-14 22:28:27 +00001034 else if (prog->Target != target) {
1035 _mesa_error(ctx, GL_INVALID_OPERATION,
1036 "glBindProgramNV/ARB(target mismatch)");
1037 return;
1038 }
Michal Krol2861e732004-03-29 11:09:34 +00001039 }
1040
1041 /* bind now */
1042 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1043 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1044 }
1045 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1046 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1047 }
1048
Brian Paul765f1a12004-09-14 22:28:27 +00001049 /* Never null pointers */
1050 ASSERT(ctx->VertexProgram.Current);
1051 ASSERT(ctx->FragmentProgram.Current);
1052
Michal Krol2861e732004-03-29 11:09:34 +00001053 if (prog)
1054 prog->RefCount++;
1055
1056 if (ctx->Driver.BindProgram)
1057 ctx->Driver.BindProgram(ctx, target, prog);
1058}
1059
1060
1061/**
1062 * Delete a list of programs.
1063 * \note Not compiled into display lists.
1064 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1065 */
1066void GLAPIENTRY
1067_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1068{
1069 GLint i;
1070 GET_CURRENT_CONTEXT(ctx);
1071 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1072
1073 if (n < 0) {
1074 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1075 return;
1076 }
1077
1078 for (i = 0; i < n; i++) {
1079 if (ids[i] != 0) {
1080 struct program *prog = (struct program *)
1081 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001082 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001083 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1084 }
1085 else if (prog) {
1086 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001087 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1088 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1089 if (ctx->VertexProgram.Current &&
1090 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1091 /* unbind this currently bound program */
1092 _mesa_BindProgram(prog->Target, 0);
1093 }
1094 }
1095 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1096 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1097 if (ctx->FragmentProgram.Current &&
1098 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1099 /* unbind this currently bound program */
1100 _mesa_BindProgram(prog->Target, 0);
1101 }
1102 }
1103 else {
1104 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1105 return;
1106 }
Brian Paulea2943e2005-01-20 04:02:02 +00001107 /* The ID is immediately available for re-use now */
1108 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1109 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001110 if (prog->RefCount <= 0) {
1111 ctx->Driver.DeleteProgram(ctx, prog);
1112 }
1113 }
Michal Krol2861e732004-03-29 11:09:34 +00001114 }
1115 }
1116}
1117
1118
1119/**
1120 * Generate a list of new program identifiers.
1121 * \note Not compiled into display lists.
1122 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1123 */
1124void GLAPIENTRY
1125_mesa_GenPrograms(GLsizei n, GLuint *ids)
1126{
1127 GLuint first;
1128 GLuint i;
1129 GET_CURRENT_CONTEXT(ctx);
1130 ASSERT_OUTSIDE_BEGIN_END(ctx);
1131
1132 if (n < 0) {
1133 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1134 return;
1135 }
1136
1137 if (!ids)
1138 return;
1139
1140 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1141
Brian Paul765f1a12004-09-14 22:28:27 +00001142 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001143 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001144 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001145 }
1146
1147 /* Return the program names */
1148 for (i = 0; i < (GLuint) n; i++) {
1149 ids[i] = first + i;
1150 }
1151}
1152
1153
1154/**
Brian Paul765f1a12004-09-14 22:28:27 +00001155 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001156 * \note Not compiled into display lists.
1157 * \note Called from both glIsProgramNV and glIsProgramARB.
1158 * \param id is the program identifier
1159 * \return GL_TRUE if id is a program, else GL_FALSE.
1160 */
1161GLboolean GLAPIENTRY
1162_mesa_IsProgram(GLuint id)
1163{
1164 GET_CURRENT_CONTEXT(ctx);
1165 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1166
1167 if (id == 0)
1168 return GL_FALSE;
1169
1170 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1171 return GL_TRUE;
1172 else
1173 return GL_FALSE;
1174}
1175
1176
1177
1178/**********************************************************************/
1179/* GL_MESA_program_debug extension */
1180/**********************************************************************/
1181
1182
1183/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001184GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001185glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1186 GLvoid *data)
1187{
1188 _mesa_ProgramCallbackMESA(target, callback, data);
1189}
1190
1191
1192void
1193_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1194 GLvoid *data)
1195{
1196 GET_CURRENT_CONTEXT(ctx);
1197
1198 switch (target) {
1199 case GL_FRAGMENT_PROGRAM_ARB:
1200 if (!ctx->Extensions.ARB_fragment_program) {
1201 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1202 return;
1203 }
1204 ctx->FragmentProgram.Callback = callback;
1205 ctx->FragmentProgram.CallbackData = data;
1206 break;
1207 case GL_FRAGMENT_PROGRAM_NV:
1208 if (!ctx->Extensions.NV_fragment_program) {
1209 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1210 return;
1211 }
1212 ctx->FragmentProgram.Callback = callback;
1213 ctx->FragmentProgram.CallbackData = data;
1214 break;
1215 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1216 if (!ctx->Extensions.ARB_vertex_program &&
1217 !ctx->Extensions.NV_vertex_program) {
1218 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1219 return;
1220 }
1221 ctx->VertexProgram.Callback = callback;
1222 ctx->VertexProgram.CallbackData = data;
1223 break;
1224 default:
1225 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1226 return;
1227 }
1228}
1229
1230
1231/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001232GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001233glGetProgramRegisterfvMESA(GLenum target,
1234 GLsizei len, const GLubyte *registerName,
1235 GLfloat *v)
1236{
1237 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1238}
1239
1240
1241void
1242_mesa_GetProgramRegisterfvMESA(GLenum target,
1243 GLsizei len, const GLubyte *registerName,
1244 GLfloat *v)
1245{
1246 char reg[1000];
1247 GET_CURRENT_CONTEXT(ctx);
1248
1249 /* We _should_ be inside glBegin/glEnd */
1250#if 0
1251 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1252 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1253 return;
1254 }
1255#endif
1256
1257 /* make null-terminated copy of registerName */
1258 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1259 _mesa_memcpy(reg, registerName, len);
1260 reg[len] = 0;
1261
1262 switch (target) {
1263 case GL_VERTEX_PROGRAM_NV:
1264 if (!ctx->Extensions.ARB_vertex_program &&
1265 !ctx->Extensions.NV_vertex_program) {
1266 _mesa_error(ctx, GL_INVALID_ENUM,
1267 "glGetProgramRegisterfvMESA(target)");
1268 return;
1269 }
Brian Paul6d460af2004-04-23 14:16:46 +00001270 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001271 _mesa_error(ctx, GL_INVALID_OPERATION,
1272 "glGetProgramRegisterfvMESA");
1273 return;
1274 }
1275 /* GL_NV_vertex_program */
1276 if (reg[0] == 'R') {
1277 /* Temp register */
1278 GLint i = _mesa_atoi(reg + 1);
1279 if (i >= (GLint)ctx->Const.MaxVertexProgramTemps) {
1280 _mesa_error(ctx, GL_INVALID_VALUE,
1281 "glGetProgramRegisterfvMESA(registerName)");
1282 return;
1283 }
1284 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1285 }
1286 else if (reg[0] == 'v' && reg[1] == '[') {
1287 /* Vertex Input attribute */
1288 GLuint i;
1289 for (i = 0; i < ctx->Const.MaxVertexProgramAttribs; i++) {
1290 const char *name = _mesa_nv_vertex_input_register_name(i);
1291 char number[10];
1292 sprintf(number, "%d", i);
1293 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1294 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1295 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1296 return;
1297 }
1298 }
1299 _mesa_error(ctx, GL_INVALID_VALUE,
1300 "glGetProgramRegisterfvMESA(registerName)");
1301 return;
1302 }
1303 else if (reg[0] == 'o' && reg[1] == '[') {
1304 /* Vertex output attribute */
1305 }
1306 /* GL_ARB_vertex_program */
1307 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1308
1309 }
1310 else {
1311 _mesa_error(ctx, GL_INVALID_VALUE,
1312 "glGetProgramRegisterfvMESA(registerName)");
1313 return;
1314 }
1315 break;
1316 case GL_FRAGMENT_PROGRAM_ARB:
1317 if (!ctx->Extensions.ARB_fragment_program) {
1318 _mesa_error(ctx, GL_INVALID_ENUM,
1319 "glGetProgramRegisterfvMESA(target)");
1320 return;
1321 }
Brian Paul6d460af2004-04-23 14:16:46 +00001322 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001323 _mesa_error(ctx, GL_INVALID_OPERATION,
1324 "glGetProgramRegisterfvMESA");
1325 return;
1326 }
1327 /* XXX to do */
1328 break;
1329 case GL_FRAGMENT_PROGRAM_NV:
1330 if (!ctx->Extensions.NV_fragment_program) {
1331 _mesa_error(ctx, GL_INVALID_ENUM,
1332 "glGetProgramRegisterfvMESA(target)");
1333 return;
1334 }
Brian Paul6d460af2004-04-23 14:16:46 +00001335 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001336 _mesa_error(ctx, GL_INVALID_OPERATION,
1337 "glGetProgramRegisterfvMESA");
1338 return;
1339 }
1340 if (reg[0] == 'R') {
1341 /* Temp register */
1342 GLint i = _mesa_atoi(reg + 1);
1343 if (i >= (GLint)ctx->Const.MaxFragmentProgramTemps) {
1344 _mesa_error(ctx, GL_INVALID_VALUE,
1345 "glGetProgramRegisterfvMESA(registerName)");
1346 return;
1347 }
1348 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1349 }
1350 else if (reg[0] == 'f' && reg[1] == '[') {
1351 /* Fragment input attribute */
1352 GLuint i;
1353 for (i = 0; i < ctx->Const.MaxFragmentProgramAttribs; i++) {
1354 const char *name = _mesa_nv_fragment_input_register_name(i);
1355 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1356 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1357 return;
1358 }
1359 }
1360 _mesa_error(ctx, GL_INVALID_VALUE,
1361 "glGetProgramRegisterfvMESA(registerName)");
1362 return;
1363 }
1364 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1365 /* Fragment output color */
1366 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR]);
1367 }
1368 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1369 /* Fragment output color */
1370 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLH]);
1371 }
1372 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1373 /* Fragment output depth */
1374 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR]);
1375 }
1376 else {
1377 /* try user-defined identifiers */
1378 const GLfloat *value = _mesa_lookup_parameter_value(
1379 ctx->FragmentProgram.Current->Parameters, -1, reg);
1380 if (value) {
1381 COPY_4V(v, value);
1382 }
1383 else {
1384 _mesa_error(ctx, GL_INVALID_VALUE,
1385 "glGetProgramRegisterfvMESA(registerName)");
1386 return;
1387 }
1388 }
1389 break;
1390 default:
1391 _mesa_error(ctx, GL_INVALID_ENUM,
1392 "glGetProgramRegisterfvMESA(target)");
1393 return;
1394 }
1395
1396}