Add more error checking.
[mesa.git] / src / mesa / shader / program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.2
4 *
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"
42 #include "nvvertprog.h"
43
44
45 /**********************************************************************/
46 /* Utility functions */
47 /**********************************************************************/
48
49
50 /* A pointer to this dummy program is put into the hash table when
51 * glGenPrograms is called.
52 */
53 struct program _mesa_DummyProgram;
54
55
56 /**
57 * Init context's vertex/fragment program state
58 */
59 void
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;
71 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
86
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
93 }
94
95
96 /**
97 * Free a context's vertex/fragment program state
98 */
99 void
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
116 #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
123 _mesa_free((void *) ctx->Program.ErrorString);
124 }
125
126
127
128
129 /**
130 * Set the vertex/fragment program error state (position and error string).
131 * This is generally called from within the parsers.
132 */
133 void
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 */
154 const 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
187 /**
188 * Initialize a new vertex/fragment program object.
189 */
190 static struct program *
191 _mesa_init_program_struct( GLcontext *ctx, struct program *prog,
192 GLenum target, GLuint id)
193 {
194 (void) ctx;
195 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
205
206 /**
207 * Initialize a new fragment program object.
208 */
209 struct program *
210 _mesa_init_fragment_program( GLcontext *ctx, struct fragment_program *prog,
211 GLenum target, GLuint id)
212 {
213 if (prog)
214 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
215 else
216 return NULL;
217 }
218
219
220 /**
221 * Initialize a new vertex program object.
222 */
223 struct program *
224 _mesa_init_vertex_program( GLcontext *ctx, struct vertex_program *prog,
225 GLenum target, GLuint id)
226 {
227 if (prog)
228 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
229 else
230 return NULL;
231 }
232
233 /**
234 * Initialize a new ATI fragment shader object.
235 */
236 struct program *
237 _mesa_init_ati_fragment_shader( GLcontext *ctx,
238 struct ati_fragment_shader *prog,
239 GLenum target, GLuint id )
240 {
241 if (prog)
242 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
243 else
244 return NULL;
245 }
246
247
248
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 */
261 struct 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 );
268 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 );
272 case GL_FRAGMENT_SHADER_ATI:
273 return _mesa_init_ati_fragment_shader( ctx, CALLOC_STRUCT(ati_fragment_shader),
274 target, id );
275
276 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 */
289 void
290 _mesa_delete_program(GLcontext *ctx, struct program *prog)
291 {
292 (void) ctx;
293 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;
300 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 }
306 _mesa_free(vprog->Instructions);
307 }
308 if (vprog->Parameters)
309 _mesa_free_parameter_list(vprog->Parameters);
310 }
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;
314 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 }
320 _mesa_free(fprog->Instructions);
321 }
322 if (fprog->Parameters)
323 _mesa_free_parameter_list(fprog->Parameters);
324 }
325 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
331 _mesa_free(prog);
332 }
333
334
335
336 /**********************************************************************/
337 /* Program parameter functions */
338 /**********************************************************************/
339
340 struct 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 */
351 void
352 _mesa_free_parameter_list(struct program_parameter_list *paramList)
353 {
354 _mesa_free_parameters(paramList);
355 _mesa_free(paramList);
356 }
357
358
359 /**
360 * Free all the parameters in the given list, but don't free the
361 * paramList structure itself.
362 */
363 void
364 _mesa_free_parameters(struct program_parameter_list *paramList)
365 {
366 GLuint i;
367 for (i = 0; i < paramList->NumParameters; i++) {
368 _mesa_free((void *) paramList->Parameters[i].Name);
369 }
370 _mesa_free(paramList->Parameters);
371 _mesa_free(paramList->ParameterValues);
372 paramList->NumParameters = 0;
373 paramList->Parameters = NULL;
374 }
375
376
377 /**
378 * Helper function used by the functions below.
379 */
380 static GLint
381 add_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
387 paramList->Parameters = (struct program_parameter *)
388 _mesa_realloc(paramList->Parameters,
389 n * sizeof(struct program_parameter),
390 (n + 1) * sizeof(struct program_parameter));
391 paramList->ParameterValues = (GLfloat (*)[4])
392 _mesa_realloc(paramList->ParameterValues,
393 n * 4 * sizeof(GLfloat),
394 (n + 1) * 4 * sizeof(GLfloat));
395
396 if (!paramList->Parameters ||
397 !paramList->ParameterValues) {
398 /* out of memory */
399 paramList->NumParameters = 0;
400 return -1;
401 }
402 else {
403 paramList->NumParameters = n + 1;
404 paramList->Parameters[n].Name = _mesa_strdup(name);
405 paramList->Parameters[n].Type = type;
406 if (values)
407 COPY_4V(paramList->ParameterValues[n], values);
408 return (GLint) n;
409 }
410 }
411
412
413 /**
414 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
415 * \return index of the new entry in the parameter list
416 */
417 GLint
418 _mesa_add_named_parameter(struct program_parameter_list *paramList,
419 const char *name, const GLfloat values[4])
420 {
421 return add_parameter(paramList, name, values, NAMED_PARAMETER);
422 }
423
424
425 /**
426 * Add a new unnamed constant to the parameter list.
427 * \param paramList - the parameter list
428 * \param values - four float values
429 * \return index of the new parameter.
430 */
431 GLint
432 _mesa_add_named_constant(struct program_parameter_list *paramList,
433 const char *name, const GLfloat values[4])
434 {
435 return add_parameter(paramList, name, values, CONSTANT);
436 }
437
438
439 /**
440 * Add a new unnamed constant to the parameter list.
441 * \param paramList - the parameter list
442 * \param values - four float values
443 * \return index of the new parameter.
444 */
445 GLint
446 _mesa_add_unnamed_constant(struct program_parameter_list *paramList,
447 const GLfloat values[4])
448 {
449 /* generate a new dummy name */
450 static GLuint n = 0;
451 char name[20];
452 _mesa_sprintf(name, "constant%d", n);
453 n++;
454 /* store it */
455 return add_parameter(paramList, name, values, CONSTANT);
456 }
457
458
459 /**
460 * Add a new state reference to the parameter list.
461 * \param paramList - the parameter list
462 * \param state - an array of 6 state tokens
463 *
464 * \return index of the new parameter.
465 */
466 GLint
467 _mesa_add_state_reference(struct program_parameter_list *paramList,
468 GLint *stateTokens)
469 {
470 /* XXX Should we parse <stateString> here and produce the parameter's
471 * list of STATE_* tokens here, or in the parser?
472 */
473 GLint a, idx;
474
475 idx = add_parameter(paramList, "Some State", NULL, STATE);
476
477 for (a=0; a<6; a++)
478 paramList->Parameters[idx].StateIndexes[a] = (enum state_index) stateTokens[a];
479
480 return idx;
481 }
482
483
484 /**
485 * Lookup a parameter value by name in the given parameter list.
486 * \return pointer to the float[4] values.
487 */
488 GLfloat *
489 _mesa_lookup_parameter_value(struct program_parameter_list *paramList,
490 GLsizei nameLen, const char *name)
491 {
492 GLuint i;
493
494 if (!paramList)
495 return NULL;
496
497 if (nameLen == -1) {
498 /* name is null-terminated */
499 for (i = 0; i < paramList->NumParameters; i++) {
500 if (_mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
501 return paramList->ParameterValues[i];
502 }
503 }
504 else {
505 /* name is not null-terminated, use nameLen */
506 for (i = 0; i < paramList->NumParameters; i++) {
507 if (_mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
508 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
509 return paramList->ParameterValues[i];
510 }
511 }
512 return NULL;
513 }
514
515
516 /**
517 * Lookup a parameter index by name in the given parameter list.
518 * \return index of parameter in the list.
519 */
520 GLint
521 _mesa_lookup_parameter_index(struct program_parameter_list *paramList,
522 GLsizei nameLen, const char *name)
523 {
524 GLint i;
525
526 if (!paramList)
527 return -1;
528
529 if (nameLen == -1) {
530 /* name is null-terminated */
531 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
532 if (_mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
533 return i;
534 }
535 }
536 else {
537 /* name is not null-terminated, use nameLen */
538 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
539 if (_mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
540 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
541 return i;
542 }
543 }
544 return -1;
545 }
546
547
548 /**
549 * Use the list of tokens in the state[] array to find global GL state
550 * and return it in <value>. Usually, four values are returned in <value>
551 * but matrix queries may return as many as 16 values.
552 * This function is used for ARB vertex/fragment programs.
553 * The program parser will produce the state[] values.
554 */
555 static void
556 _mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
557 GLfloat *value)
558 {
559 switch (state[0]) {
560 case STATE_MATERIAL:
561 {
562 /* state[1] is either 0=front or 1=back side */
563 const GLuint face = (GLuint) state[1];
564 /* state[2] is the material attribute */
565 switch (state[2]) {
566 case STATE_AMBIENT:
567 if (face == 0)
568 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
569 else
570 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
571 return;
572 case STATE_DIFFUSE:
573 if (face == 0)
574 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
575 else
576 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
577 return;
578 case STATE_SPECULAR:
579 if (face == 0)
580 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
581 else
582 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
583 return;
584 case STATE_EMISSION:
585 if (face == 0)
586 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
587 else
588 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
589 return;
590 case STATE_SHININESS:
591 if (face == 0)
592 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
593 else
594 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
595 value[1] = 0.0F;
596 value[2] = 0.0F;
597 value[3] = 1.0F;
598 return;
599 default:
600 _mesa_problem(ctx, "Invalid material state in fetch_state");
601 return;
602 }
603 }
604 case STATE_LIGHT:
605 {
606 /* state[1] is the light number */
607 const GLuint ln = (GLuint) state[1];
608 /* state[2] is the light attribute */
609 switch (state[2]) {
610 case STATE_AMBIENT:
611 COPY_4V(value, ctx->Light.Light[ln].Ambient);
612 return;
613 case STATE_DIFFUSE:
614 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
615 return;
616 case STATE_SPECULAR:
617 COPY_4V(value, ctx->Light.Light[ln].Specular);
618 return;
619 case STATE_POSITION:
620 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
621 return;
622 case STATE_ATTENUATION:
623 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
624 value[1] = ctx->Light.Light[ln].LinearAttenuation;
625 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
626 value[3] = ctx->Light.Light[ln].SpotExponent;
627 return;
628 case STATE_SPOT_DIRECTION:
629 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
630 value[3] = ctx->Light.Light[ln]._CosCutoff;
631 return;
632 case STATE_HALF:
633 {
634 GLfloat eye_z[] = {0, 0, 1};
635
636 /* Compute infinite half angle vector:
637 * half-vector = light_position + (0, 0, 1)
638 * and then normalize. w = 0
639 *
640 * light.EyePosition.w should be 0 for infinite lights.
641 */
642 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
643 NORMALIZE_3FV(value);
644 value[3] = 0;
645 }
646 return;
647 case STATE_POSITION_NORMALIZED:
648 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
649 NORMALIZE_3FV( value );
650 return;
651 default:
652 _mesa_problem(ctx, "Invalid light state in fetch_state");
653 return;
654 }
655 }
656 case STATE_LIGHTMODEL_AMBIENT:
657 COPY_4V(value, ctx->Light.Model.Ambient);
658 return;
659 case STATE_LIGHTMODEL_SCENECOLOR:
660 if (state[1] == 0) {
661 /* front */
662 GLint i;
663 for (i = 0; i < 3; i++) {
664 value[i] = ctx->Light.Model.Ambient[i]
665 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
666 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
667 }
668 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
669 }
670 else {
671 /* back */
672 GLint i;
673 for (i = 0; i < 3; i++) {
674 value[i] = ctx->Light.Model.Ambient[i]
675 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
676 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
677 }
678 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
679 }
680 return;
681 case STATE_LIGHTPROD:
682 {
683 const GLuint ln = (GLuint) state[1];
684 const GLuint face = (GLuint) state[2];
685 GLint i;
686 ASSERT(face == 0 || face == 1);
687 switch (state[3]) {
688 case STATE_AMBIENT:
689 for (i = 0; i < 3; i++) {
690 value[i] = ctx->Light.Light[ln].Ambient[i] *
691 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
692 }
693 /* [3] = material alpha */
694 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
695 return;
696 case STATE_DIFFUSE:
697 for (i = 0; i < 3; i++) {
698 value[i] = ctx->Light.Light[ln].Diffuse[i] *
699 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
700 }
701 /* [3] = material alpha */
702 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
703 return;
704 case STATE_SPECULAR:
705 for (i = 0; i < 3; i++) {
706 value[i] = ctx->Light.Light[ln].Specular[i] *
707 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
708 }
709 /* [3] = material alpha */
710 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
711 return;
712 default:
713 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
714 return;
715 }
716 }
717 case STATE_TEXGEN:
718 {
719 /* state[1] is the texture unit */
720 const GLuint unit = (GLuint) state[1];
721 /* state[2] is the texgen attribute */
722 switch (state[2]) {
723 case STATE_TEXGEN_EYE_S:
724 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
725 return;
726 case STATE_TEXGEN_EYE_T:
727 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
728 return;
729 case STATE_TEXGEN_EYE_R:
730 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
731 return;
732 case STATE_TEXGEN_EYE_Q:
733 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
734 return;
735 case STATE_TEXGEN_OBJECT_S:
736 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
737 return;
738 case STATE_TEXGEN_OBJECT_T:
739 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
740 return;
741 case STATE_TEXGEN_OBJECT_R:
742 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
743 return;
744 case STATE_TEXGEN_OBJECT_Q:
745 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
746 return;
747 default:
748 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
749 return;
750 }
751 }
752 case STATE_TEXENV_COLOR:
753 {
754 /* state[1] is the texture unit */
755 const GLuint unit = (GLuint) state[1];
756 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
757 }
758 return;
759 case STATE_FOG_COLOR:
760 COPY_4V(value, ctx->Fog.Color);
761 return;
762 case STATE_FOG_PARAMS:
763 value[0] = ctx->Fog.Density;
764 value[1] = ctx->Fog.Start;
765 value[2] = ctx->Fog.End;
766 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
767 return;
768 case STATE_CLIPPLANE:
769 {
770 const GLuint plane = (GLuint) state[1];
771 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
772 }
773 return;
774 case STATE_POINT_SIZE:
775 value[0] = ctx->Point.Size;
776 value[1] = ctx->Point.MinSize;
777 value[2] = ctx->Point.MaxSize;
778 value[3] = ctx->Point.Threshold;
779 return;
780 case STATE_POINT_ATTENUATION:
781 value[0] = ctx->Point.Params[0];
782 value[1] = ctx->Point.Params[1];
783 value[2] = ctx->Point.Params[2];
784 value[3] = 1.0F;
785 return;
786 case STATE_MATRIX:
787 {
788 /* state[1] = modelview, projection, texture, etc. */
789 /* state[2] = which texture matrix or program matrix */
790 /* state[3] = first column to fetch */
791 /* state[4] = last column to fetch */
792 /* state[5] = transpose, inverse or invtrans */
793
794 const GLmatrix *matrix;
795 const enum state_index mat = state[1];
796 const GLuint index = (GLuint) state[2];
797 const GLuint first = (GLuint) state[3];
798 const GLuint last = (GLuint) state[4];
799 const enum state_index modifier = state[5];
800 const GLfloat *m;
801 GLuint row, i;
802 if (mat == STATE_MODELVIEW) {
803 matrix = ctx->ModelviewMatrixStack.Top;
804 }
805 else if (mat == STATE_PROJECTION) {
806 matrix = ctx->ProjectionMatrixStack.Top;
807 }
808 else if (mat == STATE_MVP) {
809 matrix = &ctx->_ModelProjectMatrix;
810 }
811 else if (mat == STATE_TEXTURE) {
812 matrix = ctx->TextureMatrixStack[index].Top;
813 }
814 else if (mat == STATE_PROGRAM) {
815 matrix = ctx->ProgramMatrixStack[index].Top;
816 }
817 else {
818 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
819 return;
820 }
821 if (modifier == STATE_MATRIX_INVERSE ||
822 modifier == STATE_MATRIX_INVTRANS) {
823 /* Be sure inverse is up to date:
824 */
825 _math_matrix_analyse( (GLmatrix*) matrix );
826 m = matrix->inv;
827 }
828 else {
829 m = matrix->m;
830 }
831 if (modifier == STATE_MATRIX_TRANSPOSE ||
832 modifier == STATE_MATRIX_INVTRANS) {
833 for (i = 0, row = first; row <= last; row++) {
834 value[i++] = m[row * 4 + 0];
835 value[i++] = m[row * 4 + 1];
836 value[i++] = m[row * 4 + 2];
837 value[i++] = m[row * 4 + 3];
838 }
839 }
840 else {
841 for (i = 0, row = first; row <= last; row++) {
842 value[i++] = m[row + 0];
843 value[i++] = m[row + 4];
844 value[i++] = m[row + 8];
845 value[i++] = m[row + 12];
846 }
847 }
848 }
849 return;
850 case STATE_DEPTH_RANGE:
851 value[0] = ctx->Viewport.Near; /* near */
852 value[1] = ctx->Viewport.Far; /* far */
853 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
854 value[3] = 0;
855 return;
856 case STATE_FRAGMENT_PROGRAM:
857 {
858 /* state[1] = {STATE_ENV, STATE_LOCAL} */
859 /* state[2] = parameter index */
860 const int idx = (int) state[2];
861 switch (state[1]) {
862 case STATE_ENV:
863 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
864 break;
865 case STATE_LOCAL:
866 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
867 break;
868 default:
869 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
870 return;
871 }
872 }
873 return;
874
875 case STATE_VERTEX_PROGRAM:
876 {
877 /* state[1] = {STATE_ENV, STATE_LOCAL} */
878 /* state[2] = parameter index */
879 const int idx = (int) state[2];
880 switch (state[1]) {
881 case STATE_ENV:
882 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
883 break;
884 case STATE_LOCAL:
885 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
886 break;
887 default:
888 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
889 return;
890 }
891 }
892 return;
893
894 case STATE_INTERNAL:
895 {
896 switch (state[1]) {
897 case STATE_NORMAL_SCALE:
898 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
899 break;
900 default:
901 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
902 return;
903 }
904 }
905 return;
906
907 default:
908 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
909 return;
910 }
911 }
912
913
914 /**
915 * Loop over all the parameters in a parameter list. If the parameter
916 * is a GL state reference, look up the current value of that state
917 * variable and put it into the parameter's Value[4] array.
918 * This would be called at glBegin time when using a fragment program.
919 */
920 void
921 _mesa_load_state_parameters(GLcontext *ctx,
922 struct program_parameter_list *paramList)
923 {
924 GLuint i;
925
926 if (!paramList)
927 return;
928
929 for (i = 0; i < paramList->NumParameters; i++) {
930 if (paramList->Parameters[i].Type == STATE) {
931 _mesa_fetch_state(ctx,
932 paramList->Parameters[i].StateIndexes,
933 paramList->ParameterValues[i]);
934 }
935 }
936 }
937
938
939
940 /**********************************************************************/
941 /* API functions */
942 /**********************************************************************/
943
944
945 /**
946 * Bind a program (make it current)
947 * \note Called from the GL API dispatcher by both glBindProgramNV
948 * and glBindProgramARB.
949 */
950 void GLAPIENTRY
951 _mesa_BindProgram(GLenum target, GLuint id)
952 {
953 struct program *prog;
954 GET_CURRENT_CONTEXT(ctx);
955 ASSERT_OUTSIDE_BEGIN_END(ctx);
956
957 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
958
959 if ((target == GL_VERTEX_PROGRAM_NV
960 && ctx->Extensions.NV_vertex_program) ||
961 (target == GL_VERTEX_PROGRAM_ARB
962 && ctx->Extensions.ARB_vertex_program)) {
963 /*** Vertex program binding ***/
964 struct vertex_program *curProg = ctx->VertexProgram.Current;
965 if (curProg->Base.Id == id) {
966 /* binding same program - no change */
967 return;
968 }
969 if (curProg->Base.Id != 0) {
970 /* decrement refcount on previously bound vertex program */
971 curProg->Base.RefCount--;
972 /* and delete if refcount goes below one */
973 if (curProg->Base.RefCount <= 0) {
974 /* the program ID was already removed from the hash table */
975 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
976 }
977 }
978 }
979 else if ((target == GL_FRAGMENT_PROGRAM_NV
980 && ctx->Extensions.NV_fragment_program) ||
981 (target == GL_FRAGMENT_PROGRAM_ARB
982 && ctx->Extensions.ARB_fragment_program)) {
983 /*** Fragment program binding ***/
984 struct fragment_program *curProg = ctx->FragmentProgram.Current;
985 if (curProg->Base.Id == id) {
986 /* binding same program - no change */
987 return;
988 }
989 if (curProg->Base.Id != 0) {
990 /* decrement refcount on previously bound fragment program */
991 curProg->Base.RefCount--;
992 /* and delete if refcount goes below one */
993 if (curProg->Base.RefCount <= 0) {
994 /* the program ID was already removed from the hash table */
995 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
996 }
997 }
998 }
999 else {
1000 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1001 return;
1002 }
1003
1004 /* NOTE: binding to a non-existant program is not an error.
1005 * That's supposed to be caught in glBegin.
1006 */
1007 if (id == 0) {
1008 /* Bind default program */
1009 prog = NULL;
1010 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1011 prog = ctx->Shared->DefaultVertexProgram;
1012 else
1013 prog = ctx->Shared->DefaultFragmentProgram;
1014 }
1015 else {
1016 /* Bind user program */
1017 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
1018 if (!prog || prog == &_mesa_DummyProgram) {
1019 /* allocate a new program now */
1020 prog = ctx->Driver.NewProgram(ctx, target, id);
1021 if (!prog) {
1022 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1023 return;
1024 }
1025 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1026 }
1027 else if (prog->Target != target) {
1028 _mesa_error(ctx, GL_INVALID_OPERATION,
1029 "glBindProgramNV/ARB(target mismatch)");
1030 return;
1031 }
1032 }
1033
1034 /* bind now */
1035 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1036 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1037 }
1038 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1039 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1040 }
1041
1042 /* Never null pointers */
1043 ASSERT(ctx->VertexProgram.Current);
1044 ASSERT(ctx->FragmentProgram.Current);
1045
1046 if (prog)
1047 prog->RefCount++;
1048
1049 if (ctx->Driver.BindProgram)
1050 ctx->Driver.BindProgram(ctx, target, prog);
1051 }
1052
1053
1054 /**
1055 * Delete a list of programs.
1056 * \note Not compiled into display lists.
1057 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1058 */
1059 void GLAPIENTRY
1060 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1061 {
1062 GLint i;
1063 GET_CURRENT_CONTEXT(ctx);
1064 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1065
1066 if (n < 0) {
1067 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1068 return;
1069 }
1070
1071 for (i = 0; i < n; i++) {
1072 if (ids[i] != 0) {
1073 struct program *prog = (struct program *)
1074 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
1075 if (prog == &_mesa_DummyProgram) {
1076 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1077 }
1078 else if (prog) {
1079 /* Unbind program if necessary */
1080 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1081 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1082 if (ctx->VertexProgram.Current &&
1083 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1084 /* unbind this currently bound program */
1085 _mesa_BindProgram(prog->Target, 0);
1086 }
1087 }
1088 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1089 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1090 if (ctx->FragmentProgram.Current &&
1091 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1092 /* unbind this currently bound program */
1093 _mesa_BindProgram(prog->Target, 0);
1094 }
1095 }
1096 else {
1097 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1098 return;
1099 }
1100 /* The ID is immediately available for re-use now */
1101 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1102 prog->RefCount--;
1103 if (prog->RefCount <= 0) {
1104 ctx->Driver.DeleteProgram(ctx, prog);
1105 }
1106 }
1107 }
1108 }
1109 }
1110
1111
1112 /**
1113 * Generate a list of new program identifiers.
1114 * \note Not compiled into display lists.
1115 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1116 */
1117 void GLAPIENTRY
1118 _mesa_GenPrograms(GLsizei n, GLuint *ids)
1119 {
1120 GLuint first;
1121 GLuint i;
1122 GET_CURRENT_CONTEXT(ctx);
1123 ASSERT_OUTSIDE_BEGIN_END(ctx);
1124
1125 if (n < 0) {
1126 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1127 return;
1128 }
1129
1130 if (!ids)
1131 return;
1132
1133 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1134
1135 /* Insert pointer to dummy program as placeholder */
1136 for (i = 0; i < (GLuint) n; i++) {
1137 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
1138 }
1139
1140 /* Return the program names */
1141 for (i = 0; i < (GLuint) n; i++) {
1142 ids[i] = first + i;
1143 }
1144 }
1145
1146
1147 /**
1148 * Determine if id names a vertex or fragment program.
1149 * \note Not compiled into display lists.
1150 * \note Called from both glIsProgramNV and glIsProgramARB.
1151 * \param id is the program identifier
1152 * \return GL_TRUE if id is a program, else GL_FALSE.
1153 */
1154 GLboolean GLAPIENTRY
1155 _mesa_IsProgram(GLuint id)
1156 {
1157 GET_CURRENT_CONTEXT(ctx);
1158 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1159
1160 if (id == 0)
1161 return GL_FALSE;
1162
1163 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1164 return GL_TRUE;
1165 else
1166 return GL_FALSE;
1167 }
1168
1169
1170
1171 /**********************************************************************/
1172 /* GL_MESA_program_debug extension */
1173 /**********************************************************************/
1174
1175
1176 /* XXX temporary */
1177 GLAPI void GLAPIENTRY
1178 glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1179 GLvoid *data)
1180 {
1181 _mesa_ProgramCallbackMESA(target, callback, data);
1182 }
1183
1184
1185 void
1186 _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1187 GLvoid *data)
1188 {
1189 GET_CURRENT_CONTEXT(ctx);
1190
1191 switch (target) {
1192 case GL_FRAGMENT_PROGRAM_ARB:
1193 if (!ctx->Extensions.ARB_fragment_program) {
1194 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1195 return;
1196 }
1197 ctx->FragmentProgram.Callback = callback;
1198 ctx->FragmentProgram.CallbackData = data;
1199 break;
1200 case GL_FRAGMENT_PROGRAM_NV:
1201 if (!ctx->Extensions.NV_fragment_program) {
1202 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1203 return;
1204 }
1205 ctx->FragmentProgram.Callback = callback;
1206 ctx->FragmentProgram.CallbackData = data;
1207 break;
1208 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1209 if (!ctx->Extensions.ARB_vertex_program &&
1210 !ctx->Extensions.NV_vertex_program) {
1211 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1212 return;
1213 }
1214 ctx->VertexProgram.Callback = callback;
1215 ctx->VertexProgram.CallbackData = data;
1216 break;
1217 default:
1218 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1219 return;
1220 }
1221 }
1222
1223
1224 /* XXX temporary */
1225 GLAPI void GLAPIENTRY
1226 glGetProgramRegisterfvMESA(GLenum target,
1227 GLsizei len, const GLubyte *registerName,
1228 GLfloat *v)
1229 {
1230 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1231 }
1232
1233
1234 void
1235 _mesa_GetProgramRegisterfvMESA(GLenum target,
1236 GLsizei len, const GLubyte *registerName,
1237 GLfloat *v)
1238 {
1239 char reg[1000];
1240 GET_CURRENT_CONTEXT(ctx);
1241
1242 /* We _should_ be inside glBegin/glEnd */
1243 #if 0
1244 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1245 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1246 return;
1247 }
1248 #endif
1249
1250 /* make null-terminated copy of registerName */
1251 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1252 _mesa_memcpy(reg, registerName, len);
1253 reg[len] = 0;
1254
1255 switch (target) {
1256 case GL_VERTEX_PROGRAM_NV:
1257 if (!ctx->Extensions.ARB_vertex_program &&
1258 !ctx->Extensions.NV_vertex_program) {
1259 _mesa_error(ctx, GL_INVALID_ENUM,
1260 "glGetProgramRegisterfvMESA(target)");
1261 return;
1262 }
1263 if (!ctx->VertexProgram._Enabled) {
1264 _mesa_error(ctx, GL_INVALID_OPERATION,
1265 "glGetProgramRegisterfvMESA");
1266 return;
1267 }
1268 /* GL_NV_vertex_program */
1269 if (reg[0] == 'R') {
1270 /* Temp register */
1271 GLint i = _mesa_atoi(reg + 1);
1272 if (i >= (GLint)ctx->Const.MaxVertexProgramTemps) {
1273 _mesa_error(ctx, GL_INVALID_VALUE,
1274 "glGetProgramRegisterfvMESA(registerName)");
1275 return;
1276 }
1277 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1278 }
1279 else if (reg[0] == 'v' && reg[1] == '[') {
1280 /* Vertex Input attribute */
1281 GLuint i;
1282 for (i = 0; i < ctx->Const.MaxVertexProgramAttribs; i++) {
1283 const char *name = _mesa_nv_vertex_input_register_name(i);
1284 char number[10];
1285 sprintf(number, "%d", i);
1286 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1287 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1288 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1289 return;
1290 }
1291 }
1292 _mesa_error(ctx, GL_INVALID_VALUE,
1293 "glGetProgramRegisterfvMESA(registerName)");
1294 return;
1295 }
1296 else if (reg[0] == 'o' && reg[1] == '[') {
1297 /* Vertex output attribute */
1298 }
1299 /* GL_ARB_vertex_program */
1300 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1301
1302 }
1303 else {
1304 _mesa_error(ctx, GL_INVALID_VALUE,
1305 "glGetProgramRegisterfvMESA(registerName)");
1306 return;
1307 }
1308 break;
1309 case GL_FRAGMENT_PROGRAM_ARB:
1310 if (!ctx->Extensions.ARB_fragment_program) {
1311 _mesa_error(ctx, GL_INVALID_ENUM,
1312 "glGetProgramRegisterfvMESA(target)");
1313 return;
1314 }
1315 if (!ctx->FragmentProgram._Enabled) {
1316 _mesa_error(ctx, GL_INVALID_OPERATION,
1317 "glGetProgramRegisterfvMESA");
1318 return;
1319 }
1320 /* XXX to do */
1321 break;
1322 case GL_FRAGMENT_PROGRAM_NV:
1323 if (!ctx->Extensions.NV_fragment_program) {
1324 _mesa_error(ctx, GL_INVALID_ENUM,
1325 "glGetProgramRegisterfvMESA(target)");
1326 return;
1327 }
1328 if (!ctx->FragmentProgram._Enabled) {
1329 _mesa_error(ctx, GL_INVALID_OPERATION,
1330 "glGetProgramRegisterfvMESA");
1331 return;
1332 }
1333 if (reg[0] == 'R') {
1334 /* Temp register */
1335 GLint i = _mesa_atoi(reg + 1);
1336 if (i >= (GLint)ctx->Const.MaxFragmentProgramTemps) {
1337 _mesa_error(ctx, GL_INVALID_VALUE,
1338 "glGetProgramRegisterfvMESA(registerName)");
1339 return;
1340 }
1341 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1342 }
1343 else if (reg[0] == 'f' && reg[1] == '[') {
1344 /* Fragment input attribute */
1345 GLuint i;
1346 for (i = 0; i < ctx->Const.MaxFragmentProgramAttribs; i++) {
1347 const char *name = _mesa_nv_fragment_input_register_name(i);
1348 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1349 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1350 return;
1351 }
1352 }
1353 _mesa_error(ctx, GL_INVALID_VALUE,
1354 "glGetProgramRegisterfvMESA(registerName)");
1355 return;
1356 }
1357 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1358 /* Fragment output color */
1359 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR]);
1360 }
1361 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1362 /* Fragment output color */
1363 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLH]);
1364 }
1365 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1366 /* Fragment output depth */
1367 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR]);
1368 }
1369 else {
1370 /* try user-defined identifiers */
1371 const GLfloat *value = _mesa_lookup_parameter_value(
1372 ctx->FragmentProgram.Current->Parameters, -1, reg);
1373 if (value) {
1374 COPY_4V(v, value);
1375 }
1376 else {
1377 _mesa_error(ctx, GL_INVALID_VALUE,
1378 "glGetProgramRegisterfvMESA(registerName)");
1379 return;
1380 }
1381 }
1382 break;
1383 default:
1384 _mesa_error(ctx, GL_INVALID_ENUM,
1385 "glGetProgramRegisterfvMESA(target)");
1386 return;
1387 }
1388
1389 }