2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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 "program_instruction.h"
41 #include "nvvertparse.h"
42 #include "atifragshader.h"
46 make_state_string(const GLint stateTokens
[6]);
49 /**********************************************************************/
50 /* Utility functions */
51 /**********************************************************************/
54 /* A pointer to this dummy program is put into the hash table when
55 * glGenPrograms is called.
57 struct program _mesa_DummyProgram
;
61 * Init context's vertex/fragment program state
64 _mesa_init_program(GLcontext
*ctx
)
68 ctx
->Program
.ErrorPos
= -1;
69 ctx
->Program
.ErrorString
= _mesa_strdup("");
71 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
72 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
73 ctx
->VertexProgram
.PointSizeEnabled
= GL_FALSE
;
74 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
75 ctx
->VertexProgram
.Current
= (struct vertex_program
*) ctx
->Shared
->DefaultVertexProgram
;
76 assert(ctx
->VertexProgram
.Current
);
77 ctx
->VertexProgram
.Current
->Base
.RefCount
++;
78 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
79 ctx
->VertexProgram
.TrackMatrix
[i
] = GL_NONE
;
80 ctx
->VertexProgram
.TrackMatrixTransform
[i
] = GL_IDENTITY_NV
;
84 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
85 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
86 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) ctx
->Shared
->DefaultFragmentProgram
;
87 assert(ctx
->FragmentProgram
.Current
);
88 ctx
->FragmentProgram
.Current
->Base
.RefCount
++;
91 #if FEATURE_ATI_fragment_shader
92 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
93 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
94 assert(ctx
->ATIFragmentShader
.Current
);
95 ctx
->ATIFragmentShader
.Current
->Base
.RefCount
++;
101 * Free a context's vertex/fragment program state
104 _mesa_free_program_data(GLcontext
*ctx
)
106 #if FEATURE_NV_vertex_program
107 if (ctx
->VertexProgram
.Current
) {
108 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
109 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0)
110 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
113 #if FEATURE_NV_fragment_program
114 if (ctx
->FragmentProgram
.Current
) {
115 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
116 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0)
117 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
120 #if FEATURE_ATI_fragment_shader
121 if (ctx
->ATIFragmentShader
.Current
) {
122 ctx
->ATIFragmentShader
.Current
->Base
.RefCount
--;
123 if (ctx
->ATIFragmentShader
.Current
->Base
.RefCount
<= 0)
124 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->ATIFragmentShader
.Current
->Base
));
127 _mesa_free((void *) ctx
->Program
.ErrorString
);
134 * Set the vertex/fragment program error state (position and error string).
135 * This is generally called from within the parsers.
138 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
140 ctx
->Program
.ErrorPos
= pos
;
141 _mesa_free((void *) ctx
->Program
.ErrorString
);
144 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
149 * Find the line number and column for 'pos' within 'string'.
150 * Return a copy of the line which contains 'pos'. Free the line with
152 * \param string the program string
153 * \param pos the position within the string
154 * \param line returns the line number corresponding to 'pos'.
155 * \param col returns the column number corresponding to 'pos'.
156 * \return copy of the line containing 'pos'.
159 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
160 GLint
*line
, GLint
*col
)
162 const GLubyte
*lineStart
= string
;
163 const GLubyte
*p
= string
;
170 if (*p
== (GLubyte
) '\n') {
177 *col
= (pos
- lineStart
) + 1;
179 /* return copy of this line */
180 while (*p
!= 0 && *p
!= '\n')
183 s
= (GLubyte
*) _mesa_malloc(len
+ 1);
184 _mesa_memcpy(s
, lineStart
, len
);
192 * Initialize a new vertex/fragment program object.
194 static struct program
*
195 _mesa_init_program_struct( GLcontext
*ctx
, struct program
*prog
,
196 GLenum target
, GLuint id
)
201 prog
->Target
= target
;
202 prog
->Resident
= GL_TRUE
;
211 * Initialize a new fragment program object.
214 _mesa_init_fragment_program( GLcontext
*ctx
, struct fragment_program
*prog
,
215 GLenum target
, GLuint id
)
218 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
225 * Initialize a new vertex program object.
228 _mesa_init_vertex_program( GLcontext
*ctx
, struct vertex_program
*prog
,
229 GLenum target
, GLuint id
)
232 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
238 * Initialize a new ATI fragment shader object.
241 _mesa_init_ati_fragment_shader( GLcontext
*ctx
,
242 struct ati_fragment_shader
*prog
,
243 GLenum target
, GLuint id
)
246 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
254 * Allocate and initialize a new fragment/vertex program object but
255 * don't put it into the program hash table. Called via
256 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
257 * device driver function to implement OO deriviation with additional
258 * types not understood by this function.
261 * \param id program id/number
262 * \param target program target/type
263 * \return pointer to new program object
266 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
269 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
270 return _mesa_init_vertex_program( ctx
, CALLOC_STRUCT(vertex_program
),
272 case GL_FRAGMENT_PROGRAM_NV
:
273 case GL_FRAGMENT_PROGRAM_ARB
:
274 return _mesa_init_fragment_program( ctx
, CALLOC_STRUCT(fragment_program
),
276 case GL_FRAGMENT_SHADER_ATI
:
277 return _mesa_init_ati_fragment_shader( ctx
, CALLOC_STRUCT(ati_fragment_shader
),
281 _mesa_problem(ctx
, "bad target in _mesa_new_program");
288 * Delete a program and remove it from the hash table, ignoring the
290 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
291 * by a device driver function.
294 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
300 _mesa_free(prog
->String
);
302 if (prog
->Instructions
) {
304 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
305 if (prog
->Instructions
[i
].Data
)
306 _mesa_free(prog
->Instructions
[i
].Data
);
308 _mesa_free(prog
->Instructions
);
311 if (prog
->Parameters
)
312 _mesa_free_parameter_list(prog
->Parameters
);
314 if (prog
->Target
== GL_FRAGMENT_SHADER_ATI
) {
315 struct ati_fragment_shader
*atifs
= (struct ati_fragment_shader
*)prog
;
317 for (i
= 0; i
< MAX_NUM_PASSES_ATI
; i
++) {
318 if (atifs
->Instructions
[i
])
319 _mesa_free(atifs
->Instructions
[i
]);
320 if (atifs
->SetupInst
[i
])
321 _mesa_free(atifs
->SetupInst
[i
]);
330 /**********************************************************************/
331 /* Program parameter functions */
332 /**********************************************************************/
334 struct program_parameter_list
*
335 _mesa_new_parameter_list(void)
337 return (struct program_parameter_list
*)
338 _mesa_calloc(sizeof(struct program_parameter_list
));
343 * Free a parameter list and all its parameters
346 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
348 _mesa_free_parameters(paramList
);
349 _mesa_free(paramList
->Parameters
);
350 if (paramList
->ParameterValues
)
351 ALIGN_FREE(paramList
->ParameterValues
);
352 _mesa_free(paramList
);
357 * Free all the parameters in the given list, but don't free the
358 * paramList structure itself.
361 _mesa_free_parameters(struct program_parameter_list
*paramList
)
364 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
365 if (paramList
->Parameters
[i
].Name
)
366 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
368 paramList
->NumParameters
= 0;
373 * Helper function used by the functions below.
374 * \return index of new parameter in the list, or -1 if error (out of mem)
377 add_parameter(struct program_parameter_list
*paramList
,
378 const char *name
, const GLfloat values
[4],
379 enum register_file type
)
381 const GLuint n
= paramList
->NumParameters
;
383 if (n
== paramList
->Size
) {
386 paramList
->Size
*= 2;
387 if (!paramList
->Size
)
390 paramList
->Parameters
= (struct program_parameter
*)
391 _mesa_realloc(paramList
->Parameters
,
392 n
* sizeof(struct program_parameter
),
393 paramList
->Size
* sizeof(struct program_parameter
));
395 tmp
= paramList
->ParameterValues
;
396 paramList
->ParameterValues
= ALIGN_MALLOC(paramList
->Size
* 4 * sizeof(GLfloat
), 16);
398 _mesa_memcpy(paramList
->ParameterValues
, tmp
,
399 n
* 4 * sizeof(GLfloat
));
404 if (!paramList
->Parameters
||
405 !paramList
->ParameterValues
) {
407 paramList
->NumParameters
= 0;
412 paramList
->NumParameters
= n
+ 1;
414 _mesa_memset(¶mList
->Parameters
[n
], 0,
415 sizeof(struct program_parameter
));
417 paramList
->Parameters
[n
].Name
= name
? _mesa_strdup(name
) : NULL
;
418 paramList
->Parameters
[n
].Type
= type
;
420 COPY_4V(paramList
->ParameterValues
[n
], values
);
427 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
428 * \return index of the new entry in the parameter list
431 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
432 const char *name
, const GLfloat values
[4])
434 return add_parameter(paramList
, name
, values
, PROGRAM_NAMED_PARAM
);
439 * Add a new unnamed constant to the parameter list.
440 * \param paramList - the parameter list
441 * \param values - four float values
442 * \return index of the new parameter.
445 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
446 const char *name
, const GLfloat values
[4])
448 return add_parameter(paramList
, name
, values
, PROGRAM_CONSTANT
);
453 * Add a new unnamed constant to the parameter list.
454 * \param paramList - the parameter list
455 * \param values - four float values
456 * \return index of the new parameter.
459 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
460 const GLfloat values
[4])
462 return add_parameter(paramList
, NULL
, values
, PROGRAM_CONSTANT
);
467 * Add a new state reference to the parameter list.
468 * \param paramList - the parameter list
469 * \param state - an array of 6 state tokens
471 * \return index of the new parameter.
474 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
475 const GLint
*stateTokens
)
477 /* XXX we should probably search the current parameter list to see if
478 * the new state reference is already present.
481 const char *name
= make_state_string(stateTokens
);
483 index
= add_parameter(paramList
, name
, NULL
, PROGRAM_STATE_VAR
);
486 for (i
= 0; i
< 6; i
++)
487 paramList
->Parameters
[index
].StateIndexes
[i
]
488 = (enum state_index
) stateTokens
[i
];
496 * Lookup a parameter value by name in the given parameter list.
497 * \return pointer to the float[4] values.
500 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
501 GLsizei nameLen
, const char *name
)
509 /* name is null-terminated */
510 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
511 if (paramList
->Parameters
[i
].Name
&&
512 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
513 return paramList
->ParameterValues
[i
];
517 /* name is not null-terminated, use nameLen */
518 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
519 if (paramList
->Parameters
[i
].Name
&&
520 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
521 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
522 return paramList
->ParameterValues
[i
];
530 * Lookup a parameter index by name in the given parameter list.
531 * \return index of parameter in the list.
534 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
535 GLsizei nameLen
, const char *name
)
543 /* name is null-terminated */
544 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
545 if (paramList
->Parameters
[i
].Name
&&
546 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
551 /* name is not null-terminated, use nameLen */
552 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
553 if (paramList
->Parameters
[i
].Name
&&
554 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
555 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
564 * Use the list of tokens in the state[] array to find global GL state
565 * and return it in <value>. Usually, four values are returned in <value>
566 * but matrix queries may return as many as 16 values.
567 * This function is used for ARB vertex/fragment programs.
568 * The program parser will produce the state[] values.
571 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
577 /* state[1] is either 0=front or 1=back side */
578 const GLuint face
= (GLuint
) state
[1];
579 /* state[2] is the material attribute */
583 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
585 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
589 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
591 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
595 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
597 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
601 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
603 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
605 case STATE_SHININESS
:
607 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
609 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
615 _mesa_problem(ctx
, "Invalid material state in fetch_state");
621 /* state[1] is the light number */
622 const GLuint ln
= (GLuint
) state
[1];
623 /* state[2] is the light attribute */
626 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
629 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
632 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
635 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
637 case STATE_ATTENUATION
:
638 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
639 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
640 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
641 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
643 case STATE_SPOT_DIRECTION
:
644 COPY_3V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
645 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
649 GLfloat eye_z
[] = {0, 0, 1};
651 /* Compute infinite half angle vector:
652 * half-vector = light_position + (0, 0, 1)
653 * and then normalize. w = 0
655 * light.EyePosition.w should be 0 for infinite lights.
657 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
658 NORMALIZE_3FV(value
);
662 case STATE_POSITION_NORMALIZED
:
663 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
664 NORMALIZE_3FV( value
);
667 _mesa_problem(ctx
, "Invalid light state in fetch_state");
671 case STATE_LIGHTMODEL_AMBIENT
:
672 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
674 case STATE_LIGHTMODEL_SCENECOLOR
:
678 for (i
= 0; i
< 3; i
++) {
679 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
680 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
681 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
683 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
688 for (i
= 0; i
< 3; i
++) {
689 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
690 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
691 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
693 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
696 case STATE_LIGHTPROD
:
698 const GLuint ln
= (GLuint
) state
[1];
699 const GLuint face
= (GLuint
) state
[2];
701 ASSERT(face
== 0 || face
== 1);
704 for (i
= 0; i
< 3; i
++) {
705 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
706 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
708 /* [3] = material alpha */
709 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
712 for (i
= 0; i
< 3; i
++) {
713 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
714 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
716 /* [3] = material alpha */
717 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
720 for (i
= 0; i
< 3; i
++) {
721 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
722 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
724 /* [3] = material alpha */
725 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
728 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
734 /* state[1] is the texture unit */
735 const GLuint unit
= (GLuint
) state
[1];
736 /* state[2] is the texgen attribute */
738 case STATE_TEXGEN_EYE_S
:
739 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
741 case STATE_TEXGEN_EYE_T
:
742 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
744 case STATE_TEXGEN_EYE_R
:
745 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
747 case STATE_TEXGEN_EYE_Q
:
748 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
750 case STATE_TEXGEN_OBJECT_S
:
751 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
753 case STATE_TEXGEN_OBJECT_T
:
754 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
756 case STATE_TEXGEN_OBJECT_R
:
757 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
759 case STATE_TEXGEN_OBJECT_Q
:
760 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
763 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
767 case STATE_TEXENV_COLOR
:
769 /* state[1] is the texture unit */
770 const GLuint unit
= (GLuint
) state
[1];
771 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
774 case STATE_FOG_COLOR
:
775 COPY_4V(value
, ctx
->Fog
.Color
);
777 case STATE_FOG_PARAMS
:
778 value
[0] = ctx
->Fog
.Density
;
779 value
[1] = ctx
->Fog
.Start
;
780 value
[2] = ctx
->Fog
.End
;
781 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
783 case STATE_CLIPPLANE
:
785 const GLuint plane
= (GLuint
) state
[1];
786 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
789 case STATE_POINT_SIZE
:
790 value
[0] = ctx
->Point
.Size
;
791 value
[1] = ctx
->Point
.MinSize
;
792 value
[2] = ctx
->Point
.MaxSize
;
793 value
[3] = ctx
->Point
.Threshold
;
795 case STATE_POINT_ATTENUATION
:
796 value
[0] = ctx
->Point
.Params
[0];
797 value
[1] = ctx
->Point
.Params
[1];
798 value
[2] = ctx
->Point
.Params
[2];
803 /* state[1] = modelview, projection, texture, etc. */
804 /* state[2] = which texture matrix or program matrix */
805 /* state[3] = first column to fetch */
806 /* state[4] = last column to fetch */
807 /* state[5] = transpose, inverse or invtrans */
809 const GLmatrix
*matrix
;
810 const enum state_index mat
= state
[1];
811 const GLuint index
= (GLuint
) state
[2];
812 const GLuint first
= (GLuint
) state
[3];
813 const GLuint last
= (GLuint
) state
[4];
814 const enum state_index modifier
= state
[5];
817 if (mat
== STATE_MODELVIEW
) {
818 matrix
= ctx
->ModelviewMatrixStack
.Top
;
820 else if (mat
== STATE_PROJECTION
) {
821 matrix
= ctx
->ProjectionMatrixStack
.Top
;
823 else if (mat
== STATE_MVP
) {
824 matrix
= &ctx
->_ModelProjectMatrix
;
826 else if (mat
== STATE_TEXTURE
) {
827 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
829 else if (mat
== STATE_PROGRAM
) {
830 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
833 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
836 if (modifier
== STATE_MATRIX_INVERSE
||
837 modifier
== STATE_MATRIX_INVTRANS
) {
838 /* Be sure inverse is up to date:
840 _math_matrix_analyse( (GLmatrix
*) matrix
);
846 if (modifier
== STATE_MATRIX_TRANSPOSE
||
847 modifier
== STATE_MATRIX_INVTRANS
) {
848 for (i
= 0, row
= first
; row
<= last
; row
++) {
849 value
[i
++] = m
[row
* 4 + 0];
850 value
[i
++] = m
[row
* 4 + 1];
851 value
[i
++] = m
[row
* 4 + 2];
852 value
[i
++] = m
[row
* 4 + 3];
856 for (i
= 0, row
= first
; row
<= last
; row
++) {
857 value
[i
++] = m
[row
+ 0];
858 value
[i
++] = m
[row
+ 4];
859 value
[i
++] = m
[row
+ 8];
860 value
[i
++] = m
[row
+ 12];
865 case STATE_DEPTH_RANGE
:
866 value
[0] = ctx
->Viewport
.Near
; /* near */
867 value
[1] = ctx
->Viewport
.Far
; /* far */
868 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
871 case STATE_FRAGMENT_PROGRAM
:
873 /* state[1] = {STATE_ENV, STATE_LOCAL} */
874 /* state[2] = parameter index */
875 const int idx
= (int) state
[2];
878 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
881 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
884 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
890 case STATE_VERTEX_PROGRAM
:
892 /* state[1] = {STATE_ENV, STATE_LOCAL} */
893 /* state[2] = parameter index */
894 const int idx
= (int) state
[2];
897 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
900 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
903 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
912 case STATE_NORMAL_SCALE
:
913 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
916 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
923 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
930 append(char *dst
, const char *src
)
940 append_token(char *dst
, enum state_index k
)
944 append(dst
, "material.");
947 append(dst
, "light");
949 case STATE_LIGHTMODEL_AMBIENT
:
950 append(dst
, "lightmodel.ambient");
952 case STATE_LIGHTMODEL_SCENECOLOR
:
954 case STATE_LIGHTPROD
:
955 append(dst
, "lightprod");
958 append(dst
, "texgen");
960 case STATE_FOG_COLOR
:
961 append(dst
, "fog.color");
963 case STATE_FOG_PARAMS
:
964 append(dst
, "fog.params");
966 case STATE_CLIPPLANE
:
969 case STATE_POINT_SIZE
:
970 append(dst
, "point.size");
972 case STATE_POINT_ATTENUATION
:
973 append(dst
, "point.attenuation");
976 append(dst
, "matrix.");
978 case STATE_MODELVIEW
:
979 append(dst
, "modelview");
981 case STATE_PROJECTION
:
982 append(dst
, "projection");
988 append(dst
, "texture");
991 append(dst
, "program");
993 case STATE_MATRIX_INVERSE
:
994 append(dst
, ".inverse");
996 case STATE_MATRIX_TRANSPOSE
:
997 append(dst
, ".transpose");
999 case STATE_MATRIX_INVTRANS
:
1000 append(dst
, ".invtrans");
1003 append(dst
, "ambient");
1006 append(dst
, "diffuse");
1008 case STATE_SPECULAR
:
1009 append(dst
, "specular");
1011 case STATE_EMISSION
:
1012 append(dst
, "emission");
1014 case STATE_SHININESS
:
1015 append(dst
, "shininess");
1018 append(dst
, "half");
1020 case STATE_POSITION
:
1021 append(dst
, ".position");
1023 case STATE_ATTENUATION
:
1024 append(dst
, ".attenuation");
1026 case STATE_SPOT_DIRECTION
:
1027 append(dst
, ".spot.direction");
1029 case STATE_TEXGEN_EYE_S
:
1030 append(dst
, "eye.s");
1032 case STATE_TEXGEN_EYE_T
:
1033 append(dst
, "eye.t");
1035 case STATE_TEXGEN_EYE_R
:
1036 append(dst
, "eye.r");
1038 case STATE_TEXGEN_EYE_Q
:
1039 append(dst
, "eye.q");
1041 case STATE_TEXGEN_OBJECT_S
:
1042 append(dst
, "object.s");
1044 case STATE_TEXGEN_OBJECT_T
:
1045 append(dst
, "object.t");
1047 case STATE_TEXGEN_OBJECT_R
:
1048 append(dst
, "object.r");
1050 case STATE_TEXGEN_OBJECT_Q
:
1051 append(dst
, "object.q");
1053 case STATE_TEXENV_COLOR
:
1054 append(dst
, "texenv");
1056 case STATE_DEPTH_RANGE
:
1057 append(dst
, "depth.range");
1059 case STATE_VERTEX_PROGRAM
:
1060 case STATE_FRAGMENT_PROGRAM
:
1066 append(dst
, "local");
1068 case STATE_INTERNAL
:
1069 case STATE_NORMAL_SCALE
:
1070 case STATE_POSITION_NORMALIZED
:
1071 append(dst
, "(internal)");
1079 append_face(char *dst
, GLint face
)
1082 append(dst
, "front.");
1084 append(dst
, "back.");
1088 append_index(char *dst
, GLint index
)
1091 _mesa_sprintf(s
, "[%d].", index
);
1096 * Make a string from the given state vector.
1097 * For example, return "state.matrix.texture[2].inverse".
1100 make_state_string(const GLint state
[6])
1102 char str
[1000] = "";
1105 append(str
, "state.");
1106 append_token(str
, state
[0]);
1109 case STATE_MATERIAL
:
1110 append_face(str
, state
[1]);
1111 append_token(str
, state
[2]);
1114 append(str
, "light");
1115 append_index(str
, state
[1]); /* light number [i]. */
1116 append_token(str
, state
[2]); /* coefficients */
1118 case STATE_LIGHTMODEL_AMBIENT
:
1119 append(str
, "lightmodel.ambient");
1121 case STATE_LIGHTMODEL_SCENECOLOR
:
1122 if (state
[1] == 0) {
1123 append(str
, "lightmodel.front.scenecolor");
1126 append(str
, "lightmodel.back.scenecolor");
1129 case STATE_LIGHTPROD
:
1130 append_index(str
, state
[1]); /* light number [i]. */
1131 append_face(str
, state
[2]);
1132 append_token(str
, state
[3]);
1135 append_index(str
, state
[1]); /* tex unit [i] */
1136 append_token(str
, state
[2]); /* plane coef */
1138 case STATE_TEXENV_COLOR
:
1139 append_index(str
, state
[1]); /* tex unit [i] */
1140 append(str
, "color");
1142 case STATE_FOG_COLOR
:
1143 case STATE_FOG_PARAMS
:
1145 case STATE_CLIPPLANE
:
1146 append_index(str
, state
[1]); /* plane [i] */
1147 append(str
, "plane");
1149 case STATE_POINT_SIZE
:
1150 case STATE_POINT_ATTENUATION
:
1154 /* state[1] = modelview, projection, texture, etc. */
1155 /* state[2] = which texture matrix or program matrix */
1156 /* state[3] = first column to fetch */
1157 /* state[4] = last column to fetch */
1158 /* state[5] = transpose, inverse or invtrans */
1159 const enum state_index mat
= state
[1];
1160 const GLuint index
= (GLuint
) state
[2];
1161 const GLuint first
= (GLuint
) state
[3];
1162 const GLuint last
= (GLuint
) state
[4];
1163 const enum state_index modifier
= state
[5];
1164 append_token(str
, mat
);
1166 append_index(str
, index
);
1168 append_token(str
, modifier
);
1170 _mesa_sprintf(tmp
, ".row[%d]", first
);
1172 _mesa_sprintf(tmp
, ".row[%d..%d]", first
, last
);
1176 case STATE_DEPTH_RANGE
:
1178 case STATE_FRAGMENT_PROGRAM
:
1179 case STATE_VERTEX_PROGRAM
:
1180 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1181 /* state[2] = parameter index */
1182 append_token(str
, state
[1]);
1183 append_index(str
, state
[2]);
1185 case STATE_INTERNAL
:
1188 _mesa_problem(NULL
, "Invalid state in maka_state_string");
1192 return _mesa_strdup(str
);
1197 * Loop over all the parameters in a parameter list. If the parameter
1198 * is a GL state reference, look up the current value of that state
1199 * variable and put it into the parameter's Value[4] array.
1200 * This would be called at glBegin time when using a fragment program.
1203 _mesa_load_state_parameters(GLcontext
*ctx
,
1204 struct program_parameter_list
*paramList
)
1211 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1212 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1213 _mesa_fetch_state(ctx
,
1214 paramList
->Parameters
[i
].StateIndexes
,
1215 paramList
->ParameterValues
[i
]);
1222 * Basic info about each instruction
1224 struct instruction_info
1226 enum prog_opcode Opcode
;
1233 * \note Opcode should equal array index!
1235 static const struct instruction_info InstInfo
[MAX_OPCODE
] = {
1236 { OPCODE_ABS
, "ABS", 1 },
1237 { OPCODE_ADD
, "ADD", 2 },
1238 { OPCODE_ARA
, "ARA", 1 },
1239 { OPCODE_ARL
, "ARL", 1 },
1240 { OPCODE_ARL_NV
, "ARL", 1 },
1241 { OPCODE_ARR
, "ARL", 1 },
1242 { OPCODE_BRA
, "BRA", 1 },
1243 { OPCODE_CAL
, "CAL", 1 },
1244 { OPCODE_CMP
, "CMP", 3 },
1245 { OPCODE_COS
, "COS", 1 },
1246 { OPCODE_DDX
, "DDX", 1 },
1247 { OPCODE_DDY
, "DDY", 1 },
1248 { OPCODE_DP3
, "DP3", 2 },
1249 { OPCODE_DP4
, "DP4", 2 },
1250 { OPCODE_DPH
, "DPH", 2 },
1251 { OPCODE_DST
, "DST", 2 },
1252 { OPCODE_END
, "END", 0 },
1253 { OPCODE_EX2
, "EX2", 1 },
1254 { OPCODE_EXP
, "EXP", 1 },
1255 { OPCODE_FLR
, "FLR", 1 },
1256 { OPCODE_FRC
, "FRC", 1 },
1257 { OPCODE_KIL
, "KIL", 1 },
1258 { OPCODE_KIL_NV
, "KIL", 0 },
1259 { OPCODE_LG2
, "LG2", 1 },
1260 { OPCODE_LIT
, "LIT", 1 },
1261 { OPCODE_LOG
, "LOG", 1 },
1262 { OPCODE_LRP
, "LRP", 3 },
1263 { OPCODE_MAD
, "MAD", 3 },
1264 { OPCODE_MAX
, "MAX", 2 },
1265 { OPCODE_MIN
, "MIN", 2 },
1266 { OPCODE_MOV
, "MOV", 1 },
1267 { OPCODE_MUL
, "MUL", 2 },
1268 { OPCODE_PK2H
, "PK2H", 1 },
1269 { OPCODE_PK2US
, "PK2US", 1 },
1270 { OPCODE_PK4B
, "PK4B", 1 },
1271 { OPCODE_PK4UB
, "PK4UB", 1 },
1272 { OPCODE_POW
, "POW", 2 },
1273 { OPCODE_POPA
, "POPA", 0 },
1274 { OPCODE_PRINT
, "PRINT", 1 },
1275 { OPCODE_PUSHA
, "PUSHA", 0 },
1276 { OPCODE_RCC
, "RCC", 1 },
1277 { OPCODE_RCP
, "RCP", 1 },
1278 { OPCODE_RET
, "RET", 1 },
1279 { OPCODE_RFL
, "RFL", 1 },
1280 { OPCODE_RSQ
, "RSQ", 1 },
1281 { OPCODE_SCS
, "SCS", 1 },
1282 { OPCODE_SEQ
, "SEQ", 2 },
1283 { OPCODE_SFL
, "SFL", 0 },
1284 { OPCODE_SGE
, "SGE", 2 },
1285 { OPCODE_SGT
, "SGT", 2 },
1286 { OPCODE_SIN
, "SIN", 1 },
1287 { OPCODE_SLE
, "SLE", 2 },
1288 { OPCODE_SLT
, "SLT", 2 },
1289 { OPCODE_SNE
, "SNE", 2 },
1290 { OPCODE_SSG
, "SSG", 1 },
1291 { OPCODE_STR
, "STR", 0 },
1292 { OPCODE_SUB
, "SUB", 2 },
1293 { OPCODE_SWZ
, "SWZ", 1 },
1294 { OPCODE_TEX
, "TEX", 1 },
1295 { OPCODE_TXB
, "TXB", 1 },
1296 { OPCODE_TXD
, "TXD", 3 },
1297 { OPCODE_TXL
, "TXL", 1 },
1298 { OPCODE_TXP
, "TXP", 1 },
1299 { OPCODE_TXP_NV
, "TXP", 1 },
1300 { OPCODE_UP2H
, "UP2H", 1 },
1301 { OPCODE_UP2US
, "UP2US", 1 },
1302 { OPCODE_UP4B
, "UP4B", 1 },
1303 { OPCODE_UP4UB
, "UP4UB", 1 },
1304 { OPCODE_X2D
, "X2D", 3 },
1305 { OPCODE_XPD
, "XPD", 2 }
1310 * Return the number of src registers for the given instruction/opcode.
1313 _mesa_num_inst_src_regs(enum prog_opcode opcode
)
1317 for (i
= 0; i
< MAX_OPCODE
; i
++) {
1318 ASSERT(i
== InstInfo
[i
].Opcode
);
1321 for (i
= 0; i
< MAX_OPCODE
; i
++) {
1322 if (InstInfo
[i
].Opcode
== opcode
) {
1323 return InstInfo
[i
].NumSrcRegs
;
1326 _mesa_problem(NULL
, "invalid opcode in _mesa_num_inst_src_regs");
1332 * Return string name for given program opcode.
1335 _mesa_opcode_string(enum prog_opcode opcode
)
1337 ASSERT(opcode
< MAX_OPCODE
);
1338 return InstInfo
[opcode
].Name
;
1342 * Return string name for given program/register file.
1345 program_file_string(enum register_file f
)
1348 case PROGRAM_TEMPORARY
:
1350 case PROGRAM_LOCAL_PARAM
:
1352 case PROGRAM_ENV_PARAM
:
1354 case PROGRAM_STATE_VAR
:
1358 case PROGRAM_OUTPUT
:
1360 case PROGRAM_NAMED_PARAM
:
1362 case PROGRAM_CONSTANT
:
1364 case PROGRAM_WRITE_ONLY
:
1365 return "WRITE_ONLY";
1366 case PROGRAM_ADDRESS
:
1375 * Return a string representation of the given swizzle word.
1376 * If extended is true, use extended (comma-separated) format.
1379 swizzle_string(GLuint swizzle
, GLuint negateBase
, GLboolean extended
)
1381 static const char swz
[] = "xyzw01";
1385 if (!extended
&& swizzle
== SWIZZLE_NOOP
&& negateBase
== 0)
1386 return ""; /* no swizzle/negation */
1391 if (negateBase
& 0x1)
1393 s
[i
++] = swz
[GET_SWZ(swizzle
, 0)];
1399 if (negateBase
& 0x2)
1401 s
[i
++] = swz
[GET_SWZ(swizzle
, 1)];
1407 if (negateBase
& 0x4)
1409 s
[i
++] = swz
[GET_SWZ(swizzle
, 2)];
1415 if (negateBase
& 0x8)
1417 s
[i
++] = swz
[GET_SWZ(swizzle
, 3)];
1425 writemask_string(GLuint writeMask
)
1430 if (writeMask
== WRITEMASK_XYZW
)
1434 if (writeMask
& WRITEMASK_X
)
1436 if (writeMask
& WRITEMASK_Y
)
1438 if (writeMask
& WRITEMASK_Z
)
1440 if (writeMask
& WRITEMASK_W
)
1448 print_dst_reg(const struct prog_dst_register
*dstReg
)
1450 _mesa_printf(" %s[%d]%s",
1451 program_file_string(dstReg
->File
),
1453 writemask_string(dstReg
->WriteMask
));
1457 print_src_reg(const struct prog_src_register
*srcReg
)
1459 _mesa_printf("%s[%d]%s",
1460 program_file_string(srcReg
->File
),
1462 swizzle_string(srcReg
->Swizzle
,
1463 srcReg
->NegateBase
, GL_FALSE
));
1468 * Print a single vertex/fragment program instruction.
1471 _mesa_print_instruction(const struct prog_instruction
*inst
)
1473 switch (inst
->Opcode
) {
1475 _mesa_printf("PRINT '%s'", inst
->Data
);
1476 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1478 _mesa_printf("%s[%d]%s",
1479 program_file_string(inst
->SrcReg
[0].File
),
1480 inst
->SrcReg
[0].Index
,
1481 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1482 inst
->SrcReg
[0].NegateBase
, GL_FALSE
));
1484 _mesa_printf(";\n");
1487 _mesa_printf("SWZ");
1489 _mesa_printf("_SAT");
1490 print_dst_reg(&inst
->DstReg
);
1491 _mesa_printf("%s[%d], %s;\n",
1492 program_file_string(inst
->SrcReg
[0].File
),
1493 inst
->SrcReg
[0].Index
,
1494 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1495 inst
->SrcReg
[0].NegateBase
, GL_TRUE
));
1500 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1502 _mesa_printf("_SAT");
1504 print_dst_reg(&inst
->DstReg
);
1506 print_src_reg(&inst
->SrcReg
[0]);
1507 _mesa_printf(", texture[%d], ", inst
->TexSrcUnit
);
1508 switch (inst
->TexSrcTarget
) {
1509 case TEXTURE_1D_INDEX
: _mesa_printf("1D"); break;
1510 case TEXTURE_2D_INDEX
: _mesa_printf("2D"); break;
1511 case TEXTURE_3D_INDEX
: _mesa_printf("3D"); break;
1512 case TEXTURE_CUBE_INDEX
: _mesa_printf("CUBE"); break;
1513 case TEXTURE_RECT_INDEX
: _mesa_printf("RECT"); break;
1520 _mesa_printf("ARL addr.x, ");
1521 print_src_reg(&inst
->SrcReg
[0]);
1522 _mesa_printf(";\n");
1524 /* XXX may need for other special-case instructions */
1526 /* typical alu instruction */
1528 const GLuint numRegs
= _mesa_num_inst_src_regs(inst
->Opcode
);
1531 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1533 /* frag prog only */
1535 _mesa_printf("_SAT");
1537 if (inst
->DstReg
.File
!= PROGRAM_UNDEFINED
) {
1538 _mesa_printf(" %s[%d]%s",
1539 program_file_string(inst
->DstReg
.File
),
1541 writemask_string(inst
->DstReg
.WriteMask
));
1547 for (j
= 0; j
< numRegs
; j
++) {
1548 print_src_reg(inst
->SrcReg
+ j
);
1549 if (j
+ 1 < numRegs
)
1553 _mesa_printf(";\n");
1560 * Print a vertx/fragment program to stdout.
1561 * XXX this function could be greatly improved.
1564 _mesa_print_program(const struct program
*prog
)
1567 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1568 _mesa_printf("%3d: ", i
);
1569 _mesa_print_instruction(prog
->Instructions
+ i
);
1575 * Print all of a program's parameters.
1578 _mesa_print_program_parameters(GLcontext
*ctx
, const struct program
*prog
)
1582 _mesa_printf("NumInstructions=%d\n", prog
->NumInstructions
);
1583 _mesa_printf("NumTemporaries=%d\n", prog
->NumTemporaries
);
1584 _mesa_printf("NumParameters=%d\n", prog
->NumParameters
);
1585 _mesa_printf("NumAttributes=%d\n", prog
->NumAttributes
);
1586 _mesa_printf("NumAddressRegs=%d\n", prog
->NumAddressRegs
);
1588 _mesa_load_state_parameters(ctx
, prog
->Parameters
);
1591 _mesa_printf("Local Params:\n");
1592 for (i
= 0; i
< MAX_PROGRAM_LOCAL_PARAMS
; i
++){
1593 const GLfloat
*p
= prog
->LocalParams
[i
];
1594 _mesa_printf("%2d: %f, %f, %f, %f\n", i
, p
[0], p
[1], p
[2], p
[3]);
1598 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++){
1599 struct program_parameter
*param
= prog
->Parameters
->Parameters
+ i
;
1600 const GLfloat
*v
= prog
->Parameters
->ParameterValues
[i
];
1601 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1602 i
, param
->Name
, v
[0], v
[1], v
[2], v
[3]);
1608 /**********************************************************************/
1610 /**********************************************************************/
1614 * Bind a program (make it current)
1615 * \note Called from the GL API dispatcher by both glBindProgramNV
1616 * and glBindProgramARB.
1619 _mesa_BindProgram(GLenum target
, GLuint id
)
1621 struct program
*prog
;
1622 GET_CURRENT_CONTEXT(ctx
);
1623 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1625 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1627 if ((target
== GL_VERTEX_PROGRAM_NV
1628 && ctx
->Extensions
.NV_vertex_program
) ||
1629 (target
== GL_VERTEX_PROGRAM_ARB
1630 && ctx
->Extensions
.ARB_vertex_program
)) {
1631 /*** Vertex program binding ***/
1632 struct vertex_program
*curProg
= ctx
->VertexProgram
.Current
;
1633 if (curProg
->Base
.Id
== id
) {
1634 /* binding same program - no change */
1637 if (curProg
->Base
.Id
!= 0) {
1638 /* decrement refcount on previously bound vertex program */
1639 curProg
->Base
.RefCount
--;
1640 /* and delete if refcount goes below one */
1641 if (curProg
->Base
.RefCount
<= 0) {
1642 /* the program ID was already removed from the hash table */
1643 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
1647 else if ((target
== GL_FRAGMENT_PROGRAM_NV
1648 && ctx
->Extensions
.NV_fragment_program
) ||
1649 (target
== GL_FRAGMENT_PROGRAM_ARB
1650 && ctx
->Extensions
.ARB_fragment_program
)) {
1651 /*** Fragment program binding ***/
1652 struct fragment_program
*curProg
= ctx
->FragmentProgram
.Current
;
1653 if (curProg
->Base
.Id
== id
) {
1654 /* binding same program - no change */
1657 if (curProg
->Base
.Id
!= 0) {
1658 /* decrement refcount on previously bound fragment program */
1659 curProg
->Base
.RefCount
--;
1660 /* and delete if refcount goes below one */
1661 if (curProg
->Base
.RefCount
<= 0) {
1662 /* the program ID was already removed from the hash table */
1663 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
1668 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
1672 /* NOTE: binding to a non-existant program is not an error.
1673 * That's supposed to be caught in glBegin.
1676 /* Bind default program */
1678 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
1679 prog
= ctx
->Shared
->DefaultVertexProgram
;
1681 prog
= ctx
->Shared
->DefaultFragmentProgram
;
1684 /* Bind user program */
1685 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
1686 if (!prog
|| prog
== &_mesa_DummyProgram
) {
1687 /* allocate a new program now */
1688 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
1690 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
1693 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
1695 else if (prog
->Target
!= target
) {
1696 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1697 "glBindProgramNV/ARB(target mismatch)");
1703 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
1704 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
1706 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
1707 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
1710 /* Never null pointers */
1711 ASSERT(ctx
->VertexProgram
.Current
);
1712 ASSERT(ctx
->FragmentProgram
.Current
);
1717 if (ctx
->Driver
.BindProgram
)
1718 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
1723 * Delete a list of programs.
1724 * \note Not compiled into display lists.
1725 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1728 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
1731 GET_CURRENT_CONTEXT(ctx
);
1732 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1735 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
1739 for (i
= 0; i
< n
; i
++) {
1741 struct program
*prog
= (struct program
*)
1742 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
1743 if (prog
== &_mesa_DummyProgram
) {
1744 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1747 /* Unbind program if necessary */
1748 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
1749 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
1750 if (ctx
->VertexProgram
.Current
&&
1751 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
1752 /* unbind this currently bound program */
1753 _mesa_BindProgram(prog
->Target
, 0);
1756 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
1757 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1758 if (ctx
->FragmentProgram
.Current
&&
1759 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
1760 /* unbind this currently bound program */
1761 _mesa_BindProgram(prog
->Target
, 0);
1765 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
1768 /* The ID is immediately available for re-use now */
1769 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1771 if (prog
->RefCount
<= 0) {
1772 ctx
->Driver
.DeleteProgram(ctx
, prog
);
1781 * Generate a list of new program identifiers.
1782 * \note Not compiled into display lists.
1783 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1786 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1790 GET_CURRENT_CONTEXT(ctx
);
1791 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1794 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1801 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1803 /* Insert pointer to dummy program as placeholder */
1804 for (i
= 0; i
< (GLuint
) n
; i
++) {
1805 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, &_mesa_DummyProgram
);
1808 /* Return the program names */
1809 for (i
= 0; i
< (GLuint
) n
; i
++) {
1816 * Determine if id names a vertex or fragment program.
1817 * \note Not compiled into display lists.
1818 * \note Called from both glIsProgramNV and glIsProgramARB.
1819 * \param id is the program identifier
1820 * \return GL_TRUE if id is a program, else GL_FALSE.
1822 GLboolean GLAPIENTRY
1823 _mesa_IsProgram(GLuint id
)
1825 GET_CURRENT_CONTEXT(ctx
);
1826 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1831 if (_mesa_HashLookup(ctx
->Shared
->Programs
, id
))
1839 /**********************************************************************/
1840 /* GL_MESA_program_debug extension */
1841 /**********************************************************************/
1845 GLAPI
void GLAPIENTRY
1846 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1849 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1854 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1857 GET_CURRENT_CONTEXT(ctx
);
1860 case GL_FRAGMENT_PROGRAM_ARB
:
1861 if (!ctx
->Extensions
.ARB_fragment_program
) {
1862 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1865 ctx
->FragmentProgram
.Callback
= callback
;
1866 ctx
->FragmentProgram
.CallbackData
= data
;
1868 case GL_FRAGMENT_PROGRAM_NV
:
1869 if (!ctx
->Extensions
.NV_fragment_program
) {
1870 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1873 ctx
->FragmentProgram
.Callback
= callback
;
1874 ctx
->FragmentProgram
.CallbackData
= data
;
1876 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1877 if (!ctx
->Extensions
.ARB_vertex_program
&&
1878 !ctx
->Extensions
.NV_vertex_program
) {
1879 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1882 ctx
->VertexProgram
.Callback
= callback
;
1883 ctx
->VertexProgram
.CallbackData
= data
;
1886 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1893 GLAPI
void GLAPIENTRY
1894 glGetProgramRegisterfvMESA(GLenum target
,
1895 GLsizei len
, const GLubyte
*registerName
,
1898 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1903 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1904 GLsizei len
, const GLubyte
*registerName
,
1908 GET_CURRENT_CONTEXT(ctx
);
1910 /* We _should_ be inside glBegin/glEnd */
1912 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1913 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1918 /* make null-terminated copy of registerName */
1919 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1920 _mesa_memcpy(reg
, registerName
, len
);
1924 case GL_VERTEX_PROGRAM_NV
:
1925 if (!ctx
->Extensions
.ARB_vertex_program
&&
1926 !ctx
->Extensions
.NV_vertex_program
) {
1927 _mesa_error(ctx
, GL_INVALID_ENUM
,
1928 "glGetProgramRegisterfvMESA(target)");
1931 if (!ctx
->VertexProgram
._Enabled
) {
1932 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1933 "glGetProgramRegisterfvMESA");
1936 /* GL_NV_vertex_program */
1937 if (reg
[0] == 'R') {
1939 GLint i
= _mesa_atoi(reg
+ 1);
1940 if (i
>= (GLint
)ctx
->Const
.VertexProgram
.MaxTemps
) {
1941 _mesa_error(ctx
, GL_INVALID_VALUE
,
1942 "glGetProgramRegisterfvMESA(registerName)");
1945 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
1947 else if (reg
[0] == 'v' && reg
[1] == '[') {
1948 /* Vertex Input attribute */
1950 for (i
= 0; i
< ctx
->Const
.VertexProgram
.MaxAttribs
; i
++) {
1951 const char *name
= _mesa_nv_vertex_input_register_name(i
);
1953 _mesa_sprintf(number
, "%d", i
);
1954 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
1955 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
1956 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
1960 _mesa_error(ctx
, GL_INVALID_VALUE
,
1961 "glGetProgramRegisterfvMESA(registerName)");
1964 else if (reg
[0] == 'o' && reg
[1] == '[') {
1965 /* Vertex output attribute */
1967 /* GL_ARB_vertex_program */
1968 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
1972 _mesa_error(ctx
, GL_INVALID_VALUE
,
1973 "glGetProgramRegisterfvMESA(registerName)");
1977 case GL_FRAGMENT_PROGRAM_ARB
:
1978 if (!ctx
->Extensions
.ARB_fragment_program
) {
1979 _mesa_error(ctx
, GL_INVALID_ENUM
,
1980 "glGetProgramRegisterfvMESA(target)");
1983 if (!ctx
->FragmentProgram
._Enabled
) {
1984 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1985 "glGetProgramRegisterfvMESA");
1990 case GL_FRAGMENT_PROGRAM_NV
:
1991 if (!ctx
->Extensions
.NV_fragment_program
) {
1992 _mesa_error(ctx
, GL_INVALID_ENUM
,
1993 "glGetProgramRegisterfvMESA(target)");
1996 if (!ctx
->FragmentProgram
._Enabled
) {
1997 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1998 "glGetProgramRegisterfvMESA");
2001 if (reg
[0] == 'R') {
2003 GLint i
= _mesa_atoi(reg
+ 1);
2004 if (i
>= (GLint
)ctx
->Const
.FragmentProgram
.MaxTemps
) {
2005 _mesa_error(ctx
, GL_INVALID_VALUE
,
2006 "glGetProgramRegisterfvMESA(registerName)");
2009 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
2011 else if (reg
[0] == 'f' && reg
[1] == '[') {
2012 /* Fragment input attribute */
2014 for (i
= 0; i
< ctx
->Const
.FragmentProgram
.MaxAttribs
; i
++) {
2015 const char *name
= _mesa_nv_fragment_input_register_name(i
);
2016 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
2017 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
2021 _mesa_error(ctx
, GL_INVALID_VALUE
,
2022 "glGetProgramRegisterfvMESA(registerName)");
2025 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
2026 /* Fragment output color */
2027 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_COLR
]);
2029 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
2030 /* Fragment output color */
2031 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_COLH
]);
2033 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
2034 /* Fragment output depth */
2035 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_DEPR
]);
2038 /* try user-defined identifiers */
2039 const GLfloat
*value
= _mesa_lookup_parameter_value(
2040 ctx
->FragmentProgram
.Current
->Base
.Parameters
, -1, reg
);
2045 _mesa_error(ctx
, GL_INVALID_VALUE
,
2046 "glGetProgramRegisterfvMESA(registerName)");
2052 _mesa_error(ctx
, GL_INVALID_ENUM
,
2053 "glGetProgramRegisterfvMESA(target)");