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
;
152 fprintf(stderr
, "%s %x %x\n", __FUNCTION__
, target
, prog
);
156 struct program
* _mesa_init_fragment_program( GLcontext
*ctx
,
157 struct fragment_program
*prog
,
158 GLenum target
, GLuint id
)
161 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
166 struct program
* _mesa_init_vertex_program( GLcontext
*ctx
,
167 struct vertex_program
*prog
,
168 GLenum target
, GLuint id
)
171 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
178 * Allocate and initialize a new fragment/vertex program object but
179 * don't put it into the program hash table. Called via
180 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
181 * device driver function to implement OO deriviation with additional
182 * types not understood by this function.
185 * \param id program id/number
186 * \param target program target/type
187 * \return pointer to new program object
190 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
192 fprintf(stderr
, "%s\n", __FUNCTION__
);
194 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
195 return _mesa_init_vertex_program( ctx
, CALLOC_STRUCT(vertex_program
),
198 case GL_FRAGMENT_PROGRAM_NV
:
199 case GL_FRAGMENT_PROGRAM_ARB
:
200 return _mesa_init_fragment_program( ctx
, CALLOC_STRUCT(fragment_program
),
204 _mesa_problem(ctx
, "bad target in _mesa_new_program");
211 * Delete a program and remove it from the hash table, ignoring the
213 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
214 * by a device driver function.
217 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
222 _mesa_free(prog
->String
);
223 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
224 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
225 struct vertex_program
*vprog
= (struct vertex_program
*) prog
;
226 if (vprog
->Instructions
)
227 _mesa_free(vprog
->Instructions
);
229 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
230 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
231 struct fragment_program
*fprog
= (struct fragment_program
*) prog
;
232 if (fprog
->Instructions
)
233 _mesa_free(fprog
->Instructions
);
234 if (fprog
->Parameters
) {
235 _mesa_free_parameter_list(fprog
->Parameters
);
243 /**********************************************************************/
244 /* Program parameter functions */
245 /**********************************************************************/
247 struct program_parameter_list
*
248 _mesa_new_parameter_list(void)
250 return (struct program_parameter_list
*)
251 _mesa_calloc(sizeof(struct program_parameter_list
));
256 * Free a parameter list and all its parameters
259 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
261 _mesa_free_parameters(paramList
);
262 _mesa_free(paramList
);
267 * Free all the parameters in the given list, but don't free the
268 * paramList structure itself.
271 _mesa_free_parameters(struct program_parameter_list
*paramList
)
274 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
275 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
277 _mesa_free(paramList
->Parameters
);
278 paramList
->NumParameters
= 0;
279 paramList
->Parameters
= NULL
;
284 * Helper function used by the functions below.
287 add_parameter(struct program_parameter_list
*paramList
,
288 const char *name
, const GLfloat values
[4],
289 enum parameter_type type
)
291 const GLuint n
= paramList
->NumParameters
;
293 paramList
->Parameters
= (struct program_parameter
*)
294 _mesa_realloc(paramList
->Parameters
,
295 n
* sizeof(struct program_parameter
),
296 (n
+ 1) * sizeof(struct program_parameter
));
297 if (!paramList
->Parameters
) {
299 paramList
->NumParameters
= 0;
303 paramList
->NumParameters
= n
+ 1;
304 paramList
->Parameters
[n
].Name
= _mesa_strdup(name
);
305 paramList
->Parameters
[n
].Type
= type
;
307 COPY_4V(paramList
->Parameters
[n
].Values
, values
);
314 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
315 * \return index of the new entry in the parameter list
318 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
319 const char *name
, const GLfloat values
[4])
321 return add_parameter(paramList
, name
, values
, NAMED_PARAMETER
);
326 * Add a new unnamed constant to the parameter list.
327 * \param paramList - the parameter list
328 * \param values - four float values
329 * \return index of the new parameter.
332 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
333 const char *name
, const GLfloat values
[4])
335 return add_parameter(paramList
, name
, values
, CONSTANT
);
340 * Add a new unnamed constant to the parameter list.
341 * \param paramList - the parameter list
342 * \param values - four float values
343 * \return index of the new parameter.
346 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
347 const GLfloat values
[4])
349 /* generate a new dummy name */
352 _mesa_sprintf(name
, "constant%d", n
);
355 return add_parameter(paramList
, name
, values
, CONSTANT
);
360 * Add a new state reference to the parameter list.
361 * \param paramList - the parameter list
362 * \param state - an array of 6 state tokens
364 * \return index of the new parameter.
367 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
370 /* XXX Should we parse <stateString> here and produce the parameter's
371 * list of STATE_* tokens here, or in the parser?
375 idx
= add_parameter(paramList
, "Some State", NULL
, STATE
);
378 paramList
->Parameters
[idx
].StateIndexes
[a
] = (enum state_index
) stateTokens
[a
];
385 * Lookup a parameter value by name in the given parameter list.
386 * \return pointer to the float[4] values.
389 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
390 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
)
424 /* name is null-terminated */
425 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
426 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
431 /* name is not null-terminated, use nameLen */
432 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
433 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
434 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
443 * Use the list of tokens in the state[] array to find global GL state
444 * and return it in <value>. Usually, four values are returned in <value>
445 * but matrix queries may return as many as 16 values.
446 * This function is used for ARB vertex/fragment programs.
447 * The program parser will produce the state[] values.
450 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
456 /* state[1] is either 0=front or 1=back side */
457 const GLuint face
= (GLuint
) state
[1];
458 /* state[2] is the material attribute */
462 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
464 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
468 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
470 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
474 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
476 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
480 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
482 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
484 case STATE_SHININESS
:
486 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
488 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
494 _mesa_problem(ctx
, "Invalid material state in fetch_state");
501 /* state[1] is the light number */
502 const GLuint ln
= (GLuint
) state
[1];
503 /* state[2] is the light attribute */
506 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
509 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
512 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
515 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
517 case STATE_ATTENUATION
:
518 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
519 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
520 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
521 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
523 case STATE_SPOT_DIRECTION
:
524 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
528 GLfloat eye_z
[] = {0, 0, 1};
530 /* Compute infinite half angle vector:
531 * half-vector = light_position + (0, 0, 1)
532 * and then normalize. w = 0
534 * light.EyePosition.w should be 0 for infinite lights.
536 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
537 NORMALIZE_3FV(value
);
542 _mesa_problem(ctx
, "Invalid light state in fetch_state");
547 case STATE_LIGHTMODEL_AMBIENT
:
548 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
550 case STATE_LIGHTMODEL_SCENECOLOR
:
554 for (i
= 0; i
< 4; i
++) {
555 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
556 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
557 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
563 for (i
= 0; i
< 4; i
++) {
564 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
565 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
566 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
570 case STATE_LIGHTPROD
:
572 const GLuint ln
= (GLuint
) state
[1];
573 const GLuint face
= (GLuint
) state
[2];
575 ASSERT(face
== 0 || face
== 1);
578 for (i
= 0; i
< 3; i
++) {
579 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
580 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
582 /* [3] = material alpha */
583 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
586 for (i
= 0; i
< 3; i
++) {
587 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
588 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
590 /* [3] = material alpha */
591 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
594 for (i
= 0; i
< 3; i
++) {
595 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
596 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
598 /* [3] = material alpha */
599 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
602 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
609 /* state[1] is the texture unit */
610 const GLuint unit
= (GLuint
) state
[1];
611 /* state[2] is the texgen attribute */
613 case STATE_TEXGEN_EYE_S
:
614 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
616 case STATE_TEXGEN_EYE_T
:
617 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
619 case STATE_TEXGEN_EYE_R
:
620 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
622 case STATE_TEXGEN_EYE_Q
:
623 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
625 case STATE_TEXGEN_OBJECT_S
:
626 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
628 case STATE_TEXGEN_OBJECT_T
:
629 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
631 case STATE_TEXGEN_OBJECT_R
:
632 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
634 case STATE_TEXGEN_OBJECT_Q
:
635 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
638 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
643 case STATE_TEXENV_COLOR
:
645 /* state[1] is the texture unit */
646 const GLuint unit
= (GLuint
) state
[1];
647 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
650 case STATE_FOG_COLOR
:
651 COPY_4V(value
, ctx
->Fog
.Color
);
653 case STATE_FOG_PARAMS
:
654 value
[0] = ctx
->Fog
.Density
;
655 value
[1] = ctx
->Fog
.Start
;
656 value
[2] = ctx
->Fog
.End
;
657 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
659 case STATE_CLIPPLANE
:
661 const GLuint plane
= (GLuint
) state
[1];
662 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
665 case STATE_POINT_SIZE
:
666 value
[0] = ctx
->Point
.Size
;
667 value
[1] = ctx
->Point
.MinSize
;
668 value
[2] = ctx
->Point
.MaxSize
;
669 value
[3] = ctx
->Point
.Threshold
;
671 case STATE_POINT_ATTENUATION
:
672 value
[0] = ctx
->Point
.Params
[0];
673 value
[1] = ctx
->Point
.Params
[1];
674 value
[2] = ctx
->Point
.Params
[2];
679 /* state[1] = modelview, projection, texture, etc. */
680 /* state[2] = which texture matrix or program matrix */
681 /* state[3] = first column to fetch */
682 /* state[4] = last column to fetch */
683 /* state[5] = transpose, inverse or invtrans */
685 const GLmatrix
*matrix
;
686 const enum state_index mat
= state
[1];
687 const GLuint index
= (GLuint
) state
[2];
688 const GLuint first
= (GLuint
) state
[3];
689 const GLuint last
= (GLuint
) state
[4];
690 const enum state_index modifier
= state
[5];
693 if (mat
== STATE_MODELVIEW
) {
694 matrix
= ctx
->ModelviewMatrixStack
.Top
;
696 else if (mat
== STATE_PROJECTION
) {
697 matrix
= ctx
->ProjectionMatrixStack
.Top
;
699 else if (mat
== STATE_MVP
) {
700 matrix
= &ctx
->_ModelProjectMatrix
;
702 else if (mat
== STATE_TEXTURE
) {
703 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
705 else if (mat
== STATE_PROGRAM
) {
706 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
709 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
712 if (modifier
== STATE_MATRIX_INVERSE
||
713 modifier
== STATE_MATRIX_INVTRANS
) {
714 /* XXX be sure inverse is up to date */
720 if (modifier
== STATE_MATRIX_TRANSPOSE
||
721 modifier
== STATE_MATRIX_INVTRANS
) {
722 for (i
= 0, row
= first
; row
<= last
; row
++) {
723 value
[i
++] = m
[row
* 4 + 0];
724 value
[i
++] = m
[row
* 4 + 1];
725 value
[i
++] = m
[row
* 4 + 2];
726 value
[i
++] = m
[row
* 4 + 3];
730 for (i
= 0, row
= first
; row
<= last
; row
++) {
731 value
[i
++] = m
[row
+ 0];
732 value
[i
++] = m
[row
+ 4];
733 value
[i
++] = m
[row
+ 8];
734 value
[i
++] = m
[row
+ 12];
739 case STATE_DEPTH_RANGE
:
740 value
[0] = ctx
->Viewport
.Near
; /* near */
741 value
[1] = ctx
->Viewport
.Far
; /* far */
742 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
745 case STATE_FRAGMENT_PROGRAM
:
747 /* state[1] = {STATE_ENV, STATE_LOCAL} */
748 /* state[2] = parameter index */
753 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
757 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
760 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
766 case STATE_VERTEX_PROGRAM
:
768 /* state[1] = {STATE_ENV, STATE_LOCAL} */
769 /* state[2] = parameter index */
774 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
778 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
781 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
787 _mesa_problem(ctx
, "Invalid state in fetch_state");
794 * Loop over all the parameters in a parameter list. If the parameter
795 * is a GL state reference, look up the current value of that state
796 * variable and put it into the parameter's Value[4] array.
797 * This would be called at glBegin time when using a fragment program.
800 _mesa_load_state_parameters(GLcontext
*ctx
,
801 struct program_parameter_list
*paramList
)
804 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
805 if (paramList
->Parameters
[i
].Type
== STATE
) {
806 _mesa_fetch_state(ctx
, paramList
->Parameters
[i
].StateIndexes
,
807 paramList
->Parameters
[i
].Values
);
814 /**********************************************************************/
816 /**********************************************************************/
820 * Bind a program (make it current)
821 * \note Called from the GL API dispatcher by both glBindProgramNV
822 * and glBindProgramARB.
825 _mesa_BindProgram(GLenum target
, GLuint id
)
827 struct program
*prog
;
828 GET_CURRENT_CONTEXT(ctx
);
829 ASSERT_OUTSIDE_BEGIN_END(ctx
);
831 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
833 if ((target
== GL_VERTEX_PROGRAM_NV
834 && ctx
->Extensions
.NV_vertex_program
) ||
835 (target
== GL_VERTEX_PROGRAM_ARB
836 && ctx
->Extensions
.ARB_vertex_program
)) {
837 if (ctx
->VertexProgram
.Current
&&
838 ctx
->VertexProgram
.Current
->Base
.Id
== id
)
840 /* decrement refcount on previously bound vertex program */
841 if (ctx
->VertexProgram
.Current
) {
842 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
843 /* and delete if refcount goes below one */
844 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0) {
845 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
846 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
850 else if ((target
== GL_FRAGMENT_PROGRAM_NV
851 && ctx
->Extensions
.NV_fragment_program
) ||
852 (target
== GL_FRAGMENT_PROGRAM_ARB
853 && ctx
->Extensions
.ARB_fragment_program
)) {
854 if (ctx
->FragmentProgram
.Current
&&
855 ctx
->FragmentProgram
.Current
->Base
.Id
== id
)
857 /* decrement refcount on previously bound fragment program */
858 if (ctx
->FragmentProgram
.Current
) {
859 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
860 /* and delete if refcount goes below one */
861 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0) {
862 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
863 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
868 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
872 /* NOTE: binding to a non-existant program is not an error.
873 * That's supposed to be caught in glBegin.
876 /* default program */
878 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
879 prog
= ctx
->Shared
->DefaultVertexProgram
;
881 prog
= ctx
->Shared
->DefaultFragmentProgram
;
884 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
886 if (prog
->Target
== 0) {
887 /* prog was allocated with glGenProgramsNV */
888 prog
->Target
= target
;
890 else if (prog
->Target
!= target
) {
891 _mesa_error(ctx
, GL_INVALID_OPERATION
,
892 "glBindProgramNV/ARB(target mismatch)");
897 /* allocate a new program now */
898 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
900 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
904 prog
->Target
= target
;
905 prog
->Resident
= GL_TRUE
;
907 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
912 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
913 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
915 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
916 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
922 if (ctx
->Driver
.BindProgram
)
923 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
928 * Delete a list of programs.
929 * \note Not compiled into display lists.
930 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
933 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
936 GET_CURRENT_CONTEXT(ctx
);
937 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
940 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
944 for (i
= 0; i
< n
; i
++) {
946 struct program
*prog
= (struct program
*)
947 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
949 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
950 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
951 if (ctx
->VertexProgram
.Current
&&
952 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
953 /* unbind this currently bound program */
954 _mesa_BindProgram(prog
->Target
, 0);
957 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
958 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
959 if (ctx
->FragmentProgram
.Current
&&
960 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
961 /* unbind this currently bound program */
962 _mesa_BindProgram(prog
->Target
, 0);
966 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
970 if (prog
->RefCount
<= 0) {
971 ctx
->Driver
.DeleteProgram(ctx
, prog
);
975 /* This is necessary as we can't tell from HashLookup
976 * whether the entry exists with data == 0, or if it
977 * doesn't exist at all. As GenPrograms creates the first
978 * case below, need to call Remove() to avoid memory leak:
980 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
988 * Generate a list of new program identifiers.
989 * \note Not compiled into display lists.
990 * \note Called by both glGenProgramsNV and glGenProgramsARB.
993 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
997 GET_CURRENT_CONTEXT(ctx
);
998 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1001 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1008 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1010 for (i
= 0; i
< (GLuint
) n
; i
++) {
1011 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, 0);
1014 /* Return the program names */
1015 for (i
= 0; i
< (GLuint
) n
; i
++) {
1022 * Determine if id names a program.
1023 * \note Not compiled into display lists.
1024 * \note Called from both glIsProgramNV and glIsProgramARB.
1025 * \param id is the program identifier
1026 * \return GL_TRUE if id is a program, else GL_FALSE.
1028 GLboolean GLAPIENTRY
1029 _mesa_IsProgram(GLuint id
)
1031 GET_CURRENT_CONTEXT(ctx
);
1032 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1037 if (_mesa_HashLookup(ctx
->Shared
->Programs
, id
))
1045 /**********************************************************************/
1046 /* GL_MESA_program_debug extension */
1047 /**********************************************************************/
1052 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1055 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1060 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1063 GET_CURRENT_CONTEXT(ctx
);
1066 case GL_FRAGMENT_PROGRAM_ARB
:
1067 if (!ctx
->Extensions
.ARB_fragment_program
) {
1068 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1071 ctx
->FragmentProgram
.Callback
= callback
;
1072 ctx
->FragmentProgram
.CallbackData
= data
;
1074 case GL_FRAGMENT_PROGRAM_NV
:
1075 if (!ctx
->Extensions
.NV_fragment_program
) {
1076 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1079 ctx
->FragmentProgram
.Callback
= callback
;
1080 ctx
->FragmentProgram
.CallbackData
= data
;
1082 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1083 if (!ctx
->Extensions
.ARB_vertex_program
&&
1084 !ctx
->Extensions
.NV_vertex_program
) {
1085 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1088 ctx
->VertexProgram
.Callback
= callback
;
1089 ctx
->VertexProgram
.CallbackData
= data
;
1092 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1100 glGetProgramRegisterfvMESA(GLenum target
,
1101 GLsizei len
, const GLubyte
*registerName
,
1104 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1109 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1110 GLsizei len
, const GLubyte
*registerName
,
1114 GET_CURRENT_CONTEXT(ctx
);
1116 /* We _should_ be inside glBegin/glEnd */
1118 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1119 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1124 /* make null-terminated copy of registerName */
1125 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1126 _mesa_memcpy(reg
, registerName
, len
);
1130 case GL_VERTEX_PROGRAM_NV
:
1131 if (!ctx
->Extensions
.ARB_vertex_program
&&
1132 !ctx
->Extensions
.NV_vertex_program
) {
1133 _mesa_error(ctx
, GL_INVALID_ENUM
,
1134 "glGetProgramRegisterfvMESA(target)");
1137 if (!ctx
->VertexProgram
.Enabled
) {
1138 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1139 "glGetProgramRegisterfvMESA");
1142 /* GL_NV_vertex_program */
1143 if (reg
[0] == 'R') {
1145 GLint i
= _mesa_atoi(reg
+ 1);
1146 if (i
>= (GLint
)ctx
->Const
.MaxVertexProgramTemps
) {
1147 _mesa_error(ctx
, GL_INVALID_VALUE
,
1148 "glGetProgramRegisterfvMESA(registerName)");
1151 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1153 else if (reg
[0] == 'v' && reg
[1] == '[') {
1154 /* Vertex Input attribute */
1156 for (i
= 0; i
< ctx
->Const
.MaxVertexProgramAttribs
; i
++) {
1157 const char *name
= _mesa_nv_vertex_input_register_name(i
);
1159 sprintf(number
, "%d", i
);
1160 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
1161 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
1162 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
1166 _mesa_error(ctx
, GL_INVALID_VALUE
,
1167 "glGetProgramRegisterfvMESA(registerName)");
1170 else if (reg
[0] == 'o' && reg
[1] == '[') {
1171 /* Vertex output attribute */
1173 /* GL_ARB_vertex_program */
1174 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1178 _mesa_error(ctx
, GL_INVALID_VALUE
,
1179 "glGetProgramRegisterfvMESA(registerName)");
1183 case GL_FRAGMENT_PROGRAM_ARB
:
1184 if (!ctx
->Extensions
.ARB_fragment_program
) {
1185 _mesa_error(ctx
, GL_INVALID_ENUM
,
1186 "glGetProgramRegisterfvMESA(target)");
1189 if (!ctx
->FragmentProgram
.Enabled
) {
1190 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1191 "glGetProgramRegisterfvMESA");
1196 case GL_FRAGMENT_PROGRAM_NV
:
1197 if (!ctx
->Extensions
.NV_fragment_program
) {
1198 _mesa_error(ctx
, GL_INVALID_ENUM
,
1199 "glGetProgramRegisterfvMESA(target)");
1202 if (!ctx
->FragmentProgram
.Enabled
) {
1203 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1204 "glGetProgramRegisterfvMESA");
1207 if (reg
[0] == 'R') {
1209 GLint i
= _mesa_atoi(reg
+ 1);
1210 if (i
>= (GLint
)ctx
->Const
.MaxFragmentProgramTemps
) {
1211 _mesa_error(ctx
, GL_INVALID_VALUE
,
1212 "glGetProgramRegisterfvMESA(registerName)");
1215 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
1217 else if (reg
[0] == 'f' && reg
[1] == '[') {
1218 /* Fragment input attribute */
1220 for (i
= 0; i
< ctx
->Const
.MaxFragmentProgramAttribs
; i
++) {
1221 const char *name
= _mesa_nv_fragment_input_register_name(i
);
1222 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
1223 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
1227 _mesa_error(ctx
, GL_INVALID_VALUE
,
1228 "glGetProgramRegisterfvMESA(registerName)");
1231 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
1232 /* Fragment output color */
1233 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
]);
1235 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
1236 /* Fragment output color */
1237 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLH
]);
1239 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
1240 /* Fragment output depth */
1241 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
]);
1244 /* try user-defined identifiers */
1245 const GLfloat
*value
= _mesa_lookup_parameter_value(
1246 ctx
->FragmentProgram
.Current
->Parameters
, -1, reg
);
1251 _mesa_error(ctx
, GL_INVALID_VALUE
,
1252 "glGetProgramRegisterfvMESA(registerName)");
1258 _mesa_error(ctx
, GL_INVALID_ENUM
,
1259 "glGetProgramRegisterfvMESA(target)");