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"
42 #include "nvvertprog.h"
45 /**********************************************************************/
46 /* Utility functions */
47 /**********************************************************************/
50 /* A pointer to this dummy program is put into the hash table when
51 * glGenPrograms is called.
53 struct program _mesa_DummyProgram
;
57 * Init context's vertex/fragment program state
60 _mesa_init_program(GLcontext
*ctx
)
64 ctx
->Program
.ErrorPos
= -1;
65 ctx
->Program
.ErrorString
= _mesa_strdup("");
67 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
68 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
69 ctx
->VertexProgram
.PointSizeEnabled
= GL_FALSE
;
70 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
71 ctx
->VertexProgram
.Current
= (struct vertex_program
*) ctx
->Shared
->DefaultVertexProgram
;
72 assert(ctx
->VertexProgram
.Current
);
73 ctx
->VertexProgram
.Current
->Base
.RefCount
++;
74 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
75 ctx
->VertexProgram
.TrackMatrix
[i
] = GL_NONE
;
76 ctx
->VertexProgram
.TrackMatrixTransform
[i
] = GL_IDENTITY_NV
;
80 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
81 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
82 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) ctx
->Shared
->DefaultFragmentProgram
;
83 assert(ctx
->FragmentProgram
.Current
);
84 ctx
->FragmentProgram
.Current
->Base
.RefCount
++;
87 #if FEATURE_ATI_fragment_shader
88 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
89 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
90 assert(ctx
->ATIFragmentShader
.Current
);
91 ctx
->ATIFragmentShader
.Current
->Base
.RefCount
++;
97 * Free a context's vertex/fragment program state
100 _mesa_free_program_data(GLcontext
*ctx
)
102 #if FEATURE_NV_vertex_program
103 if (ctx
->VertexProgram
.Current
) {
104 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
105 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0)
106 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
109 #if FEATURE_NV_fragment_program
110 if (ctx
->FragmentProgram
.Current
) {
111 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
112 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0)
113 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
116 #if FEATURE_ATI_fragment_shader
117 if (ctx
->ATIFragmentShader
.Current
) {
118 ctx
->ATIFragmentShader
.Current
->Base
.RefCount
--;
119 if (ctx
->ATIFragmentShader
.Current
->Base
.RefCount
<= 0)
120 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->ATIFragmentShader
.Current
->Base
));
123 _mesa_free((void *) ctx
->Program
.ErrorString
);
130 * Set the vertex/fragment program error state (position and error string).
131 * This is generally called from within the parsers.
134 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
136 ctx
->Program
.ErrorPos
= pos
;
137 _mesa_free((void *) ctx
->Program
.ErrorString
);
140 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
145 * Find the line number and column for 'pos' within 'string'.
146 * Return a copy of the line which contains 'pos'. Free the line with
148 * \param string the program string
149 * \param pos the position within the string
150 * \param line returns the line number corresponding to 'pos'.
151 * \param col returns the column number corresponding to 'pos'.
152 * \return copy of the line containing 'pos'.
155 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
156 GLint
*line
, GLint
*col
)
158 const GLubyte
*lineStart
= string
;
159 const GLubyte
*p
= string
;
166 if (*p
== (GLubyte
) '\n') {
173 *col
= (pos
- lineStart
) + 1;
175 /* return copy of this line */
176 while (*p
!= 0 && *p
!= '\n')
179 s
= (GLubyte
*) _mesa_malloc(len
+ 1);
180 _mesa_memcpy(s
, lineStart
, len
);
188 * Initialize a new vertex/fragment program object.
190 static struct program
*
191 _mesa_init_program_struct( GLcontext
*ctx
, struct program
*prog
,
192 GLenum target
, GLuint id
)
197 prog
->Target
= target
;
198 prog
->Resident
= GL_TRUE
;
207 * Initialize a new fragment program object.
210 _mesa_init_fragment_program( GLcontext
*ctx
, struct fragment_program
*prog
,
211 GLenum target
, GLuint id
)
214 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
221 * Initialize a new vertex program object.
224 _mesa_init_vertex_program( GLcontext
*ctx
, struct vertex_program
*prog
,
225 GLenum target
, GLuint id
)
228 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
234 * Initialize a new ATI fragment shader object.
237 _mesa_init_ati_fragment_shader( GLcontext
*ctx
,
238 struct ati_fragment_shader
*prog
,
239 GLenum target
, GLuint id
)
242 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
250 * Allocate and initialize a new fragment/vertex program object but
251 * don't put it into the program hash table. Called via
252 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
253 * device driver function to implement OO deriviation with additional
254 * types not understood by this function.
257 * \param id program id/number
258 * \param target program target/type
259 * \return pointer to new program object
262 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
265 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
266 return _mesa_init_vertex_program( ctx
, CALLOC_STRUCT(vertex_program
),
268 case GL_FRAGMENT_PROGRAM_NV
:
269 case GL_FRAGMENT_PROGRAM_ARB
:
270 return _mesa_init_fragment_program( ctx
, CALLOC_STRUCT(fragment_program
),
272 case GL_FRAGMENT_SHADER_ATI
:
273 return _mesa_init_ati_fragment_shader( ctx
, CALLOC_STRUCT(ati_fragment_shader
),
277 _mesa_problem(ctx
, "bad target in _mesa_new_program");
284 * Delete a program and remove it from the hash table, ignoring the
286 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
287 * by a device driver function.
290 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
296 _mesa_free(prog
->String
);
297 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
298 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
299 struct vertex_program
*vprog
= (struct vertex_program
*) prog
;
300 if (vprog
->Instructions
) {
302 for (i
= 0; i
< vprog
->Base
.NumInstructions
; i
++) {
303 if (vprog
->Instructions
[i
].Data
)
304 _mesa_free(vprog
->Instructions
[i
].Data
);
306 _mesa_free(vprog
->Instructions
);
308 if (vprog
->Parameters
)
309 _mesa_free_parameter_list(vprog
->Parameters
);
311 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
312 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
313 struct fragment_program
*fprog
= (struct fragment_program
*) prog
;
314 if (fprog
->Instructions
) {
316 for (i
= 0; i
< fprog
->Base
.NumInstructions
; i
++) {
317 if (fprog
->Instructions
[i
].Data
)
318 _mesa_free(fprog
->Instructions
[i
].Data
);
320 _mesa_free(fprog
->Instructions
);
322 if (fprog
->Parameters
)
323 _mesa_free_parameter_list(fprog
->Parameters
);
325 else if (prog
->Target
== GL_FRAGMENT_SHADER_ATI
) {
326 struct ati_fragment_shader
*atifs
= (struct ati_fragment_shader
*)prog
;
327 if (atifs
->Instructions
)
328 _mesa_free(atifs
->Instructions
);
336 /**********************************************************************/
337 /* Program parameter functions */
338 /**********************************************************************/
340 struct program_parameter_list
*
341 _mesa_new_parameter_list(void)
343 return (struct program_parameter_list
*)
344 _mesa_calloc(sizeof(struct program_parameter_list
));
349 * Free a parameter list and all its parameters
352 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
354 _mesa_free_parameters(paramList
);
355 _mesa_free(paramList
);
360 * Free all the parameters in the given list, but don't free the
361 * paramList structure itself.
364 _mesa_free_parameters(struct program_parameter_list
*paramList
)
367 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
368 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
370 _mesa_free(paramList
->Parameters
);
371 paramList
->NumParameters
= 0;
372 paramList
->Parameters
= NULL
;
377 * Helper function used by the functions below.
380 add_parameter(struct program_parameter_list
*paramList
,
381 const char *name
, const GLfloat values
[4],
382 enum parameter_type type
)
384 const GLuint n
= paramList
->NumParameters
;
386 paramList
->Parameters
= (struct program_parameter
*)
387 _mesa_realloc(paramList
->Parameters
,
388 n
* sizeof(struct program_parameter
),
389 (n
+ 1) * sizeof(struct program_parameter
));
390 if (!paramList
->Parameters
) {
392 paramList
->NumParameters
= 0;
396 paramList
->NumParameters
= n
+ 1;
397 paramList
->Parameters
[n
].Name
= _mesa_strdup(name
);
398 paramList
->Parameters
[n
].Type
= type
;
400 COPY_4V(paramList
->Parameters
[n
].Values
, values
);
407 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
408 * \return index of the new entry in the parameter list
411 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
412 const char *name
, const GLfloat values
[4])
414 return add_parameter(paramList
, name
, values
, NAMED_PARAMETER
);
419 * Add a new unnamed constant to the parameter list.
420 * \param paramList - the parameter list
421 * \param values - four float values
422 * \return index of the new parameter.
425 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
426 const char *name
, const GLfloat values
[4])
428 return add_parameter(paramList
, name
, values
, CONSTANT
);
433 * Add a new unnamed constant to the parameter list.
434 * \param paramList - the parameter list
435 * \param values - four float values
436 * \return index of the new parameter.
439 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
440 const GLfloat values
[4])
442 /* generate a new dummy name */
445 _mesa_sprintf(name
, "constant%d", n
);
448 return add_parameter(paramList
, name
, values
, CONSTANT
);
453 * Add a new state reference to the parameter list.
454 * \param paramList - the parameter list
455 * \param state - an array of 6 state tokens
457 * \return index of the new parameter.
460 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
463 /* XXX Should we parse <stateString> here and produce the parameter's
464 * list of STATE_* tokens here, or in the parser?
468 idx
= add_parameter(paramList
, "Some State", NULL
, STATE
);
471 paramList
->Parameters
[idx
].StateIndexes
[a
] = (enum state_index
) stateTokens
[a
];
478 * Lookup a parameter value by name in the given parameter list.
479 * \return pointer to the float[4] values.
482 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
483 GLsizei nameLen
, const char *name
)
491 /* name is null-terminated */
492 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
493 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
494 return paramList
->Parameters
[i
].Values
;
498 /* name is not null-terminated, use nameLen */
499 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
500 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
501 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
502 return paramList
->Parameters
[i
].Values
;
510 * Lookup a parameter index by name in the given parameter list.
511 * \return index of parameter in the list.
514 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
515 GLsizei nameLen
, const char *name
)
523 /* name is null-terminated */
524 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
525 if (_mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
530 /* name is not null-terminated, use nameLen */
531 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
532 if (_mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
533 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
542 * Use the list of tokens in the state[] array to find global GL state
543 * and return it in <value>. Usually, four values are returned in <value>
544 * but matrix queries may return as many as 16 values.
545 * This function is used for ARB vertex/fragment programs.
546 * The program parser will produce the state[] values.
549 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
555 /* state[1] is either 0=front or 1=back side */
556 const GLuint face
= (GLuint
) state
[1];
557 /* state[2] is the material attribute */
561 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
563 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
567 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
569 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
573 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
575 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
579 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
581 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
583 case STATE_SHININESS
:
585 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
587 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
593 _mesa_problem(ctx
, "Invalid material state in fetch_state");
599 /* state[1] is the light number */
600 const GLuint ln
= (GLuint
) state
[1];
601 /* state[2] is the light attribute */
604 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
607 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
610 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
613 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
615 case STATE_ATTENUATION
:
616 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
617 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
618 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
619 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
621 case STATE_SPOT_DIRECTION
:
622 COPY_3V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
623 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
627 GLfloat eye_z
[] = {0, 0, 1};
629 /* Compute infinite half angle vector:
630 * half-vector = light_position + (0, 0, 1)
631 * and then normalize. w = 0
633 * light.EyePosition.w should be 0 for infinite lights.
635 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
636 NORMALIZE_3FV(value
);
641 _mesa_problem(ctx
, "Invalid light state in fetch_state");
645 case STATE_LIGHTMODEL_AMBIENT
:
646 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
648 case STATE_LIGHTMODEL_SCENECOLOR
:
652 for (i
= 0; i
< 3; i
++) {
653 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
654 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
655 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
657 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
662 for (i
= 0; i
< 3; i
++) {
663 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
664 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
665 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
667 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
670 case STATE_LIGHTPROD
:
672 const GLuint ln
= (GLuint
) state
[1];
673 const GLuint face
= (GLuint
) state
[2];
675 ASSERT(face
== 0 || face
== 1);
678 for (i
= 0; i
< 3; i
++) {
679 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
680 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
682 /* [3] = material alpha */
683 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
686 for (i
= 0; i
< 3; i
++) {
687 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
688 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
690 /* [3] = material alpha */
691 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
694 for (i
= 0; i
< 3; i
++) {
695 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
696 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
698 /* [3] = material alpha */
699 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
702 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
708 /* state[1] is the texture unit */
709 const GLuint unit
= (GLuint
) state
[1];
710 /* state[2] is the texgen attribute */
712 case STATE_TEXGEN_EYE_S
:
713 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
715 case STATE_TEXGEN_EYE_T
:
716 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
718 case STATE_TEXGEN_EYE_R
:
719 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
721 case STATE_TEXGEN_EYE_Q
:
722 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
724 case STATE_TEXGEN_OBJECT_S
:
725 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
727 case STATE_TEXGEN_OBJECT_T
:
728 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
730 case STATE_TEXGEN_OBJECT_R
:
731 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
733 case STATE_TEXGEN_OBJECT_Q
:
734 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
737 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
741 case STATE_TEXENV_COLOR
:
743 /* state[1] is the texture unit */
744 const GLuint unit
= (GLuint
) state
[1];
745 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
748 case STATE_FOG_COLOR
:
749 COPY_4V(value
, ctx
->Fog
.Color
);
751 case STATE_FOG_PARAMS
:
752 value
[0] = ctx
->Fog
.Density
;
753 value
[1] = ctx
->Fog
.Start
;
754 value
[2] = ctx
->Fog
.End
;
755 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
757 case STATE_CLIPPLANE
:
759 const GLuint plane
= (GLuint
) state
[1];
760 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
763 case STATE_POINT_SIZE
:
764 value
[0] = ctx
->Point
.Size
;
765 value
[1] = ctx
->Point
.MinSize
;
766 value
[2] = ctx
->Point
.MaxSize
;
767 value
[3] = ctx
->Point
.Threshold
;
769 case STATE_POINT_ATTENUATION
:
770 value
[0] = ctx
->Point
.Params
[0];
771 value
[1] = ctx
->Point
.Params
[1];
772 value
[2] = ctx
->Point
.Params
[2];
777 /* state[1] = modelview, projection, texture, etc. */
778 /* state[2] = which texture matrix or program matrix */
779 /* state[3] = first column to fetch */
780 /* state[4] = last column to fetch */
781 /* state[5] = transpose, inverse or invtrans */
783 const GLmatrix
*matrix
;
784 const enum state_index mat
= state
[1];
785 const GLuint index
= (GLuint
) state
[2];
786 const GLuint first
= (GLuint
) state
[3];
787 const GLuint last
= (GLuint
) state
[4];
788 const enum state_index modifier
= state
[5];
791 if (mat
== STATE_MODELVIEW
) {
792 matrix
= ctx
->ModelviewMatrixStack
.Top
;
794 else if (mat
== STATE_PROJECTION
) {
795 matrix
= ctx
->ProjectionMatrixStack
.Top
;
797 else if (mat
== STATE_MVP
) {
798 matrix
= &ctx
->_ModelProjectMatrix
;
800 else if (mat
== STATE_TEXTURE
) {
801 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
803 else if (mat
== STATE_PROGRAM
) {
804 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
807 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
810 if (modifier
== STATE_MATRIX_INVERSE
||
811 modifier
== STATE_MATRIX_INVTRANS
) {
812 /* Be sure inverse is up to date:
814 _math_matrix_analyse( (GLmatrix
*) matrix
);
820 if (modifier
== STATE_MATRIX_TRANSPOSE
||
821 modifier
== STATE_MATRIX_INVTRANS
) {
822 for (i
= 0, row
= first
; row
<= last
; row
++) {
823 value
[i
++] = m
[row
* 4 + 0];
824 value
[i
++] = m
[row
* 4 + 1];
825 value
[i
++] = m
[row
* 4 + 2];
826 value
[i
++] = m
[row
* 4 + 3];
830 for (i
= 0, row
= first
; row
<= last
; row
++) {
831 value
[i
++] = m
[row
+ 0];
832 value
[i
++] = m
[row
+ 4];
833 value
[i
++] = m
[row
+ 8];
834 value
[i
++] = m
[row
+ 12];
839 case STATE_DEPTH_RANGE
:
840 value
[0] = ctx
->Viewport
.Near
; /* near */
841 value
[1] = ctx
->Viewport
.Far
; /* far */
842 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
845 case STATE_FRAGMENT_PROGRAM
:
847 /* state[1] = {STATE_ENV, STATE_LOCAL} */
848 /* state[2] = parameter index */
849 const int idx
= (int) state
[2];
852 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
855 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
858 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
864 case STATE_VERTEX_PROGRAM
:
866 /* state[1] = {STATE_ENV, STATE_LOCAL} */
867 /* state[2] = parameter index */
868 const int idx
= (int) state
[2];
871 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
874 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
877 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
883 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
890 * Loop over all the parameters in a parameter list. If the parameter
891 * is a GL state reference, look up the current value of that state
892 * variable and put it into the parameter's Value[4] array.
893 * This would be called at glBegin time when using a fragment program.
896 _mesa_load_state_parameters(GLcontext
*ctx
,
897 struct program_parameter_list
*paramList
)
904 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
905 if (paramList
->Parameters
[i
].Type
== STATE
) {
906 _mesa_fetch_state(ctx
, paramList
->Parameters
[i
].StateIndexes
,
907 paramList
->Parameters
[i
].Values
);
914 /**********************************************************************/
916 /**********************************************************************/
920 * Bind a program (make it current)
921 * \note Called from the GL API dispatcher by both glBindProgramNV
922 * and glBindProgramARB.
925 _mesa_BindProgram(GLenum target
, GLuint id
)
927 struct program
*prog
;
928 GET_CURRENT_CONTEXT(ctx
);
929 ASSERT_OUTSIDE_BEGIN_END(ctx
);
931 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
933 if ((target
== GL_VERTEX_PROGRAM_NV
934 && ctx
->Extensions
.NV_vertex_program
) ||
935 (target
== GL_VERTEX_PROGRAM_ARB
936 && ctx
->Extensions
.ARB_vertex_program
)) {
937 /*** Vertex program binding ***/
938 struct vertex_program
*curProg
= ctx
->VertexProgram
.Current
;
939 if (curProg
->Base
.Id
== id
) {
940 /* binding same program - no change */
943 if (curProg
->Base
.Id
!= 0) {
944 /* decrement refcount on previously bound vertex program */
945 curProg
->Base
.RefCount
--;
946 /* and delete if refcount goes below one */
947 if (curProg
->Base
.RefCount
<= 0) {
948 /* the program ID was already removed from the hash table */
949 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
953 else if ((target
== GL_FRAGMENT_PROGRAM_NV
954 && ctx
->Extensions
.NV_fragment_program
) ||
955 (target
== GL_FRAGMENT_PROGRAM_ARB
956 && ctx
->Extensions
.ARB_fragment_program
)) {
957 /*** Fragment program binding ***/
958 struct fragment_program
*curProg
= ctx
->FragmentProgram
.Current
;
959 if (curProg
->Base
.Id
== id
) {
960 /* binding same program - no change */
963 if (curProg
->Base
.Id
!= 0) {
964 /* decrement refcount on previously bound fragment program */
965 curProg
->Base
.RefCount
--;
966 /* and delete if refcount goes below one */
967 if (curProg
->Base
.RefCount
<= 0) {
968 /* the program ID was already removed from the hash table */
969 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
974 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
978 /* NOTE: binding to a non-existant program is not an error.
979 * That's supposed to be caught in glBegin.
982 /* Bind default program */
984 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
985 prog
= ctx
->Shared
->DefaultVertexProgram
;
987 prog
= ctx
->Shared
->DefaultFragmentProgram
;
990 /* Bind user program */
991 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
992 if (!prog
|| prog
== &_mesa_DummyProgram
) {
993 /* allocate a new program now */
994 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
996 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
999 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
1001 else if (prog
->Target
!= target
) {
1002 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1003 "glBindProgramNV/ARB(target mismatch)");
1009 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
1010 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
1012 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
1013 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
1016 /* Never null pointers */
1017 ASSERT(ctx
->VertexProgram
.Current
);
1018 ASSERT(ctx
->FragmentProgram
.Current
);
1023 if (ctx
->Driver
.BindProgram
)
1024 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
1029 * Delete a list of programs.
1030 * \note Not compiled into display lists.
1031 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1034 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
1037 GET_CURRENT_CONTEXT(ctx
);
1038 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1041 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
1045 for (i
= 0; i
< n
; i
++) {
1047 struct program
*prog
= (struct program
*)
1048 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
1049 if (prog
== &_mesa_DummyProgram
) {
1050 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1053 /* Unbind program if necessary */
1054 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
1055 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
1056 if (ctx
->VertexProgram
.Current
&&
1057 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
1058 /* unbind this currently bound program */
1059 _mesa_BindProgram(prog
->Target
, 0);
1062 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
1063 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1064 if (ctx
->FragmentProgram
.Current
&&
1065 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
1066 /* unbind this currently bound program */
1067 _mesa_BindProgram(prog
->Target
, 0);
1071 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
1074 /* The ID is immediately available for re-use now */
1075 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1077 if (prog
->RefCount
<= 0) {
1078 ctx
->Driver
.DeleteProgram(ctx
, prog
);
1087 * Generate a list of new program identifiers.
1088 * \note Not compiled into display lists.
1089 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1092 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1096 GET_CURRENT_CONTEXT(ctx
);
1097 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1100 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1107 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1109 /* Insert pointer to dummy program as placeholder */
1110 for (i
= 0; i
< (GLuint
) n
; i
++) {
1111 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, &_mesa_DummyProgram
);
1114 /* Return the program names */
1115 for (i
= 0; i
< (GLuint
) n
; i
++) {
1122 * Determine if id names a vertex or fragment program.
1123 * \note Not compiled into display lists.
1124 * \note Called from both glIsProgramNV and glIsProgramARB.
1125 * \param id is the program identifier
1126 * \return GL_TRUE if id is a program, else GL_FALSE.
1128 GLboolean GLAPIENTRY
1129 _mesa_IsProgram(GLuint id
)
1131 GET_CURRENT_CONTEXT(ctx
);
1132 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1137 if (_mesa_HashLookup(ctx
->Shared
->Programs
, id
))
1145 /**********************************************************************/
1146 /* GL_MESA_program_debug extension */
1147 /**********************************************************************/
1151 GLAPI
void GLAPIENTRY
1152 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1155 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1160 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1163 GET_CURRENT_CONTEXT(ctx
);
1166 case GL_FRAGMENT_PROGRAM_ARB
:
1167 if (!ctx
->Extensions
.ARB_fragment_program
) {
1168 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1171 ctx
->FragmentProgram
.Callback
= callback
;
1172 ctx
->FragmentProgram
.CallbackData
= data
;
1174 case GL_FRAGMENT_PROGRAM_NV
:
1175 if (!ctx
->Extensions
.NV_fragment_program
) {
1176 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1179 ctx
->FragmentProgram
.Callback
= callback
;
1180 ctx
->FragmentProgram
.CallbackData
= data
;
1182 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1183 if (!ctx
->Extensions
.ARB_vertex_program
&&
1184 !ctx
->Extensions
.NV_vertex_program
) {
1185 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1188 ctx
->VertexProgram
.Callback
= callback
;
1189 ctx
->VertexProgram
.CallbackData
= data
;
1192 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1199 GLAPI
void GLAPIENTRY
1200 glGetProgramRegisterfvMESA(GLenum target
,
1201 GLsizei len
, const GLubyte
*registerName
,
1204 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1209 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1210 GLsizei len
, const GLubyte
*registerName
,
1214 GET_CURRENT_CONTEXT(ctx
);
1216 /* We _should_ be inside glBegin/glEnd */
1218 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1219 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1224 /* make null-terminated copy of registerName */
1225 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1226 _mesa_memcpy(reg
, registerName
, len
);
1230 case GL_VERTEX_PROGRAM_NV
:
1231 if (!ctx
->Extensions
.ARB_vertex_program
&&
1232 !ctx
->Extensions
.NV_vertex_program
) {
1233 _mesa_error(ctx
, GL_INVALID_ENUM
,
1234 "glGetProgramRegisterfvMESA(target)");
1237 if (!ctx
->VertexProgram
._Enabled
) {
1238 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1239 "glGetProgramRegisterfvMESA");
1242 /* GL_NV_vertex_program */
1243 if (reg
[0] == 'R') {
1245 GLint i
= _mesa_atoi(reg
+ 1);
1246 if (i
>= (GLint
)ctx
->Const
.MaxVertexProgramTemps
) {
1247 _mesa_error(ctx
, GL_INVALID_VALUE
,
1248 "glGetProgramRegisterfvMESA(registerName)");
1251 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1253 else if (reg
[0] == 'v' && reg
[1] == '[') {
1254 /* Vertex Input attribute */
1256 for (i
= 0; i
< ctx
->Const
.MaxVertexProgramAttribs
; i
++) {
1257 const char *name
= _mesa_nv_vertex_input_register_name(i
);
1259 sprintf(number
, "%d", i
);
1260 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
1261 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
1262 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
1266 _mesa_error(ctx
, GL_INVALID_VALUE
,
1267 "glGetProgramRegisterfvMESA(registerName)");
1270 else if (reg
[0] == 'o' && reg
[1] == '[') {
1271 /* Vertex output attribute */
1273 /* GL_ARB_vertex_program */
1274 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1278 _mesa_error(ctx
, GL_INVALID_VALUE
,
1279 "glGetProgramRegisterfvMESA(registerName)");
1283 case GL_FRAGMENT_PROGRAM_ARB
:
1284 if (!ctx
->Extensions
.ARB_fragment_program
) {
1285 _mesa_error(ctx
, GL_INVALID_ENUM
,
1286 "glGetProgramRegisterfvMESA(target)");
1289 if (!ctx
->FragmentProgram
._Enabled
) {
1290 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1291 "glGetProgramRegisterfvMESA");
1296 case GL_FRAGMENT_PROGRAM_NV
:
1297 if (!ctx
->Extensions
.NV_fragment_program
) {
1298 _mesa_error(ctx
, GL_INVALID_ENUM
,
1299 "glGetProgramRegisterfvMESA(target)");
1302 if (!ctx
->FragmentProgram
._Enabled
) {
1303 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1304 "glGetProgramRegisterfvMESA");
1307 if (reg
[0] == 'R') {
1309 GLint i
= _mesa_atoi(reg
+ 1);
1310 if (i
>= (GLint
)ctx
->Const
.MaxFragmentProgramTemps
) {
1311 _mesa_error(ctx
, GL_INVALID_VALUE
,
1312 "glGetProgramRegisterfvMESA(registerName)");
1315 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
1317 else if (reg
[0] == 'f' && reg
[1] == '[') {
1318 /* Fragment input attribute */
1320 for (i
= 0; i
< ctx
->Const
.MaxFragmentProgramAttribs
; i
++) {
1321 const char *name
= _mesa_nv_fragment_input_register_name(i
);
1322 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
1323 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
1327 _mesa_error(ctx
, GL_INVALID_VALUE
,
1328 "glGetProgramRegisterfvMESA(registerName)");
1331 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
1332 /* Fragment output color */
1333 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
]);
1335 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
1336 /* Fragment output color */
1337 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLH
]);
1339 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
1340 /* Fragment output depth */
1341 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
]);
1344 /* try user-defined identifiers */
1345 const GLfloat
*value
= _mesa_lookup_parameter_value(
1346 ctx
->FragmentProgram
.Current
->Parameters
, -1, reg
);
1351 _mesa_error(ctx
, GL_INVALID_VALUE
,
1352 "glGetProgramRegisterfvMESA(registerName)");
1358 _mesa_error(ctx
, GL_INVALID_ENUM
,
1359 "glGetProgramRegisterfvMESA(target)");