2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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 || FEATURE_ARB_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 || FEATURE_ARB_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
);
307 /**********************************************************************/
308 /* Program parameter functions */
309 /**********************************************************************/
311 struct program_parameter_list
*
312 _mesa_new_parameter_list(void)
314 return (struct program_parameter_list
*)
315 _mesa_calloc(sizeof(struct program_parameter_list
));
320 * Free a parameter list and all its parameters
323 _mesa_free_parameter_list(struct program_parameter_list
*paramList
)
326 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
327 if (paramList
->Parameters
[i
].Name
)
328 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
330 _mesa_free(paramList
->Parameters
);
331 if (paramList
->ParameterValues
)
332 _mesa_align_free(paramList
->ParameterValues
);
333 _mesa_free(paramList
);
338 * Add a new parameter to a parameter list.
339 * \param paramList the list to add the parameter to
340 * \param name the parameter name, will be duplicated/copied!
341 * \param values initial parameter value, 4 GLfloats
342 * \param type type of parameter, such as
343 * \return index of new parameter in the list, or -1 if error (out of mem)
346 add_parameter(struct program_parameter_list
*paramList
,
347 const char *name
, const GLfloat values
[4],
348 enum register_file type
)
350 const GLuint n
= paramList
->NumParameters
;
352 if (n
== paramList
->Size
) {
353 /* Need to grow the parameter list array */
354 if (paramList
->Size
== 0)
357 paramList
->Size
*= 2;
360 paramList
->Parameters
= (struct program_parameter
*)
361 _mesa_realloc(paramList
->Parameters
,
362 n
* sizeof(struct program_parameter
),
363 paramList
->Size
* sizeof(struct program_parameter
));
365 paramList
->ParameterValues
= (GLfloat (*)[4])
366 _mesa_align_realloc(paramList
->ParameterValues
, /* old buf */
367 n
* 4 * sizeof(GLfloat
), /* old size */
368 paramList
->Size
* 4 *sizeof(GLfloat
), /* new sz */
372 if (!paramList
->Parameters
||
373 !paramList
->ParameterValues
) {
375 paramList
->NumParameters
= 0;
380 paramList
->NumParameters
= n
+ 1;
382 _mesa_memset(¶mList
->Parameters
[n
], 0,
383 sizeof(struct program_parameter
));
385 paramList
->Parameters
[n
].Name
= name
? _mesa_strdup(name
) : NULL
;
386 paramList
->Parameters
[n
].Type
= type
;
388 COPY_4V(paramList
->ParameterValues
[n
], values
);
395 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
396 * \return index of the new entry in the parameter list
399 _mesa_add_named_parameter(struct program_parameter_list
*paramList
,
400 const char *name
, const GLfloat values
[4])
402 return add_parameter(paramList
, name
, values
, PROGRAM_NAMED_PARAM
);
407 * Add a new unnamed constant to the parameter list.
408 * \param paramList - the parameter list
409 * \param values - four float values
410 * \return index of the new parameter.
413 _mesa_add_named_constant(struct program_parameter_list
*paramList
,
414 const char *name
, const GLfloat values
[4])
416 return add_parameter(paramList
, name
, values
, PROGRAM_CONSTANT
);
421 * Add a new unnamed constant to the parameter list.
422 * \param paramList - the parameter list
423 * \param values - four float values
424 * \return index of the new parameter.
427 _mesa_add_unnamed_constant(struct program_parameter_list
*paramList
,
428 const GLfloat values
[4])
430 return add_parameter(paramList
, NULL
, values
, PROGRAM_CONSTANT
);
435 * Add a new state reference to the parameter list.
436 * \param paramList - the parameter list
437 * \param state - an array of 6 state tokens
439 * \return index of the new parameter.
442 _mesa_add_state_reference(struct program_parameter_list
*paramList
,
443 const GLint
*stateTokens
)
445 /* XXX we should probably search the current parameter list to see if
446 * the new state reference is already present.
449 const char *name
= make_state_string(stateTokens
);
451 index
= add_parameter(paramList
, name
, NULL
, PROGRAM_STATE_VAR
);
454 for (i
= 0; i
< 6; i
++) {
455 paramList
->Parameters
[index
].StateIndexes
[i
]
456 = (enum state_index
) stateTokens
[i
];
458 paramList
->StateFlags
|=
459 make_state_flags(stateTokens
);
462 /* free name string here since we duplicated it in add_parameter() */
463 _mesa_free((void *) name
);
470 * Lookup a parameter value by name in the given parameter list.
471 * \return pointer to the float[4] values.
474 _mesa_lookup_parameter_value(struct program_parameter_list
*paramList
,
475 GLsizei nameLen
, const char *name
)
483 /* name is null-terminated */
484 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
485 if (paramList
->Parameters
[i
].Name
&&
486 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
487 return paramList
->ParameterValues
[i
];
491 /* name is not null-terminated, use nameLen */
492 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
493 if (paramList
->Parameters
[i
].Name
&&
494 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
495 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
496 return paramList
->ParameterValues
[i
];
504 * Lookup a parameter index by name in the given parameter list.
505 * \return index of parameter in the list.
508 _mesa_lookup_parameter_index(struct program_parameter_list
*paramList
,
509 GLsizei nameLen
, const char *name
)
517 /* name is null-terminated */
518 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
519 if (paramList
->Parameters
[i
].Name
&&
520 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
525 /* name is not null-terminated, use nameLen */
526 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
527 if (paramList
->Parameters
[i
].Name
&&
528 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
529 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
538 * Use the list of tokens in the state[] array to find global GL state
539 * and return it in <value>. Usually, four values are returned in <value>
540 * but matrix queries may return as many as 16 values.
541 * This function is used for ARB vertex/fragment programs.
542 * The program parser will produce the state[] values.
545 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
551 /* state[1] is either 0=front or 1=back side */
552 const GLuint face
= (GLuint
) state
[1];
553 /* state[2] is the material attribute */
557 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
559 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
563 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
565 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
569 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
571 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
575 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
577 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
579 case STATE_SHININESS
:
581 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
583 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
589 _mesa_problem(ctx
, "Invalid material state in fetch_state");
595 /* state[1] is the light number */
596 const GLuint ln
= (GLuint
) state
[1];
597 /* state[2] is the light attribute */
600 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
603 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
606 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
609 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
611 case STATE_ATTENUATION
:
612 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
613 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
614 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
615 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
617 case STATE_SPOT_DIRECTION
:
618 COPY_3V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
619 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
623 GLfloat eye_z
[] = {0, 0, 1};
625 /* Compute infinite half angle vector:
626 * half-vector = light_position + (0, 0, 1)
627 * and then normalize. w = 0
629 * light.EyePosition.w should be 0 for infinite lights.
631 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
632 NORMALIZE_3FV(value
);
636 case STATE_POSITION_NORMALIZED
:
637 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
638 NORMALIZE_3FV( value
);
641 _mesa_problem(ctx
, "Invalid light state in fetch_state");
645 case STATE_LIGHTMODEL_AMBIENT
:
646 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
648 case STATE_LIGHTMODEL_SCENECOLOR
:
652 for (i
= 0; i
< 3; i
++) {
653 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
654 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
655 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
657 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
662 for (i
= 0; i
< 3; i
++) {
663 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
664 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
665 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
667 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
670 case STATE_LIGHTPROD
:
672 const GLuint ln
= (GLuint
) state
[1];
673 const GLuint face
= (GLuint
) state
[2];
675 ASSERT(face
== 0 || face
== 1);
678 for (i
= 0; i
< 3; i
++) {
679 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
680 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
682 /* [3] = material alpha */
683 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
686 for (i
= 0; i
< 3; i
++) {
687 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
688 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
690 /* [3] = material alpha */
691 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
694 for (i
= 0; i
< 3; i
++) {
695 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
696 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
698 /* [3] = material alpha */
699 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
702 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
708 /* state[1] is the texture unit */
709 const GLuint unit
= (GLuint
) state
[1];
710 /* state[2] is the texgen attribute */
712 case STATE_TEXGEN_EYE_S
:
713 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
715 case STATE_TEXGEN_EYE_T
:
716 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
718 case STATE_TEXGEN_EYE_R
:
719 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
721 case STATE_TEXGEN_EYE_Q
:
722 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
724 case STATE_TEXGEN_OBJECT_S
:
725 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
727 case STATE_TEXGEN_OBJECT_T
:
728 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
730 case STATE_TEXGEN_OBJECT_R
:
731 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
733 case STATE_TEXGEN_OBJECT_Q
:
734 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
737 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
741 case STATE_TEXENV_COLOR
:
743 /* state[1] is the texture unit */
744 const GLuint unit
= (GLuint
) state
[1];
745 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
748 case STATE_FOG_COLOR
:
749 COPY_4V(value
, ctx
->Fog
.Color
);
751 case STATE_FOG_PARAMS
:
752 value
[0] = ctx
->Fog
.Density
;
753 value
[1] = ctx
->Fog
.Start
;
754 value
[2] = ctx
->Fog
.End
;
755 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
757 case STATE_CLIPPLANE
:
759 const GLuint plane
= (GLuint
) state
[1];
760 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
763 case STATE_POINT_SIZE
:
764 value
[0] = ctx
->Point
.Size
;
765 value
[1] = ctx
->Point
.MinSize
;
766 value
[2] = ctx
->Point
.MaxSize
;
767 value
[3] = ctx
->Point
.Threshold
;
769 case STATE_POINT_ATTENUATION
:
770 value
[0] = ctx
->Point
.Params
[0];
771 value
[1] = ctx
->Point
.Params
[1];
772 value
[2] = ctx
->Point
.Params
[2];
777 /* state[1] = modelview, projection, texture, etc. */
778 /* state[2] = which texture matrix or program matrix */
779 /* state[3] = first column to fetch */
780 /* state[4] = last column to fetch */
781 /* state[5] = transpose, inverse or invtrans */
783 const GLmatrix
*matrix
;
784 const enum state_index mat
= state
[1];
785 const GLuint index
= (GLuint
) state
[2];
786 const GLuint first
= (GLuint
) state
[3];
787 const GLuint last
= (GLuint
) state
[4];
788 const enum state_index modifier
= state
[5];
791 if (mat
== STATE_MODELVIEW
) {
792 matrix
= ctx
->ModelviewMatrixStack
.Top
;
794 else if (mat
== STATE_PROJECTION
) {
795 matrix
= ctx
->ProjectionMatrixStack
.Top
;
797 else if (mat
== STATE_MVP
) {
798 matrix
= &ctx
->_ModelProjectMatrix
;
800 else if (mat
== STATE_TEXTURE
) {
801 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
803 else if (mat
== STATE_PROGRAM
) {
804 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
807 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
810 if (modifier
== STATE_MATRIX_INVERSE
||
811 modifier
== STATE_MATRIX_INVTRANS
) {
812 /* Be sure inverse is up to date:
814 _math_matrix_analyse( (GLmatrix
*) matrix
);
820 if (modifier
== STATE_MATRIX_TRANSPOSE
||
821 modifier
== STATE_MATRIX_INVTRANS
) {
822 for (i
= 0, row
= first
; row
<= last
; row
++) {
823 value
[i
++] = m
[row
* 4 + 0];
824 value
[i
++] = m
[row
* 4 + 1];
825 value
[i
++] = m
[row
* 4 + 2];
826 value
[i
++] = m
[row
* 4 + 3];
830 for (i
= 0, row
= first
; row
<= last
; row
++) {
831 value
[i
++] = m
[row
+ 0];
832 value
[i
++] = m
[row
+ 4];
833 value
[i
++] = m
[row
+ 8];
834 value
[i
++] = m
[row
+ 12];
839 case STATE_DEPTH_RANGE
:
840 value
[0] = ctx
->Viewport
.Near
; /* near */
841 value
[1] = ctx
->Viewport
.Far
; /* far */
842 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
845 case STATE_FRAGMENT_PROGRAM
:
847 /* state[1] = {STATE_ENV, STATE_LOCAL} */
848 /* state[2] = parameter index */
849 const int idx
= (int) state
[2];
852 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
855 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
858 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
864 case STATE_VERTEX_PROGRAM
:
866 /* state[1] = {STATE_ENV, STATE_LOCAL} */
867 /* state[2] = parameter index */
868 const int idx
= (int) state
[2];
871 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
874 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
877 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
886 case STATE_NORMAL_SCALE
:
887 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
890 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
897 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
904 * Return a bit mask of the Mesa state flags under which a parameter's
905 * value might change.
907 static GLuint
make_state_flags(const GLint state
[])
912 case STATE_LIGHTMODEL_AMBIENT
:
913 case STATE_LIGHTMODEL_SCENECOLOR
:
914 case STATE_LIGHTPROD
:
918 case STATE_TEXENV_COLOR
:
921 case STATE_FOG_COLOR
:
922 case STATE_FOG_PARAMS
:
925 case STATE_CLIPPLANE
:
926 return _NEW_TRANSFORM
;
928 case STATE_POINT_SIZE
:
929 case STATE_POINT_ATTENUATION
:
934 case STATE_MODELVIEW
:
935 return _NEW_MODELVIEW
;
936 case STATE_PROJECTION
:
937 return _NEW_PROJECTION
;
939 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
941 return _NEW_TEXTURE_MATRIX
;
943 return _NEW_TRACK_MATRIX
;
945 _mesa_problem(NULL
, "unexpected matrix in make_state_flags()");
949 case STATE_DEPTH_RANGE
:
950 return _NEW_VIEWPORT
;
952 case STATE_FRAGMENT_PROGRAM
:
953 case STATE_VERTEX_PROGRAM
:
958 case STATE_NORMAL_SCALE
:
959 return _NEW_MODELVIEW
;
961 _mesa_problem(NULL
, "unexpected int. state in make_state_flags()");
966 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
973 append(char *dst
, const char *src
)
984 append_token(char *dst
, enum state_index k
)
988 append(dst
, "material.");
991 append(dst
, "light");
993 case STATE_LIGHTMODEL_AMBIENT
:
994 append(dst
, "lightmodel.ambient");
996 case STATE_LIGHTMODEL_SCENECOLOR
:
998 case STATE_LIGHTPROD
:
999 append(dst
, "lightprod");
1002 append(dst
, "texgen");
1004 case STATE_FOG_COLOR
:
1005 append(dst
, "fog.color");
1007 case STATE_FOG_PARAMS
:
1008 append(dst
, "fog.params");
1010 case STATE_CLIPPLANE
:
1011 append(dst
, "clip");
1013 case STATE_POINT_SIZE
:
1014 append(dst
, "point.size");
1016 case STATE_POINT_ATTENUATION
:
1017 append(dst
, "point.attenuation");
1020 append(dst
, "matrix.");
1022 case STATE_MODELVIEW
:
1023 append(dst
, "modelview");
1025 case STATE_PROJECTION
:
1026 append(dst
, "projection");
1032 append(dst
, "texture");
1035 append(dst
, "program");
1037 case STATE_MATRIX_INVERSE
:
1038 append(dst
, ".inverse");
1040 case STATE_MATRIX_TRANSPOSE
:
1041 append(dst
, ".transpose");
1043 case STATE_MATRIX_INVTRANS
:
1044 append(dst
, ".invtrans");
1047 append(dst
, "ambient");
1050 append(dst
, "diffuse");
1052 case STATE_SPECULAR
:
1053 append(dst
, "specular");
1055 case STATE_EMISSION
:
1056 append(dst
, "emission");
1058 case STATE_SHININESS
:
1059 append(dst
, "shininess");
1062 append(dst
, "half");
1064 case STATE_POSITION
:
1065 append(dst
, ".position");
1067 case STATE_ATTENUATION
:
1068 append(dst
, ".attenuation");
1070 case STATE_SPOT_DIRECTION
:
1071 append(dst
, ".spot.direction");
1073 case STATE_TEXGEN_EYE_S
:
1074 append(dst
, "eye.s");
1076 case STATE_TEXGEN_EYE_T
:
1077 append(dst
, "eye.t");
1079 case STATE_TEXGEN_EYE_R
:
1080 append(dst
, "eye.r");
1082 case STATE_TEXGEN_EYE_Q
:
1083 append(dst
, "eye.q");
1085 case STATE_TEXGEN_OBJECT_S
:
1086 append(dst
, "object.s");
1088 case STATE_TEXGEN_OBJECT_T
:
1089 append(dst
, "object.t");
1091 case STATE_TEXGEN_OBJECT_R
:
1092 append(dst
, "object.r");
1094 case STATE_TEXGEN_OBJECT_Q
:
1095 append(dst
, "object.q");
1097 case STATE_TEXENV_COLOR
:
1098 append(dst
, "texenv");
1100 case STATE_DEPTH_RANGE
:
1101 append(dst
, "depth.range");
1103 case STATE_VERTEX_PROGRAM
:
1104 case STATE_FRAGMENT_PROGRAM
:
1110 append(dst
, "local");
1112 case STATE_INTERNAL
:
1113 case STATE_NORMAL_SCALE
:
1114 case STATE_POSITION_NORMALIZED
:
1115 append(dst
, "(internal)");
1123 append_face(char *dst
, GLint face
)
1126 append(dst
, "front.");
1128 append(dst
, "back.");
1132 append_index(char *dst
, GLint index
)
1135 _mesa_sprintf(s
, "[%d].", index
);
1140 * Make a string from the given state vector.
1141 * For example, return "state.matrix.texture[2].inverse".
1142 * Use _mesa_free() to deallocate the string.
1145 make_state_string(const GLint state
[6])
1147 char str
[1000] = "";
1150 append(str
, "state.");
1151 append_token(str
, (enum state_index
) state
[0]);
1154 case STATE_MATERIAL
:
1155 append_face(str
, state
[1]);
1156 append_token(str
, (enum state_index
) state
[2]);
1159 append(str
, "light");
1160 append_index(str
, state
[1]); /* light number [i]. */
1161 append_token(str
, (enum state_index
) state
[2]); /* coefficients */
1163 case STATE_LIGHTMODEL_AMBIENT
:
1164 append(str
, "lightmodel.ambient");
1166 case STATE_LIGHTMODEL_SCENECOLOR
:
1167 if (state
[1] == 0) {
1168 append(str
, "lightmodel.front.scenecolor");
1171 append(str
, "lightmodel.back.scenecolor");
1174 case STATE_LIGHTPROD
:
1175 append_index(str
, state
[1]); /* light number [i]. */
1176 append_face(str
, state
[2]);
1177 append_token(str
, (enum state_index
) state
[3]);
1180 append_index(str
, state
[1]); /* tex unit [i] */
1181 append_token(str
, (enum state_index
) state
[2]); /* plane coef */
1183 case STATE_TEXENV_COLOR
:
1184 append_index(str
, state
[1]); /* tex unit [i] */
1185 append(str
, "color");
1187 case STATE_FOG_COLOR
:
1188 case STATE_FOG_PARAMS
:
1190 case STATE_CLIPPLANE
:
1191 append_index(str
, state
[1]); /* plane [i] */
1192 append(str
, "plane");
1194 case STATE_POINT_SIZE
:
1195 case STATE_POINT_ATTENUATION
:
1199 /* state[1] = modelview, projection, texture, etc. */
1200 /* state[2] = which texture matrix or program matrix */
1201 /* state[3] = first column to fetch */
1202 /* state[4] = last column to fetch */
1203 /* state[5] = transpose, inverse or invtrans */
1204 const enum state_index mat
= (enum state_index
) state
[1];
1205 const GLuint index
= (GLuint
) state
[2];
1206 const GLuint first
= (GLuint
) state
[3];
1207 const GLuint last
= (GLuint
) state
[4];
1208 const enum state_index modifier
= (enum state_index
) state
[5];
1209 append_token(str
, mat
);
1211 append_index(str
, index
);
1213 append_token(str
, modifier
);
1215 _mesa_sprintf(tmp
, ".row[%d]", first
);
1217 _mesa_sprintf(tmp
, ".row[%d..%d]", first
, last
);
1221 case STATE_DEPTH_RANGE
:
1223 case STATE_FRAGMENT_PROGRAM
:
1224 case STATE_VERTEX_PROGRAM
:
1225 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1226 /* state[2] = parameter index */
1227 append_token(str
, (enum state_index
) state
[1]);
1228 append_index(str
, state
[2]);
1230 case STATE_INTERNAL
:
1233 _mesa_problem(NULL
, "Invalid state in maka_state_string");
1237 return _mesa_strdup(str
);
1242 * Loop over all the parameters in a parameter list. If the parameter
1243 * is a GL state reference, look up the current value of that state
1244 * variable and put it into the parameter's Value[4] array.
1245 * This would be called at glBegin time when using a fragment program.
1248 _mesa_load_state_parameters(GLcontext
*ctx
,
1249 struct program_parameter_list
*paramList
)
1256 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1257 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1258 _mesa_fetch_state(ctx
,
1259 paramList
->Parameters
[i
].StateIndexes
,
1260 paramList
->ParameterValues
[i
]);
1267 * Initialize program instruction fields to defaults.
1270 _mesa_init_instruction(struct prog_instruction
*inst
)
1272 _mesa_bzero(inst
, sizeof(struct prog_instruction
));
1274 inst
->SrcReg
[0].File
= PROGRAM_UNDEFINED
;
1275 inst
->SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
1276 inst
->SrcReg
[1].File
= PROGRAM_UNDEFINED
;
1277 inst
->SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
1278 inst
->SrcReg
[2].File
= PROGRAM_UNDEFINED
;
1279 inst
->SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
1281 inst
->DstReg
.File
= PROGRAM_UNDEFINED
;
1282 inst
->DstReg
.WriteMask
= WRITEMASK_XYZW
;
1283 inst
->DstReg
.CondMask
= COND_TR
;
1284 inst
->DstReg
.CondSwizzle
= SWIZZLE_NOOP
;
1286 inst
->SaturateMode
= SATURATE_OFF
;
1287 inst
->Precision
= FLOAT32
;
1292 * Basic info about each instruction
1294 struct instruction_info
1296 enum prog_opcode Opcode
;
1303 * \note Opcode should equal array index!
1305 static const struct instruction_info InstInfo
[MAX_OPCODE
] = {
1306 { OPCODE_ABS
, "ABS", 1 },
1307 { OPCODE_ADD
, "ADD", 2 },
1308 { OPCODE_ARA
, "ARA", 1 },
1309 { OPCODE_ARL
, "ARL", 1 },
1310 { OPCODE_ARL_NV
, "ARL", 1 },
1311 { OPCODE_ARR
, "ARL", 1 },
1312 { OPCODE_BRA
, "BRA", 1 },
1313 { OPCODE_CAL
, "CAL", 1 },
1314 { OPCODE_CMP
, "CMP", 3 },
1315 { OPCODE_COS
, "COS", 1 },
1316 { OPCODE_DDX
, "DDX", 1 },
1317 { OPCODE_DDY
, "DDY", 1 },
1318 { OPCODE_DP3
, "DP3", 2 },
1319 { OPCODE_DP4
, "DP4", 2 },
1320 { OPCODE_DPH
, "DPH", 2 },
1321 { OPCODE_DST
, "DST", 2 },
1322 { OPCODE_END
, "END", 0 },
1323 { OPCODE_EX2
, "EX2", 1 },
1324 { OPCODE_EXP
, "EXP", 1 },
1325 { OPCODE_FLR
, "FLR", 1 },
1326 { OPCODE_FRC
, "FRC", 1 },
1327 { OPCODE_KIL
, "KIL", 1 },
1328 { OPCODE_KIL_NV
, "KIL", 0 },
1329 { OPCODE_LG2
, "LG2", 1 },
1330 { OPCODE_LIT
, "LIT", 1 },
1331 { OPCODE_LOG
, "LOG", 1 },
1332 { OPCODE_LRP
, "LRP", 3 },
1333 { OPCODE_MAD
, "MAD", 3 },
1334 { OPCODE_MAX
, "MAX", 2 },
1335 { OPCODE_MIN
, "MIN", 2 },
1336 { OPCODE_MOV
, "MOV", 1 },
1337 { OPCODE_MUL
, "MUL", 2 },
1338 { OPCODE_PK2H
, "PK2H", 1 },
1339 { OPCODE_PK2US
, "PK2US", 1 },
1340 { OPCODE_PK4B
, "PK4B", 1 },
1341 { OPCODE_PK4UB
, "PK4UB", 1 },
1342 { OPCODE_POW
, "POW", 2 },
1343 { OPCODE_POPA
, "POPA", 0 },
1344 { OPCODE_PRINT
, "PRINT", 1 },
1345 { OPCODE_PUSHA
, "PUSHA", 0 },
1346 { OPCODE_RCC
, "RCC", 1 },
1347 { OPCODE_RCP
, "RCP", 1 },
1348 { OPCODE_RET
, "RET", 1 },
1349 { OPCODE_RFL
, "RFL", 1 },
1350 { OPCODE_RSQ
, "RSQ", 1 },
1351 { OPCODE_SCS
, "SCS", 1 },
1352 { OPCODE_SEQ
, "SEQ", 2 },
1353 { OPCODE_SFL
, "SFL", 0 },
1354 { OPCODE_SGE
, "SGE", 2 },
1355 { OPCODE_SGT
, "SGT", 2 },
1356 { OPCODE_SIN
, "SIN", 1 },
1357 { OPCODE_SLE
, "SLE", 2 },
1358 { OPCODE_SLT
, "SLT", 2 },
1359 { OPCODE_SNE
, "SNE", 2 },
1360 { OPCODE_SSG
, "SSG", 1 },
1361 { OPCODE_STR
, "STR", 0 },
1362 { OPCODE_SUB
, "SUB", 2 },
1363 { OPCODE_SWZ
, "SWZ", 1 },
1364 { OPCODE_TEX
, "TEX", 1 },
1365 { OPCODE_TXB
, "TXB", 1 },
1366 { OPCODE_TXD
, "TXD", 3 },
1367 { OPCODE_TXL
, "TXL", 1 },
1368 { OPCODE_TXP
, "TXP", 1 },
1369 { OPCODE_TXP_NV
, "TXP", 1 },
1370 { OPCODE_UP2H
, "UP2H", 1 },
1371 { OPCODE_UP2US
, "UP2US", 1 },
1372 { OPCODE_UP4B
, "UP4B", 1 },
1373 { OPCODE_UP4UB
, "UP4UB", 1 },
1374 { OPCODE_X2D
, "X2D", 3 },
1375 { OPCODE_XPD
, "XPD", 2 }
1380 * Return the number of src registers for the given instruction/opcode.
1383 _mesa_num_inst_src_regs(enum prog_opcode opcode
)
1387 for (i
= 0; i
< MAX_OPCODE
; i
++) {
1388 ASSERT(i
== InstInfo
[i
].Opcode
);
1391 for (i
= 0; i
< MAX_OPCODE
; i
++) {
1392 if (InstInfo
[i
].Opcode
== opcode
) {
1393 return InstInfo
[i
].NumSrcRegs
;
1396 _mesa_problem(NULL
, "invalid opcode in _mesa_num_inst_src_regs");
1402 * Return string name for given program opcode.
1405 _mesa_opcode_string(enum prog_opcode opcode
)
1407 ASSERT(opcode
< MAX_OPCODE
);
1408 return InstInfo
[opcode
].Name
;
1412 * Return string name for given program/register file.
1415 program_file_string(enum register_file f
)
1418 case PROGRAM_TEMPORARY
:
1420 case PROGRAM_LOCAL_PARAM
:
1422 case PROGRAM_ENV_PARAM
:
1424 case PROGRAM_STATE_VAR
:
1428 case PROGRAM_OUTPUT
:
1430 case PROGRAM_NAMED_PARAM
:
1432 case PROGRAM_CONSTANT
:
1434 case PROGRAM_WRITE_ONLY
:
1435 return "WRITE_ONLY";
1436 case PROGRAM_ADDRESS
:
1445 * Return a string representation of the given swizzle word.
1446 * If extended is true, use extended (comma-separated) format.
1449 swizzle_string(GLuint swizzle
, GLuint negateBase
, GLboolean extended
)
1451 static const char swz
[] = "xyzw01";
1455 if (!extended
&& swizzle
== SWIZZLE_NOOP
&& negateBase
== 0)
1456 return ""; /* no swizzle/negation */
1461 if (negateBase
& 0x1)
1463 s
[i
++] = swz
[GET_SWZ(swizzle
, 0)];
1469 if (negateBase
& 0x2)
1471 s
[i
++] = swz
[GET_SWZ(swizzle
, 1)];
1477 if (negateBase
& 0x4)
1479 s
[i
++] = swz
[GET_SWZ(swizzle
, 2)];
1485 if (negateBase
& 0x8)
1487 s
[i
++] = swz
[GET_SWZ(swizzle
, 3)];
1495 writemask_string(GLuint writeMask
)
1500 if (writeMask
== WRITEMASK_XYZW
)
1504 if (writeMask
& WRITEMASK_X
)
1506 if (writeMask
& WRITEMASK_Y
)
1508 if (writeMask
& WRITEMASK_Z
)
1510 if (writeMask
& WRITEMASK_W
)
1518 print_dst_reg(const struct prog_dst_register
*dstReg
)
1520 _mesa_printf(" %s[%d]%s",
1521 program_file_string((enum register_file
) dstReg
->File
),
1523 writemask_string(dstReg
->WriteMask
));
1527 print_src_reg(const struct prog_src_register
*srcReg
)
1529 _mesa_printf("%s[%d]%s",
1530 program_file_string((enum register_file
) srcReg
->File
),
1532 swizzle_string(srcReg
->Swizzle
,
1533 srcReg
->NegateBase
, GL_FALSE
));
1538 * Print a single vertex/fragment program instruction.
1541 _mesa_print_instruction(const struct prog_instruction
*inst
)
1543 switch (inst
->Opcode
) {
1545 _mesa_printf("PRINT '%s'", inst
->Data
);
1546 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1548 _mesa_printf("%s[%d]%s",
1549 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1550 inst
->SrcReg
[0].Index
,
1551 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1552 inst
->SrcReg
[0].NegateBase
, GL_FALSE
));
1554 _mesa_printf(";\n");
1557 _mesa_printf("SWZ");
1558 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1559 _mesa_printf("_SAT");
1560 print_dst_reg(&inst
->DstReg
);
1561 _mesa_printf("%s[%d], %s;\n",
1562 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1563 inst
->SrcReg
[0].Index
,
1564 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1565 inst
->SrcReg
[0].NegateBase
, GL_TRUE
));
1570 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1571 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1572 _mesa_printf("_SAT");
1574 print_dst_reg(&inst
->DstReg
);
1576 print_src_reg(&inst
->SrcReg
[0]);
1577 _mesa_printf(", texture[%d], ", inst
->TexSrcUnit
);
1578 switch (inst
->TexSrcTarget
) {
1579 case TEXTURE_1D_INDEX
: _mesa_printf("1D"); break;
1580 case TEXTURE_2D_INDEX
: _mesa_printf("2D"); break;
1581 case TEXTURE_3D_INDEX
: _mesa_printf("3D"); break;
1582 case TEXTURE_CUBE_INDEX
: _mesa_printf("CUBE"); break;
1583 case TEXTURE_RECT_INDEX
: _mesa_printf("RECT"); break;
1590 _mesa_printf("ARL addr.x, ");
1591 print_src_reg(&inst
->SrcReg
[0]);
1592 _mesa_printf(";\n");
1594 /* XXX may need for other special-case instructions */
1596 /* typical alu instruction */
1598 const GLuint numRegs
= _mesa_num_inst_src_regs(inst
->Opcode
);
1601 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1603 /* frag prog only */
1604 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1605 _mesa_printf("_SAT");
1607 if (inst
->DstReg
.File
!= PROGRAM_UNDEFINED
) {
1608 _mesa_printf(" %s[%d]%s",
1609 program_file_string((enum register_file
) inst
->DstReg
.File
),
1611 writemask_string(inst
->DstReg
.WriteMask
));
1617 for (j
= 0; j
< numRegs
; j
++) {
1618 print_src_reg(inst
->SrcReg
+ j
);
1619 if (j
+ 1 < numRegs
)
1623 _mesa_printf(";\n");
1630 * Print a vertx/fragment program to stdout.
1631 * XXX this function could be greatly improved.
1634 _mesa_print_program(const struct program
*prog
)
1637 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1638 _mesa_printf("%3d: ", i
);
1639 _mesa_print_instruction(prog
->Instructions
+ i
);
1645 * Print all of a program's parameters.
1648 _mesa_print_program_parameters(GLcontext
*ctx
, const struct program
*prog
)
1652 _mesa_printf("NumInstructions=%d\n", prog
->NumInstructions
);
1653 _mesa_printf("NumTemporaries=%d\n", prog
->NumTemporaries
);
1654 _mesa_printf("NumParameters=%d\n", prog
->NumParameters
);
1655 _mesa_printf("NumAttributes=%d\n", prog
->NumAttributes
);
1656 _mesa_printf("NumAddressRegs=%d\n", prog
->NumAddressRegs
);
1658 _mesa_load_state_parameters(ctx
, prog
->Parameters
);
1661 _mesa_printf("Local Params:\n");
1662 for (i
= 0; i
< MAX_PROGRAM_LOCAL_PARAMS
; i
++){
1663 const GLfloat
*p
= prog
->LocalParams
[i
];
1664 _mesa_printf("%2d: %f, %f, %f, %f\n", i
, p
[0], p
[1], p
[2], p
[3]);
1668 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++){
1669 struct program_parameter
*param
= prog
->Parameters
->Parameters
+ i
;
1670 const GLfloat
*v
= prog
->Parameters
->ParameterValues
[i
];
1671 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1672 i
, param
->Name
, v
[0], v
[1], v
[2], v
[3]);
1678 /**********************************************************************/
1680 /**********************************************************************/
1684 * Bind a program (make it current)
1685 * \note Called from the GL API dispatcher by both glBindProgramNV
1686 * and glBindProgramARB.
1689 _mesa_BindProgram(GLenum target
, GLuint id
)
1691 struct program
*prog
;
1692 GET_CURRENT_CONTEXT(ctx
);
1693 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1695 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1697 if ((target
== GL_VERTEX_PROGRAM_ARB
) && /* == GL_VERTEX_PROGRAM_NV */
1698 (ctx
->Extensions
.NV_vertex_program
||
1699 ctx
->Extensions
.ARB_vertex_program
)) {
1700 /*** Vertex program binding ***/
1701 struct vertex_program
*curProg
= ctx
->VertexProgram
.Current
;
1702 if (curProg
->Base
.Id
== id
) {
1703 /* binding same program - no change */
1706 if (curProg
->Base
.Id
!= 0) {
1707 /* decrement refcount on previously bound vertex program */
1708 curProg
->Base
.RefCount
--;
1709 /* and delete if refcount goes below one */
1710 if (curProg
->Base
.RefCount
<= 0) {
1711 /* the program ID was already removed from the hash table */
1712 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
1716 else if ((target
== GL_FRAGMENT_PROGRAM_NV
1717 && ctx
->Extensions
.NV_fragment_program
) ||
1718 (target
== GL_FRAGMENT_PROGRAM_ARB
1719 && ctx
->Extensions
.ARB_fragment_program
)) {
1720 /*** Fragment program binding ***/
1721 struct fragment_program
*curProg
= ctx
->FragmentProgram
.Current
;
1722 if (curProg
->Base
.Id
== id
) {
1723 /* binding same program - no change */
1726 if (curProg
->Base
.Id
!= 0) {
1727 /* decrement refcount on previously bound fragment program */
1728 curProg
->Base
.RefCount
--;
1729 /* and delete if refcount goes below one */
1730 if (curProg
->Base
.RefCount
<= 0) {
1731 /* the program ID was already removed from the hash table */
1732 ctx
->Driver
.DeleteProgram(ctx
, &(curProg
->Base
));
1737 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
1741 /* NOTE: binding to a non-existant program is not an error.
1742 * That's supposed to be caught in glBegin.
1745 /* Bind default program */
1747 if (target
== GL_VERTEX_PROGRAM_ARB
) /* == GL_VERTEX_PROGRAM_NV */
1748 prog
= ctx
->Shared
->DefaultVertexProgram
;
1750 prog
= ctx
->Shared
->DefaultFragmentProgram
;
1753 /* Bind user program */
1754 prog
= (struct program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
1755 if (!prog
|| prog
== &_mesa_DummyProgram
) {
1756 /* allocate a new program now */
1757 prog
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
1759 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
1762 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, prog
);
1764 else if (prog
->Target
!= target
) {
1765 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1766 "glBindProgramNV/ARB(target mismatch)");
1772 if (target
== GL_VERTEX_PROGRAM_ARB
) { /* == GL_VERTEX_PROGRAM_NV */
1773 ctx
->VertexProgram
.Current
= (struct vertex_program
*) prog
;
1775 else if (target
== GL_FRAGMENT_PROGRAM_NV
|| target
== GL_FRAGMENT_PROGRAM_ARB
) {
1776 ctx
->FragmentProgram
.Current
= (struct fragment_program
*) prog
;
1779 /* Never null pointers */
1780 ASSERT(ctx
->VertexProgram
.Current
);
1781 ASSERT(ctx
->FragmentProgram
.Current
);
1786 if (ctx
->Driver
.BindProgram
)
1787 ctx
->Driver
.BindProgram(ctx
, target
, prog
);
1792 * Delete a list of programs.
1793 * \note Not compiled into display lists.
1794 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1797 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
1800 GET_CURRENT_CONTEXT(ctx
);
1801 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1804 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
1808 for (i
= 0; i
< n
; i
++) {
1810 struct program
*prog
= (struct program
*)
1811 _mesa_HashLookup(ctx
->Shared
->Programs
, ids
[i
]);
1812 if (prog
== &_mesa_DummyProgram
) {
1813 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1816 /* Unbind program if necessary */
1817 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| /* == GL_VERTEX_PROGRAM_NV */
1818 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
1819 if (ctx
->VertexProgram
.Current
&&
1820 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
1821 /* unbind this currently bound program */
1822 _mesa_BindProgram(prog
->Target
, 0);
1825 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
1826 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1827 if (ctx
->FragmentProgram
.Current
&&
1828 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
1829 /* unbind this currently bound program */
1830 _mesa_BindProgram(prog
->Target
, 0);
1834 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
1837 /* The ID is immediately available for re-use now */
1838 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1840 if (prog
->RefCount
<= 0) {
1841 ctx
->Driver
.DeleteProgram(ctx
, prog
);
1850 * Generate a list of new program identifiers.
1851 * \note Not compiled into display lists.
1852 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1855 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1859 GET_CURRENT_CONTEXT(ctx
);
1860 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1863 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1870 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1872 /* Insert pointer to dummy program as placeholder */
1873 for (i
= 0; i
< (GLuint
) n
; i
++) {
1874 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, &_mesa_DummyProgram
);
1877 /* Return the program names */
1878 for (i
= 0; i
< (GLuint
) n
; i
++) {
1885 * Determine if id names a vertex or fragment program.
1886 * \note Not compiled into display lists.
1887 * \note Called from both glIsProgramNV and glIsProgramARB.
1888 * \param id is the program identifier
1889 * \return GL_TRUE if id is a program, else GL_FALSE.
1891 GLboolean GLAPIENTRY
1892 _mesa_IsProgram(GLuint id
)
1894 GET_CURRENT_CONTEXT(ctx
);
1895 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1900 if (_mesa_HashLookup(ctx
->Shared
->Programs
, id
))
1908 /**********************************************************************/
1909 /* GL_MESA_program_debug extension */
1910 /**********************************************************************/
1914 GLAPI
void GLAPIENTRY
1915 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1918 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1923 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1926 GET_CURRENT_CONTEXT(ctx
);
1929 case GL_FRAGMENT_PROGRAM_ARB
:
1930 if (!ctx
->Extensions
.ARB_fragment_program
) {
1931 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1934 ctx
->FragmentProgram
.Callback
= callback
;
1935 ctx
->FragmentProgram
.CallbackData
= data
;
1937 case GL_FRAGMENT_PROGRAM_NV
:
1938 if (!ctx
->Extensions
.NV_fragment_program
) {
1939 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1942 ctx
->FragmentProgram
.Callback
= callback
;
1943 ctx
->FragmentProgram
.CallbackData
= data
;
1945 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1946 if (!ctx
->Extensions
.ARB_vertex_program
&&
1947 !ctx
->Extensions
.NV_vertex_program
) {
1948 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1951 ctx
->VertexProgram
.Callback
= callback
;
1952 ctx
->VertexProgram
.CallbackData
= data
;
1955 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
1962 GLAPI
void GLAPIENTRY
1963 glGetProgramRegisterfvMESA(GLenum target
,
1964 GLsizei len
, const GLubyte
*registerName
,
1967 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
1972 _mesa_GetProgramRegisterfvMESA(GLenum target
,
1973 GLsizei len
, const GLubyte
*registerName
,
1977 GET_CURRENT_CONTEXT(ctx
);
1979 /* We _should_ be inside glBegin/glEnd */
1981 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
1982 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
1987 /* make null-terminated copy of registerName */
1988 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
1989 _mesa_memcpy(reg
, registerName
, len
);
1993 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
1994 if (!ctx
->Extensions
.ARB_vertex_program
&&
1995 !ctx
->Extensions
.NV_vertex_program
) {
1996 _mesa_error(ctx
, GL_INVALID_ENUM
,
1997 "glGetProgramRegisterfvMESA(target)");
2000 if (!ctx
->VertexProgram
._Enabled
) {
2001 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2002 "glGetProgramRegisterfvMESA");
2005 /* GL_NV_vertex_program */
2006 if (reg
[0] == 'R') {
2008 GLint i
= _mesa_atoi(reg
+ 1);
2009 if (i
>= (GLint
)ctx
->Const
.VertexProgram
.MaxTemps
) {
2010 _mesa_error(ctx
, GL_INVALID_VALUE
,
2011 "glGetProgramRegisterfvMESA(registerName)");
2014 COPY_4V(v
, ctx
->VertexProgram
.Temporaries
[i
]);
2016 else if (reg
[0] == 'v' && reg
[1] == '[') {
2017 /* Vertex Input attribute */
2019 for (i
= 0; i
< ctx
->Const
.VertexProgram
.MaxAttribs
; i
++) {
2020 const char *name
= _mesa_nv_vertex_input_register_name(i
);
2022 _mesa_sprintf(number
, "%d", i
);
2023 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
2024 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
2025 COPY_4V(v
, ctx
->VertexProgram
.Inputs
[i
]);
2029 _mesa_error(ctx
, GL_INVALID_VALUE
,
2030 "glGetProgramRegisterfvMESA(registerName)");
2033 else if (reg
[0] == 'o' && reg
[1] == '[') {
2034 /* Vertex output attribute */
2036 /* GL_ARB_vertex_program */
2037 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
2041 _mesa_error(ctx
, GL_INVALID_VALUE
,
2042 "glGetProgramRegisterfvMESA(registerName)");
2046 case GL_FRAGMENT_PROGRAM_ARB
:
2047 if (!ctx
->Extensions
.ARB_fragment_program
) {
2048 _mesa_error(ctx
, GL_INVALID_ENUM
,
2049 "glGetProgramRegisterfvMESA(target)");
2052 if (!ctx
->FragmentProgram
._Enabled
) {
2053 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2054 "glGetProgramRegisterfvMESA");
2059 case GL_FRAGMENT_PROGRAM_NV
:
2060 if (!ctx
->Extensions
.NV_fragment_program
) {
2061 _mesa_error(ctx
, GL_INVALID_ENUM
,
2062 "glGetProgramRegisterfvMESA(target)");
2065 if (!ctx
->FragmentProgram
._Enabled
) {
2066 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2067 "glGetProgramRegisterfvMESA");
2070 if (reg
[0] == 'R') {
2072 GLint i
= _mesa_atoi(reg
+ 1);
2073 if (i
>= (GLint
)ctx
->Const
.FragmentProgram
.MaxTemps
) {
2074 _mesa_error(ctx
, GL_INVALID_VALUE
,
2075 "glGetProgramRegisterfvMESA(registerName)");
2078 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Temporaries
[i
]);
2080 else if (reg
[0] == 'f' && reg
[1] == '[') {
2081 /* Fragment input attribute */
2083 for (i
= 0; i
< ctx
->Const
.FragmentProgram
.MaxAttribs
; i
++) {
2084 const char *name
= _mesa_nv_fragment_input_register_name(i
);
2085 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
2086 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Inputs
[i
]);
2090 _mesa_error(ctx
, GL_INVALID_VALUE
,
2091 "glGetProgramRegisterfvMESA(registerName)");
2094 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
2095 /* Fragment output color */
2096 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_COLR
]);
2098 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
2099 /* Fragment output color */
2100 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_COLH
]);
2102 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
2103 /* Fragment output depth */
2104 COPY_4V(v
, ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_RESULT_DEPR
]);
2107 /* try user-defined identifiers */
2108 const GLfloat
*value
= _mesa_lookup_parameter_value(
2109 ctx
->FragmentProgram
.Current
->Base
.Parameters
, -1, reg
);
2114 _mesa_error(ctx
, GL_INVALID_VALUE
,
2115 "glGetProgramRegisterfvMESA(registerName)");
2121 _mesa_error(ctx
, GL_INVALID_ENUM
,
2122 "glGetProgramRegisterfvMESA(target)");