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