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
);
143 * Allocate and initialize a new fragment/vertex program object but don't
144 * put it into the program hash table.
145 * Called via ctx->Driver.NewProgram. May be wrapped (OO deriviation)
146 * by a device driver function.
148 * \param id program id/number
149 * \param target program target/type
150 * \return pointer to new program object
153 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
155 struct program
*prog
;
157 if (target
== GL_VERTEX_PROGRAM_NV
158 || target
== GL_VERTEX_PROGRAM_ARB
) {
159 struct vertex_program
*vprog
= CALLOC_STRUCT(vertex_program
);
163 prog
= &(vprog
->Base
);
165 else if (target
== GL_FRAGMENT_PROGRAM_NV
166 || target
== GL_FRAGMENT_PROGRAM_ARB
) {
167 struct fragment_program
*fprog
= CALLOC_STRUCT(fragment_program
);
171 prog
= &(fprog
->Base
);
174 _mesa_problem(ctx
, "bad target in _mesa_new_program");
178 prog
->Target
= target
;
179 prog
->Resident
= GL_TRUE
;
186 * Delete a program and remove it from the hash table, ignoring the
188 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
189 * by a device driver function.
192 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
197 _mesa_free(prog
->String
);
198 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
199 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
200 struct vertex_program
*vprog
= (struct vertex_program
*) prog
;
201 if (vprog
->Instructions
)
202 _mesa_free(vprog
->Instructions
);
204 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
205 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
206 struct fragment_program
*fprog
= (struct fragment_program
*) prog
;
207 if (fprog
->Instructions
)
208 _mesa_free(fprog
->Instructions
);
209 if (fprog
->Parameters
) {
210 _mesa_free_parameter_list(fprog
->Parameters
);
218 /**********************************************************************/
219 /* Program parameter functions */
220 /**********************************************************************/
222 struct program_parameter_list
*
223 _mesa_new_parameter_list(void)
225 return (struct program_parameter_list
*)
226 _mesa_calloc(sizeof(struct program_parameter_list
));
231 * Free a parameter list and all its parameters
234 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
236 _mesa_free_parameters(paramList
);
237 _mesa_free(paramList
);
242 * Free all the parameters in the given list, but don't free the
243 * paramList structure itself.
246 _mesa_free_parameters(struct program_parameter_list
*paramList
)
249 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
250 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
252 _mesa_free(paramList
->Parameters
);
253 paramList
->NumParameters
= 0;
254 paramList
->Parameters
= NULL
;
259 * Helper function used by the functions below.
262 add_parameter(struct program_parameter_list
*paramList
,
263 const char *name
, const GLfloat values
[4],
264 enum parameter_type type
)
266 const GLuint n
= paramList
->NumParameters
;
268 paramList
->Parameters
= (struct program_parameter
*)
269 _mesa_realloc(paramList
->Parameters
,
270 n
* sizeof(struct program_parameter
),
271 (n
+ 1) * sizeof(struct program_parameter
));
272 if (!paramList
->Parameters
) {
274 paramList
->NumParameters
= 0;
278 paramList
->NumParameters
= n
+ 1;
279 paramList
->Parameters
[n
].Name
= _mesa_strdup(name
);
280 paramList
->Parameters
[n
].Type
= type
;
282 COPY_4V(paramList
->Parameters
[n
].Values
, values
);
289 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
290 * \return index of the new entry in the parameter list
293 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
294 const char *name
, const GLfloat values
[4])
296 return add_parameter(paramList
, name
, values
, NAMED_PARAMETER
);
301 * Add a new unnamed constant to the parameter list.
302 * \param paramList - the parameter list
303 * \param values - four float values
304 * \return index of the new parameter.
307 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
308 const char *name
, const GLfloat values
[4])
310 return add_parameter(paramList
, name
, values
, CONSTANT
);
315 * Add a new unnamed constant to the parameter list.
316 * \param paramList - the parameter list
317 * \param values - four float values
318 * \return index of the new parameter.
321 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
322 const GLfloat values
[4])
324 /* generate a new dummy name */
327 _mesa_sprintf(name
, "constant%d", n
);
330 return add_parameter(paramList
, name
, values
, CONSTANT
);
335 * Add a new state reference to the parameter list.
336 * \param paramList - the parameter list
337 * \param state - an array of 6 state tokens
339 * \return index of the new parameter.
342 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
345 /* XXX Should we parse <stateString> here and produce the parameter's
346 * list of STATE_* tokens here, or in the parser?
350 idx
= add_parameter(paramList
, "Some State", NULL
, STATE
);
353 paramList
->Parameters
[idx
].StateIndexes
[a
] = (enum state_index
) stateTokens
[a
];
360 * Lookup a parameter value by name in the given parameter list.
361 * \return pointer to the float[4] values.
364 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
365 GLsizei nameLen
, const char *name
)
370 /* name is null-terminated */
371 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
372 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
373 return paramList
->Parameters
[i
].Values
;
377 /* name is not null-terminated, use nameLen */
378 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
379 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
380 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
381 return paramList
->Parameters
[i
].Values
;
389 * Lookup a parameter index by name in the given parameter list.
390 * \return index of parameter in the list.
393 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
394 GLsizei nameLen
, const char *name
)
399 /* name is null-terminated */
400 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
401 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
406 /* name is not null-terminated, use nameLen */
407 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
408 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
409 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
418 * Use the list of tokens in the state[] array to find global GL state
419 * and return it in <value>. Usually, four values are returned in <value>
420 * but matrix queries may return as many as 16 values.
421 * This function is used for ARB vertex/fragment programs.
422 * The program parser will produce the state[] values.
425 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
431 /* state[1] is either 0=front or 1=back side */
432 const GLuint face
= (GLuint
) state
[1];
433 /* state[2] is the material attribute */
437 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
439 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
443 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
445 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
449 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
451 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
455 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
457 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
459 case STATE_SHININESS
:
461 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
463 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
469 _mesa_problem(ctx
, "Invalid material state in fetch_state");
476 /* state[1] is the light number */
477 const GLuint ln
= (GLuint
) state
[1];
478 /* state[2] is the light attribute */
481 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
484 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
487 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
490 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
492 case STATE_ATTENUATION
:
493 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
494 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
495 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
496 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
498 case STATE_SPOT_DIRECTION
:
499 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
503 GLfloat eye_z
[] = {0, 0, 1};
505 /* Compute infinite half angle vector:
506 * half-vector = light_position + (0, 0, 1)
507 * and then normalize. w = 0
509 * light.EyePosition.w should be 0 for infinite lights.
511 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
512 NORMALIZE_3FV(value
);
517 _mesa_problem(ctx
, "Invalid light state in fetch_state");
522 case STATE_LIGHTMODEL_AMBIENT
:
523 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
525 case STATE_LIGHTMODEL_SCENECOLOR
:
529 for (i
= 0; i
< 4; i
++) {
530 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
531 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
532 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
538 for (i
= 0; i
< 4; i
++) {
539 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
540 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
541 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
545 case STATE_LIGHTPROD
:
547 const GLuint ln
= (GLuint
) state
[1];
548 const GLuint face
= (GLuint
) state
[2];
550 ASSERT(face
== 0 || face
== 1);
553 for (i
= 0; i
< 3; i
++) {
554 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
555 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
557 /* [3] = material alpha */
558 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
561 for (i
= 0; i
< 3; i
++) {
562 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
563 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
565 /* [3] = material alpha */
566 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
569 for (i
= 0; i
< 3; i
++) {
570 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
571 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
573 /* [3] = material alpha */
574 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
577 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
584 /* state[1] is the texture unit */
585 const GLuint unit
= (GLuint
) state
[1];
586 /* state[2] is the texgen attribute */
588 case STATE_TEXGEN_EYE_S
:
589 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
591 case STATE_TEXGEN_EYE_T
:
592 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
594 case STATE_TEXGEN_EYE_R
:
595 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
597 case STATE_TEXGEN_EYE_Q
:
598 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
600 case STATE_TEXGEN_OBJECT_S
:
601 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
603 case STATE_TEXGEN_OBJECT_T
:
604 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
606 case STATE_TEXGEN_OBJECT_R
:
607 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
609 case STATE_TEXGEN_OBJECT_Q
:
610 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
613 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
618 case STATE_TEXENV_COLOR
:
620 /* state[1] is the texture unit */
621 const GLuint unit
= (GLuint
) state
[1];
622 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
625 case STATE_FOG_COLOR
:
626 COPY_4V(value
, ctx
->Fog
.Color
);
628 case STATE_FOG_PARAMS
:
629 value
[0] = ctx
->Fog
.Density
;
630 value
[1] = ctx
->Fog
.Start
;
631 value
[2] = ctx
->Fog
.End
;
632 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
634 case STATE_CLIPPLANE
:
636 const GLuint plane
= (GLuint
) state
[1];
637 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
640 case STATE_POINT_SIZE
:
641 value
[0] = ctx
->Point
.Size
;
642 value
[1] = ctx
->Point
.MinSize
;
643 value
[2] = ctx
->Point
.MaxSize
;
644 value
[3] = ctx
->Point
.Threshold
;
646 case STATE_POINT_ATTENUATION
:
647 value
[0] = ctx
->Point
.Params
[0];
648 value
[1] = ctx
->Point
.Params
[1];
649 value
[2] = ctx
->Point
.Params
[2];
654 /* state[1] = modelview, projection, texture, etc. */
655 /* state[2] = which texture matrix or program matrix */
656 /* state[3] = first column to fetch */
657 /* state[4] = last column to fetch */
658 /* state[5] = transpose, inverse or invtrans */
660 const GLmatrix
*matrix
;
661 const enum state_index mat
= state
[1];
662 const GLuint index
= (GLuint
) state
[2];
663 const GLuint first
= (GLuint
) state
[3];
664 const GLuint last
= (GLuint
) state
[4];
665 const enum state_index modifier
= state
[5];
668 if (mat
== STATE_MODELVIEW
) {
669 matrix
= ctx
->ModelviewMatrixStack
.Top
;
671 else if (mat
== STATE_PROJECTION
) {
672 matrix
= ctx
->ProjectionMatrixStack
.Top
;
674 else if (mat
== STATE_MVP
) {
675 matrix
= &ctx
->_ModelProjectMatrix
;
677 else if (mat
== STATE_TEXTURE
) {
678 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
680 else if (mat
== STATE_PROGRAM
) {
681 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
684 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
687 if (modifier
== STATE_MATRIX_INVERSE
||
688 modifier
== STATE_MATRIX_INVTRANS
) {
689 /* XXX be sure inverse is up to date */
695 if (modifier
== STATE_MATRIX_TRANSPOSE
||
696 modifier
== STATE_MATRIX_INVTRANS
) {
697 for (i
= 0, row
= first
; row
<= last
; row
++) {
698 value
[i
++] = m
[row
* 4 + 0];
699 value
[i
++] = m
[row
* 4 + 1];
700 value
[i
++] = m
[row
* 4 + 2];
701 value
[i
++] = m
[row
* 4 + 3];
705 for (i
= 0, row
= first
; row
<= last
; row
++) {
706 value
[i
++] = m
[row
+ 0];
707 value
[i
++] = m
[row
+ 4];
708 value
[i
++] = m
[row
+ 8];
709 value
[i
++] = m
[row
+ 12];
714 case STATE_DEPTH_RANGE
:
715 value
[0] = ctx
->Viewport
.Near
; /* near */
716 value
[1] = ctx
->Viewport
.Far
; /* far */
717 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
720 case STATE_FRAGMENT_PROGRAM
:
722 /* state[1] = {STATE_ENV, STATE_LOCAL} */
723 /* state[2] = parameter index */
728 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
732 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
735 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
741 case STATE_VERTEX_PROGRAM
:
743 /* state[1] = {STATE_ENV, STATE_LOCAL} */
744 /* state[2] = parameter index */
749 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
753 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
756 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
762 _mesa_problem(ctx
, "Invalid state in fetch_state");
769 * Loop over all the parameters in a parameter list. If the parameter
770 * is a GL state reference, look up the current value of that state
771 * variable and put it into the parameter's Value[4] array.
772 * This would be called at glBegin time when using a fragment program.
775 _mesa_load_state_parameters(GLcontext
*ctx
,
776 struct program_parameter_list
*paramList
)
779 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
780 if (paramList
->Parameters
[i
].Type
== STATE
) {
781 _mesa_fetch_state(ctx
, paramList
->Parameters
[i
].StateIndexes
,
782 paramList
->Parameters
[i
].Values
);
789 /**********************************************************************/
791 /**********************************************************************/
795 * Bind a program (make it current)
796 * \note Called from the GL API dispatcher by both glBindProgramNV
797 * and glBindProgramARB.
800 _mesa_BindProgram(GLenum target
, GLuint id
)
802 struct program
*prog
;
803 GET_CURRENT_CONTEXT(ctx
);
804 ASSERT_OUTSIDE_BEGIN_END(ctx
);
806 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
808 if ((target
== GL_VERTEX_PROGRAM_NV
809 && ctx
->Extensions
.NV_vertex_program
) ||
810 (target
== GL_VERTEX_PROGRAM_ARB
811 && ctx
->Extensions
.ARB_vertex_program
)) {
812 if (ctx
->VertexProgram
.Current
&&
813 ctx
->VertexProgram
.Current
->Base
.Id
== id
)
815 /* decrement refcount on previously bound vertex program */
816 if (ctx
->VertexProgram
.Current
) {
817 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
818 /* and delete if refcount goes below one */
819 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0) {
820 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
821 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
825 else if ((target
== GL_FRAGMENT_PROGRAM_NV
826 && ctx
->Extensions
.NV_fragment_program
) ||
827 (target
== GL_FRAGMENT_PROGRAM_ARB
828 && ctx
->Extensions
.ARB_fragment_program
)) {
829 if (ctx
->FragmentProgram
.Current
&&
830 ctx
->FragmentProgram
.Current
->Base
.Id
== id
)
832 /* decrement refcount on previously bound fragment program */
833 if (ctx
->FragmentProgram
.Current
) {
834 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
835 /* and delete if refcount goes below one */
836 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0) {
837 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
838 _mesa_HashRemove(ctx
->Shared
->Programs
, id
);
843 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
847 /* NOTE: binding to a non-existant program is not an error.
848 * That's supposed to be caught in glBegin.
851 /* default program */
853 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
854 prog
= ctx
->Shared
->DefaultVertexProgram
;
856 prog
= ctx
->Shared
->DefaultFragmentProgram
;
859 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
861 if (prog
->Target
== 0) {
862 /* prog was allocated with glGenProgramsNV */
863 prog
->Target
= target
;
865 else if (prog
->Target
!= target
) {
866 _mesa_error(ctx
, GL_INVALID_OPERATION
,
867 "glBindProgramNV/ARB(target mismatch)");
872 /* allocate a new program now */
873 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
875 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
879 prog
->Target
= target
;
880 prog
->Resident
= GL_TRUE
;
882 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
887 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
888 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
890 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
891 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
897 if (ctx
->Driver
.BindProgram
)
898 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
903 * Delete a list of programs.
904 * \note Not compiled into display lists.
905 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
908 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
911 GET_CURRENT_CONTEXT(ctx
);
912 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
915 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
919 for (i
= 0; i
< n
; i
++) {
921 struct program
*prog
= (struct program
*)
922 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
924 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
925 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
926 if (ctx
->VertexProgram
.Current
&&
927 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
928 /* unbind this currently bound program */
929 _mesa_BindProgram(prog
->Target
, 0);
932 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
933 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
934 if (ctx
->FragmentProgram
.Current
&&
935 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
936 /* unbind this currently bound program */
937 _mesa_BindProgram(prog
->Target
, 0);
941 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
945 if (prog
->RefCount
<= 0) {
946 ctx
->Driver
.DeleteProgram(ctx
, prog
);
955 * Generate a list of new program identifiers.
956 * \note Not compiled into display lists.
957 * \note Called by both glGenProgramsNV and glGenProgramsARB.
960 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
964 GET_CURRENT_CONTEXT(ctx
);
965 ASSERT_OUTSIDE_BEGIN_END(ctx
);
968 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
975 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
977 for (i
= 0; i
< (GLuint
) n
; i
++) {
978 const int bytes
= MAX2(sizeof(struct vertex_program
),
979 sizeof(struct fragment_program
));
980 struct program
*prog
= (struct program
*) _mesa_calloc(bytes
);
982 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPrograms");
986 prog
->Id
= first
+ i
;
987 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, prog
);
990 /* Return the program names */
991 for (i
= 0; i
< (GLuint
) n
; i
++) {
998 * Determine if id names a program.
999 * \note Not compiled into display lists.
1000 * \note Called from both glIsProgramNV and glIsProgramARB.
1001 * \param id is the program identifier
1002 * \return GL_TRUE if id is a program, else GL_FALSE.
1004 GLboolean GLAPIENTRY
1005 _mesa_IsProgram(GLuint id
)
1007 struct program
*prog
;
1008 GET_CURRENT_CONTEXT(ctx
);
1009 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1014 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
1015 if (prog
&& prog
->Target
)
1023 /**********************************************************************/
1024 /* GL_MESA_program_debug extension */
1025 /**********************************************************************/
1030 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1033 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1038 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1041 GET_CURRENT_CONTEXT(ctx
);
1044 case GL_FRAGMENT_PROGRAM_ARB
:
1045 if (!ctx
->Extensions
.ARB_fragment_program
) {
1046 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1049 ctx
->FragmentProgram
.Callback
= callback
;
1050 ctx
->FragmentProgram
.CallbackData
= data
;
1052 case GL_FRAGMENT_PROGRAM_NV
:
1053 if (!ctx
->Extensions
.NV_fragment_program
) {
1054 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1057 ctx
->FragmentProgram
.Callback
= callback
;
1058 ctx
->FragmentProgram
.CallbackData
= data
;
1060 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1061 if (!ctx
->Extensions
.ARB_vertex_program
&&
1062 !ctx
->Extensions
.NV_vertex_program
) {
1063 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1066 ctx
->VertexProgram
.Callback
= callback
;
1067 ctx
->VertexProgram
.CallbackData
= data
;
1070 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1078 glGetProgramRegisterfvMESA(GLenum target
,
1079 GLsizei len
, const GLubyte
*registerName
,
1082 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1087 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1088 GLsizei len
, const GLubyte
*registerName
,
1092 GET_CURRENT_CONTEXT(ctx
);
1094 /* We _should_ be inside glBegin/glEnd */
1096 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1097 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1102 /* make null-terminated copy of registerName */
1103 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1104 _mesa_memcpy(reg
, registerName
, len
);
1108 case GL_VERTEX_PROGRAM_NV
:
1109 if (!ctx
->Extensions
.ARB_vertex_program
&&
1110 !ctx
->Extensions
.NV_vertex_program
) {
1111 _mesa_error(ctx
, GL_INVALID_ENUM
,
1112 "glGetProgramRegisterfvMESA(target)");
1115 if (!ctx
->VertexProgram
.Enabled
) {
1116 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1117 "glGetProgramRegisterfvMESA");
1120 /* GL_NV_vertex_program */
1121 if (reg
[0] == 'R') {
1123 GLint i
= _mesa_atoi(reg
+ 1);
1124 if (i
>= (GLint
)ctx
->Const
.MaxVertexProgramTemps
) {
1125 _mesa_error(ctx
, GL_INVALID_VALUE
,
1126 "glGetProgramRegisterfvMESA(registerName)");
1129 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1131 else if (reg
[0] == 'v' && reg
[1] == '[') {
1132 /* Vertex Input attribute */
1134 for (i
= 0; i
< ctx
->Const
.MaxVertexProgramAttribs
; i
++) {
1135 const char *name
= _mesa_nv_vertex_input_register_name(i
);
1137 sprintf(number
, "%d", i
);
1138 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
1139 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
1140 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
1144 _mesa_error(ctx
, GL_INVALID_VALUE
,
1145 "glGetProgramRegisterfvMESA(registerName)");
1148 else if (reg
[0] == 'o' && reg
[1] == '[') {
1149 /* Vertex output attribute */
1151 /* GL_ARB_vertex_program */
1152 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1156 _mesa_error(ctx
, GL_INVALID_VALUE
,
1157 "glGetProgramRegisterfvMESA(registerName)");
1161 case GL_FRAGMENT_PROGRAM_ARB
:
1162 if (!ctx
->Extensions
.ARB_fragment_program
) {
1163 _mesa_error(ctx
, GL_INVALID_ENUM
,
1164 "glGetProgramRegisterfvMESA(target)");
1167 if (!ctx
->FragmentProgram
.Enabled
) {
1168 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1169 "glGetProgramRegisterfvMESA");
1174 case GL_FRAGMENT_PROGRAM_NV
:
1175 if (!ctx
->Extensions
.NV_fragment_program
) {
1176 _mesa_error(ctx
, GL_INVALID_ENUM
,
1177 "glGetProgramRegisterfvMESA(target)");
1180 if (!ctx
->FragmentProgram
.Enabled
) {
1181 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1182 "glGetProgramRegisterfvMESA");
1185 if (reg
[0] == 'R') {
1187 GLint i
= _mesa_atoi(reg
+ 1);
1188 if (i
>= (GLint
)ctx
->Const
.MaxFragmentProgramTemps
) {
1189 _mesa_error(ctx
, GL_INVALID_VALUE
,
1190 "glGetProgramRegisterfvMESA(registerName)");
1193 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
1195 else if (reg
[0] == 'f' && reg
[1] == '[') {
1196 /* Fragment input attribute */
1198 for (i
= 0; i
< ctx
->Const
.MaxFragmentProgramAttribs
; i
++) {
1199 const char *name
= _mesa_nv_fragment_input_register_name(i
);
1200 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
1201 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
1205 _mesa_error(ctx
, GL_INVALID_VALUE
,
1206 "glGetProgramRegisterfvMESA(registerName)");
1209 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
1210 /* Fragment output color */
1211 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
]);
1213 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
1214 /* Fragment output color */
1215 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLH
]);
1217 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
1218 /* Fragment output depth */
1219 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
]);
1222 /* try user-defined identifiers */
1223 const GLfloat
*value
= _mesa_lookup_parameter_value(
1224 ctx
->FragmentProgram
.Current
->Parameters
, -1, reg
);
1229 _mesa_error(ctx
, GL_INVALID_VALUE
,
1230 "glGetProgramRegisterfvMESA(registerName)");
1236 _mesa_error(ctx
, GL_INVALID_ENUM
,
1237 "glGetProgramRegisterfvMESA(target)");