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