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
|=
489 make_state_flags(stateTokens
);
492 /* free name string here since we duplicated it in add_parameter() */
493 _mesa_free((void *) name
);
500 * Lookup a parameter value by name in the given parameter list.
501 * \return pointer to the float[4] values.
504 _mesa_lookup_parameter_value(struct gl_program_parameter_list
*paramList
,
505 GLsizei nameLen
, const char *name
)
513 /* name is null-terminated */
514 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
515 if (paramList
->Parameters
[i
].Name
&&
516 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
517 return paramList
->ParameterValues
[i
];
521 /* name is not null-terminated, use nameLen */
522 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
523 if (paramList
->Parameters
[i
].Name
&&
524 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
525 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
526 return paramList
->ParameterValues
[i
];
534 * Lookup a parameter index by name in the given parameter list.
535 * \return index of parameter in the list.
538 _mesa_lookup_parameter_index(struct gl_program_parameter_list
*paramList
,
539 GLsizei nameLen
, const char *name
)
547 /* name is null-terminated */
548 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
549 if (paramList
->Parameters
[i
].Name
&&
550 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
555 /* name is not null-terminated, use nameLen */
556 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
557 if (paramList
->Parameters
[i
].Name
&&
558 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
559 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
568 * Use the list of tokens in the state[] array to find global GL state
569 * and return it in <value>. Usually, four values are returned in <value>
570 * but matrix queries may return as many as 16 values.
571 * This function is used for ARB vertex/fragment programs.
572 * The program parser will produce the state[] values.
575 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
581 /* state[1] is either 0=front or 1=back side */
582 const GLuint face
= (GLuint
) state
[1];
583 /* state[2] is the material attribute */
587 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
]);
589 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
]);
593 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
]);
595 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
]);
599 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
]);
601 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SPECULAR
]);
605 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
]);
607 COPY_4V(value
, ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
]);
609 case STATE_SHININESS
:
611 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
][0];
613 value
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_SHININESS
][0];
619 _mesa_problem(ctx
, "Invalid material state in fetch_state");
625 /* state[1] is the light number */
626 const GLuint ln
= (GLuint
) state
[1];
627 /* state[2] is the light attribute */
630 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
633 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
636 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
639 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
641 case STATE_ATTENUATION
:
642 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
643 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
644 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
645 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
647 case STATE_SPOT_DIRECTION
:
648 COPY_3V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
649 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
653 GLfloat eye_z
[] = {0, 0, 1};
655 /* Compute infinite half angle vector:
656 * half-vector = light_position + (0, 0, 1)
657 * and then normalize. w = 0
659 * light.EyePosition.w should be 0 for infinite lights.
661 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
662 NORMALIZE_3FV(value
);
666 case STATE_POSITION_NORMALIZED
:
667 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
668 NORMALIZE_3FV( value
);
671 _mesa_problem(ctx
, "Invalid light state in fetch_state");
675 case STATE_LIGHTMODEL_AMBIENT
:
676 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
678 case STATE_LIGHTMODEL_SCENECOLOR
:
682 for (i
= 0; i
< 3; i
++) {
683 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
684 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
685 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
687 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
692 for (i
= 0; i
< 3; i
++) {
693 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
694 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
695 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
697 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
700 case STATE_LIGHTPROD
:
702 const GLuint ln
= (GLuint
) state
[1];
703 const GLuint face
= (GLuint
) state
[2];
705 ASSERT(face
== 0 || face
== 1);
708 for (i
= 0; i
< 3; i
++) {
709 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
710 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
712 /* [3] = material alpha */
713 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
716 for (i
= 0; i
< 3; i
++) {
717 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
718 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
720 /* [3] = material alpha */
721 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
724 for (i
= 0; i
< 3; i
++) {
725 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
726 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
728 /* [3] = material alpha */
729 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
732 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
738 /* state[1] is the texture unit */
739 const GLuint unit
= (GLuint
) state
[1];
740 /* state[2] is the texgen attribute */
742 case STATE_TEXGEN_EYE_S
:
743 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
745 case STATE_TEXGEN_EYE_T
:
746 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
748 case STATE_TEXGEN_EYE_R
:
749 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
751 case STATE_TEXGEN_EYE_Q
:
752 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
754 case STATE_TEXGEN_OBJECT_S
:
755 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
757 case STATE_TEXGEN_OBJECT_T
:
758 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
760 case STATE_TEXGEN_OBJECT_R
:
761 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
763 case STATE_TEXGEN_OBJECT_Q
:
764 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
767 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
771 case STATE_TEXENV_COLOR
:
773 /* state[1] is the texture unit */
774 const GLuint unit
= (GLuint
) state
[1];
775 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
778 case STATE_FOG_COLOR
:
779 COPY_4V(value
, ctx
->Fog
.Color
);
781 case STATE_FOG_PARAMS
:
782 value
[0] = ctx
->Fog
.Density
;
783 value
[1] = ctx
->Fog
.Start
;
784 value
[2] = ctx
->Fog
.End
;
785 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
787 case STATE_CLIPPLANE
:
789 const GLuint plane
= (GLuint
) state
[1];
790 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
793 case STATE_POINT_SIZE
:
794 value
[0] = ctx
->Point
.Size
;
795 value
[1] = ctx
->Point
.MinSize
;
796 value
[2] = ctx
->Point
.MaxSize
;
797 value
[3] = ctx
->Point
.Threshold
;
799 case STATE_POINT_ATTENUATION
:
800 value
[0] = ctx
->Point
.Params
[0];
801 value
[1] = ctx
->Point
.Params
[1];
802 value
[2] = ctx
->Point
.Params
[2];
807 /* state[1] = modelview, projection, texture, etc. */
808 /* state[2] = which texture matrix or program matrix */
809 /* state[3] = first column to fetch */
810 /* state[4] = last column to fetch */
811 /* state[5] = transpose, inverse or invtrans */
813 const GLmatrix
*matrix
;
814 const enum state_index mat
= state
[1];
815 const GLuint index
= (GLuint
) state
[2];
816 const GLuint first
= (GLuint
) state
[3];
817 const GLuint last
= (GLuint
) state
[4];
818 const enum state_index modifier
= state
[5];
821 if (mat
== STATE_MODELVIEW
) {
822 matrix
= ctx
->ModelviewMatrixStack
.Top
;
824 else if (mat
== STATE_PROJECTION
) {
825 matrix
= ctx
->ProjectionMatrixStack
.Top
;
827 else if (mat
== STATE_MVP
) {
828 matrix
= &ctx
->_ModelProjectMatrix
;
830 else if (mat
== STATE_TEXTURE
) {
831 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
833 else if (mat
== STATE_PROGRAM
) {
834 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
837 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
840 if (modifier
== STATE_MATRIX_INVERSE
||
841 modifier
== STATE_MATRIX_INVTRANS
) {
842 /* Be sure inverse is up to date:
844 _math_matrix_alloc_inv( (GLmatrix
*) matrix
);
845 _math_matrix_analyse( (GLmatrix
*) matrix
);
851 if (modifier
== STATE_MATRIX_TRANSPOSE
||
852 modifier
== STATE_MATRIX_INVTRANS
) {
853 for (i
= 0, row
= first
; row
<= last
; row
++) {
854 value
[i
++] = m
[row
* 4 + 0];
855 value
[i
++] = m
[row
* 4 + 1];
856 value
[i
++] = m
[row
* 4 + 2];
857 value
[i
++] = m
[row
* 4 + 3];
861 for (i
= 0, row
= first
; row
<= last
; row
++) {
862 value
[i
++] = m
[row
+ 0];
863 value
[i
++] = m
[row
+ 4];
864 value
[i
++] = m
[row
+ 8];
865 value
[i
++] = m
[row
+ 12];
870 case STATE_DEPTH_RANGE
:
871 value
[0] = ctx
->Viewport
.Near
; /* near */
872 value
[1] = ctx
->Viewport
.Far
; /* far */
873 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
876 case STATE_FRAGMENT_PROGRAM
:
878 /* state[1] = {STATE_ENV, STATE_LOCAL} */
879 /* state[2] = parameter index */
880 const int idx
= (int) state
[2];
883 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
886 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
889 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
895 case STATE_VERTEX_PROGRAM
:
897 /* state[1] = {STATE_ENV, STATE_LOCAL} */
898 /* state[2] = parameter index */
899 const int idx
= (int) state
[2];
902 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
905 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
908 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
917 case STATE_NORMAL_SCALE
:
918 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
920 case STATE_TEXRECT_SCALE
: {
921 const int unit
= (int) state
[2];
922 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
924 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
925 ASSIGN_4V(value
, 1.0 / texImage
->Width
, 1.0 / texImage
->Height
, 0, 1);
930 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
937 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
944 * Return a bit mask of the Mesa state flags under which a parameter's
945 * value might change.
947 static GLuint
make_state_flags(const GLint state
[])
952 case STATE_LIGHTMODEL_AMBIENT
:
953 case STATE_LIGHTMODEL_SCENECOLOR
:
954 case STATE_LIGHTPROD
:
958 case STATE_TEXENV_COLOR
:
961 case STATE_FOG_COLOR
:
962 case STATE_FOG_PARAMS
:
965 case STATE_CLIPPLANE
:
966 return _NEW_TRANSFORM
;
968 case STATE_POINT_SIZE
:
969 case STATE_POINT_ATTENUATION
:
974 case STATE_MODELVIEW
:
975 return _NEW_MODELVIEW
;
976 case STATE_PROJECTION
:
977 return _NEW_PROJECTION
;
979 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
981 return _NEW_TEXTURE_MATRIX
;
983 return _NEW_TRACK_MATRIX
;
985 _mesa_problem(NULL
, "unexpected matrix in make_state_flags()");
989 case STATE_DEPTH_RANGE
:
990 return _NEW_VIEWPORT
;
992 case STATE_FRAGMENT_PROGRAM
:
993 case STATE_VERTEX_PROGRAM
:
998 case STATE_NORMAL_SCALE
:
999 return _NEW_MODELVIEW
;
1000 case STATE_TEXRECT_SCALE
:
1001 return _NEW_TEXTURE
;
1003 _mesa_problem(NULL
, "unexpected int. state in make_state_flags()");
1008 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
1015 append(char *dst
, const char *src
)
1026 append_token(char *dst
, enum state_index k
)
1029 case STATE_MATERIAL
:
1030 append(dst
, "material.");
1033 append(dst
, "light");
1035 case STATE_LIGHTMODEL_AMBIENT
:
1036 append(dst
, "lightmodel.ambient");
1038 case STATE_LIGHTMODEL_SCENECOLOR
:
1040 case STATE_LIGHTPROD
:
1041 append(dst
, "lightprod");
1044 append(dst
, "texgen");
1046 case STATE_FOG_COLOR
:
1047 append(dst
, "fog.color");
1049 case STATE_FOG_PARAMS
:
1050 append(dst
, "fog.params");
1052 case STATE_CLIPPLANE
:
1053 append(dst
, "clip");
1055 case STATE_POINT_SIZE
:
1056 append(dst
, "point.size");
1058 case STATE_POINT_ATTENUATION
:
1059 append(dst
, "point.attenuation");
1062 append(dst
, "matrix.");
1064 case STATE_MODELVIEW
:
1065 append(dst
, "modelview");
1067 case STATE_PROJECTION
:
1068 append(dst
, "projection");
1074 append(dst
, "texture");
1077 append(dst
, "program");
1079 case STATE_MATRIX_INVERSE
:
1080 append(dst
, ".inverse");
1082 case STATE_MATRIX_TRANSPOSE
:
1083 append(dst
, ".transpose");
1085 case STATE_MATRIX_INVTRANS
:
1086 append(dst
, ".invtrans");
1089 append(dst
, "ambient");
1092 append(dst
, "diffuse");
1094 case STATE_SPECULAR
:
1095 append(dst
, "specular");
1097 case STATE_EMISSION
:
1098 append(dst
, "emission");
1100 case STATE_SHININESS
:
1101 append(dst
, "shininess");
1104 append(dst
, "half");
1106 case STATE_POSITION
:
1107 append(dst
, ".position");
1109 case STATE_ATTENUATION
:
1110 append(dst
, ".attenuation");
1112 case STATE_SPOT_DIRECTION
:
1113 append(dst
, ".spot.direction");
1115 case STATE_TEXGEN_EYE_S
:
1116 append(dst
, "eye.s");
1118 case STATE_TEXGEN_EYE_T
:
1119 append(dst
, "eye.t");
1121 case STATE_TEXGEN_EYE_R
:
1122 append(dst
, "eye.r");
1124 case STATE_TEXGEN_EYE_Q
:
1125 append(dst
, "eye.q");
1127 case STATE_TEXGEN_OBJECT_S
:
1128 append(dst
, "object.s");
1130 case STATE_TEXGEN_OBJECT_T
:
1131 append(dst
, "object.t");
1133 case STATE_TEXGEN_OBJECT_R
:
1134 append(dst
, "object.r");
1136 case STATE_TEXGEN_OBJECT_Q
:
1137 append(dst
, "object.q");
1139 case STATE_TEXENV_COLOR
:
1140 append(dst
, "texenv");
1142 case STATE_DEPTH_RANGE
:
1143 append(dst
, "depth.range");
1145 case STATE_VERTEX_PROGRAM
:
1146 case STATE_FRAGMENT_PROGRAM
:
1152 append(dst
, "local");
1154 case STATE_INTERNAL
:
1155 case STATE_NORMAL_SCALE
:
1156 case STATE_POSITION_NORMALIZED
:
1157 append(dst
, "(internal)");
1165 append_face(char *dst
, GLint face
)
1168 append(dst
, "front.");
1170 append(dst
, "back.");
1174 append_index(char *dst
, GLint index
)
1177 _mesa_sprintf(s
, "[%d].", index
);
1182 * Make a string from the given state vector.
1183 * For example, return "state.matrix.texture[2].inverse".
1184 * Use _mesa_free() to deallocate the string.
1187 make_state_string(const GLint state
[6])
1189 char str
[1000] = "";
1192 append(str
, "state.");
1193 append_token(str
, (enum state_index
) state
[0]);
1196 case STATE_MATERIAL
:
1197 append_face(str
, state
[1]);
1198 append_token(str
, (enum state_index
) state
[2]);
1201 append(str
, "light");
1202 append_index(str
, state
[1]); /* light number [i]. */
1203 append_token(str
, (enum state_index
) state
[2]); /* coefficients */
1205 case STATE_LIGHTMODEL_AMBIENT
:
1206 append(str
, "lightmodel.ambient");
1208 case STATE_LIGHTMODEL_SCENECOLOR
:
1209 if (state
[1] == 0) {
1210 append(str
, "lightmodel.front.scenecolor");
1213 append(str
, "lightmodel.back.scenecolor");
1216 case STATE_LIGHTPROD
:
1217 append_index(str
, state
[1]); /* light number [i]. */
1218 append_face(str
, state
[2]);
1219 append_token(str
, (enum state_index
) state
[3]);
1222 append_index(str
, state
[1]); /* tex unit [i] */
1223 append_token(str
, (enum state_index
) state
[2]); /* plane coef */
1225 case STATE_TEXENV_COLOR
:
1226 append_index(str
, state
[1]); /* tex unit [i] */
1227 append(str
, "color");
1229 case STATE_FOG_COLOR
:
1230 case STATE_FOG_PARAMS
:
1232 case STATE_CLIPPLANE
:
1233 append_index(str
, state
[1]); /* plane [i] */
1234 append(str
, "plane");
1236 case STATE_POINT_SIZE
:
1237 case STATE_POINT_ATTENUATION
:
1241 /* state[1] = modelview, projection, texture, etc. */
1242 /* state[2] = which texture matrix or program matrix */
1243 /* state[3] = first column to fetch */
1244 /* state[4] = last column to fetch */
1245 /* state[5] = transpose, inverse or invtrans */
1246 const enum state_index mat
= (enum state_index
) state
[1];
1247 const GLuint index
= (GLuint
) state
[2];
1248 const GLuint first
= (GLuint
) state
[3];
1249 const GLuint last
= (GLuint
) state
[4];
1250 const enum state_index modifier
= (enum state_index
) state
[5];
1251 append_token(str
, mat
);
1253 append_index(str
, index
);
1255 append_token(str
, modifier
);
1257 _mesa_sprintf(tmp
, ".row[%d]", first
);
1259 _mesa_sprintf(tmp
, ".row[%d..%d]", first
, last
);
1263 case STATE_DEPTH_RANGE
:
1265 case STATE_FRAGMENT_PROGRAM
:
1266 case STATE_VERTEX_PROGRAM
:
1267 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1268 /* state[2] = parameter index */
1269 append_token(str
, (enum state_index
) state
[1]);
1270 append_index(str
, state
[2]);
1272 case STATE_INTERNAL
:
1275 _mesa_problem(NULL
, "Invalid state in maka_state_string");
1279 return _mesa_strdup(str
);
1284 * Loop over all the parameters in a parameter list. If the parameter
1285 * is a GL state reference, look up the current value of that state
1286 * variable and put it into the parameter's Value[4] array.
1287 * This would be called at glBegin time when using a fragment program.
1290 _mesa_load_state_parameters(GLcontext
*ctx
,
1291 struct gl_program_parameter_list
*paramList
)
1298 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1299 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1300 _mesa_fetch_state(ctx
,
1301 paramList
->Parameters
[i
].StateIndexes
,
1302 paramList
->ParameterValues
[i
]);
1309 * Initialize program instruction fields to defaults.
1310 * \param inst first instruction to initialize
1311 * \param count number of instructions to initialize
1314 _mesa_init_instructions(struct prog_instruction
*inst
, GLuint count
)
1318 _mesa_bzero(inst
, count
* sizeof(struct prog_instruction
));
1320 for (i
= 0; i
< count
; i
++) {
1321 inst
[i
].SrcReg
[0].File
= PROGRAM_UNDEFINED
;
1322 inst
[i
].SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
1323 inst
[i
].SrcReg
[1].File
= PROGRAM_UNDEFINED
;
1324 inst
[i
].SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
1325 inst
[i
].SrcReg
[2].File
= PROGRAM_UNDEFINED
;
1326 inst
[i
].SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
1328 inst
[i
].DstReg
.File
= PROGRAM_UNDEFINED
;
1329 inst
[i
].DstReg
.WriteMask
= WRITEMASK_XYZW
;
1330 inst
[i
].DstReg
.CondMask
= COND_TR
;
1331 inst
[i
].DstReg
.CondSwizzle
= SWIZZLE_NOOP
;
1333 inst
[i
].SaturateMode
= SATURATE_OFF
;
1334 inst
[i
].Precision
= FLOAT32
;
1340 * Allocate an array of program instructions.
1341 * \param numInst number of instructions
1342 * \return pointer to instruction memory
1344 struct prog_instruction
*
1345 _mesa_alloc_instructions(GLuint numInst
)
1347 return (struct prog_instruction
*)
1348 _mesa_calloc(numInst
* sizeof(struct prog_instruction
));
1353 * Reallocate memory storing an array of program instructions.
1354 * This is used when we need to append additional instructions onto an
1356 * \param oldInst pointer to first of old/src instructions
1357 * \param numOldInst number of instructions at <oldInst>
1358 * \param numNewInst desired size of new instruction array.
1359 * \return pointer to start of new instruction array.
1361 struct prog_instruction
*
1362 _mesa_realloc_instructions(struct prog_instruction
*oldInst
,
1363 GLuint numOldInst
, GLuint numNewInst
)
1365 struct prog_instruction
*newInst
;
1367 newInst
= (struct prog_instruction
*)
1368 _mesa_realloc(oldInst
,
1369 numOldInst
* sizeof(struct prog_instruction
),
1370 numNewInst
* sizeof(struct prog_instruction
));
1377 * Basic info about each instruction
1379 struct instruction_info
1381 enum prog_opcode Opcode
;
1388 * \note Opcode should equal array index!
1390 static const struct instruction_info InstInfo
[MAX_OPCODE
] = {
1391 { OPCODE_ABS
, "ABS", 1 },
1392 { OPCODE_ADD
, "ADD", 2 },
1393 { OPCODE_ARA
, "ARA", 1 },
1394 { OPCODE_ARL
, "ARL", 1 },
1395 { OPCODE_ARL_NV
, "ARL", 1 },
1396 { OPCODE_ARR
, "ARL", 1 },
1397 { OPCODE_BRA
, "BRA", 1 },
1398 { OPCODE_CAL
, "CAL", 1 },
1399 { OPCODE_CMP
, "CMP", 3 },
1400 { OPCODE_COS
, "COS", 1 },
1401 { OPCODE_DDX
, "DDX", 1 },
1402 { OPCODE_DDY
, "DDY", 1 },
1403 { OPCODE_DP3
, "DP3", 2 },
1404 { OPCODE_DP4
, "DP4", 2 },
1405 { OPCODE_DPH
, "DPH", 2 },
1406 { OPCODE_DST
, "DST", 2 },
1407 { OPCODE_END
, "END", 0 },
1408 { OPCODE_EX2
, "EX2", 1 },
1409 { OPCODE_EXP
, "EXP", 1 },
1410 { OPCODE_FLR
, "FLR", 1 },
1411 { OPCODE_FRC
, "FRC", 1 },
1412 { OPCODE_KIL
, "KIL", 1 },
1413 { OPCODE_KIL_NV
, "KIL", 0 },
1414 { OPCODE_LG2
, "LG2", 1 },
1415 { OPCODE_LIT
, "LIT", 1 },
1416 { OPCODE_LOG
, "LOG", 1 },
1417 { OPCODE_LRP
, "LRP", 3 },
1418 { OPCODE_MAD
, "MAD", 3 },
1419 { OPCODE_MAX
, "MAX", 2 },
1420 { OPCODE_MIN
, "MIN", 2 },
1421 { OPCODE_MOV
, "MOV", 1 },
1422 { OPCODE_MUL
, "MUL", 2 },
1423 { OPCODE_PK2H
, "PK2H", 1 },
1424 { OPCODE_PK2US
, "PK2US", 1 },
1425 { OPCODE_PK4B
, "PK4B", 1 },
1426 { OPCODE_PK4UB
, "PK4UB", 1 },
1427 { OPCODE_POW
, "POW", 2 },
1428 { OPCODE_POPA
, "POPA", 0 },
1429 { OPCODE_PRINT
, "PRINT", 1 },
1430 { OPCODE_PUSHA
, "PUSHA", 0 },
1431 { OPCODE_RCC
, "RCC", 1 },
1432 { OPCODE_RCP
, "RCP", 1 },
1433 { OPCODE_RET
, "RET", 1 },
1434 { OPCODE_RFL
, "RFL", 1 },
1435 { OPCODE_RSQ
, "RSQ", 1 },
1436 { OPCODE_SCS
, "SCS", 1 },
1437 { OPCODE_SEQ
, "SEQ", 2 },
1438 { OPCODE_SFL
, "SFL", 0 },
1439 { OPCODE_SGE
, "SGE", 2 },
1440 { OPCODE_SGT
, "SGT", 2 },
1441 { OPCODE_SIN
, "SIN", 1 },
1442 { OPCODE_SLE
, "SLE", 2 },
1443 { OPCODE_SLT
, "SLT", 2 },
1444 { OPCODE_SNE
, "SNE", 2 },
1445 { OPCODE_SSG
, "SSG", 1 },
1446 { OPCODE_STR
, "STR", 0 },
1447 { OPCODE_SUB
, "SUB", 2 },
1448 { OPCODE_SWZ
, "SWZ", 1 },
1449 { OPCODE_TEX
, "TEX", 1 },
1450 { OPCODE_TXB
, "TXB", 1 },
1451 { OPCODE_TXD
, "TXD", 3 },
1452 { OPCODE_TXL
, "TXL", 1 },
1453 { OPCODE_TXP
, "TXP", 1 },
1454 { OPCODE_TXP_NV
, "TXP", 1 },
1455 { OPCODE_UP2H
, "UP2H", 1 },
1456 { OPCODE_UP2US
, "UP2US", 1 },
1457 { OPCODE_UP4B
, "UP4B", 1 },
1458 { OPCODE_UP4UB
, "UP4UB", 1 },
1459 { OPCODE_X2D
, "X2D", 3 },
1460 { OPCODE_XPD
, "XPD", 2 }
1465 * Return the number of src registers for the given instruction/opcode.
1468 _mesa_num_inst_src_regs(enum prog_opcode opcode
)
1470 ASSERT(opcode
== InstInfo
[opcode
].Opcode
);
1471 return InstInfo
[opcode
].NumSrcRegs
;
1476 * Return string name for given program opcode.
1479 _mesa_opcode_string(enum prog_opcode opcode
)
1481 ASSERT(opcode
< MAX_OPCODE
);
1482 return InstInfo
[opcode
].Name
;
1486 * Return string name for given program/register file.
1489 program_file_string(enum register_file f
)
1492 case PROGRAM_TEMPORARY
:
1494 case PROGRAM_LOCAL_PARAM
:
1496 case PROGRAM_ENV_PARAM
:
1498 case PROGRAM_STATE_VAR
:
1502 case PROGRAM_OUTPUT
:
1504 case PROGRAM_NAMED_PARAM
:
1506 case PROGRAM_CONSTANT
:
1508 case PROGRAM_WRITE_ONLY
:
1509 return "WRITE_ONLY";
1510 case PROGRAM_ADDRESS
:
1519 * Return a string representation of the given swizzle word.
1520 * If extended is true, use extended (comma-separated) format.
1523 swizzle_string(GLuint swizzle
, GLuint negateBase
, GLboolean extended
)
1525 static const char swz
[] = "xyzw01";
1529 if (!extended
&& swizzle
== SWIZZLE_NOOP
&& negateBase
== 0)
1530 return ""; /* no swizzle/negation */
1535 if (negateBase
& 0x1)
1537 s
[i
++] = swz
[GET_SWZ(swizzle
, 0)];
1543 if (negateBase
& 0x2)
1545 s
[i
++] = swz
[GET_SWZ(swizzle
, 1)];
1551 if (negateBase
& 0x4)
1553 s
[i
++] = swz
[GET_SWZ(swizzle
, 2)];
1559 if (negateBase
& 0x8)
1561 s
[i
++] = swz
[GET_SWZ(swizzle
, 3)];
1569 writemask_string(GLuint writeMask
)
1574 if (writeMask
== WRITEMASK_XYZW
)
1578 if (writeMask
& WRITEMASK_X
)
1580 if (writeMask
& WRITEMASK_Y
)
1582 if (writeMask
& WRITEMASK_Z
)
1584 if (writeMask
& WRITEMASK_W
)
1592 print_dst_reg(const struct prog_dst_register
*dstReg
)
1594 _mesa_printf(" %s[%d]%s",
1595 program_file_string((enum register_file
) dstReg
->File
),
1597 writemask_string(dstReg
->WriteMask
));
1601 print_src_reg(const struct prog_src_register
*srcReg
)
1603 _mesa_printf("%s[%d]%s",
1604 program_file_string((enum register_file
) srcReg
->File
),
1606 swizzle_string(srcReg
->Swizzle
,
1607 srcReg
->NegateBase
, GL_FALSE
));
1611 _mesa_print_alu_instruction(const struct prog_instruction
*inst
,
1612 const char *opcode_string
,
1617 _mesa_printf("%s", opcode_string
);
1619 /* frag prog only */
1620 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1621 _mesa_printf("_SAT");
1623 if (inst
->DstReg
.File
!= PROGRAM_UNDEFINED
) {
1624 _mesa_printf(" %s[%d]%s",
1625 program_file_string((enum register_file
) inst
->DstReg
.File
),
1627 writemask_string(inst
->DstReg
.WriteMask
));
1633 for (j
= 0; j
< numRegs
; j
++) {
1634 print_src_reg(inst
->SrcReg
+ j
);
1635 if (j
+ 1 < numRegs
)
1639 _mesa_printf(";\n");
1644 * Print a single vertex/fragment program instruction.
1647 _mesa_print_instruction(const struct prog_instruction
*inst
)
1649 switch (inst
->Opcode
) {
1651 _mesa_printf("PRINT '%s'", inst
->Data
);
1652 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1654 _mesa_printf("%s[%d]%s",
1655 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1656 inst
->SrcReg
[0].Index
,
1657 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1658 inst
->SrcReg
[0].NegateBase
, GL_FALSE
));
1660 _mesa_printf(";\n");
1663 _mesa_printf("SWZ");
1664 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1665 _mesa_printf("_SAT");
1666 print_dst_reg(&inst
->DstReg
);
1667 _mesa_printf("%s[%d], %s;\n",
1668 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1669 inst
->SrcReg
[0].Index
,
1670 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1671 inst
->SrcReg
[0].NegateBase
, GL_TRUE
));
1676 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1677 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1678 _mesa_printf("_SAT");
1680 print_dst_reg(&inst
->DstReg
);
1682 print_src_reg(&inst
->SrcReg
[0]);
1683 _mesa_printf(", texture[%d], ", inst
->TexSrcUnit
);
1684 switch (inst
->TexSrcTarget
) {
1685 case TEXTURE_1D_INDEX
: _mesa_printf("1D"); break;
1686 case TEXTURE_2D_INDEX
: _mesa_printf("2D"); break;
1687 case TEXTURE_3D_INDEX
: _mesa_printf("3D"); break;
1688 case TEXTURE_CUBE_INDEX
: _mesa_printf("CUBE"); break;
1689 case TEXTURE_RECT_INDEX
: _mesa_printf("RECT"); break;
1696 _mesa_printf("ARL addr.x, ");
1697 print_src_reg(&inst
->SrcReg
[0]);
1698 _mesa_printf(";\n");
1701 _mesa_printf("END;\n");
1703 /* XXX may need for other special-case instructions */
1705 /* typical alu instruction */
1706 _mesa_print_alu_instruction(inst
,
1707 _mesa_opcode_string(inst
->Opcode
),
1708 _mesa_num_inst_src_regs(inst
->Opcode
));
1715 * Print a vertx/fragment program to stdout.
1716 * XXX this function could be greatly improved.
1719 _mesa_print_program(const struct gl_program
*prog
)
1722 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1723 _mesa_printf("%3d: ", i
);
1724 _mesa_print_instruction(prog
->Instructions
+ i
);
1730 * Print all of a program's parameters.
1733 _mesa_print_program_parameters(GLcontext
*ctx
, const struct gl_program
*prog
)
1737 _mesa_printf("NumInstructions=%d\n", prog
->NumInstructions
);
1738 _mesa_printf("NumTemporaries=%d\n", prog
->NumTemporaries
);
1739 _mesa_printf("NumParameters=%d\n", prog
->NumParameters
);
1740 _mesa_printf("NumAttributes=%d\n", prog
->NumAttributes
);
1741 _mesa_printf("NumAddressRegs=%d\n", prog
->NumAddressRegs
);
1743 _mesa_load_state_parameters(ctx
, prog
->Parameters
);
1746 _mesa_printf("Local Params:\n");
1747 for (i
= 0; i
< MAX_PROGRAM_LOCAL_PARAMS
; i
++){
1748 const GLfloat
*p
= prog
->LocalParams
[i
];
1749 _mesa_printf("%2d: %f, %f, %f, %f\n", i
, p
[0], p
[1], p
[2], p
[3]);
1753 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++){
1754 struct gl_program_parameter
*param
= prog
->Parameters
->Parameters
+ i
;
1755 const GLfloat
*v
= prog
->Parameters
->ParameterValues
[i
];
1756 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1757 i
, param
->Name
, v
[0], v
[1], v
[2], v
[3]);
1763 * Mixing ARB and NV vertex/fragment programs can be tricky.
1764 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
1765 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
1766 * The two different fragment program targets are supposed to be compatible
1767 * to some extent (see GL_ARB_fragment_program spec).
1768 * This function does the compatibility check.
1771 compatible_program_targets(GLenum t1
, GLenum t2
)
1775 if (t1
== GL_FRAGMENT_PROGRAM_ARB
&& t2
== GL_FRAGMENT_PROGRAM_NV
)
1777 if (t1
== GL_FRAGMENT_PROGRAM_NV
&& t2
== GL_FRAGMENT_PROGRAM_ARB
)
1784 /**********************************************************************/
1786 /**********************************************************************/
1790 * Bind a program (make it current)
1791 * \note Called from the GL API dispatcher by both glBindProgramNV
1792 * and glBindProgramARB.
1795 _mesa_BindProgram(GLenum target
, GLuint id
)
1797 struct gl_program
*curProg
, *newProg
;
1798 GET_CURRENT_CONTEXT(ctx
);
1799 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1801 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1803 /* Error-check target and get curProg */
1804 if ((target
== GL_VERTEX_PROGRAM_ARB
) && /* == GL_VERTEX_PROGRAM_NV */
1805 (ctx
->Extensions
.NV_vertex_program
||
1806 ctx
->Extensions
.ARB_vertex_program
)) {
1807 curProg
= &ctx
->VertexProgram
.Current
->Base
;
1809 else if ((target
== GL_FRAGMENT_PROGRAM_NV
1810 && ctx
->Extensions
.NV_fragment_program
) ||
1811 (target
== GL_FRAGMENT_PROGRAM_ARB
1812 && ctx
->Extensions
.ARB_fragment_program
)) {
1813 curProg
= &ctx
->FragmentProgram
.Current
->Base
;
1816 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
1821 * Get pointer to new program to bind.
1822 * NOTE: binding to a non-existant program is not an error.
1823 * That's supposed to be caught in glBegin.
1826 /* Bind a default program */
1828 if (target
== GL_VERTEX_PROGRAM_ARB
) /* == GL_VERTEX_PROGRAM_NV */
1829 newProg
= ctx
->Shared
->DefaultVertexProgram
;
1831 newProg
= ctx
->Shared
->DefaultFragmentProgram
;
1834 /* Bind a user program */
1835 newProg
= _mesa_lookup_program(ctx
, id
);
1836 if (!newProg
|| newProg
== &_mesa_DummyProgram
) {
1837 /* allocate a new program now */
1838 newProg
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
1840 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
1843 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, newProg
);
1845 else if (!compatible_program_targets(newProg
->Target
, target
)) {
1846 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1847 "glBindProgramNV/ARB(target mismatch)");
1852 /** All error checking is complete now **/
1854 if (curProg
->Id
== id
) {
1855 /* binding same program - no change */
1859 /* unbind/delete oldProg */
1860 if (curProg
->Id
!= 0) {
1861 /* decrement refcount on previously bound fragment program */
1862 curProg
->RefCount
--;
1863 /* and delete if refcount goes below one */
1864 if (curProg
->RefCount
<= 0) {
1865 /* the program ID was already removed from the hash table */
1866 ctx
->Driver
.DeleteProgram(ctx
, curProg
);
1871 if (target
== GL_VERTEX_PROGRAM_ARB
) { /* == GL_VERTEX_PROGRAM_NV */
1872 ctx
->VertexProgram
.Current
= (struct gl_vertex_program
*) newProg
;
1874 else if (target
== GL_FRAGMENT_PROGRAM_NV
||
1875 target
== GL_FRAGMENT_PROGRAM_ARB
) {
1876 ctx
->FragmentProgram
.Current
= (struct gl_fragment_program
*) newProg
;
1878 newProg
->RefCount
++;
1880 /* Never null pointers */
1881 ASSERT(ctx
->VertexProgram
.Current
);
1882 ASSERT(ctx
->FragmentProgram
.Current
);
1884 if (ctx
->Driver
.BindProgram
)
1885 ctx
->Driver
.BindProgram(ctx
, target
, newProg
);
1890 * Delete a list of programs.
1891 * \note Not compiled into display lists.
1892 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1895 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
1898 GET_CURRENT_CONTEXT(ctx
);
1899 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1902 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
1906 for (i
= 0; i
< n
; i
++) {
1908 struct gl_program
*prog
= _mesa_lookup_program(ctx
, ids
[i
]);
1909 if (prog
== &_mesa_DummyProgram
) {
1910 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1913 /* Unbind program if necessary */
1914 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| /* == GL_VERTEX_PROGRAM_NV */
1915 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
1916 if (ctx
->VertexProgram
.Current
&&
1917 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
1918 /* unbind this currently bound program */
1919 _mesa_BindProgram(prog
->Target
, 0);
1922 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
1923 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1924 if (ctx
->FragmentProgram
.Current
&&
1925 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
1926 /* unbind this currently bound program */
1927 _mesa_BindProgram(prog
->Target
, 0);
1931 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
1934 /* The ID is immediately available for re-use now */
1935 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1937 if (prog
->RefCount
<= 0) {
1938 ctx
->Driver
.DeleteProgram(ctx
, prog
);
1947 * Generate a list of new program identifiers.
1948 * \note Not compiled into display lists.
1949 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1952 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
1956 GET_CURRENT_CONTEXT(ctx
);
1957 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1960 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
1967 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
1969 /* Insert pointer to dummy program as placeholder */
1970 for (i
= 0; i
< (GLuint
) n
; i
++) {
1971 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, &_mesa_DummyProgram
);
1974 /* Return the program names */
1975 for (i
= 0; i
< (GLuint
) n
; i
++) {
1982 * Determine if id names a vertex or fragment program.
1983 * \note Not compiled into display lists.
1984 * \note Called from both glIsProgramNV and glIsProgramARB.
1985 * \param id is the program identifier
1986 * \return GL_TRUE if id is a program, else GL_FALSE.
1988 GLboolean GLAPIENTRY
1989 _mesa_IsProgram(GLuint id
)
1991 GET_CURRENT_CONTEXT(ctx
);
1992 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1997 if (_mesa_lookup_program(ctx
, id
))
2005 /**********************************************************************/
2006 /* GL_MESA_program_debug extension */
2007 /**********************************************************************/
2011 GLAPI
void GLAPIENTRY
2012 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
2015 _mesa_ProgramCallbackMESA(target
, callback
, data
);
2020 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
2023 GET_CURRENT_CONTEXT(ctx
);
2026 case GL_FRAGMENT_PROGRAM_ARB
:
2027 if (!ctx
->Extensions
.ARB_fragment_program
) {
2028 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2031 ctx
->FragmentProgram
.Callback
= callback
;
2032 ctx
->FragmentProgram
.CallbackData
= data
;
2034 case GL_FRAGMENT_PROGRAM_NV
:
2035 if (!ctx
->Extensions
.NV_fragment_program
) {
2036 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2039 ctx
->FragmentProgram
.Callback
= callback
;
2040 ctx
->FragmentProgram
.CallbackData
= data
;
2042 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
2043 if (!ctx
->Extensions
.ARB_vertex_program
&&
2044 !ctx
->Extensions
.NV_vertex_program
) {
2045 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2048 ctx
->VertexProgram
.Callback
= callback
;
2049 ctx
->VertexProgram
.CallbackData
= data
;
2052 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2059 GLAPI
void GLAPIENTRY
2060 glGetProgramRegisterfvMESA(GLenum target
,
2061 GLsizei len
, const GLubyte
*registerName
,
2064 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
2069 _mesa_GetProgramRegisterfvMESA(GLenum target
,
2070 GLsizei len
, const GLubyte
*registerName
,
2074 GET_CURRENT_CONTEXT(ctx
);
2076 /* We _should_ be inside glBegin/glEnd */
2078 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
2079 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
2084 /* make null-terminated copy of registerName */
2085 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
2086 _mesa_memcpy(reg
, registerName
, len
);
2090 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
2091 if (!ctx
->Extensions
.ARB_vertex_program
&&
2092 !ctx
->Extensions
.NV_vertex_program
) {
2093 _mesa_error(ctx
, GL_INVALID_ENUM
,
2094 "glGetProgramRegisterfvMESA(target)");
2097 if (!ctx
->VertexProgram
._Enabled
) {
2098 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2099 "glGetProgramRegisterfvMESA");
2102 /* GL_NV_vertex_program */
2103 if (reg
[0] == 'R') {
2105 GLint i
= _mesa_atoi(reg
+ 1);
2106 if (i
>= (GLint
)ctx
->Const
.VertexProgram
.MaxTemps
) {
2107 _mesa_error(ctx
, GL_INVALID_VALUE
,
2108 "glGetProgramRegisterfvMESA(registerName)");
2112 ctx
->Driver
.GetVertexProgramRegister(ctx
, PROGRAM_TEMPORARY
, i
, v
);
2115 else if (reg
[0] == 'v' && reg
[1] == '[') {
2116 /* Vertex Input attribute */
2118 for (i
= 0; i
< ctx
->Const
.VertexProgram
.MaxAttribs
; i
++) {
2119 const char *name
= _mesa_nv_vertex_input_register_name(i
);
2121 _mesa_sprintf(number
, "%d", i
);
2122 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
2123 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
2125 ctx
->Driver
.GetVertexProgramRegister(ctx
, PROGRAM_INPUT
,
2131 _mesa_error(ctx
, GL_INVALID_VALUE
,
2132 "glGetProgramRegisterfvMESA(registerName)");
2135 else if (reg
[0] == 'o' && reg
[1] == '[') {
2136 /* Vertex output attribute */
2138 /* GL_ARB_vertex_program */
2139 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
2143 _mesa_error(ctx
, GL_INVALID_VALUE
,
2144 "glGetProgramRegisterfvMESA(registerName)");
2148 case GL_FRAGMENT_PROGRAM_ARB
:
2149 if (!ctx
->Extensions
.ARB_fragment_program
) {
2150 _mesa_error(ctx
, GL_INVALID_ENUM
,
2151 "glGetProgramRegisterfvMESA(target)");
2154 if (!ctx
->FragmentProgram
._Enabled
) {
2155 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2156 "glGetProgramRegisterfvMESA");
2161 case GL_FRAGMENT_PROGRAM_NV
:
2162 if (!ctx
->Extensions
.NV_fragment_program
) {
2163 _mesa_error(ctx
, GL_INVALID_ENUM
,
2164 "glGetProgramRegisterfvMESA(target)");
2167 if (!ctx
->FragmentProgram
._Enabled
) {
2168 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2169 "glGetProgramRegisterfvMESA");
2172 if (reg
[0] == 'R') {
2174 GLint i
= _mesa_atoi(reg
+ 1);
2175 if (i
>= (GLint
)ctx
->Const
.FragmentProgram
.MaxTemps
) {
2176 _mesa_error(ctx
, GL_INVALID_VALUE
,
2177 "glGetProgramRegisterfvMESA(registerName)");
2180 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_TEMPORARY
,
2183 else if (reg
[0] == 'f' && reg
[1] == '[') {
2184 /* Fragment input attribute */
2186 for (i
= 0; i
< ctx
->Const
.FragmentProgram
.MaxAttribs
; i
++) {
2187 const char *name
= _mesa_nv_fragment_input_register_name(i
);
2188 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
2189 ctx
->Driver
.GetFragmentProgramRegister(ctx
,
2190 PROGRAM_INPUT
, i
, v
);
2194 _mesa_error(ctx
, GL_INVALID_VALUE
,
2195 "glGetProgramRegisterfvMESA(registerName)");
2198 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
2199 /* Fragment output color */
2200 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2201 FRAG_RESULT_COLR
, v
);
2203 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
2204 /* Fragment output color */
2205 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2206 FRAG_RESULT_COLH
, v
);
2208 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
2209 /* Fragment output depth */
2210 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2211 FRAG_RESULT_DEPR
, v
);
2214 /* try user-defined identifiers */
2215 const GLfloat
*value
= _mesa_lookup_parameter_value(
2216 ctx
->FragmentProgram
.Current
->Base
.Parameters
, -1, reg
);
2221 _mesa_error(ctx
, GL_INVALID_VALUE
,
2222 "glGetProgramRegisterfvMESA(registerName)");
2228 _mesa_error(ctx
, GL_INVALID_ENUM
,
2229 "glGetProgramRegisterfvMESA(target)");