2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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
= (struct vertex_program
*) ctx
->Shared
->DefaultVertexProgram
;
65 assert(ctx
->VertexProgram
.Current
);
66 ctx
->VertexProgram
.Current
->Base
.RefCount
++;
67 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
68 ctx
->VertexProgram
.TrackMatrix
[i
] = GL_NONE
;
69 ctx
->VertexProgram
.TrackMatrixTransform
[i
] = GL_IDENTITY_NV
;
73 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
74 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
75 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) ctx
->Shared
->DefaultFragmentProgram
;
76 assert(ctx
->FragmentProgram
.Current
);
77 ctx
->FragmentProgram
.Current
->Base
.RefCount
++;
83 * Set the vertex/fragment program error state (position and error string).
84 * This is generally called from within the parsers.
87 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
89 ctx
->Program
.ErrorPos
= pos
;
90 _mesa_free((void *) ctx
->Program
.ErrorString
);
93 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
98 * Find the line number and column for 'pos' within 'string'.
99 * Return a copy of the line which contains 'pos'. Free the line with
101 * \param string the program string
102 * \param pos the position within the string
103 * \param line returns the line number corresponding to 'pos'.
104 * \param col returns the column number corresponding to 'pos'.
105 * \return copy of the line containing 'pos'.
108 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
109 GLint
*line
, GLint
*col
)
111 const GLubyte
*lineStart
= string
;
112 const GLubyte
*p
= string
;
119 if (*p
== (GLubyte
) '\n') {
126 *col
= (pos
- lineStart
) + 1;
128 /* return copy of this line */
129 while (*p
!= 0 && *p
!= '\n')
132 s
= (GLubyte
*) _mesa_malloc(len
+ 1);
133 _mesa_memcpy(s
, lineStart
, len
);
140 static struct program
* _mesa_init_program_struct( GLcontext
*ctx
,
141 struct program
*prog
,
142 GLenum target
, GLuint id
)
146 prog
->Target
= target
;
147 prog
->Resident
= GL_TRUE
;
154 struct program
* _mesa_init_fragment_program( GLcontext
*ctx
,
155 struct fragment_program
*prog
,
156 GLenum target
, GLuint id
)
159 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
164 struct program
* _mesa_init_vertex_program( GLcontext
*ctx
,
165 struct vertex_program
*prog
,
166 GLenum target
, GLuint id
)
169 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
176 * Allocate and initialize a new fragment/vertex program object but
177 * don't put it into the program hash table. Called via
178 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
179 * device driver function to implement OO deriviation with additional
180 * types not understood by this function.
183 * \param id program id/number
184 * \param target program target/type
185 * \return pointer to new program object
188 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
191 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
192 return _mesa_init_vertex_program( ctx
, CALLOC_STRUCT(vertex_program
),
195 case GL_FRAGMENT_PROGRAM_NV
:
196 case GL_FRAGMENT_PROGRAM_ARB
:
197 return _mesa_init_fragment_program( ctx
, CALLOC_STRUCT(fragment_program
),
201 _mesa_problem(ctx
, "bad target in _mesa_new_program");
208 * Delete a program and remove it from the hash table, ignoring the
210 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
211 * by a device driver function.
214 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
219 _mesa_free(prog
->String
);
220 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
221 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
222 struct vertex_program
*vprog
= (struct vertex_program
*) prog
;
223 if (vprog
->Instructions
)
224 _mesa_free(vprog
->Instructions
);
226 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
227 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
228 struct fragment_program
*fprog
= (struct fragment_program
*) prog
;
229 if (fprog
->Instructions
)
230 _mesa_free(fprog
->Instructions
);
231 if (fprog
->Parameters
) {
232 _mesa_free_parameter_list(fprog
->Parameters
);
240 /**********************************************************************/
241 /* Program parameter functions */
242 /**********************************************************************/
244 struct program_parameter_list
*
245 _mesa_new_parameter_list(void)
247 return (struct program_parameter_list
*)
248 _mesa_calloc(sizeof(struct program_parameter_list
));
253 * Free a parameter list and all its parameters
256 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
258 _mesa_free_parameters(paramList
);
259 _mesa_free(paramList
);
264 * Free all the parameters in the given list, but don't free the
265 * paramList structure itself.
268 _mesa_free_parameters(struct program_parameter_list
*paramList
)
271 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
272 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
274 _mesa_free(paramList
->Parameters
);
275 paramList
->NumParameters
= 0;
276 paramList
->Parameters
= NULL
;
281 * Helper function used by the functions below.
284 add_parameter(struct program_parameter_list
*paramList
,
285 const char *name
, const GLfloat values
[4],
286 enum parameter_type type
)
288 const GLuint n
= paramList
->NumParameters
;
290 paramList
->Parameters
= (struct program_parameter
*)
291 _mesa_realloc(paramList
->Parameters
,
292 n
* sizeof(struct program_parameter
),
293 (n
+ 1) * sizeof(struct program_parameter
));
294 if (!paramList
->Parameters
) {
296 paramList
->NumParameters
= 0;
300 paramList
->NumParameters
= n
+ 1;
301 paramList
->Parameters
[n
].Name
= _mesa_strdup(name
);
302 paramList
->Parameters
[n
].Type
= type
;
304 COPY_4V(paramList
->Parameters
[n
].Values
, values
);
311 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
312 * \return index of the new entry in the parameter list
315 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
316 const char *name
, const GLfloat values
[4])
318 return add_parameter(paramList
, name
, values
, NAMED_PARAMETER
);
323 * Add a new unnamed constant to the parameter list.
324 * \param paramList - the parameter list
325 * \param values - four float values
326 * \return index of the new parameter.
329 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
330 const char *name
, const GLfloat values
[4])
332 return add_parameter(paramList
, name
, values
, CONSTANT
);
337 * Add a new unnamed constant to the parameter list.
338 * \param paramList - the parameter list
339 * \param values - four float values
340 * \return index of the new parameter.
343 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
344 const GLfloat values
[4])
346 /* generate a new dummy name */
349 _mesa_sprintf(name
, "constant%d", n
);
352 return add_parameter(paramList
, name
, values
, CONSTANT
);
357 * Add a new state reference to the parameter list.
358 * \param paramList - the parameter list
359 * \param state - an array of 6 state tokens
361 * \return index of the new parameter.
364 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
367 /* XXX Should we parse <stateString> here and produce the parameter's
368 * list of STATE_* tokens here, or in the parser?
372 idx
= add_parameter(paramList
, "Some State", NULL
, STATE
);
375 paramList
->Parameters
[idx
].StateIndexes
[a
] = (enum state_index
) stateTokens
[a
];
382 * Lookup a parameter value by name in the given parameter list.
383 * \return pointer to the float[4] values.
386 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
387 GLsizei nameLen
, const char *name
)
395 /* name is null-terminated */
396 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
397 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
398 return paramList
->Parameters
[i
].Values
;
402 /* name is not null-terminated, use nameLen */
403 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
404 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
405 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
406 return paramList
->Parameters
[i
].Values
;
414 * Lookup a parameter index by name in the given parameter list.
415 * \return index of parameter in the list.
418 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
419 GLsizei nameLen
, const char *name
)
427 /* name is null-terminated */
428 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
429 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
434 /* name is not null-terminated, use nameLen */
435 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
436 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
437 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
446 * Use the list of tokens in the state[] array to find global GL state
447 * and return it in <value>. Usually, four values are returned in <value>
448 * but matrix queries may return as many as 16 values.
449 * This function is used for ARB vertex/fragment programs.
450 * The program parser will produce the state[] values.
453 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
459 /* state[1] is either 0=front or 1=back side */
460 const GLuint face
= (GLuint
) state
[1];
461 /* state[2] is the material attribute */
465 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
467 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
471 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
473 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
477 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
479 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
483 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
485 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
487 case STATE_SHININESS
:
489 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
491 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
497 _mesa_problem(ctx
, "Invalid material state in fetch_state");
504 /* state[1] is the light number */
505 const GLuint ln
= (GLuint
) state
[1];
506 /* state[2] is the light attribute */
509 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
512 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
515 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
518 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
520 case STATE_ATTENUATION
:
521 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
522 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
523 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
524 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
526 case STATE_SPOT_DIRECTION
:
527 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
531 GLfloat eye_z
[] = {0, 0, 1};
533 /* Compute infinite half angle vector:
534 * half-vector = light_position + (0, 0, 1)
535 * and then normalize. w = 0
537 * light.EyePosition.w should be 0 for infinite lights.
539 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
540 NORMALIZE_3FV(value
);
545 _mesa_problem(ctx
, "Invalid light state in fetch_state");
550 case STATE_LIGHTMODEL_AMBIENT
:
551 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
553 case STATE_LIGHTMODEL_SCENECOLOR
:
557 for (i
= 0; i
< 4; i
++) {
558 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
559 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
560 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
566 for (i
= 0; i
< 4; i
++) {
567 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
568 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
569 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
573 case STATE_LIGHTPROD
:
575 const GLuint ln
= (GLuint
) state
[1];
576 const GLuint face
= (GLuint
) state
[2];
578 ASSERT(face
== 0 || face
== 1);
581 for (i
= 0; i
< 3; i
++) {
582 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
583 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
585 /* [3] = material alpha */
586 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
589 for (i
= 0; i
< 3; i
++) {
590 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
591 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
593 /* [3] = material alpha */
594 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
597 for (i
= 0; i
< 3; i
++) {
598 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
599 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
601 /* [3] = material alpha */
602 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
605 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
612 /* state[1] is the texture unit */
613 const GLuint unit
= (GLuint
) state
[1];
614 /* state[2] is the texgen attribute */
616 case STATE_TEXGEN_EYE_S
:
617 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
619 case STATE_TEXGEN_EYE_T
:
620 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
622 case STATE_TEXGEN_EYE_R
:
623 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
625 case STATE_TEXGEN_EYE_Q
:
626 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
628 case STATE_TEXGEN_OBJECT_S
:
629 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
631 case STATE_TEXGEN_OBJECT_T
:
632 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
634 case STATE_TEXGEN_OBJECT_R
:
635 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
637 case STATE_TEXGEN_OBJECT_Q
:
638 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
641 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
646 case STATE_TEXENV_COLOR
:
648 /* state[1] is the texture unit */
649 const GLuint unit
= (GLuint
) state
[1];
650 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
653 case STATE_FOG_COLOR
:
654 COPY_4V(value
, ctx
->Fog
.Color
);
656 case STATE_FOG_PARAMS
:
657 value
[0] = ctx
->Fog
.Density
;
658 value
[1] = ctx
->Fog
.Start
;
659 value
[2] = ctx
->Fog
.End
;
660 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
662 case STATE_CLIPPLANE
:
664 const GLuint plane
= (GLuint
) state
[1];
665 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
668 case STATE_POINT_SIZE
:
669 value
[0] = ctx
->Point
.Size
;
670 value
[1] = ctx
->Point
.MinSize
;
671 value
[2] = ctx
->Point
.MaxSize
;
672 value
[3] = ctx
->Point
.Threshold
;
674 case STATE_POINT_ATTENUATION
:
675 value
[0] = ctx
->Point
.Params
[0];
676 value
[1] = ctx
->Point
.Params
[1];
677 value
[2] = ctx
->Point
.Params
[2];
682 /* state[1] = modelview, projection, texture, etc. */
683 /* state[2] = which texture matrix or program matrix */
684 /* state[3] = first column to fetch */
685 /* state[4] = last column to fetch */
686 /* state[5] = transpose, inverse or invtrans */
688 const GLmatrix
*matrix
;
689 const enum state_index mat
= state
[1];
690 const GLuint index
= (GLuint
) state
[2];
691 const GLuint first
= (GLuint
) state
[3];
692 const GLuint last
= (GLuint
) state
[4];
693 const enum state_index modifier
= state
[5];
696 if (mat
== STATE_MODELVIEW
) {
697 matrix
= ctx
->ModelviewMatrixStack
.Top
;
699 else if (mat
== STATE_PROJECTION
) {
700 matrix
= ctx
->ProjectionMatrixStack
.Top
;
702 else if (mat
== STATE_MVP
) {
703 matrix
= &ctx
->_ModelProjectMatrix
;
705 else if (mat
== STATE_TEXTURE
) {
706 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
708 else if (mat
== STATE_PROGRAM
) {
709 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
712 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
715 if (modifier
== STATE_MATRIX_INVERSE
||
716 modifier
== STATE_MATRIX_INVTRANS
) {
717 /* XXX be sure inverse is up to date */
723 if (modifier
== STATE_MATRIX_TRANSPOSE
||
724 modifier
== STATE_MATRIX_INVTRANS
) {
725 for (i
= 0, row
= first
; row
<= last
; row
++) {
726 value
[i
++] = m
[row
* 4 + 0];
727 value
[i
++] = m
[row
* 4 + 1];
728 value
[i
++] = m
[row
* 4 + 2];
729 value
[i
++] = m
[row
* 4 + 3];
733 for (i
= 0, row
= first
; row
<= last
; row
++) {
734 value
[i
++] = m
[row
+ 0];
735 value
[i
++] = m
[row
+ 4];
736 value
[i
++] = m
[row
+ 8];
737 value
[i
++] = m
[row
+ 12];
742 case STATE_DEPTH_RANGE
:
743 value
[0] = ctx
->Viewport
.Near
; /* near */
744 value
[1] = ctx
->Viewport
.Far
; /* far */
745 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
748 case STATE_FRAGMENT_PROGRAM
:
750 /* state[1] = {STATE_ENV, STATE_LOCAL} */
751 /* state[2] = parameter index */
756 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
760 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
763 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
769 case STATE_VERTEX_PROGRAM
:
771 /* state[1] = {STATE_ENV, STATE_LOCAL} */
772 /* state[2] = parameter index */
777 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
781 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
784 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
790 _mesa_problem(ctx
, "Invalid state in fetch_state");
797 * Loop over all the parameters in a parameter list. If the parameter
798 * is a GL state reference, look up the current value of that state
799 * variable and put it into the parameter's Value[4] array.
800 * This would be called at glBegin time when using a fragment program.
803 _mesa_load_state_parameters(GLcontext
*ctx
,
804 struct program_parameter_list
*paramList
)
811 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
812 if (paramList
->Parameters
[i
].Type
== STATE
) {
813 _mesa_fetch_state(ctx
, paramList
->Parameters
[i
].StateIndexes
,
814 paramList
->Parameters
[i
].Values
);
821 /**********************************************************************/
823 /**********************************************************************/
827 * Bind a program (make it current)
828 * \note Called from the GL API dispatcher by both glBindProgramNV
829 * and glBindProgramARB.
832 _mesa_BindProgram(GLenum target
, GLuint id
)
834 struct program
*prog
;
835 GET_CURRENT_CONTEXT(ctx
);
836 ASSERT_OUTSIDE_BEGIN_END(ctx
);
838 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
840 if ((target
== GL_VERTEX_PROGRAM_NV
841 && ctx
->Extensions
.NV_vertex_program
) ||
842 (target
== GL_VERTEX_PROGRAM_ARB
843 && ctx
->Extensions
.ARB_vertex_program
)) {
844 if (ctx
->VertexProgram
.Current
&&
845 ctx
->VertexProgram
.Current
->Base
.Id
== id
)
847 /* decrement refcount on previously bound vertex program */
848 if (ctx
->VertexProgram
.Current
) {
849 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
850 /* and delete if refcount goes below one */
851 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0) {
852 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
853 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
857 else if ((target
== GL_FRAGMENT_PROGRAM_NV
858 && ctx
->Extensions
.NV_fragment_program
) ||
859 (target
== GL_FRAGMENT_PROGRAM_ARB
860 && ctx
->Extensions
.ARB_fragment_program
)) {
861 if (ctx
->FragmentProgram
.Current
&&
862 ctx
->FragmentProgram
.Current
->Base
.Id
== id
)
864 /* decrement refcount on previously bound fragment program */
865 if (ctx
->FragmentProgram
.Current
) {
866 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
867 /* and delete if refcount goes below one */
868 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0) {
869 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
870 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
875 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
879 /* NOTE: binding to a non-existant program is not an error.
880 * That's supposed to be caught in glBegin.
883 /* default program */
885 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
886 prog
= ctx
->Shared
->DefaultVertexProgram
;
888 prog
= ctx
->Shared
->DefaultFragmentProgram
;
891 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
893 if (prog
->Target
== 0) {
894 /* prog was allocated with glGenProgramsNV */
895 prog
->Target
= target
;
897 else if (prog
->Target
!= target
) {
898 _mesa_error(ctx
, GL_INVALID_OPERATION
,
899 "glBindProgramNV/ARB(target mismatch)");
904 /* allocate a new program now */
905 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
907 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
911 prog
->Target
= target
;
912 prog
->Resident
= GL_TRUE
;
914 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
919 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
920 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
922 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
923 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
929 if (ctx
->Driver
.BindProgram
)
930 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
935 * Delete a list of programs.
936 * \note Not compiled into display lists.
937 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
940 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
943 GET_CURRENT_CONTEXT(ctx
);
944 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
947 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
951 for (i
= 0; i
< n
; i
++) {
953 struct program
*prog
= (struct program
*)
954 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
956 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
957 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
958 if (ctx
->VertexProgram
.Current
&&
959 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
960 /* unbind this currently bound program */
961 _mesa_BindProgram(prog
->Target
, 0);
964 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
965 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
966 if (ctx
->FragmentProgram
.Current
&&
967 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
968 /* unbind this currently bound program */
969 _mesa_BindProgram(prog
->Target
, 0);
973 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
977 if (prog
->RefCount
<= 0) {
978 ctx
->Driver
.DeleteProgram(ctx
, prog
);
982 /* This is necessary as we can't tell from HashLookup
983 * whether the entry exists with data == 0, or if it
984 * doesn't exist at all. As GenPrograms creates the first
985 * case below, need to call Remove() to avoid memory leak:
987 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
995 * Generate a list of new program identifiers.
996 * \note Not compiled into display lists.
997 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1000 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1004 GET_CURRENT_CONTEXT(ctx
);
1005 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1008 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1015 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1017 for (i
= 0; i
< (GLuint
) n
; i
++) {
1018 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, 0);
1021 /* Return the program names */
1022 for (i
= 0; i
< (GLuint
) n
; i
++) {
1029 * Determine if id names a program.
1030 * \note Not compiled into display lists.
1031 * \note Called from both glIsProgramNV and glIsProgramARB.
1032 * \param id is the program identifier
1033 * \return GL_TRUE if id is a program, else GL_FALSE.
1035 GLboolean GLAPIENTRY
1036 _mesa_IsProgram(GLuint id
)
1038 GET_CURRENT_CONTEXT(ctx
);
1039 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1044 if (_mesa_HashLookup(ctx
->Shared
->Programs
, id
))
1052 /**********************************************************************/
1053 /* GL_MESA_program_debug extension */
1054 /**********************************************************************/
1059 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1062 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1067 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1070 GET_CURRENT_CONTEXT(ctx
);
1073 case GL_FRAGMENT_PROGRAM_ARB
:
1074 if (!ctx
->Extensions
.ARB_fragment_program
) {
1075 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1078 ctx
->FragmentProgram
.Callback
= callback
;
1079 ctx
->FragmentProgram
.CallbackData
= data
;
1081 case GL_FRAGMENT_PROGRAM_NV
:
1082 if (!ctx
->Extensions
.NV_fragment_program
) {
1083 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1086 ctx
->FragmentProgram
.Callback
= callback
;
1087 ctx
->FragmentProgram
.CallbackData
= data
;
1089 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1090 if (!ctx
->Extensions
.ARB_vertex_program
&&
1091 !ctx
->Extensions
.NV_vertex_program
) {
1092 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1095 ctx
->VertexProgram
.Callback
= callback
;
1096 ctx
->VertexProgram
.CallbackData
= data
;
1099 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1107 glGetProgramRegisterfvMESA(GLenum target
,
1108 GLsizei len
, const GLubyte
*registerName
,
1111 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1116 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1117 GLsizei len
, const GLubyte
*registerName
,
1121 GET_CURRENT_CONTEXT(ctx
);
1123 /* We _should_ be inside glBegin/glEnd */
1125 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1126 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1131 /* make null-terminated copy of registerName */
1132 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1133 _mesa_memcpy(reg
, registerName
, len
);
1137 case GL_VERTEX_PROGRAM_NV
:
1138 if (!ctx
->Extensions
.ARB_vertex_program
&&
1139 !ctx
->Extensions
.NV_vertex_program
) {
1140 _mesa_error(ctx
, GL_INVALID_ENUM
,
1141 "glGetProgramRegisterfvMESA(target)");
1144 if (!ctx
->VertexProgram
._Enabled
) {
1145 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1146 "glGetProgramRegisterfvMESA");
1149 /* GL_NV_vertex_program */
1150 if (reg
[0] == 'R') {
1152 GLint i
= _mesa_atoi(reg
+ 1);
1153 if (i
>= (GLint
)ctx
->Const
.MaxVertexProgramTemps
) {
1154 _mesa_error(ctx
, GL_INVALID_VALUE
,
1155 "glGetProgramRegisterfvMESA(registerName)");
1158 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1160 else if (reg
[0] == 'v' && reg
[1] == '[') {
1161 /* Vertex Input attribute */
1163 for (i
= 0; i
< ctx
->Const
.MaxVertexProgramAttribs
; i
++) {
1164 const char *name
= _mesa_nv_vertex_input_register_name(i
);
1166 sprintf(number
, "%d", i
);
1167 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
1168 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
1169 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
1173 _mesa_error(ctx
, GL_INVALID_VALUE
,
1174 "glGetProgramRegisterfvMESA(registerName)");
1177 else if (reg
[0] == 'o' && reg
[1] == '[') {
1178 /* Vertex output attribute */
1180 /* GL_ARB_vertex_program */
1181 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1185 _mesa_error(ctx
, GL_INVALID_VALUE
,
1186 "glGetProgramRegisterfvMESA(registerName)");
1190 case GL_FRAGMENT_PROGRAM_ARB
:
1191 if (!ctx
->Extensions
.ARB_fragment_program
) {
1192 _mesa_error(ctx
, GL_INVALID_ENUM
,
1193 "glGetProgramRegisterfvMESA(target)");
1196 if (!ctx
->FragmentProgram
._Enabled
) {
1197 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1198 "glGetProgramRegisterfvMESA");
1203 case GL_FRAGMENT_PROGRAM_NV
:
1204 if (!ctx
->Extensions
.NV_fragment_program
) {
1205 _mesa_error(ctx
, GL_INVALID_ENUM
,
1206 "glGetProgramRegisterfvMESA(target)");
1209 if (!ctx
->FragmentProgram
._Enabled
) {
1210 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1211 "glGetProgramRegisterfvMESA");
1214 if (reg
[0] == 'R') {
1216 GLint i
= _mesa_atoi(reg
+ 1);
1217 if (i
>= (GLint
)ctx
->Const
.MaxFragmentProgramTemps
) {
1218 _mesa_error(ctx
, GL_INVALID_VALUE
,
1219 "glGetProgramRegisterfvMESA(registerName)");
1222 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
1224 else if (reg
[0] == 'f' && reg
[1] == '[') {
1225 /* Fragment input attribute */
1227 for (i
= 0; i
< ctx
->Const
.MaxFragmentProgramAttribs
; i
++) {
1228 const char *name
= _mesa_nv_fragment_input_register_name(i
);
1229 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
1230 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
1234 _mesa_error(ctx
, GL_INVALID_VALUE
,
1235 "glGetProgramRegisterfvMESA(registerName)");
1238 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
1239 /* Fragment output color */
1240 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
]);
1242 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
1243 /* Fragment output color */
1244 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLH
]);
1246 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
1247 /* Fragment output depth */
1248 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
]);
1251 /* try user-defined identifiers */
1252 const GLfloat
*value
= _mesa_lookup_parameter_value(
1253 ctx
->FragmentProgram
.Current
->Parameters
, -1, reg
);
1258 _mesa_error(ctx
, GL_INVALID_VALUE
,
1259 "glGetProgramRegisterfvMESA(registerName)");
1265 _mesa_error(ctx
, GL_INVALID_ENUM
,
1266 "glGetProgramRegisterfvMESA(target)");