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 make_state_flags(const GLint state
[]);
52 /**********************************************************************/
53 /* Utility functions */
54 /**********************************************************************/
57 /* A pointer to this dummy program is put into the hash table when
58 * glGenPrograms is called.
60 struct program _mesa_DummyProgram
;
64 * Init context's vertex/fragment program state
67 _mesa_init_program(GLcontext
*ctx
)
71 ctx
->Program
.ErrorPos
= -1;
72 ctx
->Program
.ErrorString
= _mesa_strdup("");
74 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
75 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
76 ctx
->VertexProgram
.PointSizeEnabled
= GL_FALSE
;
77 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
78 ctx
->VertexProgram
.Current
= (struct vertex_program
*) ctx
->Shared
->DefaultVertexProgram
;
79 assert(ctx
->VertexProgram
.Current
);
80 ctx
->VertexProgram
.Current
->Base
.RefCount
++;
81 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
82 ctx
->VertexProgram
.TrackMatrix
[i
] = GL_NONE
;
83 ctx
->VertexProgram
.TrackMatrixTransform
[i
] = GL_IDENTITY_NV
;
87 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
88 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
89 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) ctx
->Shared
->DefaultFragmentProgram
;
90 assert(ctx
->FragmentProgram
.Current
);
91 ctx
->FragmentProgram
.Current
->Base
.RefCount
++;
94 /* XXX probably move this stuff */
95 #if FEATURE_ATI_fragment_shader
96 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
97 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
98 assert(ctx
->ATIFragmentShader
.Current
);
99 ctx
->ATIFragmentShader
.Current
->RefCount
++;
105 * Free a context's vertex/fragment program state
108 _mesa_free_program_data(GLcontext
*ctx
)
110 #if FEATURE_NV_vertex_program
111 if (ctx
->VertexProgram
.Current
) {
112 ctx
->VertexProgram
.Current
->Base
.RefCount
--;
113 if (ctx
->VertexProgram
.Current
->Base
.RefCount
<= 0)
114 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->VertexProgram
.Current
->Base
));
117 #if FEATURE_NV_fragment_program
118 if (ctx
->FragmentProgram
.Current
) {
119 ctx
->FragmentProgram
.Current
->Base
.RefCount
--;
120 if (ctx
->FragmentProgram
.Current
->Base
.RefCount
<= 0)
121 ctx
->Driver
.DeleteProgram(ctx
, &(ctx
->FragmentProgram
.Current
->Base
));
124 /* XXX probably move this stuff */
125 #if FEATURE_ATI_fragment_shader
126 if (ctx
->ATIFragmentShader
.Current
) {
127 ctx
->ATIFragmentShader
.Current
->RefCount
--;
128 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
129 _mesa_free(ctx
->ATIFragmentShader
.Current
);
133 _mesa_free((void *) ctx
->Program
.ErrorString
);
140 * Set the vertex/fragment program error state (position and error string).
141 * This is generally called from within the parsers.
144 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
146 ctx
->Program
.ErrorPos
= pos
;
147 _mesa_free((void *) ctx
->Program
.ErrorString
);
150 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
155 * Find the line number and column for 'pos' within 'string'.
156 * Return a copy of the line which contains 'pos'. Free the line with
158 * \param string the program string
159 * \param pos the position within the string
160 * \param line returns the line number corresponding to 'pos'.
161 * \param col returns the column number corresponding to 'pos'.
162 * \return copy of the line containing 'pos'.
165 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
166 GLint
*line
, GLint
*col
)
168 const GLubyte
*lineStart
= string
;
169 const GLubyte
*p
= string
;
176 if (*p
== (GLubyte
) '\n') {
183 *col
= (pos
- lineStart
) + 1;
185 /* return copy of this line */
186 while (*p
!= 0 && *p
!= '\n')
189 s
= (GLubyte
*) _mesa_malloc(len
+ 1);
190 _mesa_memcpy(s
, lineStart
, len
);
198 * Initialize a new vertex/fragment program object.
200 static struct program
*
201 _mesa_init_program_struct( GLcontext
*ctx
, struct program
*prog
,
202 GLenum target
, GLuint id
)
207 prog
->Target
= target
;
208 prog
->Resident
= GL_TRUE
;
217 * Initialize a new fragment program object.
220 _mesa_init_fragment_program( GLcontext
*ctx
, struct fragment_program
*prog
,
221 GLenum target
, GLuint id
)
224 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
231 * Initialize a new vertex program object.
234 _mesa_init_vertex_program( GLcontext
*ctx
, struct vertex_program
*prog
,
235 GLenum target
, GLuint id
)
238 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
245 * Allocate and initialize a new fragment/vertex program object but
246 * don't put it into the program hash table. Called via
247 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
248 * device driver function to implement OO deriviation with additional
249 * types not understood by this function.
252 * \param id program id/number
253 * \param target program target/type
254 * \return pointer to new program object
257 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
260 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
261 return _mesa_init_vertex_program( ctx
, CALLOC_STRUCT(vertex_program
),
263 case GL_FRAGMENT_PROGRAM_NV
:
264 case GL_FRAGMENT_PROGRAM_ARB
:
265 return _mesa_init_fragment_program( ctx
, CALLOC_STRUCT(fragment_program
),
268 _mesa_problem(ctx
, "bad target in _mesa_new_program");
275 * Delete a program and remove it from the hash table, ignoring the
277 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
278 * by a device driver function.
281 _mesa_delete_program(GLcontext
*ctx
, struct program
*prog
)
287 _mesa_free(prog
->String
);
289 if (prog
->Instructions
) {
291 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
292 if (prog
->Instructions
[i
].Data
)
293 _mesa_free(prog
->Instructions
[i
].Data
);
295 _mesa_free(prog
->Instructions
);
298 if (prog
->Parameters
)
299 _mesa_free_parameter_list(prog
->Parameters
);
306 /**********************************************************************/
307 /* Program parameter functions */
308 /**********************************************************************/
310 struct program_parameter_list
*
311 _mesa_new_parameter_list(void)
313 return (struct program_parameter_list
*)
314 _mesa_calloc(sizeof(struct program_parameter_list
));
319 * Free a parameter list and all its parameters
322 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
324 _mesa_free_parameters(paramList
);
325 _mesa_free(paramList
->Parameters
);
326 if (paramList
->ParameterValues
)
327 ALIGN_FREE(paramList
->ParameterValues
);
328 _mesa_free(paramList
);
333 * Free all the parameters in the given list, but don't free the
334 * paramList structure itself.
337 _mesa_free_parameters(struct program_parameter_list
*paramList
)
340 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
341 if (paramList
->Parameters
[i
].Name
)
342 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
344 paramList
->NumParameters
= 0;
349 * Helper function used by the functions below.
350 * \return index of new parameter in the list, or -1 if error (out of mem)
353 add_parameter(struct program_parameter_list
*paramList
,
354 const char *name
, const GLfloat values
[4],
355 enum register_file type
)
357 const GLuint n
= paramList
->NumParameters
;
359 if (n
== paramList
->Size
) {
362 paramList
->Size
*= 2;
363 if (!paramList
->Size
)
366 paramList
->Parameters
= (struct program_parameter
*)
367 _mesa_realloc(paramList
->Parameters
,
368 n
* sizeof(struct program_parameter
),
369 paramList
->Size
* sizeof(struct program_parameter
));
371 tmp
= paramList
->ParameterValues
;
372 paramList
->ParameterValues
= (GLfloat(*)[4]) ALIGN_MALLOC(paramList
->Size
* 4 * sizeof(GLfloat
), 16);
374 _mesa_memcpy(paramList
->ParameterValues
, tmp
,
375 n
* 4 * sizeof(GLfloat
));
380 if (!paramList
->Parameters
||
381 !paramList
->ParameterValues
) {
383 paramList
->NumParameters
= 0;
388 paramList
->NumParameters
= n
+ 1;
390 _mesa_memset(¶mList
->Parameters
[n
], 0,
391 sizeof(struct program_parameter
));
393 paramList
->Parameters
[n
].Name
= name
? _mesa_strdup(name
) : NULL
;
394 paramList
->Parameters
[n
].Type
= type
;
396 COPY_4V(paramList
->ParameterValues
[n
], values
);
403 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
404 * \return index of the new entry in the parameter list
407 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
408 const char *name
, const GLfloat values
[4])
410 return add_parameter(paramList
, name
, values
, PROGRAM_NAMED_PARAM
);
415 * Add a new unnamed constant to the parameter list.
416 * \param paramList - the parameter list
417 * \param values - four float values
418 * \return index of the new parameter.
421 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
422 const char *name
, const GLfloat values
[4])
424 return add_parameter(paramList
, name
, values
, PROGRAM_CONSTANT
);
429 * Add a new unnamed constant to the parameter list.
430 * \param paramList - the parameter list
431 * \param values - four float values
432 * \return index of the new parameter.
435 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
436 const GLfloat values
[4])
438 return add_parameter(paramList
, NULL
, values
, PROGRAM_CONSTANT
);
443 * Add a new state reference to the parameter list.
444 * \param paramList - the parameter list
445 * \param state - an array of 6 state tokens
447 * \return index of the new parameter.
450 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
451 const GLint
*stateTokens
)
453 /* XXX we should probably search the current parameter list to see if
454 * the new state reference is already present.
457 const char *name
= make_state_string(stateTokens
);
459 index
= add_parameter(paramList
, name
, NULL
, PROGRAM_STATE_VAR
);
462 for (i
= 0; i
< 6; i
++)
463 paramList
->Parameters
[index
].StateIndexes
[i
]
464 = (enum state_index
) stateTokens
[i
];
466 paramList
->StateFlags
|=
467 make_state_flags(stateTokens
);
475 * Lookup a parameter value by name in the given parameter list.
476 * \return pointer to the float[4] values.
479 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
480 GLsizei nameLen
, const char *name
)
488 /* name is null-terminated */
489 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
490 if (paramList
->Parameters
[i
].Name
&&
491 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
492 return paramList
->ParameterValues
[i
];
496 /* name is not null-terminated, use nameLen */
497 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
498 if (paramList
->Parameters
[i
].Name
&&
499 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
500 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
501 return paramList
->ParameterValues
[i
];
509 * Lookup a parameter index by name in the given parameter list.
510 * \return index of parameter in the list.
513 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
514 GLsizei nameLen
, const char *name
)
522 /* name is null-terminated */
523 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
524 if (paramList
->Parameters
[i
].Name
&&
525 _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 (paramList
->Parameters
[i
].Name
&&
533 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
534 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
543 * Use the list of tokens in the state[] array to find global GL state
544 * and return it in <value>. Usually, four values are returned in <value>
545 * but matrix queries may return as many as 16 values.
546 * This function is used for ARB vertex/fragment programs.
547 * The program parser will produce the state[] values.
550 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
556 /* state[1] is either 0=front or 1=back side */
557 const GLuint face
= (GLuint
) state
[1];
558 /* state[2] is the material attribute */
562 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
564 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
568 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
570 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
574 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
576 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
580 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
582 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
584 case STATE_SHININESS
:
586 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
588 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
594 _mesa_problem(ctx
, "Invalid material state in fetch_state");
600 /* state[1] is the light number */
601 const GLuint ln
= (GLuint
) state
[1];
602 /* state[2] is the light attribute */
605 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
608 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
611 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
614 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
616 case STATE_ATTENUATION
:
617 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
618 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
619 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
620 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
622 case STATE_SPOT_DIRECTION
:
623 COPY_3V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
624 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
628 GLfloat eye_z
[] = {0, 0, 1};
630 /* Compute infinite half angle vector:
631 * half-vector = light_position + (0, 0, 1)
632 * and then normalize. w = 0
634 * light.EyePosition.w should be 0 for infinite lights.
636 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
637 NORMALIZE_3FV(value
);
641 case STATE_POSITION_NORMALIZED
:
642 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
643 NORMALIZE_3FV( value
);
646 _mesa_problem(ctx
, "Invalid light state in fetch_state");
650 case STATE_LIGHTMODEL_AMBIENT
:
651 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
653 case STATE_LIGHTMODEL_SCENECOLOR
:
657 for (i
= 0; i
< 3; i
++) {
658 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
659 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
660 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
662 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
667 for (i
= 0; i
< 3; i
++) {
668 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
669 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
670 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
672 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
675 case STATE_LIGHTPROD
:
677 const GLuint ln
= (GLuint
) state
[1];
678 const GLuint face
= (GLuint
) state
[2];
680 ASSERT(face
== 0 || face
== 1);
683 for (i
= 0; i
< 3; i
++) {
684 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
685 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
687 /* [3] = material alpha */
688 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
691 for (i
= 0; i
< 3; i
++) {
692 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
693 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
695 /* [3] = material alpha */
696 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
699 for (i
= 0; i
< 3; i
++) {
700 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
701 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
703 /* [3] = material alpha */
704 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
707 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
713 /* state[1] is the texture unit */
714 const GLuint unit
= (GLuint
) state
[1];
715 /* state[2] is the texgen attribute */
717 case STATE_TEXGEN_EYE_S
:
718 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
720 case STATE_TEXGEN_EYE_T
:
721 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
723 case STATE_TEXGEN_EYE_R
:
724 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
726 case STATE_TEXGEN_EYE_Q
:
727 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
729 case STATE_TEXGEN_OBJECT_S
:
730 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
732 case STATE_TEXGEN_OBJECT_T
:
733 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
735 case STATE_TEXGEN_OBJECT_R
:
736 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
738 case STATE_TEXGEN_OBJECT_Q
:
739 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
742 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
746 case STATE_TEXENV_COLOR
:
748 /* state[1] is the texture unit */
749 const GLuint unit
= (GLuint
) state
[1];
750 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
753 case STATE_FOG_COLOR
:
754 COPY_4V(value
, ctx
->Fog
.Color
);
756 case STATE_FOG_PARAMS
:
757 value
[0] = ctx
->Fog
.Density
;
758 value
[1] = ctx
->Fog
.Start
;
759 value
[2] = ctx
->Fog
.End
;
760 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
762 case STATE_CLIPPLANE
:
764 const GLuint plane
= (GLuint
) state
[1];
765 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
768 case STATE_POINT_SIZE
:
769 value
[0] = ctx
->Point
.Size
;
770 value
[1] = ctx
->Point
.MinSize
;
771 value
[2] = ctx
->Point
.MaxSize
;
772 value
[3] = ctx
->Point
.Threshold
;
774 case STATE_POINT_ATTENUATION
:
775 value
[0] = ctx
->Point
.Params
[0];
776 value
[1] = ctx
->Point
.Params
[1];
777 value
[2] = ctx
->Point
.Params
[2];
782 /* state[1] = modelview, projection, texture, etc. */
783 /* state[2] = which texture matrix or program matrix */
784 /* state[3] = first column to fetch */
785 /* state[4] = last column to fetch */
786 /* state[5] = transpose, inverse or invtrans */
788 const GLmatrix
*matrix
;
789 const enum state_index mat
= state
[1];
790 const GLuint index
= (GLuint
) state
[2];
791 const GLuint first
= (GLuint
) state
[3];
792 const GLuint last
= (GLuint
) state
[4];
793 const enum state_index modifier
= state
[5];
796 if (mat
== STATE_MODELVIEW
) {
797 matrix
= ctx
->ModelviewMatrixStack
.Top
;
799 else if (mat
== STATE_PROJECTION
) {
800 matrix
= ctx
->ProjectionMatrixStack
.Top
;
802 else if (mat
== STATE_MVP
) {
803 matrix
= &ctx
->_ModelProjectMatrix
;
805 else if (mat
== STATE_TEXTURE
) {
806 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
808 else if (mat
== STATE_PROGRAM
) {
809 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
812 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
815 if (modifier
== STATE_MATRIX_INVERSE
||
816 modifier
== STATE_MATRIX_INVTRANS
) {
817 /* Be sure inverse is up to date:
819 _math_matrix_analyse( (GLmatrix
*) matrix
);
825 if (modifier
== STATE_MATRIX_TRANSPOSE
||
826 modifier
== STATE_MATRIX_INVTRANS
) {
827 for (i
= 0, row
= first
; row
<= last
; row
++) {
828 value
[i
++] = m
[row
* 4 + 0];
829 value
[i
++] = m
[row
* 4 + 1];
830 value
[i
++] = m
[row
* 4 + 2];
831 value
[i
++] = m
[row
* 4 + 3];
835 for (i
= 0, row
= first
; row
<= last
; row
++) {
836 value
[i
++] = m
[row
+ 0];
837 value
[i
++] = m
[row
+ 4];
838 value
[i
++] = m
[row
+ 8];
839 value
[i
++] = m
[row
+ 12];
844 case STATE_DEPTH_RANGE
:
845 value
[0] = ctx
->Viewport
.Near
; /* near */
846 value
[1] = ctx
->Viewport
.Far
; /* far */
847 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
850 case STATE_FRAGMENT_PROGRAM
:
852 /* state[1] = {STATE_ENV, STATE_LOCAL} */
853 /* state[2] = parameter index */
854 const int idx
= (int) state
[2];
857 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
860 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
863 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
869 case STATE_VERTEX_PROGRAM
:
871 /* state[1] = {STATE_ENV, STATE_LOCAL} */
872 /* state[2] = parameter index */
873 const int idx
= (int) state
[2];
876 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
879 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
882 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
891 case STATE_NORMAL_SCALE
:
892 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
895 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
902 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
908 /* Return a bit mask of the Mesa state flags under which a parameter's
909 * value might change.
911 static GLuint
make_state_flags(const GLint state
[])
916 case STATE_LIGHTMODEL_AMBIENT
:
917 case STATE_LIGHTMODEL_SCENECOLOR
:
918 case STATE_LIGHTPROD
:
922 case STATE_TEXENV_COLOR
:
925 case STATE_FOG_COLOR
:
926 case STATE_FOG_PARAMS
:
929 case STATE_CLIPPLANE
:
930 return _NEW_TRANSFORM
;
932 case STATE_POINT_SIZE
:
933 case STATE_POINT_ATTENUATION
:
938 case STATE_MODELVIEW
:
939 return _NEW_MODELVIEW
;
940 case STATE_PROJECTION
:
941 return _NEW_PROJECTION
;
943 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
945 return _NEW_TEXTURE_MATRIX
;
947 return _NEW_TRACK_MATRIX
;
953 case STATE_DEPTH_RANGE
:
954 return _NEW_VIEWPORT
;
956 case STATE_FRAGMENT_PROGRAM
:
957 case STATE_VERTEX_PROGRAM
:
962 case STATE_NORMAL_SCALE
:
963 return _NEW_MODELVIEW
;
980 append(char *dst
, const char *src
)
990 append_token(char *dst
, enum state_index k
)
994 append(dst
, "material.");
997 append(dst
, "light");
999 case STATE_LIGHTMODEL_AMBIENT
:
1000 append(dst
, "lightmodel.ambient");
1002 case STATE_LIGHTMODEL_SCENECOLOR
:
1004 case STATE_LIGHTPROD
:
1005 append(dst
, "lightprod");
1008 append(dst
, "texgen");
1010 case STATE_FOG_COLOR
:
1011 append(dst
, "fog.color");
1013 case STATE_FOG_PARAMS
:
1014 append(dst
, "fog.params");
1016 case STATE_CLIPPLANE
:
1017 append(dst
, "clip");
1019 case STATE_POINT_SIZE
:
1020 append(dst
, "point.size");
1022 case STATE_POINT_ATTENUATION
:
1023 append(dst
, "point.attenuation");
1026 append(dst
, "matrix.");
1028 case STATE_MODELVIEW
:
1029 append(dst
, "modelview");
1031 case STATE_PROJECTION
:
1032 append(dst
, "projection");
1038 append(dst
, "texture");
1041 append(dst
, "program");
1043 case STATE_MATRIX_INVERSE
:
1044 append(dst
, ".inverse");
1046 case STATE_MATRIX_TRANSPOSE
:
1047 append(dst
, ".transpose");
1049 case STATE_MATRIX_INVTRANS
:
1050 append(dst
, ".invtrans");
1053 append(dst
, "ambient");
1056 append(dst
, "diffuse");
1058 case STATE_SPECULAR
:
1059 append(dst
, "specular");
1061 case STATE_EMISSION
:
1062 append(dst
, "emission");
1064 case STATE_SHININESS
:
1065 append(dst
, "shininess");
1068 append(dst
, "half");
1070 case STATE_POSITION
:
1071 append(dst
, ".position");
1073 case STATE_ATTENUATION
:
1074 append(dst
, ".attenuation");
1076 case STATE_SPOT_DIRECTION
:
1077 append(dst
, ".spot.direction");
1079 case STATE_TEXGEN_EYE_S
:
1080 append(dst
, "eye.s");
1082 case STATE_TEXGEN_EYE_T
:
1083 append(dst
, "eye.t");
1085 case STATE_TEXGEN_EYE_R
:
1086 append(dst
, "eye.r");
1088 case STATE_TEXGEN_EYE_Q
:
1089 append(dst
, "eye.q");
1091 case STATE_TEXGEN_OBJECT_S
:
1092 append(dst
, "object.s");
1094 case STATE_TEXGEN_OBJECT_T
:
1095 append(dst
, "object.t");
1097 case STATE_TEXGEN_OBJECT_R
:
1098 append(dst
, "object.r");
1100 case STATE_TEXGEN_OBJECT_Q
:
1101 append(dst
, "object.q");
1103 case STATE_TEXENV_COLOR
:
1104 append(dst
, "texenv");
1106 case STATE_DEPTH_RANGE
:
1107 append(dst
, "depth.range");
1109 case STATE_VERTEX_PROGRAM
:
1110 case STATE_FRAGMENT_PROGRAM
:
1116 append(dst
, "local");
1118 case STATE_INTERNAL
:
1119 case STATE_NORMAL_SCALE
:
1120 case STATE_POSITION_NORMALIZED
:
1121 append(dst
, "(internal)");
1129 append_face(char *dst
, GLint face
)
1132 append(dst
, "front.");
1134 append(dst
, "back.");
1138 append_index(char *dst
, GLint index
)
1141 _mesa_sprintf(s
, "[%d].", index
);
1146 * Make a string from the given state vector.
1147 * For example, return "state.matrix.texture[2].inverse".
1150 make_state_string(const GLint state
[6])
1152 char str
[1000] = "";
1155 append(str
, "state.");
1156 append_token(str
, (enum state_index
) state
[0]);
1159 case STATE_MATERIAL
:
1160 append_face(str
, state
[1]);
1161 append_token(str
, (enum state_index
) state
[2]);
1164 append(str
, "light");
1165 append_index(str
, state
[1]); /* light number [i]. */
1166 append_token(str
, (enum state_index
) state
[2]); /* coefficients */
1168 case STATE_LIGHTMODEL_AMBIENT
:
1169 append(str
, "lightmodel.ambient");
1171 case STATE_LIGHTMODEL_SCENECOLOR
:
1172 if (state
[1] == 0) {
1173 append(str
, "lightmodel.front.scenecolor");
1176 append(str
, "lightmodel.back.scenecolor");
1179 case STATE_LIGHTPROD
:
1180 append_index(str
, state
[1]); /* light number [i]. */
1181 append_face(str
, state
[2]);
1182 append_token(str
, (enum state_index
) state
[3]);
1185 append_index(str
, state
[1]); /* tex unit [i] */
1186 append_token(str
, (enum state_index
) state
[2]); /* plane coef */
1188 case STATE_TEXENV_COLOR
:
1189 append_index(str
, state
[1]); /* tex unit [i] */
1190 append(str
, "color");
1192 case STATE_FOG_COLOR
:
1193 case STATE_FOG_PARAMS
:
1195 case STATE_CLIPPLANE
:
1196 append_index(str
, state
[1]); /* plane [i] */
1197 append(str
, "plane");
1199 case STATE_POINT_SIZE
:
1200 case STATE_POINT_ATTENUATION
:
1204 /* state[1] = modelview, projection, texture, etc. */
1205 /* state[2] = which texture matrix or program matrix */
1206 /* state[3] = first column to fetch */
1207 /* state[4] = last column to fetch */
1208 /* state[5] = transpose, inverse or invtrans */
1209 const enum state_index mat
= (enum state_index
) state
[1];
1210 const GLuint index
= (GLuint
) state
[2];
1211 const GLuint first
= (GLuint
) state
[3];
1212 const GLuint last
= (GLuint
) state
[4];
1213 const enum state_index modifier
= (enum state_index
) state
[5];
1214 append_token(str
, mat
);
1216 append_index(str
, index
);
1218 append_token(str
, modifier
);
1220 _mesa_sprintf(tmp
, ".row[%d]", first
);
1222 _mesa_sprintf(tmp
, ".row[%d..%d]", first
, last
);
1226 case STATE_DEPTH_RANGE
:
1228 case STATE_FRAGMENT_PROGRAM
:
1229 case STATE_VERTEX_PROGRAM
:
1230 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1231 /* state[2] = parameter index */
1232 append_token(str
, (enum state_index
) state
[1]);
1233 append_index(str
, state
[2]);
1235 case STATE_INTERNAL
:
1238 _mesa_problem(NULL
, "Invalid state in maka_state_string");
1242 return _mesa_strdup(str
);
1247 * Loop over all the parameters in a parameter list. If the parameter
1248 * is a GL state reference, look up the current value of that state
1249 * variable and put it into the parameter's Value[4] array.
1250 * This would be called at glBegin time when using a fragment program.
1253 _mesa_load_state_parameters(GLcontext
*ctx
,
1254 struct program_parameter_list
*paramList
)
1261 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1262 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1263 _mesa_fetch_state(ctx
,
1264 paramList
->Parameters
[i
].StateIndexes
,
1265 paramList
->ParameterValues
[i
]);
1272 * Initialize program instruction fields to defaults.
1275 _mesa_init_instruction(struct prog_instruction
*inst
)
1277 _mesa_bzero(inst
, sizeof(struct prog_instruction
));
1279 inst
->SrcReg
[0].File
= PROGRAM_UNDEFINED
;
1280 inst
->SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
1281 inst
->SrcReg
[1].File
= PROGRAM_UNDEFINED
;
1282 inst
->SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
1283 inst
->SrcReg
[2].File
= PROGRAM_UNDEFINED
;
1284 inst
->SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
1286 inst
->DstReg
.File
= PROGRAM_UNDEFINED
;
1287 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1288 inst
->DstReg
.CondMask
= COND_TR
;
1289 inst
->DstReg
.CondSwizzle
= SWIZZLE_NOOP
;
1291 inst
->SaturateMode
= SATURATE_OFF
;
1292 inst
->Precision
= FLOAT32
;
1297 * Basic info about each instruction
1299 struct instruction_info
1301 enum prog_opcode Opcode
;
1308 * \note Opcode should equal array index!
1310 static const struct instruction_info InstInfo
[MAX_OPCODE
] = {
1311 { OPCODE_ABS
, "ABS", 1 },
1312 { OPCODE_ADD
, "ADD", 2 },
1313 { OPCODE_ARA
, "ARA", 1 },
1314 { OPCODE_ARL
, "ARL", 1 },
1315 { OPCODE_ARL_NV
, "ARL", 1 },
1316 { OPCODE_ARR
, "ARL", 1 },
1317 { OPCODE_BRA
, "BRA", 1 },
1318 { OPCODE_CAL
, "CAL", 1 },
1319 { OPCODE_CMP
, "CMP", 3 },
1320 { OPCODE_COS
, "COS", 1 },
1321 { OPCODE_DDX
, "DDX", 1 },
1322 { OPCODE_DDY
, "DDY", 1 },
1323 { OPCODE_DP3
, "DP3", 2 },
1324 { OPCODE_DP4
, "DP4", 2 },
1325 { OPCODE_DPH
, "DPH", 2 },
1326 { OPCODE_DST
, "DST", 2 },
1327 { OPCODE_END
, "END", 0 },
1328 { OPCODE_EX2
, "EX2", 1 },
1329 { OPCODE_EXP
, "EXP", 1 },
1330 { OPCODE_FLR
, "FLR", 1 },
1331 { OPCODE_FRC
, "FRC", 1 },
1332 { OPCODE_KIL
, "KIL", 1 },
1333 { OPCODE_KIL_NV
, "KIL", 0 },
1334 { OPCODE_LG2
, "LG2", 1 },
1335 { OPCODE_LIT
, "LIT", 1 },
1336 { OPCODE_LOG
, "LOG", 1 },
1337 { OPCODE_LRP
, "LRP", 3 },
1338 { OPCODE_MAD
, "MAD", 3 },
1339 { OPCODE_MAX
, "MAX", 2 },
1340 { OPCODE_MIN
, "MIN", 2 },
1341 { OPCODE_MOV
, "MOV", 1 },
1342 { OPCODE_MUL
, "MUL", 2 },
1343 { OPCODE_PK2H
, "PK2H", 1 },
1344 { OPCODE_PK2US
, "PK2US", 1 },
1345 { OPCODE_PK4B
, "PK4B", 1 },
1346 { OPCODE_PK4UB
, "PK4UB", 1 },
1347 { OPCODE_POW
, "POW", 2 },
1348 { OPCODE_POPA
, "POPA", 0 },
1349 { OPCODE_PRINT
, "PRINT", 1 },
1350 { OPCODE_PUSHA
, "PUSHA", 0 },
1351 { OPCODE_RCC
, "RCC", 1 },
1352 { OPCODE_RCP
, "RCP", 1 },
1353 { OPCODE_RET
, "RET", 1 },
1354 { OPCODE_RFL
, "RFL", 1 },
1355 { OPCODE_RSQ
, "RSQ", 1 },
1356 { OPCODE_SCS
, "SCS", 1 },
1357 { OPCODE_SEQ
, "SEQ", 2 },
1358 { OPCODE_SFL
, "SFL", 0 },
1359 { OPCODE_SGE
, "SGE", 2 },
1360 { OPCODE_SGT
, "SGT", 2 },
1361 { OPCODE_SIN
, "SIN", 1 },
1362 { OPCODE_SLE
, "SLE", 2 },
1363 { OPCODE_SLT
, "SLT", 2 },
1364 { OPCODE_SNE
, "SNE", 2 },
1365 { OPCODE_SSG
, "SSG", 1 },
1366 { OPCODE_STR
, "STR", 0 },
1367 { OPCODE_SUB
, "SUB", 2 },
1368 { OPCODE_SWZ
, "SWZ", 1 },
1369 { OPCODE_TEX
, "TEX", 1 },
1370 { OPCODE_TXB
, "TXB", 1 },
1371 { OPCODE_TXD
, "TXD", 3 },
1372 { OPCODE_TXL
, "TXL", 1 },
1373 { OPCODE_TXP
, "TXP", 1 },
1374 { OPCODE_TXP_NV
, "TXP", 1 },
1375 { OPCODE_UP2H
, "UP2H", 1 },
1376 { OPCODE_UP2US
, "UP2US", 1 },
1377 { OPCODE_UP4B
, "UP4B", 1 },
1378 { OPCODE_UP4UB
, "UP4UB", 1 },
1379 { OPCODE_X2D
, "X2D", 3 },
1380 { OPCODE_XPD
, "XPD", 2 }
1385 * Return the number of src registers for the given instruction/opcode.
1388 _mesa_num_inst_src_regs(enum prog_opcode opcode
)
1392 for (i
= 0; i
< MAX_OPCODE
; i
++) {
1393 ASSERT(i
== InstInfo
[i
].Opcode
);
1396 for (i
= 0; i
< MAX_OPCODE
; i
++) {
1397 if (InstInfo
[i
].Opcode
== opcode
) {
1398 return InstInfo
[i
].NumSrcRegs
;
1401 _mesa_problem(NULL
, "invalid opcode in _mesa_num_inst_src_regs");
1407 * Return string name for given program opcode.
1410 _mesa_opcode_string(enum prog_opcode opcode
)
1412 ASSERT(opcode
< MAX_OPCODE
);
1413 return InstInfo
[opcode
].Name
;
1417 * Return string name for given program/register file.
1420 program_file_string(enum register_file f
)
1423 case PROGRAM_TEMPORARY
:
1425 case PROGRAM_LOCAL_PARAM
:
1427 case PROGRAM_ENV_PARAM
:
1429 case PROGRAM_STATE_VAR
:
1433 case PROGRAM_OUTPUT
:
1435 case PROGRAM_NAMED_PARAM
:
1437 case PROGRAM_CONSTANT
:
1439 case PROGRAM_WRITE_ONLY
:
1440 return "WRITE_ONLY";
1441 case PROGRAM_ADDRESS
:
1450 * Return a string representation of the given swizzle word.
1451 * If extended is true, use extended (comma-separated) format.
1454 swizzle_string(GLuint swizzle
, GLuint negateBase
, GLboolean extended
)
1456 static const char swz
[] = "xyzw01";
1460 if (!extended
&& swizzle
== SWIZZLE_NOOP
&& negateBase
== 0)
1461 return ""; /* no swizzle/negation */
1466 if (negateBase
& 0x1)
1468 s
[i
++] = swz
[GET_SWZ(swizzle
, 0)];
1474 if (negateBase
& 0x2)
1476 s
[i
++] = swz
[GET_SWZ(swizzle
, 1)];
1482 if (negateBase
& 0x4)
1484 s
[i
++] = swz
[GET_SWZ(swizzle
, 2)];
1490 if (negateBase
& 0x8)
1492 s
[i
++] = swz
[GET_SWZ(swizzle
, 3)];
1500 writemask_string(GLuint writeMask
)
1505 if (writeMask
== WRITEMASK_XYZW
)
1509 if (writeMask
& WRITEMASK_X
)
1511 if (writeMask
& WRITEMASK_Y
)
1513 if (writeMask
& WRITEMASK_Z
)
1515 if (writeMask
& WRITEMASK_W
)
1523 print_dst_reg(const struct prog_dst_register
*dstReg
)
1525 _mesa_printf(" %s[%d]%s",
1526 program_file_string((enum register_file
) dstReg
->File
),
1528 writemask_string(dstReg
->WriteMask
));
1532 print_src_reg(const struct prog_src_register
*srcReg
)
1534 _mesa_printf("%s[%d]%s",
1535 program_file_string((enum register_file
) srcReg
->File
),
1537 swizzle_string(srcReg
->Swizzle
,
1538 srcReg
->NegateBase
, GL_FALSE
));
1543 * Print a single vertex/fragment program instruction.
1546 _mesa_print_instruction(const struct prog_instruction
*inst
)
1548 switch (inst
->Opcode
) {
1550 _mesa_printf("PRINT '%s'", inst
->Data
);
1551 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1553 _mesa_printf("%s[%d]%s",
1554 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1555 inst
->SrcReg
[0].Index
,
1556 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1557 inst
->SrcReg
[0].NegateBase
, GL_FALSE
));
1559 _mesa_printf(";\n");
1562 _mesa_printf("SWZ");
1563 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1564 _mesa_printf("_SAT");
1565 print_dst_reg(&inst
->DstReg
);
1566 _mesa_printf("%s[%d], %s;\n",
1567 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1568 inst
->SrcReg
[0].Index
,
1569 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1570 inst
->SrcReg
[0].NegateBase
, GL_TRUE
));
1575 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1576 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1577 _mesa_printf("_SAT");
1579 print_dst_reg(&inst
->DstReg
);
1581 print_src_reg(&inst
->SrcReg
[0]);
1582 _mesa_printf(", texture[%d], ", inst
->TexSrcUnit
);
1583 switch (inst
->TexSrcTarget
) {
1584 case TEXTURE_1D_INDEX
: _mesa_printf("1D"); break;
1585 case TEXTURE_2D_INDEX
: _mesa_printf("2D"); break;
1586 case TEXTURE_3D_INDEX
: _mesa_printf("3D"); break;
1587 case TEXTURE_CUBE_INDEX
: _mesa_printf("CUBE"); break;
1588 case TEXTURE_RECT_INDEX
: _mesa_printf("RECT"); break;
1595 _mesa_printf("ARL addr.x, ");
1596 print_src_reg(&inst
->SrcReg
[0]);
1597 _mesa_printf(";\n");
1599 /* XXX may need for other special-case instructions */
1601 /* typical alu instruction */
1603 const GLuint numRegs
= _mesa_num_inst_src_regs(inst
->Opcode
);
1606 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1608 /* frag prog only */
1609 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1610 _mesa_printf("_SAT");
1612 if (inst
->DstReg
.File
!= PROGRAM_UNDEFINED
) {
1613 _mesa_printf(" %s[%d]%s",
1614 program_file_string((enum register_file
) inst
->DstReg
.File
),
1616 writemask_string(inst
->DstReg
.WriteMask
));
1622 for (j
= 0; j
< numRegs
; j
++) {
1623 print_src_reg(inst
->SrcReg
+ j
);
1624 if (j
+ 1 < numRegs
)
1628 _mesa_printf(";\n");
1635 * Print a vertx/fragment program to stdout.
1636 * XXX this function could be greatly improved.
1639 _mesa_print_program(const struct program
*prog
)
1642 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1643 _mesa_printf("%3d: ", i
);
1644 _mesa_print_instruction(prog
->Instructions
+ i
);
1650 * Print all of a program's parameters.
1653 _mesa_print_program_parameters(GLcontext
*ctx
, const struct program
*prog
)
1657 _mesa_printf("NumInstructions=%d\n", prog
->NumInstructions
);
1658 _mesa_printf("NumTemporaries=%d\n", prog
->NumTemporaries
);
1659 _mesa_printf("NumParameters=%d\n", prog
->NumParameters
);
1660 _mesa_printf("NumAttributes=%d\n", prog
->NumAttributes
);
1661 _mesa_printf("NumAddressRegs=%d\n", prog
->NumAddressRegs
);
1663 _mesa_load_state_parameters(ctx
, prog
->Parameters
);
1666 _mesa_printf("Local Params:\n");
1667 for (i
= 0; i
< MAX_PROGRAM_LOCAL_PARAMS
; i
++){
1668 const GLfloat
*p
= prog
->LocalParams
[i
];
1669 _mesa_printf("%2d: %f, %f, %f, %f\n", i
, p
[0], p
[1], p
[2], p
[3]);
1673 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++){
1674 struct program_parameter
*param
= prog
->Parameters
->Parameters
+ i
;
1675 const GLfloat
*v
= prog
->Parameters
->ParameterValues
[i
];
1676 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1677 i
, param
->Name
, v
[0], v
[1], v
[2], v
[3]);
1683 /**********************************************************************/
1685 /**********************************************************************/
1689 * Bind a program (make it current)
1690 * \note Called from the GL API dispatcher by both glBindProgramNV
1691 * and glBindProgramARB.
1694 _mesa_BindProgram(GLenum target
, GLuint id
)
1696 struct program
*prog
;
1697 GET_CURRENT_CONTEXT(ctx
);
1698 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1700 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1702 if ((target
== GL_VERTEX_PROGRAM_NV
1703 && ctx
->Extensions
.NV_vertex_program
) ||
1704 (target
== GL_VERTEX_PROGRAM_ARB
1705 && ctx
->Extensions
.ARB_vertex_program
)) {
1706 /*** Vertex program binding ***/
1707 struct vertex_program
*curProg
= ctx
->VertexProgram
.Current
;
1708 if (curProg
->Base
.Id
== id
) {
1709 /* binding same program - no change */
1712 if (curProg
->Base
.Id
!= 0) {
1713 /* decrement refcount on previously bound vertex program */
1714 curProg
->Base
.RefCount
--;
1715 /* and delete if refcount goes below one */
1716 if (curProg
->Base
.RefCount
<= 0) {
1717 /* the program ID was already removed from the hash table */
1718 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
1722 else if ((target
== GL_FRAGMENT_PROGRAM_NV
1723 && ctx
->Extensions
.NV_fragment_program
) ||
1724 (target
== GL_FRAGMENT_PROGRAM_ARB
1725 && ctx
->Extensions
.ARB_fragment_program
)) {
1726 /*** Fragment program binding ***/
1727 struct fragment_program
*curProg
= ctx
->FragmentProgram
.Current
;
1728 if (curProg
->Base
.Id
== id
) {
1729 /* binding same program - no change */
1732 if (curProg
->Base
.Id
!= 0) {
1733 /* decrement refcount on previously bound fragment program */
1734 curProg
->Base
.RefCount
--;
1735 /* and delete if refcount goes below one */
1736 if (curProg
->Base
.RefCount
<= 0) {
1737 /* the program ID was already removed from the hash table */
1738 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
1743 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
1747 /* NOTE: binding to a non-existant program is not an error.
1748 * That's supposed to be caught in glBegin.
1751 /* Bind default program */
1753 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
)
1754 prog
= ctx
->Shared
->DefaultVertexProgram
;
1756 prog
= ctx
->Shared
->DefaultFragmentProgram
;
1759 /* Bind user program */
1760 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
1761 if (!prog
|| prog
== &_mesa_DummyProgram
) {
1762 /* allocate a new program now */
1763 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
1765 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
1768 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
1770 else if (prog
->Target
!= target
) {
1771 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1772 "glBindProgramNV/ARB(target mismatch)");
1778 if (target
== GL_VERTEX_PROGRAM_NV
|| target
== GL_VERTEX_PROGRAM_ARB
) {
1779 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
1781 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
1782 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
1785 /* Never null pointers */
1786 ASSERT(ctx
->VertexProgram
.Current
);
1787 ASSERT(ctx
->FragmentProgram
.Current
);
1792 if (ctx
->Driver
.BindProgram
)
1793 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
1798 * Delete a list of programs.
1799 * \note Not compiled into display lists.
1800 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1803 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
1806 GET_CURRENT_CONTEXT(ctx
);
1807 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1810 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
1814 for (i
= 0; i
< n
; i
++) {
1816 struct program
*prog
= (struct program
*)
1817 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
1818 if (prog
== &_mesa_DummyProgram
) {
1819 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1822 /* Unbind program if necessary */
1823 if (prog
->Target
== GL_VERTEX_PROGRAM_NV
||
1824 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
1825 if (ctx
->VertexProgram
.Current
&&
1826 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
1827 /* unbind this currently bound program */
1828 _mesa_BindProgram(prog
->Target
, 0);
1831 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
1832 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1833 if (ctx
->FragmentProgram
.Current
&&
1834 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
1835 /* unbind this currently bound program */
1836 _mesa_BindProgram(prog
->Target
, 0);
1840 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
1843 /* The ID is immediately available for re-use now */
1844 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1846 if (prog
->RefCount
<= 0) {
1847 ctx
->Driver
.DeleteProgram(ctx
, prog
);
1856 * Generate a list of new program identifiers.
1857 * \note Not compiled into display lists.
1858 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1861 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1865 GET_CURRENT_CONTEXT(ctx
);
1866 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1869 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1876 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1878 /* Insert pointer to dummy program as placeholder */
1879 for (i
= 0; i
< (GLuint
) n
; i
++) {
1880 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, &_mesa_DummyProgram
);
1883 /* Return the program names */
1884 for (i
= 0; i
< (GLuint
) n
; i
++) {
1891 * Determine if id names a vertex or fragment program.
1892 * \note Not compiled into display lists.
1893 * \note Called from both glIsProgramNV and glIsProgramARB.
1894 * \param id is the program identifier
1895 * \return GL_TRUE if id is a program, else GL_FALSE.
1897 GLboolean GLAPIENTRY
1898 _mesa_IsProgram(GLuint id
)
1900 GET_CURRENT_CONTEXT(ctx
);
1901 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1906 if (_mesa_HashLookup(ctx
->Shared
->Programs
, id
))
1914 /**********************************************************************/
1915 /* GL_MESA_program_debug extension */
1916 /**********************************************************************/
1920 GLAPI
void GLAPIENTRY
1921 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1924 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1929 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1932 GET_CURRENT_CONTEXT(ctx
);
1935 case GL_FRAGMENT_PROGRAM_ARB
:
1936 if (!ctx
->Extensions
.ARB_fragment_program
) {
1937 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1940 ctx
->FragmentProgram
.Callback
= callback
;
1941 ctx
->FragmentProgram
.CallbackData
= data
;
1943 case GL_FRAGMENT_PROGRAM_NV
:
1944 if (!ctx
->Extensions
.NV_fragment_program
) {
1945 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1948 ctx
->FragmentProgram
.Callback
= callback
;
1949 ctx
->FragmentProgram
.CallbackData
= data
;
1951 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1952 if (!ctx
->Extensions
.ARB_vertex_program
&&
1953 !ctx
->Extensions
.NV_vertex_program
) {
1954 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1957 ctx
->VertexProgram
.Callback
= callback
;
1958 ctx
->VertexProgram
.CallbackData
= data
;
1961 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1968 GLAPI
void GLAPIENTRY
1969 glGetProgramRegisterfvMESA(GLenum target
,
1970 GLsizei len
, const GLubyte
*registerName
,
1973 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1978 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1979 GLsizei len
, const GLubyte
*registerName
,
1983 GET_CURRENT_CONTEXT(ctx
);
1985 /* We _should_ be inside glBegin/glEnd */
1987 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1988 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1993 /* make null-terminated copy of registerName */
1994 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1995 _mesa_memcpy(reg
, registerName
, len
);
1999 case GL_VERTEX_PROGRAM_NV
:
2000 if (!ctx
->Extensions
.ARB_vertex_program
&&
2001 !ctx
->Extensions
.NV_vertex_program
) {
2002 _mesa_error(ctx
, GL_INVALID_ENUM
,
2003 "glGetProgramRegisterfvMESA(target)");
2006 if (!ctx
->VertexProgram
._Enabled
) {
2007 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2008 "glGetProgramRegisterfvMESA");
2011 /* GL_NV_vertex_program */
2012 if (reg
[0] == 'R') {
2014 GLint i
= _mesa_atoi(reg
+ 1);
2015 if (i
>= (GLint
)ctx
->Const
.VertexProgram
.MaxTemps
) {
2016 _mesa_error(ctx
, GL_INVALID_VALUE
,
2017 "glGetProgramRegisterfvMESA(registerName)");
2020 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
2022 else if (reg
[0] == 'v' && reg
[1] == '[') {
2023 /* Vertex Input attribute */
2025 for (i
= 0; i
< ctx
->Const
.VertexProgram
.MaxAttribs
; i
++) {
2026 const char *name
= _mesa_nv_vertex_input_register_name(i
);
2028 _mesa_sprintf(number
, "%d", i
);
2029 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
2030 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
2031 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
2035 _mesa_error(ctx
, GL_INVALID_VALUE
,
2036 "glGetProgramRegisterfvMESA(registerName)");
2039 else if (reg
[0] == 'o' && reg
[1] == '[') {
2040 /* Vertex output attribute */
2042 /* GL_ARB_vertex_program */
2043 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
2047 _mesa_error(ctx
, GL_INVALID_VALUE
,
2048 "glGetProgramRegisterfvMESA(registerName)");
2052 case GL_FRAGMENT_PROGRAM_ARB
:
2053 if (!ctx
->Extensions
.ARB_fragment_program
) {
2054 _mesa_error(ctx
, GL_INVALID_ENUM
,
2055 "glGetProgramRegisterfvMESA(target)");
2058 if (!ctx
->FragmentProgram
._Enabled
) {
2059 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2060 "glGetProgramRegisterfvMESA");
2065 case GL_FRAGMENT_PROGRAM_NV
:
2066 if (!ctx
->Extensions
.NV_fragment_program
) {
2067 _mesa_error(ctx
, GL_INVALID_ENUM
,
2068 "glGetProgramRegisterfvMESA(target)");
2071 if (!ctx
->FragmentProgram
._Enabled
) {
2072 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2073 "glGetProgramRegisterfvMESA");
2076 if (reg
[0] == 'R') {
2078 GLint i
= _mesa_atoi(reg
+ 1);
2079 if (i
>= (GLint
)ctx
->Const
.FragmentProgram
.MaxTemps
) {
2080 _mesa_error(ctx
, GL_INVALID_VALUE
,
2081 "glGetProgramRegisterfvMESA(registerName)");
2084 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
2086 else if (reg
[0] == 'f' && reg
[1] == '[') {
2087 /* Fragment input attribute */
2089 for (i
= 0; i
< ctx
->Const
.FragmentProgram
.MaxAttribs
; i
++) {
2090 const char *name
= _mesa_nv_fragment_input_register_name(i
);
2091 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
2092 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
2096 _mesa_error(ctx
, GL_INVALID_VALUE
,
2097 "glGetProgramRegisterfvMESA(registerName)");
2100 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
2101 /* Fragment output color */
2102 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_COLR
]);
2104 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
2105 /* Fragment output color */
2106 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_COLH
]);
2108 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
2109 /* Fragment output depth */
2110 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_DEPR
]);
2113 /* try user-defined identifiers */
2114 const GLfloat
*value
= _mesa_lookup_parameter_value(
2115 ctx
->FragmentProgram
.Current
->Base
.Parameters
, -1, reg
);
2120 _mesa_error(ctx
, GL_INVALID_VALUE
,
2121 "glGetProgramRegisterfvMESA(registerName)");
2127 _mesa_error(ctx
, GL_INVALID_ENUM
,
2128 "glGetProgramRegisterfvMESA(target)");