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 gl_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 gl_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 gl_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 gl_program
*
201 _mesa_init_program_struct( GLcontext
*ctx
, struct gl_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 gl_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 gl_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(gl_vertex_program
),
263 case GL_FRAGMENT_PROGRAM_NV
:
264 case GL_FRAGMENT_PROGRAM_ARB
:
265 return _mesa_init_fragment_program(ctx
,
266 CALLOC_STRUCT(gl_fragment_program
),
269 _mesa_problem(ctx
, "bad target in _mesa_new_program");
276 * Delete a program and remove it from the hash table, ignoring the
278 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
279 * by a device driver function.
282 _mesa_delete_program(GLcontext
*ctx
, struct gl_program
*prog
)
288 _mesa_free(prog
->String
);
290 if (prog
->Instructions
) {
292 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
293 if (prog
->Instructions
[i
].Data
)
294 _mesa_free(prog
->Instructions
[i
].Data
);
296 _mesa_free(prog
->Instructions
);
299 if (prog
->Parameters
) {
300 _mesa_free_parameter_list(prog
->Parameters
);
303 /* XXX this is a little ugly */
304 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
305 struct gl_vertex_program
*vprog
= (struct gl_vertex_program
*) prog
;
307 _mesa_free(vprog
->TnlData
);
315 * Return the gl_program object for a given ID.
316 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
320 _mesa_lookup_program(GLcontext
*ctx
, GLuint id
)
323 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
329 /**********************************************************************/
330 /* Program parameter functions */
331 /**********************************************************************/
333 struct gl_program_parameter_list
*
334 _mesa_new_parameter_list(void)
336 return (struct gl_program_parameter_list
*)
337 _mesa_calloc(sizeof(struct gl_program_parameter_list
));
342 * Free a parameter list and all its parameters
345 _mesa_free_parameter_list(struct gl_program_parameter_list
*paramList
)
348 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
349 if (paramList
->Parameters
[i
].Name
)
350 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
352 _mesa_free(paramList
->Parameters
);
353 if (paramList
->ParameterValues
)
354 _mesa_align_free(paramList
->ParameterValues
);
355 _mesa_free(paramList
);
360 * Add a new parameter to a parameter list.
361 * \param paramList the list to add the parameter to
362 * \param name the parameter name, will be duplicated/copied!
363 * \param values initial parameter value, 4 GLfloats
364 * \param type type of parameter, such as
365 * \return index of new parameter in the list, or -1 if error (out of mem)
368 add_parameter(struct gl_program_parameter_list
*paramList
,
369 const char *name
, const GLfloat values
[4],
370 enum register_file type
)
372 const GLuint n
= paramList
->NumParameters
;
374 if (n
== paramList
->Size
) {
375 /* Need to grow the parameter list array */
376 if (paramList
->Size
== 0)
379 paramList
->Size
*= 2;
382 paramList
->Parameters
= (struct gl_program_parameter
*)
383 _mesa_realloc(paramList
->Parameters
,
384 n
* sizeof(struct gl_program_parameter
),
385 paramList
->Size
* sizeof(struct gl_program_parameter
));
387 paramList
->ParameterValues
= (GLfloat (*)[4])
388 _mesa_align_realloc(paramList
->ParameterValues
, /* old buf */
389 n
* 4 * sizeof(GLfloat
), /* old size */
390 paramList
->Size
* 4 *sizeof(GLfloat
), /* new sz */
394 if (!paramList
->Parameters
||
395 !paramList
->ParameterValues
) {
397 paramList
->NumParameters
= 0;
402 paramList
->NumParameters
= n
+ 1;
404 _mesa_memset(¶mList
->Parameters
[n
], 0,
405 sizeof(struct gl_program_parameter
));
407 paramList
->Parameters
[n
].Name
= name
? _mesa_strdup(name
) : NULL
;
408 paramList
->Parameters
[n
].Type
= type
;
410 COPY_4V(paramList
->ParameterValues
[n
], values
);
417 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
418 * \return index of the new entry in the parameter list
421 _mesa_add_named_parameter(struct gl_program_parameter_list
*paramList
,
422 const char *name
, const GLfloat values
[4])
424 return add_parameter(paramList
, name
, values
, PROGRAM_NAMED_PARAM
);
429 * Add a new named constant to the parameter list.
430 * This will be used when the program contains something like this:
431 * PARAM myVals = { 0, 1, 2, 3 };
433 * \param paramList - the parameter list
434 * \param values - four float values
435 * \return index of the new parameter.
438 _mesa_add_named_constant(struct gl_program_parameter_list
*paramList
,
439 const char *name
, const GLfloat values
[4])
441 return add_parameter(paramList
, name
, values
, PROGRAM_CONSTANT
);
446 * Add a new unnamed constant to the parameter list.
447 * This will be used when the program contains something like this:
448 * MOV r, { 0, 1, 2, 3 };
450 * \param paramList - the parameter list
451 * \param values - four float values
452 * \return index of the new parameter.
455 _mesa_add_unnamed_constant(struct gl_program_parameter_list
*paramList
,
456 const GLfloat values
[4])
458 return add_parameter(paramList
, NULL
, values
, PROGRAM_CONSTANT
);
463 * Add a new state reference to the parameter list.
464 * This will be used when the program contains something like this:
465 * PARAM ambient = state.material.front.ambient;
467 * \param paramList - the parameter list
468 * \param state - an array of 6 state tokens
469 * \return index of the new parameter.
472 _mesa_add_state_reference(struct gl_program_parameter_list
*paramList
,
473 const GLint
*stateTokens
)
475 /* XXX we should probably search the current parameter list to see if
476 * the new state reference is already present.
479 const char *name
= make_state_string(stateTokens
);
481 index
= add_parameter(paramList
, name
, NULL
, PROGRAM_STATE_VAR
);
484 for (i
= 0; i
< 6; i
++) {
485 paramList
->Parameters
[index
].StateIndexes
[i
]
486 = (enum state_index
) stateTokens
[i
];
488 paramList
->StateFlags
|= make_state_flags(stateTokens
);
491 /* free name string here since we duplicated it in add_parameter() */
492 _mesa_free((void *) name
);
499 * Lookup a parameter value by name in the given parameter list.
500 * \return pointer to the float[4] values.
503 _mesa_lookup_parameter_value(struct gl_program_parameter_list
*paramList
,
504 GLsizei nameLen
, const char *name
)
512 /* name is null-terminated */
513 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
514 if (paramList
->Parameters
[i
].Name
&&
515 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
516 return paramList
->ParameterValues
[i
];
520 /* name is not null-terminated, use nameLen */
521 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
522 if (paramList
->Parameters
[i
].Name
&&
523 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
524 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
525 return paramList
->ParameterValues
[i
];
533 * Lookup a parameter index by name in the given parameter list.
534 * \return index of parameter in the list.
537 _mesa_lookup_parameter_index(struct gl_program_parameter_list
*paramList
,
538 GLsizei nameLen
, const char *name
)
546 /* name is null-terminated */
547 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
548 if (paramList
->Parameters
[i
].Name
&&
549 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
554 /* name is not null-terminated, use nameLen */
555 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
556 if (paramList
->Parameters
[i
].Name
&&
557 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
558 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
567 * Use the list of tokens in the state[] array to find global GL state
568 * and return it in <value>. Usually, four values are returned in <value>
569 * but matrix queries may return as many as 16 values.
570 * This function is used for ARB vertex/fragment programs.
571 * The program parser will produce the state[] values.
574 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
580 /* state[1] is either 0=front or 1=back side */
581 const GLuint face
= (GLuint
) state
[1];
582 const struct gl_material
*mat
= &ctx
->Light
.Material
;
583 ASSERT(face
== 0 || face
== 1);
584 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
585 ASSERT(MAT_ATTRIB_FRONT_AMBIENT
+ 1 == MAT_ATTRIB_BACK_AMBIENT
);
586 /* XXX we could get rid of this switch entirely with a little
587 * work in arbprogparse.c's parse_state_single_item().
589 /* state[2] is the material attribute */
592 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+ face
]);
595 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+ face
]);
598 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+ face
]);
601 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
+ face
]);
603 case STATE_SHININESS
:
604 value
[0] = mat
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ face
][0];
610 _mesa_problem(ctx
, "Invalid material state in fetch_state");
616 /* state[1] is the light number */
617 const GLuint ln
= (GLuint
) state
[1];
618 /* state[2] is the light attribute */
621 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
624 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
627 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
630 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
632 case STATE_ATTENUATION
:
633 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
634 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
635 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
636 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
638 case STATE_SPOT_DIRECTION
:
639 COPY_3V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
640 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
644 GLfloat eye_z
[] = {0, 0, 1};
646 /* Compute infinite half angle vector:
647 * half-vector = light_position + (0, 0, 1)
648 * and then normalize. w = 0
650 * light.EyePosition.w should be 0 for infinite lights.
652 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
653 NORMALIZE_3FV(value
);
657 case STATE_POSITION_NORMALIZED
:
658 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
659 NORMALIZE_3FV( value
);
662 _mesa_problem(ctx
, "Invalid light state in fetch_state");
666 case STATE_LIGHTMODEL_AMBIENT
:
667 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
669 case STATE_LIGHTMODEL_SCENECOLOR
:
673 for (i
= 0; i
< 3; i
++) {
674 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
675 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
676 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
678 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
683 for (i
= 0; i
< 3; i
++) {
684 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
685 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
686 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
688 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
691 case STATE_LIGHTPROD
:
693 const GLuint ln
= (GLuint
) state
[1];
694 const GLuint face
= (GLuint
) state
[2];
696 ASSERT(face
== 0 || face
== 1);
699 for (i
= 0; i
< 3; i
++) {
700 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
701 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
703 /* [3] = material alpha */
704 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
707 for (i
= 0; i
< 3; i
++) {
708 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
709 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
711 /* [3] = material alpha */
712 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
715 for (i
= 0; i
< 3; i
++) {
716 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
717 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
719 /* [3] = material alpha */
720 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
723 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
729 /* state[1] is the texture unit */
730 const GLuint unit
= (GLuint
) state
[1];
731 /* state[2] is the texgen attribute */
733 case STATE_TEXGEN_EYE_S
:
734 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
736 case STATE_TEXGEN_EYE_T
:
737 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
739 case STATE_TEXGEN_EYE_R
:
740 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
742 case STATE_TEXGEN_EYE_Q
:
743 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
745 case STATE_TEXGEN_OBJECT_S
:
746 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
748 case STATE_TEXGEN_OBJECT_T
:
749 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
751 case STATE_TEXGEN_OBJECT_R
:
752 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
754 case STATE_TEXGEN_OBJECT_Q
:
755 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
758 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
762 case STATE_TEXENV_COLOR
:
764 /* state[1] is the texture unit */
765 const GLuint unit
= (GLuint
) state
[1];
766 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
769 case STATE_FOG_COLOR
:
770 COPY_4V(value
, ctx
->Fog
.Color
);
772 case STATE_FOG_PARAMS
:
773 value
[0] = ctx
->Fog
.Density
;
774 value
[1] = ctx
->Fog
.Start
;
775 value
[2] = ctx
->Fog
.End
;
776 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
778 case STATE_CLIPPLANE
:
780 const GLuint plane
= (GLuint
) state
[1];
781 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
784 case STATE_POINT_SIZE
:
785 value
[0] = ctx
->Point
.Size
;
786 value
[1] = ctx
->Point
.MinSize
;
787 value
[2] = ctx
->Point
.MaxSize
;
788 value
[3] = ctx
->Point
.Threshold
;
790 case STATE_POINT_ATTENUATION
:
791 value
[0] = ctx
->Point
.Params
[0];
792 value
[1] = ctx
->Point
.Params
[1];
793 value
[2] = ctx
->Point
.Params
[2];
798 /* state[1] = modelview, projection, texture, etc. */
799 /* state[2] = which texture matrix or program matrix */
800 /* state[3] = first column to fetch */
801 /* state[4] = last column to fetch */
802 /* state[5] = transpose, inverse or invtrans */
804 const GLmatrix
*matrix
;
805 const enum state_index mat
= state
[1];
806 const GLuint index
= (GLuint
) state
[2];
807 const GLuint first
= (GLuint
) state
[3];
808 const GLuint last
= (GLuint
) state
[4];
809 const enum state_index modifier
= state
[5];
812 if (mat
== STATE_MODELVIEW
) {
813 matrix
= ctx
->ModelviewMatrixStack
.Top
;
815 else if (mat
== STATE_PROJECTION
) {
816 matrix
= ctx
->ProjectionMatrixStack
.Top
;
818 else if (mat
== STATE_MVP
) {
819 matrix
= &ctx
->_ModelProjectMatrix
;
821 else if (mat
== STATE_TEXTURE
) {
822 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
824 else if (mat
== STATE_PROGRAM
) {
825 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
828 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
831 if (modifier
== STATE_MATRIX_INVERSE
||
832 modifier
== STATE_MATRIX_INVTRANS
) {
833 /* Be sure inverse is up to date:
835 _math_matrix_alloc_inv( (GLmatrix
*) matrix
);
836 _math_matrix_analyse( (GLmatrix
*) matrix
);
842 if (modifier
== STATE_MATRIX_TRANSPOSE
||
843 modifier
== STATE_MATRIX_INVTRANS
) {
844 for (i
= 0, row
= first
; row
<= last
; row
++) {
845 value
[i
++] = m
[row
* 4 + 0];
846 value
[i
++] = m
[row
* 4 + 1];
847 value
[i
++] = m
[row
* 4 + 2];
848 value
[i
++] = m
[row
* 4 + 3];
852 for (i
= 0, row
= first
; row
<= last
; row
++) {
853 value
[i
++] = m
[row
+ 0];
854 value
[i
++] = m
[row
+ 4];
855 value
[i
++] = m
[row
+ 8];
856 value
[i
++] = m
[row
+ 12];
861 case STATE_DEPTH_RANGE
:
862 value
[0] = ctx
->Viewport
.Near
; /* near */
863 value
[1] = ctx
->Viewport
.Far
; /* far */
864 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
867 case STATE_FRAGMENT_PROGRAM
:
869 /* state[1] = {STATE_ENV, STATE_LOCAL} */
870 /* state[2] = parameter index */
871 const int idx
= (int) state
[2];
874 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
877 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
880 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
886 case STATE_VERTEX_PROGRAM
:
888 /* state[1] = {STATE_ENV, STATE_LOCAL} */
889 /* state[2] = parameter index */
890 const int idx
= (int) state
[2];
893 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
896 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
899 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
908 case STATE_NORMAL_SCALE
:
909 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
911 case STATE_TEXRECT_SCALE
: {
912 const int unit
= (int) state
[2];
913 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
915 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
916 ASSIGN_4V(value
, 1.0 / texImage
->Width
, 1.0 / texImage
->Height
, 0, 1);
921 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
928 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
935 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
936 * indicate that the given context state may have changed.
937 * The bitmask is used during validation to determine if we need to update
938 * vertex/fragment program parameters (like "state.material.color") when
939 * some GL state has changed.
942 make_state_flags(const GLint state
[])
947 case STATE_LIGHTMODEL_AMBIENT
:
948 case STATE_LIGHTMODEL_SCENECOLOR
:
949 case STATE_LIGHTPROD
:
953 case STATE_TEXENV_COLOR
:
956 case STATE_FOG_COLOR
:
957 case STATE_FOG_PARAMS
:
960 case STATE_CLIPPLANE
:
961 return _NEW_TRANSFORM
;
963 case STATE_POINT_SIZE
:
964 case STATE_POINT_ATTENUATION
:
969 case STATE_MODELVIEW
:
970 return _NEW_MODELVIEW
;
971 case STATE_PROJECTION
:
972 return _NEW_PROJECTION
;
974 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
976 return _NEW_TEXTURE_MATRIX
;
978 return _NEW_TRACK_MATRIX
;
980 _mesa_problem(NULL
, "unexpected matrix in make_state_flags()");
984 case STATE_DEPTH_RANGE
:
985 return _NEW_VIEWPORT
;
987 case STATE_FRAGMENT_PROGRAM
:
988 case STATE_VERTEX_PROGRAM
:
993 case STATE_NORMAL_SCALE
:
994 return _NEW_MODELVIEW
;
995 case STATE_TEXRECT_SCALE
:
998 _mesa_problem(NULL
, "unexpected int. state in make_state_flags()");
1003 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
1010 append(char *dst
, const char *src
)
1021 append_token(char *dst
, enum state_index k
)
1024 case STATE_MATERIAL
:
1025 append(dst
, "material.");
1028 append(dst
, "light");
1030 case STATE_LIGHTMODEL_AMBIENT
:
1031 append(dst
, "lightmodel.ambient");
1033 case STATE_LIGHTMODEL_SCENECOLOR
:
1035 case STATE_LIGHTPROD
:
1036 append(dst
, "lightprod");
1039 append(dst
, "texgen");
1041 case STATE_FOG_COLOR
:
1042 append(dst
, "fog.color");
1044 case STATE_FOG_PARAMS
:
1045 append(dst
, "fog.params");
1047 case STATE_CLIPPLANE
:
1048 append(dst
, "clip");
1050 case STATE_POINT_SIZE
:
1051 append(dst
, "point.size");
1053 case STATE_POINT_ATTENUATION
:
1054 append(dst
, "point.attenuation");
1057 append(dst
, "matrix.");
1059 case STATE_MODELVIEW
:
1060 append(dst
, "modelview");
1062 case STATE_PROJECTION
:
1063 append(dst
, "projection");
1069 append(dst
, "texture");
1072 append(dst
, "program");
1074 case STATE_MATRIX_INVERSE
:
1075 append(dst
, ".inverse");
1077 case STATE_MATRIX_TRANSPOSE
:
1078 append(dst
, ".transpose");
1080 case STATE_MATRIX_INVTRANS
:
1081 append(dst
, ".invtrans");
1084 append(dst
, "ambient");
1087 append(dst
, "diffuse");
1089 case STATE_SPECULAR
:
1090 append(dst
, "specular");
1092 case STATE_EMISSION
:
1093 append(dst
, "emission");
1095 case STATE_SHININESS
:
1096 append(dst
, "shininess");
1099 append(dst
, "half");
1101 case STATE_POSITION
:
1102 append(dst
, ".position");
1104 case STATE_ATTENUATION
:
1105 append(dst
, ".attenuation");
1107 case STATE_SPOT_DIRECTION
:
1108 append(dst
, ".spot.direction");
1110 case STATE_TEXGEN_EYE_S
:
1111 append(dst
, "eye.s");
1113 case STATE_TEXGEN_EYE_T
:
1114 append(dst
, "eye.t");
1116 case STATE_TEXGEN_EYE_R
:
1117 append(dst
, "eye.r");
1119 case STATE_TEXGEN_EYE_Q
:
1120 append(dst
, "eye.q");
1122 case STATE_TEXGEN_OBJECT_S
:
1123 append(dst
, "object.s");
1125 case STATE_TEXGEN_OBJECT_T
:
1126 append(dst
, "object.t");
1128 case STATE_TEXGEN_OBJECT_R
:
1129 append(dst
, "object.r");
1131 case STATE_TEXGEN_OBJECT_Q
:
1132 append(dst
, "object.q");
1134 case STATE_TEXENV_COLOR
:
1135 append(dst
, "texenv");
1137 case STATE_DEPTH_RANGE
:
1138 append(dst
, "depth.range");
1140 case STATE_VERTEX_PROGRAM
:
1141 case STATE_FRAGMENT_PROGRAM
:
1147 append(dst
, "local");
1149 case STATE_INTERNAL
:
1150 case STATE_NORMAL_SCALE
:
1151 case STATE_POSITION_NORMALIZED
:
1152 append(dst
, "(internal)");
1160 append_face(char *dst
, GLint face
)
1163 append(dst
, "front.");
1165 append(dst
, "back.");
1169 append_index(char *dst
, GLint index
)
1172 _mesa_sprintf(s
, "[%d].", index
);
1177 * Make a string from the given state vector.
1178 * For example, return "state.matrix.texture[2].inverse".
1179 * Use _mesa_free() to deallocate the string.
1182 make_state_string(const GLint state
[6])
1184 char str
[1000] = "";
1187 append(str
, "state.");
1188 append_token(str
, (enum state_index
) state
[0]);
1191 case STATE_MATERIAL
:
1192 append_face(str
, state
[1]);
1193 append_token(str
, (enum state_index
) state
[2]);
1196 append(str
, "light");
1197 append_index(str
, state
[1]); /* light number [i]. */
1198 append_token(str
, (enum state_index
) state
[2]); /* coefficients */
1200 case STATE_LIGHTMODEL_AMBIENT
:
1201 append(str
, "lightmodel.ambient");
1203 case STATE_LIGHTMODEL_SCENECOLOR
:
1204 if (state
[1] == 0) {
1205 append(str
, "lightmodel.front.scenecolor");
1208 append(str
, "lightmodel.back.scenecolor");
1211 case STATE_LIGHTPROD
:
1212 append_index(str
, state
[1]); /* light number [i]. */
1213 append_face(str
, state
[2]);
1214 append_token(str
, (enum state_index
) state
[3]);
1217 append_index(str
, state
[1]); /* tex unit [i] */
1218 append_token(str
, (enum state_index
) state
[2]); /* plane coef */
1220 case STATE_TEXENV_COLOR
:
1221 append_index(str
, state
[1]); /* tex unit [i] */
1222 append(str
, "color");
1224 case STATE_FOG_COLOR
:
1225 case STATE_FOG_PARAMS
:
1227 case STATE_CLIPPLANE
:
1228 append_index(str
, state
[1]); /* plane [i] */
1229 append(str
, "plane");
1231 case STATE_POINT_SIZE
:
1232 case STATE_POINT_ATTENUATION
:
1236 /* state[1] = modelview, projection, texture, etc. */
1237 /* state[2] = which texture matrix or program matrix */
1238 /* state[3] = first column to fetch */
1239 /* state[4] = last column to fetch */
1240 /* state[5] = transpose, inverse or invtrans */
1241 const enum state_index mat
= (enum state_index
) state
[1];
1242 const GLuint index
= (GLuint
) state
[2];
1243 const GLuint first
= (GLuint
) state
[3];
1244 const GLuint last
= (GLuint
) state
[4];
1245 const enum state_index modifier
= (enum state_index
) state
[5];
1246 append_token(str
, mat
);
1248 append_index(str
, index
);
1250 append_token(str
, modifier
);
1252 _mesa_sprintf(tmp
, ".row[%d]", first
);
1254 _mesa_sprintf(tmp
, ".row[%d..%d]", first
, last
);
1258 case STATE_DEPTH_RANGE
:
1260 case STATE_FRAGMENT_PROGRAM
:
1261 case STATE_VERTEX_PROGRAM
:
1262 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1263 /* state[2] = parameter index */
1264 append_token(str
, (enum state_index
) state
[1]);
1265 append_index(str
, state
[2]);
1267 case STATE_INTERNAL
:
1270 _mesa_problem(NULL
, "Invalid state in maka_state_string");
1274 return _mesa_strdup(str
);
1279 * Loop over all the parameters in a parameter list. If the parameter
1280 * is a GL state reference, look up the current value of that state
1281 * variable and put it into the parameter's Value[4] array.
1282 * This would be called at glBegin time when using a fragment program.
1285 _mesa_load_state_parameters(GLcontext
*ctx
,
1286 struct gl_program_parameter_list
*paramList
)
1293 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1294 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1295 _mesa_fetch_state(ctx
,
1296 paramList
->Parameters
[i
].StateIndexes
,
1297 paramList
->ParameterValues
[i
]);
1304 * Initialize program instruction fields to defaults.
1305 * \param inst first instruction to initialize
1306 * \param count number of instructions to initialize
1309 _mesa_init_instructions(struct prog_instruction
*inst
, GLuint count
)
1313 _mesa_bzero(inst
, count
* sizeof(struct prog_instruction
));
1315 for (i
= 0; i
< count
; i
++) {
1316 inst
[i
].SrcReg
[0].File
= PROGRAM_UNDEFINED
;
1317 inst
[i
].SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
1318 inst
[i
].SrcReg
[1].File
= PROGRAM_UNDEFINED
;
1319 inst
[i
].SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
1320 inst
[i
].SrcReg
[2].File
= PROGRAM_UNDEFINED
;
1321 inst
[i
].SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
1323 inst
[i
].DstReg
.File
= PROGRAM_UNDEFINED
;
1324 inst
[i
].DstReg
.WriteMask
= WRITEMASK_XYZW
;
1325 inst
[i
].DstReg
.CondMask
= COND_TR
;
1326 inst
[i
].DstReg
.CondSwizzle
= SWIZZLE_NOOP
;
1328 inst
[i
].SaturateMode
= SATURATE_OFF
;
1329 inst
[i
].Precision
= FLOAT32
;
1335 * Allocate an array of program instructions.
1336 * \param numInst number of instructions
1337 * \return pointer to instruction memory
1339 struct prog_instruction
*
1340 _mesa_alloc_instructions(GLuint numInst
)
1342 return (struct prog_instruction
*)
1343 _mesa_calloc(numInst
* sizeof(struct prog_instruction
));
1348 * Reallocate memory storing an array of program instructions.
1349 * This is used when we need to append additional instructions onto an
1351 * \param oldInst pointer to first of old/src instructions
1352 * \param numOldInst number of instructions at <oldInst>
1353 * \param numNewInst desired size of new instruction array.
1354 * \return pointer to start of new instruction array.
1356 struct prog_instruction
*
1357 _mesa_realloc_instructions(struct prog_instruction
*oldInst
,
1358 GLuint numOldInst
, GLuint numNewInst
)
1360 struct prog_instruction
*newInst
;
1362 newInst
= (struct prog_instruction
*)
1363 _mesa_realloc(oldInst
,
1364 numOldInst
* sizeof(struct prog_instruction
),
1365 numNewInst
* sizeof(struct prog_instruction
));
1372 * Basic info about each instruction
1374 struct instruction_info
1376 enum prog_opcode Opcode
;
1383 * \note Opcode should equal array index!
1385 static const struct instruction_info InstInfo
[MAX_OPCODE
] = {
1386 { OPCODE_ABS
, "ABS", 1 },
1387 { OPCODE_ADD
, "ADD", 2 },
1388 { OPCODE_ARA
, "ARA", 1 },
1389 { OPCODE_ARL
, "ARL", 1 },
1390 { OPCODE_ARL_NV
, "ARL", 1 },
1391 { OPCODE_ARR
, "ARL", 1 },
1392 { OPCODE_BRA
, "BRA", 1 },
1393 { OPCODE_CAL
, "CAL", 1 },
1394 { OPCODE_CMP
, "CMP", 3 },
1395 { OPCODE_COS
, "COS", 1 },
1396 { OPCODE_DDX
, "DDX", 1 },
1397 { OPCODE_DDY
, "DDY", 1 },
1398 { OPCODE_DP3
, "DP3", 2 },
1399 { OPCODE_DP4
, "DP4", 2 },
1400 { OPCODE_DPH
, "DPH", 2 },
1401 { OPCODE_DST
, "DST", 2 },
1402 { OPCODE_END
, "END", 0 },
1403 { OPCODE_EX2
, "EX2", 1 },
1404 { OPCODE_EXP
, "EXP", 1 },
1405 { OPCODE_FLR
, "FLR", 1 },
1406 { OPCODE_FRC
, "FRC", 1 },
1407 { OPCODE_KIL
, "KIL", 1 },
1408 { OPCODE_KIL_NV
, "KIL", 0 },
1409 { OPCODE_LG2
, "LG2", 1 },
1410 { OPCODE_LIT
, "LIT", 1 },
1411 { OPCODE_LOG
, "LOG", 1 },
1412 { OPCODE_LRP
, "LRP", 3 },
1413 { OPCODE_MAD
, "MAD", 3 },
1414 { OPCODE_MAX
, "MAX", 2 },
1415 { OPCODE_MIN
, "MIN", 2 },
1416 { OPCODE_MOV
, "MOV", 1 },
1417 { OPCODE_MUL
, "MUL", 2 },
1418 { OPCODE_PK2H
, "PK2H", 1 },
1419 { OPCODE_PK2US
, "PK2US", 1 },
1420 { OPCODE_PK4B
, "PK4B", 1 },
1421 { OPCODE_PK4UB
, "PK4UB", 1 },
1422 { OPCODE_POW
, "POW", 2 },
1423 { OPCODE_POPA
, "POPA", 0 },
1424 { OPCODE_PRINT
, "PRINT", 1 },
1425 { OPCODE_PUSHA
, "PUSHA", 0 },
1426 { OPCODE_RCC
, "RCC", 1 },
1427 { OPCODE_RCP
, "RCP", 1 },
1428 { OPCODE_RET
, "RET", 1 },
1429 { OPCODE_RFL
, "RFL", 1 },
1430 { OPCODE_RSQ
, "RSQ", 1 },
1431 { OPCODE_SCS
, "SCS", 1 },
1432 { OPCODE_SEQ
, "SEQ", 2 },
1433 { OPCODE_SFL
, "SFL", 0 },
1434 { OPCODE_SGE
, "SGE", 2 },
1435 { OPCODE_SGT
, "SGT", 2 },
1436 { OPCODE_SIN
, "SIN", 1 },
1437 { OPCODE_SLE
, "SLE", 2 },
1438 { OPCODE_SLT
, "SLT", 2 },
1439 { OPCODE_SNE
, "SNE", 2 },
1440 { OPCODE_SSG
, "SSG", 1 },
1441 { OPCODE_STR
, "STR", 0 },
1442 { OPCODE_SUB
, "SUB", 2 },
1443 { OPCODE_SWZ
, "SWZ", 1 },
1444 { OPCODE_TEX
, "TEX", 1 },
1445 { OPCODE_TXB
, "TXB", 1 },
1446 { OPCODE_TXD
, "TXD", 3 },
1447 { OPCODE_TXL
, "TXL", 1 },
1448 { OPCODE_TXP
, "TXP", 1 },
1449 { OPCODE_TXP_NV
, "TXP", 1 },
1450 { OPCODE_UP2H
, "UP2H", 1 },
1451 { OPCODE_UP2US
, "UP2US", 1 },
1452 { OPCODE_UP4B
, "UP4B", 1 },
1453 { OPCODE_UP4UB
, "UP4UB", 1 },
1454 { OPCODE_X2D
, "X2D", 3 },
1455 { OPCODE_XPD
, "XPD", 2 }
1460 * Return the number of src registers for the given instruction/opcode.
1463 _mesa_num_inst_src_regs(enum prog_opcode opcode
)
1465 ASSERT(opcode
== InstInfo
[opcode
].Opcode
);
1466 return InstInfo
[opcode
].NumSrcRegs
;
1471 * Return string name for given program opcode.
1474 _mesa_opcode_string(enum prog_opcode opcode
)
1476 ASSERT(opcode
< MAX_OPCODE
);
1477 return InstInfo
[opcode
].Name
;
1481 * Return string name for given program/register file.
1484 program_file_string(enum register_file f
)
1487 case PROGRAM_TEMPORARY
:
1489 case PROGRAM_LOCAL_PARAM
:
1491 case PROGRAM_ENV_PARAM
:
1493 case PROGRAM_STATE_VAR
:
1497 case PROGRAM_OUTPUT
:
1499 case PROGRAM_NAMED_PARAM
:
1501 case PROGRAM_CONSTANT
:
1503 case PROGRAM_WRITE_ONLY
:
1504 return "WRITE_ONLY";
1505 case PROGRAM_ADDRESS
:
1514 * Return a string representation of the given swizzle word.
1515 * If extended is true, use extended (comma-separated) format.
1518 swizzle_string(GLuint swizzle
, GLuint negateBase
, GLboolean extended
)
1520 static const char swz
[] = "xyzw01";
1524 if (!extended
&& swizzle
== SWIZZLE_NOOP
&& negateBase
== 0)
1525 return ""; /* no swizzle/negation */
1530 if (negateBase
& 0x1)
1532 s
[i
++] = swz
[GET_SWZ(swizzle
, 0)];
1538 if (negateBase
& 0x2)
1540 s
[i
++] = swz
[GET_SWZ(swizzle
, 1)];
1546 if (negateBase
& 0x4)
1548 s
[i
++] = swz
[GET_SWZ(swizzle
, 2)];
1554 if (negateBase
& 0x8)
1556 s
[i
++] = swz
[GET_SWZ(swizzle
, 3)];
1564 writemask_string(GLuint writeMask
)
1569 if (writeMask
== WRITEMASK_XYZW
)
1573 if (writeMask
& WRITEMASK_X
)
1575 if (writeMask
& WRITEMASK_Y
)
1577 if (writeMask
& WRITEMASK_Z
)
1579 if (writeMask
& WRITEMASK_W
)
1587 print_dst_reg(const struct prog_dst_register
*dstReg
)
1589 _mesa_printf(" %s[%d]%s",
1590 program_file_string((enum register_file
) dstReg
->File
),
1592 writemask_string(dstReg
->WriteMask
));
1596 print_src_reg(const struct prog_src_register
*srcReg
)
1598 _mesa_printf("%s[%d]%s",
1599 program_file_string((enum register_file
) srcReg
->File
),
1601 swizzle_string(srcReg
->Swizzle
,
1602 srcReg
->NegateBase
, GL_FALSE
));
1606 _mesa_print_alu_instruction(const struct prog_instruction
*inst
,
1607 const char *opcode_string
,
1612 _mesa_printf("%s", opcode_string
);
1614 /* frag prog only */
1615 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1616 _mesa_printf("_SAT");
1618 if (inst
->DstReg
.File
!= PROGRAM_UNDEFINED
) {
1619 _mesa_printf(" %s[%d]%s",
1620 program_file_string((enum register_file
) inst
->DstReg
.File
),
1622 writemask_string(inst
->DstReg
.WriteMask
));
1628 for (j
= 0; j
< numRegs
; j
++) {
1629 print_src_reg(inst
->SrcReg
+ j
);
1630 if (j
+ 1 < numRegs
)
1634 _mesa_printf(";\n");
1639 * Print a single vertex/fragment program instruction.
1642 _mesa_print_instruction(const struct prog_instruction
*inst
)
1644 switch (inst
->Opcode
) {
1646 _mesa_printf("PRINT '%s'", inst
->Data
);
1647 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1649 _mesa_printf("%s[%d]%s",
1650 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1651 inst
->SrcReg
[0].Index
,
1652 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1653 inst
->SrcReg
[0].NegateBase
, GL_FALSE
));
1655 _mesa_printf(";\n");
1658 _mesa_printf("SWZ");
1659 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1660 _mesa_printf("_SAT");
1661 print_dst_reg(&inst
->DstReg
);
1662 _mesa_printf("%s[%d], %s;\n",
1663 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1664 inst
->SrcReg
[0].Index
,
1665 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1666 inst
->SrcReg
[0].NegateBase
, GL_TRUE
));
1671 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1672 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1673 _mesa_printf("_SAT");
1675 print_dst_reg(&inst
->DstReg
);
1677 print_src_reg(&inst
->SrcReg
[0]);
1678 _mesa_printf(", texture[%d], ", inst
->TexSrcUnit
);
1679 switch (inst
->TexSrcTarget
) {
1680 case TEXTURE_1D_INDEX
: _mesa_printf("1D"); break;
1681 case TEXTURE_2D_INDEX
: _mesa_printf("2D"); break;
1682 case TEXTURE_3D_INDEX
: _mesa_printf("3D"); break;
1683 case TEXTURE_CUBE_INDEX
: _mesa_printf("CUBE"); break;
1684 case TEXTURE_RECT_INDEX
: _mesa_printf("RECT"); break;
1691 _mesa_printf("ARL addr.x, ");
1692 print_src_reg(&inst
->SrcReg
[0]);
1693 _mesa_printf(";\n");
1696 _mesa_printf("END;\n");
1698 /* XXX may need for other special-case instructions */
1700 /* typical alu instruction */
1701 _mesa_print_alu_instruction(inst
,
1702 _mesa_opcode_string(inst
->Opcode
),
1703 _mesa_num_inst_src_regs(inst
->Opcode
));
1710 * Print a vertx/fragment program to stdout.
1711 * XXX this function could be greatly improved.
1714 _mesa_print_program(const struct gl_program
*prog
)
1717 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1718 _mesa_printf("%3d: ", i
);
1719 _mesa_print_instruction(prog
->Instructions
+ i
);
1725 * Print all of a program's parameters.
1728 _mesa_print_program_parameters(GLcontext
*ctx
, const struct gl_program
*prog
)
1732 _mesa_printf("NumInstructions=%d\n", prog
->NumInstructions
);
1733 _mesa_printf("NumTemporaries=%d\n", prog
->NumTemporaries
);
1734 _mesa_printf("NumParameters=%d\n", prog
->NumParameters
);
1735 _mesa_printf("NumAttributes=%d\n", prog
->NumAttributes
);
1736 _mesa_printf("NumAddressRegs=%d\n", prog
->NumAddressRegs
);
1738 _mesa_load_state_parameters(ctx
, prog
->Parameters
);
1741 _mesa_printf("Local Params:\n");
1742 for (i
= 0; i
< MAX_PROGRAM_LOCAL_PARAMS
; i
++){
1743 const GLfloat
*p
= prog
->LocalParams
[i
];
1744 _mesa_printf("%2d: %f, %f, %f, %f\n", i
, p
[0], p
[1], p
[2], p
[3]);
1748 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++){
1749 struct gl_program_parameter
*param
= prog
->Parameters
->Parameters
+ i
;
1750 const GLfloat
*v
= prog
->Parameters
->ParameterValues
[i
];
1751 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1752 i
, param
->Name
, v
[0], v
[1], v
[2], v
[3]);
1758 * Mixing ARB and NV vertex/fragment programs can be tricky.
1759 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
1760 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
1761 * The two different fragment program targets are supposed to be compatible
1762 * to some extent (see GL_ARB_fragment_program spec).
1763 * This function does the compatibility check.
1766 compatible_program_targets(GLenum t1
, GLenum t2
)
1770 if (t1
== GL_FRAGMENT_PROGRAM_ARB
&& t2
== GL_FRAGMENT_PROGRAM_NV
)
1772 if (t1
== GL_FRAGMENT_PROGRAM_NV
&& t2
== GL_FRAGMENT_PROGRAM_ARB
)
1779 /**********************************************************************/
1781 /**********************************************************************/
1785 * Bind a program (make it current)
1786 * \note Called from the GL API dispatcher by both glBindProgramNV
1787 * and glBindProgramARB.
1790 _mesa_BindProgram(GLenum target
, GLuint id
)
1792 struct gl_program
*curProg
, *newProg
;
1793 GET_CURRENT_CONTEXT(ctx
);
1794 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1796 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1798 /* Error-check target and get curProg */
1799 if ((target
== GL_VERTEX_PROGRAM_ARB
) && /* == GL_VERTEX_PROGRAM_NV */
1800 (ctx
->Extensions
.NV_vertex_program
||
1801 ctx
->Extensions
.ARB_vertex_program
)) {
1802 curProg
= &ctx
->VertexProgram
.Current
->Base
;
1804 else if ((target
== GL_FRAGMENT_PROGRAM_NV
1805 && ctx
->Extensions
.NV_fragment_program
) ||
1806 (target
== GL_FRAGMENT_PROGRAM_ARB
1807 && ctx
->Extensions
.ARB_fragment_program
)) {
1808 curProg
= &ctx
->FragmentProgram
.Current
->Base
;
1811 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
1816 * Get pointer to new program to bind.
1817 * NOTE: binding to a non-existant program is not an error.
1818 * That's supposed to be caught in glBegin.
1821 /* Bind a default program */
1823 if (target
== GL_VERTEX_PROGRAM_ARB
) /* == GL_VERTEX_PROGRAM_NV */
1824 newProg
= ctx
->Shared
->DefaultVertexProgram
;
1826 newProg
= ctx
->Shared
->DefaultFragmentProgram
;
1829 /* Bind a user program */
1830 newProg
= _mesa_lookup_program(ctx
, id
);
1831 if (!newProg
|| newProg
== &_mesa_DummyProgram
) {
1832 /* allocate a new program now */
1833 newProg
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
1835 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
1838 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, newProg
);
1840 else if (!compatible_program_targets(newProg
->Target
, target
)) {
1841 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1842 "glBindProgramNV/ARB(target mismatch)");
1847 /** All error checking is complete now **/
1849 if (curProg
->Id
== id
) {
1850 /* binding same program - no change */
1854 /* unbind/delete oldProg */
1855 if (curProg
->Id
!= 0) {
1856 /* decrement refcount on previously bound fragment program */
1857 curProg
->RefCount
--;
1858 /* and delete if refcount goes below one */
1859 if (curProg
->RefCount
<= 0) {
1860 /* the program ID was already removed from the hash table */
1861 ctx
->Driver
.DeleteProgram(ctx
, curProg
);
1866 if (target
== GL_VERTEX_PROGRAM_ARB
) { /* == GL_VERTEX_PROGRAM_NV */
1867 ctx
->VertexProgram
.Current
= (struct gl_vertex_program
*) newProg
;
1869 else if (target
== GL_FRAGMENT_PROGRAM_NV
||
1870 target
== GL_FRAGMENT_PROGRAM_ARB
) {
1871 ctx
->FragmentProgram
.Current
= (struct gl_fragment_program
*) newProg
;
1873 newProg
->RefCount
++;
1875 /* Never null pointers */
1876 ASSERT(ctx
->VertexProgram
.Current
);
1877 ASSERT(ctx
->FragmentProgram
.Current
);
1879 if (ctx
->Driver
.BindProgram
)
1880 ctx
->Driver
.BindProgram(ctx
, target
, newProg
);
1885 * Delete a list of programs.
1886 * \note Not compiled into display lists.
1887 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1890 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
1893 GET_CURRENT_CONTEXT(ctx
);
1894 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1897 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
1901 for (i
= 0; i
< n
; i
++) {
1903 struct gl_program
*prog
= _mesa_lookup_program(ctx
, ids
[i
]);
1904 if (prog
== &_mesa_DummyProgram
) {
1905 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1908 /* Unbind program if necessary */
1909 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| /* == GL_VERTEX_PROGRAM_NV */
1910 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
1911 if (ctx
->VertexProgram
.Current
&&
1912 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
1913 /* unbind this currently bound program */
1914 _mesa_BindProgram(prog
->Target
, 0);
1917 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
1918 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1919 if (ctx
->FragmentProgram
.Current
&&
1920 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
1921 /* unbind this currently bound program */
1922 _mesa_BindProgram(prog
->Target
, 0);
1926 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
1929 /* The ID is immediately available for re-use now */
1930 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1932 if (prog
->RefCount
<= 0) {
1933 ctx
->Driver
.DeleteProgram(ctx
, prog
);
1942 * Generate a list of new program identifiers.
1943 * \note Not compiled into display lists.
1944 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1947 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1951 GET_CURRENT_CONTEXT(ctx
);
1952 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1955 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1962 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1964 /* Insert pointer to dummy program as placeholder */
1965 for (i
= 0; i
< (GLuint
) n
; i
++) {
1966 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, &_mesa_DummyProgram
);
1969 /* Return the program names */
1970 for (i
= 0; i
< (GLuint
) n
; i
++) {
1976 /**********************************************************************/
1977 /* GL_MESA_program_debug extension */
1978 /**********************************************************************/
1982 GLAPI
void GLAPIENTRY
1983 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1986 _mesa_ProgramCallbackMESA(target
, callback
, data
);
1991 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
1994 GET_CURRENT_CONTEXT(ctx
);
1997 case GL_FRAGMENT_PROGRAM_ARB
:
1998 if (!ctx
->Extensions
.ARB_fragment_program
) {
1999 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2002 ctx
->FragmentProgram
.Callback
= callback
;
2003 ctx
->FragmentProgram
.CallbackData
= data
;
2005 case GL_FRAGMENT_PROGRAM_NV
:
2006 if (!ctx
->Extensions
.NV_fragment_program
) {
2007 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2010 ctx
->FragmentProgram
.Callback
= callback
;
2011 ctx
->FragmentProgram
.CallbackData
= data
;
2013 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
2014 if (!ctx
->Extensions
.ARB_vertex_program
&&
2015 !ctx
->Extensions
.NV_vertex_program
) {
2016 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2019 ctx
->VertexProgram
.Callback
= callback
;
2020 ctx
->VertexProgram
.CallbackData
= data
;
2023 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2030 GLAPI
void GLAPIENTRY
2031 glGetProgramRegisterfvMESA(GLenum target
,
2032 GLsizei len
, const GLubyte
*registerName
,
2035 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
2040 _mesa_GetProgramRegisterfvMESA(GLenum target
,
2041 GLsizei len
, const GLubyte
*registerName
,
2045 GET_CURRENT_CONTEXT(ctx
);
2047 /* We _should_ be inside glBegin/glEnd */
2049 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
2050 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
2055 /* make null-terminated copy of registerName */
2056 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
2057 _mesa_memcpy(reg
, registerName
, len
);
2061 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
2062 if (!ctx
->Extensions
.ARB_vertex_program
&&
2063 !ctx
->Extensions
.NV_vertex_program
) {
2064 _mesa_error(ctx
, GL_INVALID_ENUM
,
2065 "glGetProgramRegisterfvMESA(target)");
2068 if (!ctx
->VertexProgram
._Enabled
) {
2069 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2070 "glGetProgramRegisterfvMESA");
2073 /* GL_NV_vertex_program */
2074 if (reg
[0] == 'R') {
2076 GLint i
= _mesa_atoi(reg
+ 1);
2077 if (i
>= (GLint
)ctx
->Const
.VertexProgram
.MaxTemps
) {
2078 _mesa_error(ctx
, GL_INVALID_VALUE
,
2079 "glGetProgramRegisterfvMESA(registerName)");
2083 ctx
->Driver
.GetVertexProgramRegister(ctx
, PROGRAM_TEMPORARY
, i
, v
);
2086 else if (reg
[0] == 'v' && reg
[1] == '[') {
2087 /* Vertex Input attribute */
2089 for (i
= 0; i
< ctx
->Const
.VertexProgram
.MaxAttribs
; i
++) {
2090 const char *name
= _mesa_nv_vertex_input_register_name(i
);
2092 _mesa_sprintf(number
, "%d", i
);
2093 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
2094 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
2096 ctx
->Driver
.GetVertexProgramRegister(ctx
, PROGRAM_INPUT
,
2102 _mesa_error(ctx
, GL_INVALID_VALUE
,
2103 "glGetProgramRegisterfvMESA(registerName)");
2106 else if (reg
[0] == 'o' && reg
[1] == '[') {
2107 /* Vertex output attribute */
2109 /* GL_ARB_vertex_program */
2110 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
2114 _mesa_error(ctx
, GL_INVALID_VALUE
,
2115 "glGetProgramRegisterfvMESA(registerName)");
2119 case GL_FRAGMENT_PROGRAM_ARB
:
2120 if (!ctx
->Extensions
.ARB_fragment_program
) {
2121 _mesa_error(ctx
, GL_INVALID_ENUM
,
2122 "glGetProgramRegisterfvMESA(target)");
2125 if (!ctx
->FragmentProgram
._Enabled
) {
2126 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2127 "glGetProgramRegisterfvMESA");
2132 case GL_FRAGMENT_PROGRAM_NV
:
2133 if (!ctx
->Extensions
.NV_fragment_program
) {
2134 _mesa_error(ctx
, GL_INVALID_ENUM
,
2135 "glGetProgramRegisterfvMESA(target)");
2138 if (!ctx
->FragmentProgram
._Enabled
) {
2139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2140 "glGetProgramRegisterfvMESA");
2143 if (reg
[0] == 'R') {
2145 GLint i
= _mesa_atoi(reg
+ 1);
2146 if (i
>= (GLint
)ctx
->Const
.FragmentProgram
.MaxTemps
) {
2147 _mesa_error(ctx
, GL_INVALID_VALUE
,
2148 "glGetProgramRegisterfvMESA(registerName)");
2151 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_TEMPORARY
,
2154 else if (reg
[0] == 'f' && reg
[1] == '[') {
2155 /* Fragment input attribute */
2157 for (i
= 0; i
< ctx
->Const
.FragmentProgram
.MaxAttribs
; i
++) {
2158 const char *name
= _mesa_nv_fragment_input_register_name(i
);
2159 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
2160 ctx
->Driver
.GetFragmentProgramRegister(ctx
,
2161 PROGRAM_INPUT
, i
, v
);
2165 _mesa_error(ctx
, GL_INVALID_VALUE
,
2166 "glGetProgramRegisterfvMESA(registerName)");
2169 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
2170 /* Fragment output color */
2171 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2172 FRAG_RESULT_COLR
, v
);
2174 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
2175 /* Fragment output color */
2176 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2177 FRAG_RESULT_COLH
, v
);
2179 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
2180 /* Fragment output depth */
2181 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2182 FRAG_RESULT_DEPR
, v
);
2185 /* try user-defined identifiers */
2186 const GLfloat
*value
= _mesa_lookup_parameter_value(
2187 ctx
->FragmentProgram
.Current
->Base
.Parameters
, -1, reg
);
2192 _mesa_error(ctx
, GL_INVALID_VALUE
,
2193 "glGetProgramRegisterfvMESA(registerName)");
2199 _mesa_error(ctx
, GL_INVALID_ENUM
,
2200 "glGetProgramRegisterfvMESA(target)");