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 state - an array of 6 state tokens
334 * \return index of the new parameter.
337 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
340 /* XXX Should we parse <stateString> here and produce the parameter's
341 * list of STATE_* tokens here, or in the parser?
345 idx
= add_parameter(paramList
, "Some State", NULL
, STATE
);
348 paramList
->Parameters
[idx
].StateIndexes
[a
] = (enum state_index
) stateTokens
[a
];
355 * Lookup a parameter value by name in the given parameter list.
356 * \return pointer to the float[4] values.
359 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
360 GLsizei nameLen
, const char *name
)
365 /* name is null-terminated */
366 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
367 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
368 return paramList
->Parameters
[i
].Values
;
372 /* name is not null-terminated, use nameLen */
373 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
374 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
375 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
376 return paramList
->Parameters
[i
].Values
;
384 * Lookup a parameter index by name in the given parameter list.
385 * \return index of parameter in the list.
388 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
389 GLsizei nameLen
, const char *name
)
394 /* name is null-terminated */
395 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
396 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
401 /* name is not null-terminated, use nameLen */
402 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
403 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
404 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
413 * Use the list of tokens in the state[] array to find global GL state
414 * and return it in <value>. Usually, four values are returned in <value>
415 * but matrix queries may return as many as 16 values.
416 * This function is used for ARB vertex/fragment programs.
417 * The program parser will produce the state[] values.
420 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
426 /* state[1] is either 0=front or 1=back side */
427 const GLuint face
= (GLuint
) state
[1];
428 /* state[2] is the material attribute */
432 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
434 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
438 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
440 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
444 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
446 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
450 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
452 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
454 case STATE_SHININESS
:
456 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
458 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
464 _mesa_problem(ctx
, "Invalid material state in fetch_state");
471 /* state[1] is the light number */
472 const GLuint ln
= (GLuint
) state
[1];
473 /* state[2] is the light attribute */
476 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
479 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
482 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
485 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
487 case STATE_ATTENUATION
:
488 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
489 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
490 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
491 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
493 case STATE_SPOT_DIRECTION
:
494 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
498 GLfloat eye_z
[] = {0, 0, 1};
500 /* Compute infinite half angle vector:
501 * half-vector = light_position + (0, 0, 1)
502 * and then normalize. w = 0
504 * light.EyePosition.w should be 0 for infinite lights.
506 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
507 NORMALIZE_3FV(value
);
512 _mesa_problem(ctx
, "Invalid light state in fetch_state");
517 case STATE_LIGHTMODEL_AMBIENT
:
518 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
520 case STATE_LIGHTMODEL_SCENECOLOR
:
524 for (i
= 0; i
< 4; i
++) {
525 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
526 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
527 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
533 for (i
= 0; i
< 4; i
++) {
534 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
535 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
536 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
540 case STATE_LIGHTPROD
:
542 const GLuint ln
= (GLuint
) state
[1];
543 const GLuint face
= (GLuint
) state
[2];
545 ASSERT(face
== 0 || face
== 1);
548 for (i
= 0; i
< 3; i
++) {
549 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
550 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
552 /* [3] = material alpha */
553 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
556 for (i
= 0; i
< 3; i
++) {
557 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
558 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
560 /* [3] = material alpha */
561 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
564 for (i
= 0; i
< 3; i
++) {
565 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
566 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
568 /* [3] = material alpha */
569 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
572 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
579 /* state[1] is the texture unit */
580 const GLuint unit
= (GLuint
) state
[1];
581 /* state[2] is the texgen attribute */
583 case STATE_TEXGEN_EYE_S
:
584 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
586 case STATE_TEXGEN_EYE_T
:
587 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
589 case STATE_TEXGEN_EYE_R
:
590 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
592 case STATE_TEXGEN_EYE_Q
:
593 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
595 case STATE_TEXGEN_OBJECT_S
:
596 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
598 case STATE_TEXGEN_OBJECT_T
:
599 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
601 case STATE_TEXGEN_OBJECT_R
:
602 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
604 case STATE_TEXGEN_OBJECT_Q
:
605 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
608 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
613 case STATE_TEXENV_COLOR
:
615 /* state[1] is the texture unit */
616 const GLuint unit
= (GLuint
) state
[1];
617 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
620 case STATE_FOG_COLOR
:
621 COPY_4V(value
, ctx
->Fog
.Color
);
623 case STATE_FOG_PARAMS
:
624 value
[0] = ctx
->Fog
.Density
;
625 value
[1] = ctx
->Fog
.Start
;
626 value
[2] = ctx
->Fog
.End
;
627 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
629 case STATE_CLIPPLANE
:
631 const GLuint plane
= (GLuint
) state
[1];
632 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
635 case STATE_POINT_SIZE
:
636 value
[0] = ctx
->Point
.Size
;
637 value
[1] = ctx
->Point
.MinSize
;
638 value
[2] = ctx
->Point
.MaxSize
;
639 value
[3] = ctx
->Point
.Threshold
;
641 case STATE_POINT_ATTENUATION
:
642 value
[0] = ctx
->Point
.Params
[0];
643 value
[1] = ctx
->Point
.Params
[1];
644 value
[2] = ctx
->Point
.Params
[2];
649 /* state[1] = modelview, projection, texture, etc. */
650 /* state[2] = which texture matrix or program matrix */
651 /* state[3] = first column to fetch */
652 /* state[4] = last column to fetch */
653 /* state[5] = transpose, inverse or invtrans */
655 const GLmatrix
*matrix
;
656 const enum state_index mat
= state
[1];
657 const GLuint index
= (GLuint
) state
[2];
658 const GLuint first
= (GLuint
) state
[3];
659 const GLuint last
= (GLuint
) state
[4];
660 const enum state_index modifier
= state
[5];
663 if (mat
== STATE_MODELVIEW
) {
664 matrix
= ctx
->ModelviewMatrixStack
.Top
;
666 else if (mat
== STATE_PROJECTION
) {
667 matrix
= ctx
->ProjectionMatrixStack
.Top
;
669 else if (mat
== STATE_MVP
) {
670 matrix
= &ctx
->_ModelProjectMatrix
;
672 else if (mat
== STATE_TEXTURE
) {
673 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
675 else if (mat
== STATE_PROGRAM
) {
676 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
679 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
682 if (modifier
== STATE_MATRIX_INVERSE
||
683 modifier
== STATE_MATRIX_INVTRANS
) {
684 /* XXX be sure inverse is up to date */
690 if (modifier
== STATE_MATRIX_TRANSPOSE
||
691 modifier
== STATE_MATRIX_INVTRANS
) {
692 for (i
= 0, row
= first
; row
<= last
; row
++) {
693 value
[i
++] = m
[row
* 4 + 0];
694 value
[i
++] = m
[row
* 4 + 1];
695 value
[i
++] = m
[row
* 4 + 2];
696 value
[i
++] = m
[row
* 4 + 3];
700 for (i
= 0, row
= first
; row
<= last
; row
++) {
701 value
[i
++] = m
[row
+ 0];
702 value
[i
++] = m
[row
+ 4];
703 value
[i
++] = m
[row
+ 8];
704 value
[i
++] = m
[row
+ 12];
709 case STATE_DEPTH_RANGE
:
710 value
[0] = ctx
->Viewport
.Near
; /* near */
711 value
[1] = ctx
->Viewport
.Far
; /* far */
712 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
715 case STATE_FRAGMENT_PROGRAM
:
717 /* state[1] = {STATE_ENV, STATE_LOCAL} */
718 /* state[2] = parameter index */
723 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
727 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
730 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
736 case STATE_VERTEX_PROGRAM
:
738 /* state[1] = {STATE_ENV, STATE_LOCAL} */
739 /* state[2] = parameter index */
744 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
748 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
751 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
757 _mesa_problem(ctx
, "Invalid state in fetch_state");
764 * Loop over all the parameters in a parameter list. If the parameter
765 * is a GL state reference, look up the current value of that state
766 * variable and put it into the parameter's Value[4] array.
767 * This would be called at glBegin time when using a fragment program.
770 _mesa_load_state_parameters(GLcontext
*ctx
,
771 struct program_parameter_list
*paramList
)
774 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
775 if (paramList
->Parameters
[i
].Type
== STATE
) {
776 _mesa_fetch_state(ctx
, paramList
->Parameters
[i
].StateIndexes
,
777 paramList
->Parameters
[i
].Values
);
784 /**********************************************************************/
786 /**********************************************************************/
790 * Bind a program (make it current)
791 * \note Called from the GL API dispatcher by both glBindProgramNV
792 * and glBindProgramARB.
795 _mesa_BindProgram(GLenum target
, GLuint id
)
797 struct program
*prog
;
798 GET_CURRENT_CONTEXT(ctx
);
799 ASSERT_OUTSIDE_BEGIN_END(ctx
);
801 if ((target
== GL_VERTEX_PROGRAM_NV
802 && ctx
->Extensions
.NV_vertex_program
) ||
803 (target
== GL_VERTEX_PROGRAM_ARB
804 && ctx
->Extensions
.ARB_vertex_program
)) {
805 if (ctx
->VertexProgram
.Current
&&
806 ctx
->VertexProgram
.Current
->Base
.Id
== id
)
808 /* decrement refcount on previously bound vertex program */
809 if (ctx
->VertexProgram
.Current
) {
810 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
811 /* and delete if refcount goes below one */
812 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0) {
813 _mesa_delete_program(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
814 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
818 else if ((target
== GL_FRAGMENT_PROGRAM_NV
819 && ctx
->Extensions
.NV_fragment_program
) ||
820 (target
== GL_FRAGMENT_PROGRAM_ARB
821 && ctx
->Extensions
.ARB_fragment_program
)) {
822 if (ctx
->FragmentProgram
.Current
&&
823 ctx
->FragmentProgram
.Current
->Base
.Id
== id
)
825 /* decrement refcount on previously bound fragment program */
826 if (ctx
->FragmentProgram
.Current
) {
827 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
828 /* and delete if refcount goes below one */
829 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0) {
830 _mesa_delete_program(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
831 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
836 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
840 /* NOTE: binding to a non-existant program is not an error.
841 * That's supposed to be caught in glBegin.
844 /* default program */
846 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
847 prog
= ctx
->Shared
->DefaultVertexProgram
;
849 prog
= ctx
->Shared
->DefaultFragmentProgram
;
852 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
854 if (prog
->Target
== 0) {
855 /* prog was allocated with glGenProgramsNV */
856 prog
->Target
= target
;
858 else if (prog
->Target
!= target
) {
859 _mesa_error(ctx
, GL_INVALID_OPERATION
,
860 "glBindProgramNV/ARB(target mismatch)");
865 /* allocate a new program now */
866 prog
= _mesa_alloc_program(ctx
, target
, id
);
868 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
872 prog
->Target
= target
;
873 prog
->Resident
= GL_TRUE
;
875 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
880 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
881 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
883 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
884 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
893 * Delete a list of programs.
894 * \note Not compiled into display lists.
895 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
898 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
901 GET_CURRENT_CONTEXT(ctx
);
902 ASSERT_OUTSIDE_BEGIN_END(ctx
);
905 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
909 for (i
= 0; i
< n
; i
++) {
911 struct program
*prog
= (struct program
*)
912 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
914 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
915 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
916 if (ctx
->VertexProgram
.Current
&&
917 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
918 /* unbind this currently bound program */
919 _mesa_BindProgram(prog
->Target
, 0);
922 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
) {
923 if (ctx
->FragmentProgram
.Current
&&
924 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
925 /* unbind this currently bound program */
926 _mesa_BindProgram(prog
->Target
, 0);
930 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
934 if (prog
->RefCount
<= 0) {
935 _mesa_delete_program(ctx
, prog
);
944 * Generate a list of new program identifiers.
945 * \note Not compiled into display lists.
946 * \note Called by both glGenProgramsNV and glGenProgramsARB.
949 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
953 GET_CURRENT_CONTEXT(ctx
);
954 ASSERT_OUTSIDE_BEGIN_END(ctx
);
957 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
964 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
966 for (i
= 0; i
< (GLuint
) n
; i
++) {
967 const int bytes
= MAX2(sizeof(struct vertex_program
),
968 sizeof(struct fragment_program
));
969 struct program
*prog
= (struct program
*) _mesa_calloc(bytes
);
971 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPrograms");
975 prog
->Id
= first
+ i
;
976 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, prog
);
979 /* Return the program names */
980 for (i
= 0; i
< (GLuint
) n
; i
++) {
987 * Determine if id names a program.
988 * \note Not compiled into display lists.
989 * \note Called from both glIsProgramNV and glIsProgramARB.
990 * \param id is the program identifier
991 * \return GL_TRUE if id is a program, else GL_FALSE.
994 _mesa_IsProgram(GLuint id
)
996 struct program
*prog
;
997 GET_CURRENT_CONTEXT(ctx
);
998 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1003 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
1004 if (prog
&& prog
->Target
)
1012 /**********************************************************************/
1013 /* GL_MESA_program_debug extension */
1014 /**********************************************************************/
1019 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1022 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1027 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1030 GET_CURRENT_CONTEXT(ctx
);
1033 case GL_FRAGMENT_PROGRAM_ARB
:
1034 if (!ctx
->Extensions
.ARB_fragment_program
) {
1035 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1038 ctx
->FragmentProgram
.Callback
= callback
;
1039 ctx
->FragmentProgram
.CallbackData
= data
;
1041 case GL_FRAGMENT_PROGRAM_NV
:
1042 if (!ctx
->Extensions
.NV_fragment_program
) {
1043 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1046 ctx
->FragmentProgram
.Callback
= callback
;
1047 ctx
->FragmentProgram
.CallbackData
= data
;
1049 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1050 if (!ctx
->Extensions
.ARB_vertex_program
&&
1051 !ctx
->Extensions
.NV_vertex_program
) {
1052 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1055 ctx
->VertexProgram
.Callback
= callback
;
1056 ctx
->VertexProgram
.CallbackData
= data
;
1059 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1067 glGetProgramRegisterfvMESA(GLenum target
,
1068 GLsizei len
, const GLubyte
*registerName
,
1071 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1076 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1077 GLsizei len
, const GLubyte
*registerName
,
1081 GET_CURRENT_CONTEXT(ctx
);
1083 /* We _should_ be inside glBegin/glEnd */
1085 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1086 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1091 /* make null-terminated copy of registerName */
1092 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1093 _mesa_memcpy(reg
, registerName
, len
);
1097 case GL_VERTEX_PROGRAM_NV
:
1098 if (!ctx
->Extensions
.ARB_vertex_program
&&
1099 !ctx
->Extensions
.NV_vertex_program
) {
1100 _mesa_error(ctx
, GL_INVALID_ENUM
,
1101 "glGetProgramRegisterfvMESA(target)");
1104 if (!ctx
->VertexProgram
.Enabled
) {
1105 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1106 "glGetProgramRegisterfvMESA");
1109 /* GL_NV_vertex_program */
1110 if (reg
[0] == 'R') {
1112 GLint i
= _mesa_atoi(reg
+ 1);
1113 if (i
>= (GLint
)ctx
->Const
.MaxVertexProgramTemps
) {
1114 _mesa_error(ctx
, GL_INVALID_VALUE
,
1115 "glGetProgramRegisterfvMESA(registerName)");
1118 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1120 else if (reg
[0] == 'v' && reg
[1] == '[') {
1121 /* Vertex Input attribute */
1123 for (i
= 0; i
< ctx
->Const
.MaxVertexProgramAttribs
; i
++) {
1124 const char *name
= _mesa_nv_vertex_input_register_name(i
);
1126 sprintf(number
, "%d", i
);
1127 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
1128 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
1129 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
1133 _mesa_error(ctx
, GL_INVALID_VALUE
,
1134 "glGetProgramRegisterfvMESA(registerName)");
1137 else if (reg
[0] == 'o' && reg
[1] == '[') {
1138 /* Vertex output attribute */
1140 /* GL_ARB_vertex_program */
1141 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1145 _mesa_error(ctx
, GL_INVALID_VALUE
,
1146 "glGetProgramRegisterfvMESA(registerName)");
1150 case GL_FRAGMENT_PROGRAM_ARB
:
1151 if (!ctx
->Extensions
.ARB_fragment_program
) {
1152 _mesa_error(ctx
, GL_INVALID_ENUM
,
1153 "glGetProgramRegisterfvMESA(target)");
1156 if (!ctx
->FragmentProgram
.Enabled
) {
1157 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1158 "glGetProgramRegisterfvMESA");
1163 case GL_FRAGMENT_PROGRAM_NV
:
1164 if (!ctx
->Extensions
.NV_fragment_program
) {
1165 _mesa_error(ctx
, GL_INVALID_ENUM
,
1166 "glGetProgramRegisterfvMESA(target)");
1169 if (!ctx
->FragmentProgram
.Enabled
) {
1170 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1171 "glGetProgramRegisterfvMESA");
1174 if (reg
[0] == 'R') {
1176 GLint i
= _mesa_atoi(reg
+ 1);
1177 if (i
>= (GLint
)ctx
->Const
.MaxFragmentProgramTemps
) {
1178 _mesa_error(ctx
, GL_INVALID_VALUE
,
1179 "glGetProgramRegisterfvMESA(registerName)");
1182 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
1184 else if (reg
[0] == 'f' && reg
[1] == '[') {
1185 /* Fragment input attribute */
1187 for (i
= 0; i
< ctx
->Const
.MaxFragmentProgramAttribs
; i
++) {
1188 const char *name
= _mesa_nv_fragment_input_register_name(i
);
1189 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
1190 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
1194 _mesa_error(ctx
, GL_INVALID_VALUE
,
1195 "glGetProgramRegisterfvMESA(registerName)");
1198 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
1199 /* Fragment output color */
1200 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
]);
1202 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
1203 /* Fragment output color */
1204 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLH
]);
1206 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
1207 /* Fragment output depth */
1208 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
]);
1211 /* try user-defined identifiers */
1212 const GLfloat
*value
= _mesa_lookup_parameter_value(
1213 ctx
->FragmentProgram
.Current
->Parameters
, -1, reg
);
1218 _mesa_error(ctx
, GL_INVALID_VALUE
,
1219 "glGetProgramRegisterfvMESA(registerName)");
1225 _mesa_error(ctx
, GL_INVALID_ENUM
,
1226 "glGetProgramRegisterfvMESA(target)");