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
= 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
);
141 static struct program
* _mesa_init_program_struct( GLcontext
*ctx
,
142 struct program
*prog
,
143 GLenum target
, GLuint id
)
147 prog
->Target
= target
;
148 prog
->Resident
= GL_TRUE
;
155 struct program
* _mesa_init_fragment_program( GLcontext
*ctx
,
156 struct fragment_program
*prog
,
157 GLenum target
, GLuint id
)
160 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
165 struct program
* _mesa_init_vertex_program( GLcontext
*ctx
,
166 struct vertex_program
*prog
,
167 GLenum target
, GLuint id
)
170 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
177 * Allocate and initialize a new fragment/vertex program object but
178 * don't put it into the program hash table. Called via
179 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
180 * device driver function to implement OO deriviation with additional
181 * types not understood by this function.
184 * \param id program id/number
185 * \param target program target/type
186 * \return pointer to new program object
189 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
192 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
193 return _mesa_init_vertex_program( ctx
, CALLOC_STRUCT(vertex_program
),
196 case GL_FRAGMENT_PROGRAM_NV
:
197 case GL_FRAGMENT_PROGRAM_ARB
:
198 return _mesa_init_fragment_program( ctx
, CALLOC_STRUCT(fragment_program
),
202 _mesa_problem(ctx
, "bad target in _mesa_new_program");
209 * Delete a program and remove it from the hash table, ignoring the
211 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
212 * by a device driver function.
215 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
220 _mesa_free(prog
->String
);
221 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
222 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
223 struct vertex_program
*vprog
= (struct vertex_program
*) prog
;
224 if (vprog
->Instructions
)
225 _mesa_free(vprog
->Instructions
);
227 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
228 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
229 struct fragment_program
*fprog
= (struct fragment_program
*) prog
;
230 if (fprog
->Instructions
)
231 _mesa_free(fprog
->Instructions
);
232 if (fprog
->Parameters
) {
233 _mesa_free_parameter_list(fprog
->Parameters
);
241 /**********************************************************************/
242 /* Program parameter functions */
243 /**********************************************************************/
245 struct program_parameter_list
*
246 _mesa_new_parameter_list(void)
248 return (struct program_parameter_list
*)
249 _mesa_calloc(sizeof(struct program_parameter_list
));
254 * Free a parameter list and all its parameters
257 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
259 _mesa_free_parameters(paramList
);
260 _mesa_free(paramList
);
265 * Free all the parameters in the given list, but don't free the
266 * paramList structure itself.
269 _mesa_free_parameters(struct program_parameter_list
*paramList
)
272 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
273 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
275 _mesa_free(paramList
->Parameters
);
276 paramList
->NumParameters
= 0;
277 paramList
->Parameters
= NULL
;
282 * Helper function used by the functions below.
285 add_parameter(struct program_parameter_list
*paramList
,
286 const char *name
, const GLfloat values
[4],
287 enum parameter_type type
)
289 const GLuint n
= paramList
->NumParameters
;
291 paramList
->Parameters
= (struct program_parameter
*)
292 _mesa_realloc(paramList
->Parameters
,
293 n
* sizeof(struct program_parameter
),
294 (n
+ 1) * sizeof(struct program_parameter
));
295 if (!paramList
->Parameters
) {
297 paramList
->NumParameters
= 0;
301 paramList
->NumParameters
= n
+ 1;
302 paramList
->Parameters
[n
].Name
= _mesa_strdup(name
);
303 paramList
->Parameters
[n
].Type
= type
;
305 COPY_4V(paramList
->Parameters
[n
].Values
, values
);
312 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
313 * \return index of the new entry in the parameter list
316 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
317 const char *name
, const GLfloat values
[4])
319 return add_parameter(paramList
, name
, values
, NAMED_PARAMETER
);
324 * Add a new unnamed constant to the parameter list.
325 * \param paramList - the parameter list
326 * \param values - four float values
327 * \return index of the new parameter.
330 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
331 const char *name
, const GLfloat values
[4])
333 return add_parameter(paramList
, name
, values
, CONSTANT
);
338 * Add a new unnamed constant to the parameter list.
339 * \param paramList - the parameter list
340 * \param values - four float values
341 * \return index of the new parameter.
344 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
345 const GLfloat values
[4])
347 /* generate a new dummy name */
350 _mesa_sprintf(name
, "constant%d", n
);
353 return add_parameter(paramList
, name
, values
, CONSTANT
);
358 * Add a new state reference to the parameter list.
359 * \param paramList - the parameter list
360 * \param state - an array of 6 state tokens
362 * \return index of the new parameter.
365 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
368 /* XXX Should we parse <stateString> here and produce the parameter's
369 * list of STATE_* tokens here, or in the parser?
373 idx
= add_parameter(paramList
, "Some State", NULL
, STATE
);
376 paramList
->Parameters
[idx
].StateIndexes
[a
] = (enum state_index
) stateTokens
[a
];
383 * Lookup a parameter value by name in the given parameter list.
384 * \return pointer to the float[4] values.
387 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
388 GLsizei nameLen
, const char *name
)
396 /* name is null-terminated */
397 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
398 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
399 return paramList
->Parameters
[i
].Values
;
403 /* name is not null-terminated, use nameLen */
404 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
405 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
406 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
407 return paramList
->Parameters
[i
].Values
;
415 * Lookup a parameter index by name in the given parameter list.
416 * \return index of parameter in the list.
419 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
420 GLsizei nameLen
, const char *name
)
428 /* name is null-terminated */
429 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
430 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
435 /* name is not null-terminated, use nameLen */
436 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
437 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
438 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
447 * Use the list of tokens in the state[] array to find global GL state
448 * and return it in <value>. Usually, four values are returned in <value>
449 * but matrix queries may return as many as 16 values.
450 * This function is used for ARB vertex/fragment programs.
451 * The program parser will produce the state[] values.
454 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
460 /* state[1] is either 0=front or 1=back side */
461 const GLuint face
= (GLuint
) state
[1];
462 /* state[2] is the material attribute */
466 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
468 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
472 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
474 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
478 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
480 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
484 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
486 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
488 case STATE_SHININESS
:
490 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
492 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
498 _mesa_problem(ctx
, "Invalid material state in fetch_state");
505 /* state[1] is the light number */
506 const GLuint ln
= (GLuint
) state
[1];
507 /* state[2] is the light attribute */
510 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
513 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
516 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
519 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
521 case STATE_ATTENUATION
:
522 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
523 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
524 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
525 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
527 case STATE_SPOT_DIRECTION
:
528 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
532 GLfloat eye_z
[] = {0, 0, 1};
534 /* Compute infinite half angle vector:
535 * half-vector = light_position + (0, 0, 1)
536 * and then normalize. w = 0
538 * light.EyePosition.w should be 0 for infinite lights.
540 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
541 NORMALIZE_3FV(value
);
546 _mesa_problem(ctx
, "Invalid light state in fetch_state");
551 case STATE_LIGHTMODEL_AMBIENT
:
552 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
554 case STATE_LIGHTMODEL_SCENECOLOR
:
558 for (i
= 0; i
< 4; i
++) {
559 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
560 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
561 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
567 for (i
= 0; i
< 4; i
++) {
568 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
569 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
570 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
574 case STATE_LIGHTPROD
:
576 const GLuint ln
= (GLuint
) state
[1];
577 const GLuint face
= (GLuint
) state
[2];
579 ASSERT(face
== 0 || face
== 1);
582 for (i
= 0; i
< 3; i
++) {
583 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
584 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
586 /* [3] = material alpha */
587 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
590 for (i
= 0; i
< 3; i
++) {
591 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
592 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
594 /* [3] = material alpha */
595 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
598 for (i
= 0; i
< 3; i
++) {
599 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
600 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
602 /* [3] = material alpha */
603 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
606 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
613 /* state[1] is the texture unit */
614 const GLuint unit
= (GLuint
) state
[1];
615 /* state[2] is the texgen attribute */
617 case STATE_TEXGEN_EYE_S
:
618 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
620 case STATE_TEXGEN_EYE_T
:
621 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
623 case STATE_TEXGEN_EYE_R
:
624 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
626 case STATE_TEXGEN_EYE_Q
:
627 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
629 case STATE_TEXGEN_OBJECT_S
:
630 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
632 case STATE_TEXGEN_OBJECT_T
:
633 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
635 case STATE_TEXGEN_OBJECT_R
:
636 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
638 case STATE_TEXGEN_OBJECT_Q
:
639 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
642 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
647 case STATE_TEXENV_COLOR
:
649 /* state[1] is the texture unit */
650 const GLuint unit
= (GLuint
) state
[1];
651 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
654 case STATE_FOG_COLOR
:
655 COPY_4V(value
, ctx
->Fog
.Color
);
657 case STATE_FOG_PARAMS
:
658 value
[0] = ctx
->Fog
.Density
;
659 value
[1] = ctx
->Fog
.Start
;
660 value
[2] = ctx
->Fog
.End
;
661 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
663 case STATE_CLIPPLANE
:
665 const GLuint plane
= (GLuint
) state
[1];
666 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
669 case STATE_POINT_SIZE
:
670 value
[0] = ctx
->Point
.Size
;
671 value
[1] = ctx
->Point
.MinSize
;
672 value
[2] = ctx
->Point
.MaxSize
;
673 value
[3] = ctx
->Point
.Threshold
;
675 case STATE_POINT_ATTENUATION
:
676 value
[0] = ctx
->Point
.Params
[0];
677 value
[1] = ctx
->Point
.Params
[1];
678 value
[2] = ctx
->Point
.Params
[2];
683 /* state[1] = modelview, projection, texture, etc. */
684 /* state[2] = which texture matrix or program matrix */
685 /* state[3] = first column to fetch */
686 /* state[4] = last column to fetch */
687 /* state[5] = transpose, inverse or invtrans */
689 const GLmatrix
*matrix
;
690 const enum state_index mat
= state
[1];
691 const GLuint index
= (GLuint
) state
[2];
692 const GLuint first
= (GLuint
) state
[3];
693 const GLuint last
= (GLuint
) state
[4];
694 const enum state_index modifier
= state
[5];
697 if (mat
== STATE_MODELVIEW
) {
698 matrix
= ctx
->ModelviewMatrixStack
.Top
;
700 else if (mat
== STATE_PROJECTION
) {
701 matrix
= ctx
->ProjectionMatrixStack
.Top
;
703 else if (mat
== STATE_MVP
) {
704 matrix
= &ctx
->_ModelProjectMatrix
;
706 else if (mat
== STATE_TEXTURE
) {
707 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
709 else if (mat
== STATE_PROGRAM
) {
710 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
713 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
716 if (modifier
== STATE_MATRIX_INVERSE
||
717 modifier
== STATE_MATRIX_INVTRANS
) {
718 /* XXX be sure inverse is up to date */
724 if (modifier
== STATE_MATRIX_TRANSPOSE
||
725 modifier
== STATE_MATRIX_INVTRANS
) {
726 for (i
= 0, row
= first
; row
<= last
; row
++) {
727 value
[i
++] = m
[row
* 4 + 0];
728 value
[i
++] = m
[row
* 4 + 1];
729 value
[i
++] = m
[row
* 4 + 2];
730 value
[i
++] = m
[row
* 4 + 3];
734 for (i
= 0, row
= first
; row
<= last
; row
++) {
735 value
[i
++] = m
[row
+ 0];
736 value
[i
++] = m
[row
+ 4];
737 value
[i
++] = m
[row
+ 8];
738 value
[i
++] = m
[row
+ 12];
743 case STATE_DEPTH_RANGE
:
744 value
[0] = ctx
->Viewport
.Near
; /* near */
745 value
[1] = ctx
->Viewport
.Far
; /* far */
746 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
749 case STATE_FRAGMENT_PROGRAM
:
751 /* state[1] = {STATE_ENV, STATE_LOCAL} */
752 /* state[2] = parameter index */
757 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
761 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
764 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
770 case STATE_VERTEX_PROGRAM
:
772 /* state[1] = {STATE_ENV, STATE_LOCAL} */
773 /* state[2] = parameter index */
778 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
782 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
785 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
791 _mesa_problem(ctx
, "Invalid state in fetch_state");
798 * Loop over all the parameters in a parameter list. If the parameter
799 * is a GL state reference, look up the current value of that state
800 * variable and put it into the parameter's Value[4] array.
801 * This would be called at glBegin time when using a fragment program.
804 _mesa_load_state_parameters(GLcontext
*ctx
,
805 struct program_parameter_list
*paramList
)
812 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
813 if (paramList
->Parameters
[i
].Type
== STATE
) {
814 _mesa_fetch_state(ctx
, paramList
->Parameters
[i
].StateIndexes
,
815 paramList
->Parameters
[i
].Values
);
822 /**********************************************************************/
824 /**********************************************************************/
828 * Bind a program (make it current)
829 * \note Called from the GL API dispatcher by both glBindProgramNV
830 * and glBindProgramARB.
833 _mesa_BindProgram(GLenum target
, GLuint id
)
835 struct program
*prog
;
836 GET_CURRENT_CONTEXT(ctx
);
837 ASSERT_OUTSIDE_BEGIN_END(ctx
);
839 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
841 if ((target
== GL_VERTEX_PROGRAM_NV
842 && ctx
->Extensions
.NV_vertex_program
) ||
843 (target
== GL_VERTEX_PROGRAM_ARB
844 && ctx
->Extensions
.ARB_vertex_program
)) {
845 if (ctx
->VertexProgram
.Current
&&
846 ctx
->VertexProgram
.Current
->Base
.Id
== id
)
848 /* decrement refcount on previously bound vertex program */
849 if (ctx
->VertexProgram
.Current
) {
850 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
851 /* and delete if refcount goes below one */
852 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0) {
853 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
854 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
858 else if ((target
== GL_FRAGMENT_PROGRAM_NV
859 && ctx
->Extensions
.NV_fragment_program
) ||
860 (target
== GL_FRAGMENT_PROGRAM_ARB
861 && ctx
->Extensions
.ARB_fragment_program
)) {
862 if (ctx
->FragmentProgram
.Current
&&
863 ctx
->FragmentProgram
.Current
->Base
.Id
== id
)
865 /* decrement refcount on previously bound fragment program */
866 if (ctx
->FragmentProgram
.Current
) {
867 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
868 /* and delete if refcount goes below one */
869 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0) {
870 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
871 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
876 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
880 /* NOTE: binding to a non-existant program is not an error.
881 * That's supposed to be caught in glBegin.
884 /* default program */
886 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
887 prog
= ctx
->Shared
->DefaultVertexProgram
;
889 prog
= ctx
->Shared
->DefaultFragmentProgram
;
892 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
894 if (prog
->Target
== 0) {
895 /* prog was allocated with glGenProgramsNV */
896 prog
->Target
= target
;
898 else if (prog
->Target
!= target
) {
899 _mesa_error(ctx
, GL_INVALID_OPERATION
,
900 "glBindProgramNV/ARB(target mismatch)");
905 /* allocate a new program now */
906 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
908 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
912 prog
->Target
= target
;
913 prog
->Resident
= GL_TRUE
;
915 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
920 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
921 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
923 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
924 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
930 if (ctx
->Driver
.BindProgram
)
931 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
936 * Delete a list of programs.
937 * \note Not compiled into display lists.
938 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
941 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
944 GET_CURRENT_CONTEXT(ctx
);
945 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
948 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
952 for (i
= 0; i
< n
; i
++) {
954 struct program
*prog
= (struct program
*)
955 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
957 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
958 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
959 if (ctx
->VertexProgram
.Current
&&
960 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
961 /* unbind this currently bound program */
962 _mesa_BindProgram(prog
->Target
, 0);
965 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
966 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
967 if (ctx
->FragmentProgram
.Current
&&
968 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
969 /* unbind this currently bound program */
970 _mesa_BindProgram(prog
->Target
, 0);
974 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
978 if (prog
->RefCount
<= 0) {
979 ctx
->Driver
.DeleteProgram(ctx
, prog
);
983 /* This is necessary as we can't tell from HashLookup
984 * whether the entry exists with data == 0, or if it
985 * doesn't exist at all. As GenPrograms creates the first
986 * case below, need to call Remove() to avoid memory leak:
988 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
996 * Generate a list of new program identifiers.
997 * \note Not compiled into display lists.
998 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1001 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1005 GET_CURRENT_CONTEXT(ctx
);
1006 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1009 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1016 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1018 for (i
= 0; i
< (GLuint
) n
; i
++) {
1019 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, 0);
1022 /* Return the program names */
1023 for (i
= 0; i
< (GLuint
) n
; i
++) {
1030 * Determine if id names a program.
1031 * \note Not compiled into display lists.
1032 * \note Called from both glIsProgramNV and glIsProgramARB.
1033 * \param id is the program identifier
1034 * \return GL_TRUE if id is a program, else GL_FALSE.
1036 GLboolean GLAPIENTRY
1037 _mesa_IsProgram(GLuint id
)
1039 GET_CURRENT_CONTEXT(ctx
);
1040 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1045 if (_mesa_HashLookup(ctx
->Shared
->Programs
, id
))
1053 /**********************************************************************/
1054 /* GL_MESA_program_debug extension */
1055 /**********************************************************************/
1060 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1063 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1068 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1071 GET_CURRENT_CONTEXT(ctx
);
1074 case GL_FRAGMENT_PROGRAM_ARB
:
1075 if (!ctx
->Extensions
.ARB_fragment_program
) {
1076 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1079 ctx
->FragmentProgram
.Callback
= callback
;
1080 ctx
->FragmentProgram
.CallbackData
= data
;
1082 case GL_FRAGMENT_PROGRAM_NV
:
1083 if (!ctx
->Extensions
.NV_fragment_program
) {
1084 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1087 ctx
->FragmentProgram
.Callback
= callback
;
1088 ctx
->FragmentProgram
.CallbackData
= data
;
1090 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1091 if (!ctx
->Extensions
.ARB_vertex_program
&&
1092 !ctx
->Extensions
.NV_vertex_program
) {
1093 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1096 ctx
->VertexProgram
.Callback
= callback
;
1097 ctx
->VertexProgram
.CallbackData
= data
;
1100 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1108 glGetProgramRegisterfvMESA(GLenum target
,
1109 GLsizei len
, const GLubyte
*registerName
,
1112 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1117 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1118 GLsizei len
, const GLubyte
*registerName
,
1122 GET_CURRENT_CONTEXT(ctx
);
1124 /* We _should_ be inside glBegin/glEnd */
1126 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1127 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1132 /* make null-terminated copy of registerName */
1133 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1134 _mesa_memcpy(reg
, registerName
, len
);
1138 case GL_VERTEX_PROGRAM_NV
:
1139 if (!ctx
->Extensions
.ARB_vertex_program
&&
1140 !ctx
->Extensions
.NV_vertex_program
) {
1141 _mesa_error(ctx
, GL_INVALID_ENUM
,
1142 "glGetProgramRegisterfvMESA(target)");
1145 if (!ctx
->VertexProgram
.Enabled
) {
1146 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1147 "glGetProgramRegisterfvMESA");
1150 /* GL_NV_vertex_program */
1151 if (reg
[0] == 'R') {
1153 GLint i
= _mesa_atoi(reg
+ 1);
1154 if (i
>= (GLint
)ctx
->Const
.MaxVertexProgramTemps
) {
1155 _mesa_error(ctx
, GL_INVALID_VALUE
,
1156 "glGetProgramRegisterfvMESA(registerName)");
1159 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1161 else if (reg
[0] == 'v' && reg
[1] == '[') {
1162 /* Vertex Input attribute */
1164 for (i
= 0; i
< ctx
->Const
.MaxVertexProgramAttribs
; i
++) {
1165 const char *name
= _mesa_nv_vertex_input_register_name(i
);
1167 sprintf(number
, "%d", i
);
1168 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
1169 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
1170 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
1174 _mesa_error(ctx
, GL_INVALID_VALUE
,
1175 "glGetProgramRegisterfvMESA(registerName)");
1178 else if (reg
[0] == 'o' && reg
[1] == '[') {
1179 /* Vertex output attribute */
1181 /* GL_ARB_vertex_program */
1182 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1186 _mesa_error(ctx
, GL_INVALID_VALUE
,
1187 "glGetProgramRegisterfvMESA(registerName)");
1191 case GL_FRAGMENT_PROGRAM_ARB
:
1192 if (!ctx
->Extensions
.ARB_fragment_program
) {
1193 _mesa_error(ctx
, GL_INVALID_ENUM
,
1194 "glGetProgramRegisterfvMESA(target)");
1197 if (!ctx
->FragmentProgram
.Enabled
) {
1198 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1199 "glGetProgramRegisterfvMESA");
1204 case GL_FRAGMENT_PROGRAM_NV
:
1205 if (!ctx
->Extensions
.NV_fragment_program
) {
1206 _mesa_error(ctx
, GL_INVALID_ENUM
,
1207 "glGetProgramRegisterfvMESA(target)");
1210 if (!ctx
->FragmentProgram
.Enabled
) {
1211 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1212 "glGetProgramRegisterfvMESA");
1215 if (reg
[0] == 'R') {
1217 GLint i
= _mesa_atoi(reg
+ 1);
1218 if (i
>= (GLint
)ctx
->Const
.MaxFragmentProgramTemps
) {
1219 _mesa_error(ctx
, GL_INVALID_VALUE
,
1220 "glGetProgramRegisterfvMESA(registerName)");
1223 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
1225 else if (reg
[0] == 'f' && reg
[1] == '[') {
1226 /* Fragment input attribute */
1228 for (i
= 0; i
< ctx
->Const
.MaxFragmentProgramAttribs
; i
++) {
1229 const char *name
= _mesa_nv_fragment_input_register_name(i
);
1230 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
1231 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
1235 _mesa_error(ctx
, GL_INVALID_VALUE
,
1236 "glGetProgramRegisterfvMESA(registerName)");
1239 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
1240 /* Fragment output color */
1241 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
]);
1243 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
1244 /* Fragment output color */
1245 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLH
]);
1247 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
1248 /* Fragment output depth */
1249 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
]);
1252 /* try user-defined identifiers */
1253 const GLfloat
*value
= _mesa_lookup_parameter_value(
1254 ctx
->FragmentProgram
.Current
->Parameters
, -1, reg
);
1259 _mesa_error(ctx
, GL_INVALID_VALUE
,
1260 "glGetProgramRegisterfvMESA(registerName)");
1266 _mesa_error(ctx
, GL_INVALID_ENUM
,
1267 "glGetProgramRegisterfvMESA(target)");