2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Vertex and fragment program support functions.
39 #include "nvfragparse.h"
40 #include "nvfragprog.h"
41 #include "nvvertparse.h"
44 /**********************************************************************/
45 /* Utility functions */
46 /**********************************************************************/
50 * Init context's program state
53 _mesa_init_program(GLcontext
*ctx
)
57 ctx
->Program
.ErrorPos
= -1;
58 ctx
->Program
.ErrorString
= _mesa_strdup("");
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
;
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
++;
84 * Set the vertex/fragment program error state (position and error string).
85 * This is generally called from within the parsers.
88 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
90 ctx
->Program
.ErrorPos
= pos
;
91 _mesa_free((void *) ctx
->Program
.ErrorString
);
94 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
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
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'.
109 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
110 GLint
*line
, GLint
*col
)
112 const GLubyte
*lineStart
= string
;
113 const GLubyte
*p
= string
;
120 if (*p
== (GLubyte
) '\n') {
127 *col
= (pos
- lineStart
) + 1;
129 /* return copy of this line */
130 while (*p
!= 0 && *p
!= '\n')
133 s
= (GLubyte
*) _mesa_malloc(len
+ 1);
134 _mesa_memcpy(s
, lineStart
, len
);
143 * Allocate and initialize a new fragment/vertex program object
145 * \param id program id/number
146 * \param target program target/type
147 * \return pointer to new program object
150 _mesa_alloc_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
152 struct program
*prog
;
154 if (target
== GL_VERTEX_PROGRAM_NV
155 || target
== GL_VERTEX_PROGRAM_ARB
) {
156 struct vertex_program
*vprog
= CALLOC_STRUCT(vertex_program
);
160 prog
= &(vprog
->Base
);
162 else if (target
== GL_FRAGMENT_PROGRAM_NV
163 || target
== GL_FRAGMENT_PROGRAM_ARB
) {
164 struct fragment_program
*fprog
= CALLOC_STRUCT(fragment_program
);
168 prog
= &(fprog
->Base
);
171 _mesa_problem(ctx
, "bad target in _mesa_alloc_program");
175 prog
->Target
= target
;
176 prog
->Resident
= GL_TRUE
;
183 * Delete a program and remove it from the hash table, ignoring the
185 * \note Called from the GL API dispatcher.
188 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
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
);
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
);
213 /**********************************************************************/
214 /* Program parameter functions */
215 /**********************************************************************/
217 struct program_parameter_list
*
218 _mesa_new_parameter_list(void)
220 return (struct program_parameter_list
*)
221 _mesa_calloc(sizeof(struct program_parameter_list
));
226 * Free a parameter list and all its parameters
229 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
231 _mesa_free_parameters(paramList
);
232 _mesa_free(paramList
);
237 * Free all the parameters in the given list, but don't free the
238 * paramList structure itself.
241 _mesa_free_parameters(struct program_parameter_list
*paramList
)
244 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
245 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
247 _mesa_free(paramList
->Parameters
);
248 paramList
->NumParameters
= 0;
249 paramList
->Parameters
= NULL
;
254 * Helper function used by the functions below.
257 add_parameter(struct program_parameter_list
*paramList
,
258 const char *name
, const GLfloat values
[4],
259 enum parameter_type type
)
261 const GLuint n
= paramList
->NumParameters
;
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
) {
269 paramList
->NumParameters
= 0;
273 paramList
->NumParameters
= n
+ 1;
274 paramList
->Parameters
[n
].Name
= _mesa_strdup(name
);
275 paramList
->Parameters
[n
].Type
= type
;
277 COPY_4V(paramList
->Parameters
[n
].Values
, values
);
284 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
285 * \return index of the new entry in the parameter list
288 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
289 const char *name
, const GLfloat values
[4])
291 return add_parameter(paramList
, name
, values
, NAMED_PARAMETER
);
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.
302 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
303 const char *name
, const GLfloat values
[4])
305 return add_parameter(paramList
, name
, values
, CONSTANT
);
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.
316 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
317 const GLfloat values
[4])
319 /* generate a new dummy name */
322 _mesa_sprintf(name
, "constant%d", n
);
325 return add_parameter(paramList
, name
, values
, CONSTANT
);
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.
336 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
337 const char *stateString
)
339 /* XXX Should we parse <stateString> here and produce the parameter's
340 * list of STATE_* tokens here, or in the parser?
342 return add_parameter(paramList
, stateString
, NULL
, STATE
);
347 * Lookup a parameter value by name in the given parameter list.
348 * \return pointer to the float[4] values.
351 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
352 GLsizei nameLen
, const char *name
)
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
;
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
;
376 * Lookup a parameter index by name in the given parameter list.
377 * \return index of parameter in the list.
380 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
381 GLsizei nameLen
, const char *name
)
386 /* name is null-terminated */
387 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
388 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
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
)
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.
412 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
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 */
424 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
426 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
430 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
432 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
436 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
438 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
442 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
444 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
446 case STATE_SHININESS
:
448 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
450 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
456 _mesa_problem(ctx
, "Invalid material state in fetch_state");
463 /* state[1] is the light number */
464 const GLuint ln
= (GLuint
) state
[1];
465 /* state[2] is the light attribute */
468 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
471 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
474 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
477 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
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
;
485 case STATE_SPOT_DIRECTION
:
486 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
489 _mesa_problem(ctx
, "Invalid light state in fetch_state");
494 case STATE_LIGHTMODEL_AMBIENT
:
495 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
497 case STATE_LIGHTMODEL_SCENECOLOR
:
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
];
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
];
517 case STATE_LIGHTPROD
:
519 const GLuint ln
= (GLuint
) state
[1];
520 const GLuint face
= (GLuint
) state
[2];
522 ASSERT(face
== 0 || face
== 1);
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
];
529 /* [3] = material alpha */
530 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
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
];
537 /* [3] = material alpha */
538 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
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
];
545 /* [3] = material alpha */
546 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
549 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
556 /* state[1] is the texture unit */
557 const GLuint unit
= (GLuint
) state
[1];
558 /* state[2] is the texgen attribute */
560 case STATE_TEXGEN_EYE_S
:
561 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
563 case STATE_TEXGEN_EYE_T
:
564 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
566 case STATE_TEXGEN_EYE_R
:
567 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
569 case STATE_TEXGEN_EYE_Q
:
570 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
572 case STATE_TEXGEN_OBJECT_S
:
573 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
575 case STATE_TEXGEN_OBJECT_T
:
576 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
578 case STATE_TEXGEN_OBJECT_R
:
579 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
581 case STATE_TEXGEN_OBJECT_Q
:
582 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
585 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
590 case STATE_FOG_COLOR
:
591 COPY_4V(value
, ctx
->Fog
.Color
);
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
);
599 case STATE_CLIPPLANE
:
601 const GLuint plane
= (GLuint
) state
[1];
602 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
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
;
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];
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 */
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];
633 if (mat
== STATE_MODELVIEW
) {
634 matrix
= ctx
->ModelviewMatrixStack
.Top
;
636 else if (mat
== STATE_PROJECTION
) {
637 matrix
= ctx
->ProjectionMatrixStack
.Top
;
639 else if (mat
== STATE_MVP
) {
640 matrix
= &ctx
->_ModelProjectMatrix
;
642 else if (mat
== STATE_TEXTURE
) {
643 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
645 else if (mat
== STATE_PROGRAM
) {
646 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
649 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
652 if (modifier
== STATE_MATRIX_INVERSE
||
653 modifier
== STATE_MATRIX_INVTRANS
) {
654 /* XXX be sure inverse is up to date */
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];
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];
680 _mesa_problem(ctx
, "Invalid state in fetch_state");
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.
693 _mesa_load_state_parameters(GLcontext
*ctx
,
694 struct program_parameter_list
*paramList
)
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
);
707 /**********************************************************************/
709 /**********************************************************************/
713 * Bind a program (make it current)
714 * \note Called from the GL API dispatcher by both glBindProgramNV
715 * and glBindProgramARB.
718 _mesa_BindProgram(GLenum target
, GLuint id
)
720 struct program
*prog
;
721 GET_CURRENT_CONTEXT(ctx
);
722 ASSERT_OUTSIDE_BEGIN_END(ctx
);
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
)
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
);
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
)
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
);
759 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
763 /* NOTE: binding to a non-existant program is not an error.
764 * That's supposed to be caught in glBegin.
767 /* default program */
769 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
770 prog
= ctx
->Shared
->DefaultVertexProgram
;
772 prog
= ctx
->Shared
->DefaultFragmentProgram
;
775 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
777 if (prog
->Target
== 0) {
778 /* prog was allocated with glGenProgramsNV */
779 prog
->Target
= target
;
781 else if (prog
->Target
!= target
) {
782 _mesa_error(ctx
, GL_INVALID_OPERATION
,
783 "glBindProgramNV/ARB(target mismatch)");
788 /* allocate a new program now */
789 prog
= _mesa_alloc_program(ctx
, target
, id
);
791 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
795 prog
->Target
= target
;
796 prog
->Resident
= GL_TRUE
;
798 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
803 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
804 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
806 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
807 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
816 * Delete a list of programs.
817 * \note Not compiled into display lists.
818 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
821 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
824 GET_CURRENT_CONTEXT(ctx
);
825 ASSERT_OUTSIDE_BEGIN_END(ctx
);
828 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
832 for (i
= 0; i
< n
; i
++) {
834 struct program
*prog
= (struct program
*)
835 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
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);
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);
853 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
857 if (prog
->RefCount
<= 0) {
858 _mesa_delete_program(ctx
, prog
);
867 * Generate a list of new program identifiers.
868 * \note Not compiled into display lists.
869 * \note Called by both glGenProgramsNV and glGenProgramsARB.
872 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
876 GET_CURRENT_CONTEXT(ctx
);
877 ASSERT_OUTSIDE_BEGIN_END(ctx
);
880 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
887 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
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
);
894 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPrograms");
898 prog
->Id
= first
+ i
;
899 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, prog
);
902 /* Return the program names */
903 for (i
= 0; i
< (GLuint
) n
; i
++) {
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.
917 _mesa_IsProgram(GLuint id
)
919 struct program
*prog
;
920 GET_CURRENT_CONTEXT(ctx
);
921 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
926 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
927 if (prog
&& prog
->Target
)
935 /**********************************************************************/
936 /* GL_MESA_program_debug extension */
937 /**********************************************************************/
942 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
945 _mesa_ProgramCallbackMESA(target
, callback
, data
);
950 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
953 GET_CURRENT_CONTEXT(ctx
);
956 case GL_FRAGMENT_PROGRAM_ARB
:
957 if (!ctx
->Extensions
.ARB_fragment_program
) {
958 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
961 ctx
->FragmentProgram
.Callback
= callback
;
962 ctx
->FragmentProgram
.CallbackData
= data
;
964 case GL_FRAGMENT_PROGRAM_NV
:
965 if (!ctx
->Extensions
.NV_fragment_program
) {
966 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
969 ctx
->FragmentProgram
.Callback
= callback
;
970 ctx
->FragmentProgram
.CallbackData
= data
;
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)");
978 ctx
->VertexProgram
.Callback
= callback
;
979 ctx
->VertexProgram
.CallbackData
= data
;
982 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
990 glGetProgramRegisterfvMESA(GLenum target
,
991 GLsizei len
, const GLubyte
*registerName
,
994 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
999 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1000 GLsizei len
, const GLubyte
*registerName
,
1004 GET_CURRENT_CONTEXT(ctx
);
1006 /* We _should_ be inside glBegin/glEnd */
1008 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1009 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1014 /* make null-terminated copy of registerName */
1015 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1016 _mesa_memcpy(reg
, registerName
, len
);
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)");
1027 if (!ctx
->VertexProgram
.Enabled
) {
1028 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1029 "glGetProgramRegisterfvMESA");
1032 /* GL_NV_vertex_program */
1033 if (reg
[0] == 'R') {
1035 GLint i
= _mesa_atoi(reg
+ 1);
1036 if (i
>= (GLint
)ctx
->Const
.MaxVertexProgramTemps
) {
1037 _mesa_error(ctx
, GL_INVALID_VALUE
,
1038 "glGetProgramRegisterfvMESA(registerName)");
1041 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1043 else if (reg
[0] == 'v' && reg
[1] == '[') {
1044 /* Vertex Input attribute */
1046 for (i
= 0; i
< ctx
->Const
.MaxVertexProgramAttribs
; i
++) {
1047 const char *name
= _mesa_nv_vertex_input_register_name(i
);
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
]);
1056 _mesa_error(ctx
, GL_INVALID_VALUE
,
1057 "glGetProgramRegisterfvMESA(registerName)");
1060 else if (reg
[0] == 'o' && reg
[1] == '[') {
1061 /* Vertex output attribute */
1063 /* GL_ARB_vertex_program */
1064 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1068 _mesa_error(ctx
, GL_INVALID_VALUE
,
1069 "glGetProgramRegisterfvMESA(registerName)");
1073 case GL_FRAGMENT_PROGRAM_ARB
:
1074 if (!ctx
->Extensions
.ARB_fragment_program
) {
1075 _mesa_error(ctx
, GL_INVALID_ENUM
,
1076 "glGetProgramRegisterfvMESA(target)");
1079 if (!ctx
->FragmentProgram
.Enabled
) {
1080 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1081 "glGetProgramRegisterfvMESA");
1086 case GL_FRAGMENT_PROGRAM_NV
:
1087 if (!ctx
->Extensions
.NV_fragment_program
) {
1088 _mesa_error(ctx
, GL_INVALID_ENUM
,
1089 "glGetProgramRegisterfvMESA(target)");
1092 if (!ctx
->FragmentProgram
.Enabled
) {
1093 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1094 "glGetProgramRegisterfvMESA");
1097 if (reg
[0] == 'R') {
1099 GLint i
= _mesa_atoi(reg
+ 1);
1100 if (i
>= (GLint
)ctx
->Const
.MaxFragmentProgramTemps
) {
1101 _mesa_error(ctx
, GL_INVALID_VALUE
,
1102 "glGetProgramRegisterfvMESA(registerName)");
1105 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
1107 else if (reg
[0] == 'f' && reg
[1] == '[') {
1108 /* Fragment input attribute */
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
]);
1117 _mesa_error(ctx
, GL_INVALID_VALUE
,
1118 "glGetProgramRegisterfvMESA(registerName)");
1121 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
1122 /* Fragment output color */
1123 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
]);
1125 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
1126 /* Fragment output color */
1127 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLH
]);
1129 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
1130 /* Fragment output depth */
1131 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
]);
1134 /* try user-defined identifiers */
1135 const GLfloat
*value
= _mesa_lookup_parameter_value(
1136 ctx
->FragmentProgram
.Current
->Parameters
, -1, reg
);
1141 _mesa_error(ctx
, GL_INVALID_VALUE
,
1142 "glGetProgramRegisterfvMESA(registerName)");
1148 _mesa_error(ctx
, GL_INVALID_ENUM
,
1149 "glGetProgramRegisterfvMESA(target)");