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