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 /**********************************************************************/
49 /* A pointer to this dummy program is put into the hash table when
50 * glGenPrograms is called.
52 static struct program DummyProgram
;
56 * Init context's vertex/fragment program state
59 _mesa_init_program(GLcontext
*ctx
)
63 ctx
->Program
.ErrorPos
= -1;
64 ctx
->Program
.ErrorString
= _mesa_strdup("");
66 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
67 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
68 ctx
->VertexProgram
.PointSizeEnabled
= GL_FALSE
;
69 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
70 ctx
->VertexProgram
.Current
= (struct vertex_program
*) ctx
->Shared
->DefaultVertexProgram
;
71 assert(ctx
->VertexProgram
.Current
);
72 ctx
->VertexProgram
.Current
->Base
.RefCount
++;
73 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
74 ctx
->VertexProgram
.TrackMatrix
[i
] = GL_NONE
;
75 ctx
->VertexProgram
.TrackMatrixTransform
[i
] = GL_IDENTITY_NV
;
79 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
80 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
81 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) ctx
->Shared
->DefaultFragmentProgram
;
82 assert(ctx
->FragmentProgram
.Current
);
83 ctx
->FragmentProgram
.Current
->Base
.RefCount
++;
89 * Free a context's vertex/fragment program state
92 _mesa_free_program_data(GLcontext
*ctx
)
94 #if FEATURE_NV_vertex_program
95 if (ctx
->VertexProgram
.Current
) {
96 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
97 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0)
98 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
101 #if FEATURE_NV_fragment_program
102 if (ctx
->FragmentProgram
.Current
) {
103 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
104 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0)
105 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
108 _mesa_free((void *) ctx
->Program
.ErrorString
);
115 * Set the vertex/fragment program error state (position and error string).
116 * This is generally called from within the parsers.
119 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
121 ctx
->Program
.ErrorPos
= pos
;
122 _mesa_free((void *) ctx
->Program
.ErrorString
);
125 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
130 * Find the line number and column for 'pos' within 'string'.
131 * Return a copy of the line which contains 'pos'. Free the line with
133 * \param string the program string
134 * \param pos the position within the string
135 * \param line returns the line number corresponding to 'pos'.
136 * \param col returns the column number corresponding to 'pos'.
137 * \return copy of the line containing 'pos'.
140 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
141 GLint
*line
, GLint
*col
)
143 const GLubyte
*lineStart
= string
;
144 const GLubyte
*p
= string
;
151 if (*p
== (GLubyte
) '\n') {
158 *col
= (pos
- lineStart
) + 1;
160 /* return copy of this line */
161 while (*p
!= 0 && *p
!= '\n')
164 s
= (GLubyte
*) _mesa_malloc(len
+ 1);
165 _mesa_memcpy(s
, lineStart
, len
);
173 * Initialize a new vertex/fragment program object.
175 static struct program
*
176 _mesa_init_program_struct( GLcontext
*ctx
, struct program
*prog
,
177 GLenum target
, GLuint id
)
182 prog
->Target
= target
;
183 prog
->Resident
= GL_TRUE
;
192 * Initialize a new fragment program object.
195 _mesa_init_fragment_program( GLcontext
*ctx
, struct fragment_program
*prog
,
196 GLenum target
, GLuint id
)
199 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
206 * Initialize a new vertex program object.
209 _mesa_init_vertex_program( GLcontext
*ctx
, struct vertex_program
*prog
,
210 GLenum target
, GLuint id
)
213 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
220 * Allocate and initialize a new fragment/vertex program object but
221 * don't put it into the program hash table. Called via
222 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
223 * device driver function to implement OO deriviation with additional
224 * types not understood by this function.
227 * \param id program id/number
228 * \param target program target/type
229 * \return pointer to new program object
232 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
235 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
236 return _mesa_init_vertex_program( ctx
, CALLOC_STRUCT(vertex_program
),
238 case GL_FRAGMENT_PROGRAM_NV
:
239 case GL_FRAGMENT_PROGRAM_ARB
:
240 return _mesa_init_fragment_program( ctx
, CALLOC_STRUCT(fragment_program
),
243 _mesa_problem(ctx
, "bad target in _mesa_new_program");
250 * Delete a program and remove it from the hash table, ignoring the
252 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
253 * by a device driver function.
256 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
262 _mesa_free(prog
->String
);
263 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
264 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
265 struct vertex_program
*vprog
= (struct vertex_program
*) prog
;
266 if (vprog
->Instructions
)
267 _mesa_free(vprog
->Instructions
);
268 if (vprog
->Parameters
)
269 _mesa_free_parameter_list(vprog
->Parameters
);
271 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
272 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
273 struct fragment_program
*fprog
= (struct fragment_program
*) prog
;
274 if (fprog
->Instructions
)
275 _mesa_free(fprog
->Instructions
);
276 if (fprog
->Parameters
)
277 _mesa_free_parameter_list(fprog
->Parameters
);
284 /**********************************************************************/
285 /* Program parameter functions */
286 /**********************************************************************/
288 struct program_parameter_list
*
289 _mesa_new_parameter_list(void)
291 return (struct program_parameter_list
*)
292 _mesa_calloc(sizeof(struct program_parameter_list
));
297 * Free a parameter list and all its parameters
300 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
302 _mesa_free_parameters(paramList
);
303 _mesa_free(paramList
);
308 * Free all the parameters in the given list, but don't free the
309 * paramList structure itself.
312 _mesa_free_parameters(struct program_parameter_list
*paramList
)
315 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
316 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
318 _mesa_free(paramList
->Parameters
);
319 paramList
->NumParameters
= 0;
320 paramList
->Parameters
= NULL
;
325 * Helper function used by the functions below.
328 add_parameter(struct program_parameter_list
*paramList
,
329 const char *name
, const GLfloat values
[4],
330 enum parameter_type type
)
332 const GLuint n
= paramList
->NumParameters
;
334 paramList
->Parameters
= (struct program_parameter
*)
335 _mesa_realloc(paramList
->Parameters
,
336 n
* sizeof(struct program_parameter
),
337 (n
+ 1) * sizeof(struct program_parameter
));
338 if (!paramList
->Parameters
) {
340 paramList
->NumParameters
= 0;
344 paramList
->NumParameters
= n
+ 1;
345 paramList
->Parameters
[n
].Name
= _mesa_strdup(name
);
346 paramList
->Parameters
[n
].Type
= type
;
348 COPY_4V(paramList
->Parameters
[n
].Values
, values
);
355 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
356 * \return index of the new entry in the parameter list
359 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
360 const char *name
, const GLfloat values
[4])
362 return add_parameter(paramList
, name
, values
, NAMED_PARAMETER
);
367 * Add a new unnamed constant to the parameter list.
368 * \param paramList - the parameter list
369 * \param values - four float values
370 * \return index of the new parameter.
373 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
374 const char *name
, const GLfloat values
[4])
376 return add_parameter(paramList
, name
, values
, CONSTANT
);
381 * Add a new unnamed constant to the parameter list.
382 * \param paramList - the parameter list
383 * \param values - four float values
384 * \return index of the new parameter.
387 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
388 const GLfloat values
[4])
390 /* generate a new dummy name */
393 _mesa_sprintf(name
, "constant%d", n
);
396 return add_parameter(paramList
, name
, values
, CONSTANT
);
401 * Add a new state reference to the parameter list.
402 * \param paramList - the parameter list
403 * \param state - an array of 6 state tokens
405 * \return index of the new parameter.
408 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
411 /* XXX Should we parse <stateString> here and produce the parameter's
412 * list of STATE_* tokens here, or in the parser?
416 idx
= add_parameter(paramList
, "Some State", NULL
, STATE
);
419 paramList
->Parameters
[idx
].StateIndexes
[a
] = (enum state_index
) stateTokens
[a
];
426 * Lookup a parameter value by name in the given parameter list.
427 * \return pointer to the float[4] values.
430 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
431 GLsizei nameLen
, const char *name
)
439 /* name is null-terminated */
440 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
441 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
442 return paramList
->Parameters
[i
].Values
;
446 /* name is not null-terminated, use nameLen */
447 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
448 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
449 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
450 return paramList
->Parameters
[i
].Values
;
458 * Lookup a parameter index by name in the given parameter list.
459 * \return index of parameter in the list.
462 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
463 GLsizei nameLen
, const char *name
)
471 /* name is null-terminated */
472 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
473 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
478 /* name is not null-terminated, use nameLen */
479 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
480 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
481 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
490 * Use the list of tokens in the state[] array to find global GL state
491 * and return it in <value>. Usually, four values are returned in <value>
492 * but matrix queries may return as many as 16 values.
493 * This function is used for ARB vertex/fragment programs.
494 * The program parser will produce the state[] values.
497 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
503 /* state[1] is either 0=front or 1=back side */
504 const GLuint face
= (GLuint
) state
[1];
505 /* state[2] is the material attribute */
509 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
511 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
515 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
517 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
521 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
523 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
527 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
529 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
531 case STATE_SHININESS
:
533 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
535 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
541 _mesa_problem(ctx
, "Invalid material state in fetch_state");
548 /* state[1] is the light number */
549 const GLuint ln
= (GLuint
) state
[1];
550 /* state[2] is the light attribute */
553 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
556 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
559 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
562 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
564 case STATE_ATTENUATION
:
565 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
566 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
567 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
568 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
570 case STATE_SPOT_DIRECTION
:
571 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
575 GLfloat eye_z
[] = {0, 0, 1};
577 /* Compute infinite half angle vector:
578 * half-vector = light_position + (0, 0, 1)
579 * and then normalize. w = 0
581 * light.EyePosition.w should be 0 for infinite lights.
583 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
584 NORMALIZE_3FV(value
);
589 _mesa_problem(ctx
, "Invalid light state in fetch_state");
594 case STATE_LIGHTMODEL_AMBIENT
:
595 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
597 case STATE_LIGHTMODEL_SCENECOLOR
:
601 for (i
= 0; i
< 4; i
++) {
602 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
603 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
604 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
610 for (i
= 0; i
< 4; i
++) {
611 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
612 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
613 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
617 case STATE_LIGHTPROD
:
619 const GLuint ln
= (GLuint
) state
[1];
620 const GLuint face
= (GLuint
) state
[2];
622 ASSERT(face
== 0 || face
== 1);
625 for (i
= 0; i
< 3; i
++) {
626 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
627 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
629 /* [3] = material alpha */
630 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
633 for (i
= 0; i
< 3; i
++) {
634 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
635 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
637 /* [3] = material alpha */
638 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
641 for (i
= 0; i
< 3; i
++) {
642 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
643 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
645 /* [3] = material alpha */
646 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
649 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
656 /* state[1] is the texture unit */
657 const GLuint unit
= (GLuint
) state
[1];
658 /* state[2] is the texgen attribute */
660 case STATE_TEXGEN_EYE_S
:
661 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
663 case STATE_TEXGEN_EYE_T
:
664 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
666 case STATE_TEXGEN_EYE_R
:
667 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
669 case STATE_TEXGEN_EYE_Q
:
670 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
672 case STATE_TEXGEN_OBJECT_S
:
673 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
675 case STATE_TEXGEN_OBJECT_T
:
676 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
678 case STATE_TEXGEN_OBJECT_R
:
679 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
681 case STATE_TEXGEN_OBJECT_Q
:
682 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
685 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
690 case STATE_TEXENV_COLOR
:
692 /* state[1] is the texture unit */
693 const GLuint unit
= (GLuint
) state
[1];
694 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
697 case STATE_FOG_COLOR
:
698 COPY_4V(value
, ctx
->Fog
.Color
);
700 case STATE_FOG_PARAMS
:
701 value
[0] = ctx
->Fog
.Density
;
702 value
[1] = ctx
->Fog
.Start
;
703 value
[2] = ctx
->Fog
.End
;
704 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
706 case STATE_CLIPPLANE
:
708 const GLuint plane
= (GLuint
) state
[1];
709 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
712 case STATE_POINT_SIZE
:
713 value
[0] = ctx
->Point
.Size
;
714 value
[1] = ctx
->Point
.MinSize
;
715 value
[2] = ctx
->Point
.MaxSize
;
716 value
[3] = ctx
->Point
.Threshold
;
718 case STATE_POINT_ATTENUATION
:
719 value
[0] = ctx
->Point
.Params
[0];
720 value
[1] = ctx
->Point
.Params
[1];
721 value
[2] = ctx
->Point
.Params
[2];
726 /* state[1] = modelview, projection, texture, etc. */
727 /* state[2] = which texture matrix or program matrix */
728 /* state[3] = first column to fetch */
729 /* state[4] = last column to fetch */
730 /* state[5] = transpose, inverse or invtrans */
732 const GLmatrix
*matrix
;
733 const enum state_index mat
= state
[1];
734 const GLuint index
= (GLuint
) state
[2];
735 const GLuint first
= (GLuint
) state
[3];
736 const GLuint last
= (GLuint
) state
[4];
737 const enum state_index modifier
= state
[5];
740 if (mat
== STATE_MODELVIEW
) {
741 matrix
= ctx
->ModelviewMatrixStack
.Top
;
743 else if (mat
== STATE_PROJECTION
) {
744 matrix
= ctx
->ProjectionMatrixStack
.Top
;
746 else if (mat
== STATE_MVP
) {
747 matrix
= &ctx
->_ModelProjectMatrix
;
749 else if (mat
== STATE_TEXTURE
) {
750 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
752 else if (mat
== STATE_PROGRAM
) {
753 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
756 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
759 if (modifier
== STATE_MATRIX_INVERSE
||
760 modifier
== STATE_MATRIX_INVTRANS
) {
761 /* XXX be sure inverse is up to date */
767 if (modifier
== STATE_MATRIX_TRANSPOSE
||
768 modifier
== STATE_MATRIX_INVTRANS
) {
769 for (i
= 0, row
= first
; row
<= last
; row
++) {
770 value
[i
++] = m
[row
* 4 + 0];
771 value
[i
++] = m
[row
* 4 + 1];
772 value
[i
++] = m
[row
* 4 + 2];
773 value
[i
++] = m
[row
* 4 + 3];
777 for (i
= 0, row
= first
; row
<= last
; row
++) {
778 value
[i
++] = m
[row
+ 0];
779 value
[i
++] = m
[row
+ 4];
780 value
[i
++] = m
[row
+ 8];
781 value
[i
++] = m
[row
+ 12];
786 case STATE_DEPTH_RANGE
:
787 value
[0] = ctx
->Viewport
.Near
; /* near */
788 value
[1] = ctx
->Viewport
.Far
; /* far */
789 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
792 case STATE_FRAGMENT_PROGRAM
:
794 /* state[1] = {STATE_ENV, STATE_LOCAL} */
795 /* state[2] = parameter index */
796 const int idx
= (int) state
[2];
799 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
802 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
805 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
811 case STATE_VERTEX_PROGRAM
:
813 /* state[1] = {STATE_ENV, STATE_LOCAL} */
814 /* state[2] = parameter index */
815 const int idx
= (int) state
[2];
818 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
821 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
824 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
830 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
837 * Loop over all the parameters in a parameter list. If the parameter
838 * is a GL state reference, look up the current value of that state
839 * variable and put it into the parameter's Value[4] array.
840 * This would be called at glBegin time when using a fragment program.
843 _mesa_load_state_parameters(GLcontext
*ctx
,
844 struct program_parameter_list
*paramList
)
851 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
852 if (paramList
->Parameters
[i
].Type
== STATE
) {
853 _mesa_fetch_state(ctx
, paramList
->Parameters
[i
].StateIndexes
,
854 paramList
->Parameters
[i
].Values
);
861 /**********************************************************************/
863 /**********************************************************************/
867 * Bind a program (make it current)
868 * \note Called from the GL API dispatcher by both glBindProgramNV
869 * and glBindProgramARB.
872 _mesa_BindProgram(GLenum target
, GLuint id
)
874 struct program
*prog
;
875 GET_CURRENT_CONTEXT(ctx
);
876 ASSERT_OUTSIDE_BEGIN_END(ctx
);
878 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
880 if ((target
== GL_VERTEX_PROGRAM_NV
881 && ctx
->Extensions
.NV_vertex_program
) ||
882 (target
== GL_VERTEX_PROGRAM_ARB
883 && ctx
->Extensions
.ARB_vertex_program
)) {
884 /*** Vertex program binding ***/
885 struct vertex_program
*curProg
= ctx
->VertexProgram
.Current
;
886 if (curProg
->Base
.Id
== id
) {
887 /* binding same program - no change */
890 if (curProg
->Base
.Id
!= 0) {
891 /* decrement refcount on previously bound vertex program */
892 curProg
->Base
.RefCount
--;
893 /* and delete if refcount goes below one */
894 if (curProg
->Base
.RefCount
<= 0) {
895 ASSERT(curProg
->Base
.DeletePending
);
896 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
897 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
901 else if ((target
== GL_FRAGMENT_PROGRAM_NV
902 && ctx
->Extensions
.NV_fragment_program
) ||
903 (target
== GL_FRAGMENT_PROGRAM_ARB
904 && ctx
->Extensions
.ARB_fragment_program
)) {
905 /*** Fragment program binding ***/
906 struct fragment_program
*curProg
= ctx
->FragmentProgram
.Current
;
907 if (curProg
->Base
.Id
== id
) {
908 /* binding same program - no change */
911 if (curProg
->Base
.Id
!= 0) {
912 /* decrement refcount on previously bound fragment program */
913 curProg
->Base
.RefCount
--;
914 /* and delete if refcount goes below one */
915 if (curProg
->Base
.RefCount
<= 0) {
916 ASSERT(curProg
->Base
.DeletePending
);
917 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
918 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
923 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
927 /* NOTE: binding to a non-existant program is not an error.
928 * That's supposed to be caught in glBegin.
931 /* Bind default program */
933 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
934 prog
= ctx
->Shared
->DefaultVertexProgram
;
936 prog
= ctx
->Shared
->DefaultFragmentProgram
;
939 /* Bind user program */
940 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
941 if (!prog
|| prog
== &DummyProgram
) {
942 /* allocate a new program now */
943 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
945 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
948 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
950 else if (prog
->Target
!= target
) {
951 _mesa_error(ctx
, GL_INVALID_OPERATION
,
952 "glBindProgramNV/ARB(target mismatch)");
958 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
959 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
961 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
962 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
965 /* Never null pointers */
966 ASSERT(ctx
->VertexProgram
.Current
);
967 ASSERT(ctx
->FragmentProgram
.Current
);
972 if (ctx
->Driver
.BindProgram
)
973 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
978 * Delete a list of programs.
979 * \note Not compiled into display lists.
980 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
983 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
986 GET_CURRENT_CONTEXT(ctx
);
987 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
990 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
994 for (i
= 0; i
< n
; i
++) {
996 struct program
*prog
= (struct program
*)
997 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
998 if (prog
== &DummyProgram
) {
999 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1002 /* Unbind program if necessary */
1003 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
1004 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
1005 if (ctx
->VertexProgram
.Current
&&
1006 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
1007 /* unbind this currently bound program */
1008 _mesa_BindProgram(prog
->Target
, 0);
1011 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
1012 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1013 if (ctx
->FragmentProgram
.Current
&&
1014 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
1015 /* unbind this currently bound program */
1016 _mesa_BindProgram(prog
->Target
, 0);
1020 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
1023 /* Decrement reference count if not already marked for delete */
1024 if (!prog
->DeletePending
) {
1025 prog
->DeletePending
= GL_TRUE
;
1028 if (prog
->RefCount
<= 0) {
1029 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1030 ctx
->Driver
.DeleteProgram(ctx
, prog
);
1039 * Generate a list of new program identifiers.
1040 * \note Not compiled into display lists.
1041 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1044 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1048 GET_CURRENT_CONTEXT(ctx
);
1049 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1052 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1059 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1061 /* Insert pointer to dummy program as placeholder */
1062 for (i
= 0; i
< (GLuint
) n
; i
++) {
1063 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, &DummyProgram
);
1066 /* Return the program names */
1067 for (i
= 0; i
< (GLuint
) n
; i
++) {
1074 * Determine if id names a vertex or fragment program.
1075 * \note Not compiled into display lists.
1076 * \note Called from both glIsProgramNV and glIsProgramARB.
1077 * \param id is the program identifier
1078 * \return GL_TRUE if id is a program, else GL_FALSE.
1080 GLboolean GLAPIENTRY
1081 _mesa_IsProgram(GLuint id
)
1083 GET_CURRENT_CONTEXT(ctx
);
1084 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1089 if (_mesa_HashLookup(ctx
->Shared
->Programs
, id
))
1097 /**********************************************************************/
1098 /* GL_MESA_program_debug extension */
1099 /**********************************************************************/
1104 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1107 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1112 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1115 GET_CURRENT_CONTEXT(ctx
);
1118 case GL_FRAGMENT_PROGRAM_ARB
:
1119 if (!ctx
->Extensions
.ARB_fragment_program
) {
1120 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1123 ctx
->FragmentProgram
.Callback
= callback
;
1124 ctx
->FragmentProgram
.CallbackData
= data
;
1126 case GL_FRAGMENT_PROGRAM_NV
:
1127 if (!ctx
->Extensions
.NV_fragment_program
) {
1128 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1131 ctx
->FragmentProgram
.Callback
= callback
;
1132 ctx
->FragmentProgram
.CallbackData
= data
;
1134 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1135 if (!ctx
->Extensions
.ARB_vertex_program
&&
1136 !ctx
->Extensions
.NV_vertex_program
) {
1137 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1140 ctx
->VertexProgram
.Callback
= callback
;
1141 ctx
->VertexProgram
.CallbackData
= data
;
1144 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1152 glGetProgramRegisterfvMESA(GLenum target
,
1153 GLsizei len
, const GLubyte
*registerName
,
1156 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1161 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1162 GLsizei len
, const GLubyte
*registerName
,
1166 GET_CURRENT_CONTEXT(ctx
);
1168 /* We _should_ be inside glBegin/glEnd */
1170 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1171 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1176 /* make null-terminated copy of registerName */
1177 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1178 _mesa_memcpy(reg
, registerName
, len
);
1182 case GL_VERTEX_PROGRAM_NV
:
1183 if (!ctx
->Extensions
.ARB_vertex_program
&&
1184 !ctx
->Extensions
.NV_vertex_program
) {
1185 _mesa_error(ctx
, GL_INVALID_ENUM
,
1186 "glGetProgramRegisterfvMESA(target)");
1189 if (!ctx
->VertexProgram
._Enabled
) {
1190 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1191 "glGetProgramRegisterfvMESA");
1194 /* GL_NV_vertex_program */
1195 if (reg
[0] == 'R') {
1197 GLint i
= _mesa_atoi(reg
+ 1);
1198 if (i
>= (GLint
)ctx
->Const
.MaxVertexProgramTemps
) {
1199 _mesa_error(ctx
, GL_INVALID_VALUE
,
1200 "glGetProgramRegisterfvMESA(registerName)");
1203 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1205 else if (reg
[0] == 'v' && reg
[1] == '[') {
1206 /* Vertex Input attribute */
1208 for (i
= 0; i
< ctx
->Const
.MaxVertexProgramAttribs
; i
++) {
1209 const char *name
= _mesa_nv_vertex_input_register_name(i
);
1211 sprintf(number
, "%d", i
);
1212 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
1213 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
1214 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
1218 _mesa_error(ctx
, GL_INVALID_VALUE
,
1219 "glGetProgramRegisterfvMESA(registerName)");
1222 else if (reg
[0] == 'o' && reg
[1] == '[') {
1223 /* Vertex output attribute */
1225 /* GL_ARB_vertex_program */
1226 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1230 _mesa_error(ctx
, GL_INVALID_VALUE
,
1231 "glGetProgramRegisterfvMESA(registerName)");
1235 case GL_FRAGMENT_PROGRAM_ARB
:
1236 if (!ctx
->Extensions
.ARB_fragment_program
) {
1237 _mesa_error(ctx
, GL_INVALID_ENUM
,
1238 "glGetProgramRegisterfvMESA(target)");
1241 if (!ctx
->FragmentProgram
._Enabled
) {
1242 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1243 "glGetProgramRegisterfvMESA");
1248 case GL_FRAGMENT_PROGRAM_NV
:
1249 if (!ctx
->Extensions
.NV_fragment_program
) {
1250 _mesa_error(ctx
, GL_INVALID_ENUM
,
1251 "glGetProgramRegisterfvMESA(target)");
1254 if (!ctx
->FragmentProgram
._Enabled
) {
1255 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1256 "glGetProgramRegisterfvMESA");
1259 if (reg
[0] == 'R') {
1261 GLint i
= _mesa_atoi(reg
+ 1);
1262 if (i
>= (GLint
)ctx
->Const
.MaxFragmentProgramTemps
) {
1263 _mesa_error(ctx
, GL_INVALID_VALUE
,
1264 "glGetProgramRegisterfvMESA(registerName)");
1267 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
1269 else if (reg
[0] == 'f' && reg
[1] == '[') {
1270 /* Fragment input attribute */
1272 for (i
= 0; i
< ctx
->Const
.MaxFragmentProgramAttribs
; i
++) {
1273 const char *name
= _mesa_nv_fragment_input_register_name(i
);
1274 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
1275 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
1279 _mesa_error(ctx
, GL_INVALID_VALUE
,
1280 "glGetProgramRegisterfvMESA(registerName)");
1283 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
1284 /* Fragment output color */
1285 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
]);
1287 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
1288 /* Fragment output color */
1289 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLH
]);
1291 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
1292 /* Fragment output depth */
1293 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
]);
1296 /* try user-defined identifiers */
1297 const GLfloat
*value
= _mesa_lookup_parameter_value(
1298 ctx
->FragmentProgram
.Current
->Parameters
, -1, reg
);
1303 _mesa_error(ctx
, GL_INVALID_VALUE
,
1304 "glGetProgramRegisterfvMESA(registerName)");
1310 _mesa_error(ctx
, GL_INVALID_ENUM
,
1311 "glGetProgramRegisterfvMESA(target)");