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
;
210 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
218 * Initialize a new fragment program object.
221 _mesa_init_fragment_program( GLcontext
*ctx
, struct gl_fragment_program
*prog
,
222 GLenum target
, GLuint id
)
225 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
232 * Initialize a new vertex program object.
235 _mesa_init_vertex_program( GLcontext
*ctx
, struct gl_vertex_program
*prog
,
236 GLenum target
, GLuint id
)
239 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
246 * Allocate and initialize a new fragment/vertex program object but
247 * don't put it into the program hash table. Called via
248 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
249 * device driver function to implement OO deriviation with additional
250 * types not understood by this function.
253 * \param id program id/number
254 * \param target program target/type
255 * \return pointer to new program object
258 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
261 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
262 return _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
264 case GL_FRAGMENT_PROGRAM_NV
:
265 case GL_FRAGMENT_PROGRAM_ARB
:
266 return _mesa_init_fragment_program(ctx
,
267 CALLOC_STRUCT(gl_fragment_program
),
270 _mesa_problem(ctx
, "bad target in _mesa_new_program");
277 * Delete a program and remove it from the hash table, ignoring the
279 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
280 * by a device driver function.
283 _mesa_delete_program(GLcontext
*ctx
, struct gl_program
*prog
)
288 if (prog
== &_mesa_DummyProgram
)
292 _mesa_free(prog
->String
);
294 if (prog
->Instructions
) {
296 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
297 if (prog
->Instructions
[i
].Data
)
298 _mesa_free(prog
->Instructions
[i
].Data
);
300 _mesa_free(prog
->Instructions
);
303 if (prog
->Parameters
) {
304 _mesa_free_parameter_list(prog
->Parameters
);
307 /* XXX this is a little ugly */
308 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
309 struct gl_vertex_program
*vprog
= (struct gl_vertex_program
*) prog
;
311 _mesa_free(vprog
->TnlData
);
319 * Return the gl_program object for a given ID.
320 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
324 _mesa_lookup_program(GLcontext
*ctx
, GLuint id
)
327 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
333 /**********************************************************************/
334 /* Program parameter functions */
335 /**********************************************************************/
337 struct gl_program_parameter_list
*
338 _mesa_new_parameter_list(void)
340 return (struct gl_program_parameter_list
*)
341 _mesa_calloc(sizeof(struct gl_program_parameter_list
));
346 * Free a parameter list and all its parameters
349 _mesa_free_parameter_list(struct gl_program_parameter_list
*paramList
)
352 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
353 if (paramList
->Parameters
[i
].Name
)
354 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
356 _mesa_free(paramList
->Parameters
);
357 if (paramList
->ParameterValues
)
358 _mesa_align_free(paramList
->ParameterValues
);
359 _mesa_free(paramList
);
364 * Add a new parameter to a parameter list.
365 * \param paramList the list to add the parameter to
366 * \param name the parameter name, will be duplicated/copied!
367 * \param values initial parameter value, 4 GLfloats
368 * \param type type of parameter, such as
369 * \return index of new parameter in the list, or -1 if error (out of mem)
372 add_parameter(struct gl_program_parameter_list
*paramList
,
373 const char *name
, const GLfloat values
[4],
374 enum register_file type
)
376 const GLuint n
= paramList
->NumParameters
;
378 if (n
== paramList
->Size
) {
379 /* Need to grow the parameter list array */
380 if (paramList
->Size
== 0)
383 paramList
->Size
*= 2;
386 paramList
->Parameters
= (struct gl_program_parameter
*)
387 _mesa_realloc(paramList
->Parameters
,
388 n
* sizeof(struct gl_program_parameter
),
389 paramList
->Size
* sizeof(struct gl_program_parameter
));
391 paramList
->ParameterValues
= (GLfloat (*)[4])
392 _mesa_align_realloc(paramList
->ParameterValues
, /* old buf */
393 n
* 4 * sizeof(GLfloat
), /* old size */
394 paramList
->Size
* 4 *sizeof(GLfloat
), /* new sz */
398 if (!paramList
->Parameters
||
399 !paramList
->ParameterValues
) {
401 paramList
->NumParameters
= 0;
406 paramList
->NumParameters
= n
+ 1;
408 _mesa_memset(¶mList
->Parameters
[n
], 0,
409 sizeof(struct gl_program_parameter
));
411 paramList
->Parameters
[n
].Name
= name
? _mesa_strdup(name
) : NULL
;
412 paramList
->Parameters
[n
].Type
= type
;
414 COPY_4V(paramList
->ParameterValues
[n
], values
);
421 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
422 * \return index of the new entry in the parameter list
425 _mesa_add_named_parameter(struct gl_program_parameter_list
*paramList
,
426 const char *name
, const GLfloat values
[4])
428 return add_parameter(paramList
, name
, values
, PROGRAM_NAMED_PARAM
);
433 * Add a new named constant to the parameter list.
434 * This will be used when the program contains something like this:
435 * PARAM myVals = { 0, 1, 2, 3 };
437 * \param paramList the parameter list
438 * \param name the name for the constant
439 * \param values four float values
440 * \return index/position of the new parameter in the parameter list
443 _mesa_add_named_constant(struct gl_program_parameter_list
*paramList
,
444 const char *name
, const GLfloat values
[4],
447 #if 0 /* disable this for now -- we need to save the name! */
449 ASSERT(size
== 4); /* XXX future feature */
450 /* check if we already have this constant */
451 if (_mesa_lookup_parameter_constant(paramList
, values
, 4, &pos
, &swizzle
)) {
455 return add_parameter(paramList
, name
, values
, PROGRAM_CONSTANT
);
460 * Add a new unnamed constant to the parameter list.
461 * This will be used when the program contains something like this:
462 * MOV r, { 0, 1, 2, 3 };
464 * \param paramList the parameter list
465 * \param values four float values
466 * \return index/position of the new parameter in the parameter list.
469 _mesa_add_unnamed_constant(struct gl_program_parameter_list
*paramList
,
470 const GLfloat values
[4], GLuint size
)
473 ASSERT(size
== 4); /* XXX future feature */
474 /* check if we already have this constant */
475 if (_mesa_lookup_parameter_constant(paramList
, values
, 4, &pos
, &swizzle
)) {
478 return add_parameter(paramList
, NULL
, values
, PROGRAM_CONSTANT
);
483 * Add a new state reference to the parameter list.
484 * This will be used when the program contains something like this:
485 * PARAM ambient = state.material.front.ambient;
487 * \param paramList the parameter list
488 * \param state an array of 6 state tokens
489 * \return index of the new parameter.
492 _mesa_add_state_reference(struct gl_program_parameter_list
*paramList
,
493 const GLint
*stateTokens
)
495 /* XXX we should probably search the current parameter list to see if
496 * the new state reference is already present.
499 const char *name
= make_state_string(stateTokens
);
501 index
= add_parameter(paramList
, name
, NULL
, PROGRAM_STATE_VAR
);
504 for (i
= 0; i
< 6; i
++) {
505 paramList
->Parameters
[index
].StateIndexes
[i
]
506 = (enum state_index
) stateTokens
[i
];
508 paramList
->StateFlags
|= make_state_flags(stateTokens
);
511 /* free name string here since we duplicated it in add_parameter() */
512 _mesa_free((void *) name
);
519 * Lookup a parameter value by name in the given parameter list.
520 * \return pointer to the float[4] values.
523 _mesa_lookup_parameter_value(const struct gl_program_parameter_list
*paramList
,
524 GLsizei nameLen
, const char *name
)
532 /* name is null-terminated */
533 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
534 if (paramList
->Parameters
[i
].Name
&&
535 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
536 return paramList
->ParameterValues
[i
];
540 /* name is not null-terminated, use nameLen */
541 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
542 if (paramList
->Parameters
[i
].Name
&&
543 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
544 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
545 return paramList
->ParameterValues
[i
];
553 * Given a program parameter name, find its position in the list of parameters.
554 * \param paramList the parameter list to search
555 * \param nameLen length of name (in chars).
556 * If length is negative, assume that name is null-terminated.
557 * \param name the name to search for
558 * \return index of parameter in the list.
561 _mesa_lookup_parameter_index(const struct gl_program_parameter_list
*paramList
,
562 GLsizei nameLen
, const char *name
)
570 /* name is null-terminated */
571 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
572 if (paramList
->Parameters
[i
].Name
&&
573 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
578 /* name is not null-terminated, use nameLen */
579 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
580 if (paramList
->Parameters
[i
].Name
&&
581 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
582 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
591 * Look for a float vector in the given parameter list. The float vector
592 * may be of length 1, 2, 3 or 4.
593 * \param paramList the parameter list to search
594 * \param v the float vector to search for
595 * \param size number of element in v
596 * \param posOut returns the position of the constant, if found
597 * \param swizzleOut returns a swizzle mask describing location of the
598 * vector elements if found
599 * \return GL_TRUE if found, GL_FALSE if not found
602 _mesa_lookup_parameter_constant(const struct gl_program_parameter_list
*paramList
,
603 const GLfloat v
[], GLsizei vSize
,
604 GLuint
*posOut
, GLuint
*swizzleOut
)
614 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
615 if (paramList
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
616 const GLint maxShift
= 4 - vSize
;
618 for (shift
= 0; shift
<= maxShift
; shift
++) {
621 swizzle
[0] = swizzle
[1] = swizzle
[2] = swizzle
[3] = 0;
622 /* XXX we could do out-of-order swizzle matches too, someday */
623 for (j
= 0; j
< vSize
; j
++) {
624 assert(shift
+ j
< 4);
625 if (paramList
->ParameterValues
[i
][shift
+ j
] == v
[j
]) {
627 swizzle
[j
] = shift
+ j
;
630 if (matched
== vSize
) {
633 *swizzleOut
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
634 swizzle
[2], swizzle
[3]);
646 * Use the list of tokens in the state[] array to find global GL state
647 * and return it in <value>. Usually, four values are returned in <value>
648 * but matrix queries may return as many as 16 values.
649 * This function is used for ARB vertex/fragment programs.
650 * The program parser will produce the state[] values.
653 _mesa_fetch_state(GLcontext
*ctx
, const enum state_index state
[],
659 /* state[1] is either 0=front or 1=back side */
660 const GLuint face
= (GLuint
) state
[1];
661 const struct gl_material
*mat
= &ctx
->Light
.Material
;
662 ASSERT(face
== 0 || face
== 1);
663 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
664 ASSERT(MAT_ATTRIB_FRONT_AMBIENT
+ 1 == MAT_ATTRIB_BACK_AMBIENT
);
665 /* XXX we could get rid of this switch entirely with a little
666 * work in arbprogparse.c's parse_state_single_item().
668 /* state[2] is the material attribute */
671 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+ face
]);
674 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+ face
]);
677 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+ face
]);
680 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
+ face
]);
682 case STATE_SHININESS
:
683 value
[0] = mat
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ face
][0];
689 _mesa_problem(ctx
, "Invalid material state in fetch_state");
695 /* state[1] is the light number */
696 const GLuint ln
= (GLuint
) state
[1];
697 /* state[2] is the light attribute */
700 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
703 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
706 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
709 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
711 case STATE_ATTENUATION
:
712 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
713 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
714 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
715 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
717 case STATE_SPOT_DIRECTION
:
718 COPY_3V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
719 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
723 GLfloat eye_z
[] = {0, 0, 1};
725 /* Compute infinite half angle vector:
726 * half-vector = light_position + (0, 0, 1)
727 * and then normalize. w = 0
729 * light.EyePosition.w should be 0 for infinite lights.
731 ADD_3V(value
, eye_z
, ctx
->Light
.Light
[ln
].EyePosition
);
732 NORMALIZE_3FV(value
);
736 case STATE_POSITION_NORMALIZED
:
737 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
738 NORMALIZE_3FV( value
);
741 _mesa_problem(ctx
, "Invalid light state in fetch_state");
745 case STATE_LIGHTMODEL_AMBIENT
:
746 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
748 case STATE_LIGHTMODEL_SCENECOLOR
:
752 for (i
= 0; i
< 3; i
++) {
753 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
754 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
755 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
757 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
762 for (i
= 0; i
< 3; i
++) {
763 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
764 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
765 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
767 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
770 case STATE_LIGHTPROD
:
772 const GLuint ln
= (GLuint
) state
[1];
773 const GLuint face
= (GLuint
) state
[2];
775 ASSERT(face
== 0 || face
== 1);
778 for (i
= 0; i
< 3; i
++) {
779 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
780 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
782 /* [3] = material alpha */
783 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
786 for (i
= 0; i
< 3; i
++) {
787 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
788 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
790 /* [3] = material alpha */
791 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
794 for (i
= 0; i
< 3; i
++) {
795 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
796 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
798 /* [3] = material alpha */
799 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
802 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
808 /* state[1] is the texture unit */
809 const GLuint unit
= (GLuint
) state
[1];
810 /* state[2] is the texgen attribute */
812 case STATE_TEXGEN_EYE_S
:
813 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
815 case STATE_TEXGEN_EYE_T
:
816 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
818 case STATE_TEXGEN_EYE_R
:
819 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
821 case STATE_TEXGEN_EYE_Q
:
822 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
824 case STATE_TEXGEN_OBJECT_S
:
825 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
827 case STATE_TEXGEN_OBJECT_T
:
828 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
830 case STATE_TEXGEN_OBJECT_R
:
831 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
833 case STATE_TEXGEN_OBJECT_Q
:
834 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
837 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
841 case STATE_TEXENV_COLOR
:
843 /* state[1] is the texture unit */
844 const GLuint unit
= (GLuint
) state
[1];
845 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
848 case STATE_FOG_COLOR
:
849 COPY_4V(value
, ctx
->Fog
.Color
);
851 case STATE_FOG_PARAMS
:
852 value
[0] = ctx
->Fog
.Density
;
853 value
[1] = ctx
->Fog
.Start
;
854 value
[2] = ctx
->Fog
.End
;
855 value
[3] = 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
857 case STATE_CLIPPLANE
:
859 const GLuint plane
= (GLuint
) state
[1];
860 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
863 case STATE_POINT_SIZE
:
864 value
[0] = ctx
->Point
.Size
;
865 value
[1] = ctx
->Point
.MinSize
;
866 value
[2] = ctx
->Point
.MaxSize
;
867 value
[3] = ctx
->Point
.Threshold
;
869 case STATE_POINT_ATTENUATION
:
870 value
[0] = ctx
->Point
.Params
[0];
871 value
[1] = ctx
->Point
.Params
[1];
872 value
[2] = ctx
->Point
.Params
[2];
877 /* state[1] = modelview, projection, texture, etc. */
878 /* state[2] = which texture matrix or program matrix */
879 /* state[3] = first column to fetch */
880 /* state[4] = last column to fetch */
881 /* state[5] = transpose, inverse or invtrans */
883 const GLmatrix
*matrix
;
884 const enum state_index mat
= state
[1];
885 const GLuint index
= (GLuint
) state
[2];
886 const GLuint first
= (GLuint
) state
[3];
887 const GLuint last
= (GLuint
) state
[4];
888 const enum state_index modifier
= state
[5];
891 if (mat
== STATE_MODELVIEW
) {
892 matrix
= ctx
->ModelviewMatrixStack
.Top
;
894 else if (mat
== STATE_PROJECTION
) {
895 matrix
= ctx
->ProjectionMatrixStack
.Top
;
897 else if (mat
== STATE_MVP
) {
898 matrix
= &ctx
->_ModelProjectMatrix
;
900 else if (mat
== STATE_TEXTURE
) {
901 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
903 else if (mat
== STATE_PROGRAM
) {
904 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
907 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
910 if (modifier
== STATE_MATRIX_INVERSE
||
911 modifier
== STATE_MATRIX_INVTRANS
) {
912 /* Be sure inverse is up to date:
914 _math_matrix_alloc_inv( (GLmatrix
*) matrix
);
915 _math_matrix_analyse( (GLmatrix
*) matrix
);
921 if (modifier
== STATE_MATRIX_TRANSPOSE
||
922 modifier
== STATE_MATRIX_INVTRANS
) {
923 for (i
= 0, row
= first
; row
<= last
; row
++) {
924 value
[i
++] = m
[row
* 4 + 0];
925 value
[i
++] = m
[row
* 4 + 1];
926 value
[i
++] = m
[row
* 4 + 2];
927 value
[i
++] = m
[row
* 4 + 3];
931 for (i
= 0, row
= first
; row
<= last
; row
++) {
932 value
[i
++] = m
[row
+ 0];
933 value
[i
++] = m
[row
+ 4];
934 value
[i
++] = m
[row
+ 8];
935 value
[i
++] = m
[row
+ 12];
940 case STATE_DEPTH_RANGE
:
941 value
[0] = ctx
->Viewport
.Near
; /* near */
942 value
[1] = ctx
->Viewport
.Far
; /* far */
943 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
946 case STATE_FRAGMENT_PROGRAM
:
948 /* state[1] = {STATE_ENV, STATE_LOCAL} */
949 /* state[2] = parameter index */
950 const int idx
= (int) state
[2];
953 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
956 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
959 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
965 case STATE_VERTEX_PROGRAM
:
967 /* state[1] = {STATE_ENV, STATE_LOCAL} */
968 /* state[2] = parameter index */
969 const int idx
= (int) state
[2];
972 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
975 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
978 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
987 case STATE_NORMAL_SCALE
:
988 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
990 case STATE_TEXRECT_SCALE
: {
991 const int unit
= (int) state
[2];
992 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
994 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
995 ASSIGN_4V(value
, 1.0 / texImage
->Width
, 1.0 / texImage
->Height
, 0, 1);
1000 /* unknown state indexes are silently ignored
1001 * should be handled by the driver.
1009 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
1016 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
1017 * indicate that the given context state may have changed.
1018 * The bitmask is used during validation to determine if we need to update
1019 * vertex/fragment program parameters (like "state.material.color") when
1020 * some GL state has changed.
1023 make_state_flags(const GLint state
[])
1026 case STATE_MATERIAL
:
1028 case STATE_LIGHTMODEL_AMBIENT
:
1029 case STATE_LIGHTMODEL_SCENECOLOR
:
1030 case STATE_LIGHTPROD
:
1034 case STATE_TEXENV_COLOR
:
1035 return _NEW_TEXTURE
;
1037 case STATE_FOG_COLOR
:
1038 case STATE_FOG_PARAMS
:
1041 case STATE_CLIPPLANE
:
1042 return _NEW_TRANSFORM
;
1044 case STATE_POINT_SIZE
:
1045 case STATE_POINT_ATTENUATION
:
1050 case STATE_MODELVIEW
:
1051 return _NEW_MODELVIEW
;
1052 case STATE_PROJECTION
:
1053 return _NEW_PROJECTION
;
1055 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
1057 return _NEW_TEXTURE_MATRIX
;
1059 return _NEW_TRACK_MATRIX
;
1061 _mesa_problem(NULL
, "unexpected matrix in make_state_flags()");
1065 case STATE_DEPTH_RANGE
:
1066 return _NEW_VIEWPORT
;
1068 case STATE_FRAGMENT_PROGRAM
:
1069 case STATE_VERTEX_PROGRAM
:
1070 return _NEW_PROGRAM
;
1072 case STATE_INTERNAL
:
1074 case STATE_NORMAL_SCALE
:
1075 return _NEW_MODELVIEW
;
1076 case STATE_TEXRECT_SCALE
:
1077 return _NEW_TEXTURE
;
1079 /* unknown state indexes are silently ignored and
1080 * no flag set, since it is handled by the driver.
1086 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
1093 append(char *dst
, const char *src
)
1104 append_token(char *dst
, enum state_index k
)
1107 case STATE_MATERIAL
:
1108 append(dst
, "material.");
1111 append(dst
, "light");
1113 case STATE_LIGHTMODEL_AMBIENT
:
1114 append(dst
, "lightmodel.ambient");
1116 case STATE_LIGHTMODEL_SCENECOLOR
:
1118 case STATE_LIGHTPROD
:
1119 append(dst
, "lightprod");
1122 append(dst
, "texgen");
1124 case STATE_FOG_COLOR
:
1125 append(dst
, "fog.color");
1127 case STATE_FOG_PARAMS
:
1128 append(dst
, "fog.params");
1130 case STATE_CLIPPLANE
:
1131 append(dst
, "clip");
1133 case STATE_POINT_SIZE
:
1134 append(dst
, "point.size");
1136 case STATE_POINT_ATTENUATION
:
1137 append(dst
, "point.attenuation");
1140 append(dst
, "matrix.");
1142 case STATE_MODELVIEW
:
1143 append(dst
, "modelview");
1145 case STATE_PROJECTION
:
1146 append(dst
, "projection");
1152 append(dst
, "texture");
1155 append(dst
, "program");
1157 case STATE_MATRIX_INVERSE
:
1158 append(dst
, ".inverse");
1160 case STATE_MATRIX_TRANSPOSE
:
1161 append(dst
, ".transpose");
1163 case STATE_MATRIX_INVTRANS
:
1164 append(dst
, ".invtrans");
1167 append(dst
, "ambient");
1170 append(dst
, "diffuse");
1172 case STATE_SPECULAR
:
1173 append(dst
, "specular");
1175 case STATE_EMISSION
:
1176 append(dst
, "emission");
1178 case STATE_SHININESS
:
1179 append(dst
, "shininess");
1182 append(dst
, "half");
1184 case STATE_POSITION
:
1185 append(dst
, ".position");
1187 case STATE_ATTENUATION
:
1188 append(dst
, ".attenuation");
1190 case STATE_SPOT_DIRECTION
:
1191 append(dst
, ".spot.direction");
1193 case STATE_TEXGEN_EYE_S
:
1194 append(dst
, "eye.s");
1196 case STATE_TEXGEN_EYE_T
:
1197 append(dst
, "eye.t");
1199 case STATE_TEXGEN_EYE_R
:
1200 append(dst
, "eye.r");
1202 case STATE_TEXGEN_EYE_Q
:
1203 append(dst
, "eye.q");
1205 case STATE_TEXGEN_OBJECT_S
:
1206 append(dst
, "object.s");
1208 case STATE_TEXGEN_OBJECT_T
:
1209 append(dst
, "object.t");
1211 case STATE_TEXGEN_OBJECT_R
:
1212 append(dst
, "object.r");
1214 case STATE_TEXGEN_OBJECT_Q
:
1215 append(dst
, "object.q");
1217 case STATE_TEXENV_COLOR
:
1218 append(dst
, "texenv");
1220 case STATE_DEPTH_RANGE
:
1221 append(dst
, "depth.range");
1223 case STATE_VERTEX_PROGRAM
:
1224 case STATE_FRAGMENT_PROGRAM
:
1230 append(dst
, "local");
1232 case STATE_INTERNAL
:
1233 case STATE_NORMAL_SCALE
:
1234 case STATE_POSITION_NORMALIZED
:
1235 append(dst
, "(internal)");
1243 append_face(char *dst
, GLint face
)
1246 append(dst
, "front.");
1248 append(dst
, "back.");
1252 append_index(char *dst
, GLint index
)
1255 _mesa_sprintf(s
, "[%d].", index
);
1260 * Make a string from the given state vector.
1261 * For example, return "state.matrix.texture[2].inverse".
1262 * Use _mesa_free() to deallocate the string.
1265 make_state_string(const GLint state
[6])
1267 char str
[1000] = "";
1270 append(str
, "state.");
1271 append_token(str
, (enum state_index
) state
[0]);
1274 case STATE_MATERIAL
:
1275 append_face(str
, state
[1]);
1276 append_token(str
, (enum state_index
) state
[2]);
1279 append(str
, "light");
1280 append_index(str
, state
[1]); /* light number [i]. */
1281 append_token(str
, (enum state_index
) state
[2]); /* coefficients */
1283 case STATE_LIGHTMODEL_AMBIENT
:
1284 append(str
, "lightmodel.ambient");
1286 case STATE_LIGHTMODEL_SCENECOLOR
:
1287 if (state
[1] == 0) {
1288 append(str
, "lightmodel.front.scenecolor");
1291 append(str
, "lightmodel.back.scenecolor");
1294 case STATE_LIGHTPROD
:
1295 append_index(str
, state
[1]); /* light number [i]. */
1296 append_face(str
, state
[2]);
1297 append_token(str
, (enum state_index
) state
[3]);
1300 append_index(str
, state
[1]); /* tex unit [i] */
1301 append_token(str
, (enum state_index
) state
[2]); /* plane coef */
1303 case STATE_TEXENV_COLOR
:
1304 append_index(str
, state
[1]); /* tex unit [i] */
1305 append(str
, "color");
1307 case STATE_FOG_COLOR
:
1308 case STATE_FOG_PARAMS
:
1310 case STATE_CLIPPLANE
:
1311 append_index(str
, state
[1]); /* plane [i] */
1312 append(str
, "plane");
1314 case STATE_POINT_SIZE
:
1315 case STATE_POINT_ATTENUATION
:
1319 /* state[1] = modelview, projection, texture, etc. */
1320 /* state[2] = which texture matrix or program matrix */
1321 /* state[3] = first column to fetch */
1322 /* state[4] = last column to fetch */
1323 /* state[5] = transpose, inverse or invtrans */
1324 const enum state_index mat
= (enum state_index
) state
[1];
1325 const GLuint index
= (GLuint
) state
[2];
1326 const GLuint first
= (GLuint
) state
[3];
1327 const GLuint last
= (GLuint
) state
[4];
1328 const enum state_index modifier
= (enum state_index
) state
[5];
1329 append_token(str
, mat
);
1331 append_index(str
, index
);
1333 append_token(str
, modifier
);
1335 _mesa_sprintf(tmp
, ".row[%d]", first
);
1337 _mesa_sprintf(tmp
, ".row[%d..%d]", first
, last
);
1341 case STATE_DEPTH_RANGE
:
1343 case STATE_FRAGMENT_PROGRAM
:
1344 case STATE_VERTEX_PROGRAM
:
1345 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1346 /* state[2] = parameter index */
1347 append_token(str
, (enum state_index
) state
[1]);
1348 append_index(str
, state
[2]);
1350 case STATE_INTERNAL
:
1353 _mesa_problem(NULL
, "Invalid state in make_state_string");
1357 return _mesa_strdup(str
);
1362 * Loop over all the parameters in a parameter list. If the parameter
1363 * is a GL state reference, look up the current value of that state
1364 * variable and put it into the parameter's Value[4] array.
1365 * This would be called at glBegin time when using a fragment program.
1368 _mesa_load_state_parameters(GLcontext
*ctx
,
1369 struct gl_program_parameter_list
*paramList
)
1376 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1377 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1378 _mesa_fetch_state(ctx
,
1379 paramList
->Parameters
[i
].StateIndexes
,
1380 paramList
->ParameterValues
[i
]);
1387 * Initialize program instruction fields to defaults.
1388 * \param inst first instruction to initialize
1389 * \param count number of instructions to initialize
1392 _mesa_init_instructions(struct prog_instruction
*inst
, GLuint count
)
1396 _mesa_bzero(inst
, count
* sizeof(struct prog_instruction
));
1398 for (i
= 0; i
< count
; i
++) {
1399 inst
[i
].SrcReg
[0].File
= PROGRAM_UNDEFINED
;
1400 inst
[i
].SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
1401 inst
[i
].SrcReg
[1].File
= PROGRAM_UNDEFINED
;
1402 inst
[i
].SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
1403 inst
[i
].SrcReg
[2].File
= PROGRAM_UNDEFINED
;
1404 inst
[i
].SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
1406 inst
[i
].DstReg
.File
= PROGRAM_UNDEFINED
;
1407 inst
[i
].DstReg
.WriteMask
= WRITEMASK_XYZW
;
1408 inst
[i
].DstReg
.CondMask
= COND_TR
;
1409 inst
[i
].DstReg
.CondSwizzle
= SWIZZLE_NOOP
;
1411 inst
[i
].SaturateMode
= SATURATE_OFF
;
1412 inst
[i
].Precision
= FLOAT32
;
1418 * Allocate an array of program instructions.
1419 * \param numInst number of instructions
1420 * \return pointer to instruction memory
1422 struct prog_instruction
*
1423 _mesa_alloc_instructions(GLuint numInst
)
1425 return (struct prog_instruction
*)
1426 _mesa_calloc(numInst
* sizeof(struct prog_instruction
));
1431 * Reallocate memory storing an array of program instructions.
1432 * This is used when we need to append additional instructions onto an
1434 * \param oldInst pointer to first of old/src instructions
1435 * \param numOldInst number of instructions at <oldInst>
1436 * \param numNewInst desired size of new instruction array.
1437 * \return pointer to start of new instruction array.
1439 struct prog_instruction
*
1440 _mesa_realloc_instructions(struct prog_instruction
*oldInst
,
1441 GLuint numOldInst
, GLuint numNewInst
)
1443 struct prog_instruction
*newInst
;
1445 newInst
= (struct prog_instruction
*)
1446 _mesa_realloc(oldInst
,
1447 numOldInst
* sizeof(struct prog_instruction
),
1448 numNewInst
* sizeof(struct prog_instruction
));
1455 * Basic info about each instruction
1457 struct instruction_info
1459 enum prog_opcode Opcode
;
1466 * \note Opcode should equal array index!
1468 static const struct instruction_info InstInfo
[MAX_OPCODE
] = {
1469 { OPCODE_ABS
, "ABS", 1 },
1470 { OPCODE_ADD
, "ADD", 2 },
1471 { OPCODE_ARA
, "ARA", 1 },
1472 { OPCODE_ARL
, "ARL", 1 },
1473 { OPCODE_ARL_NV
, "ARL", 1 },
1474 { OPCODE_ARR
, "ARL", 1 },
1475 { OPCODE_BRA
, "BRA", 1 },
1476 { OPCODE_CAL
, "CAL", 1 },
1477 { OPCODE_CMP
, "CMP", 3 },
1478 { OPCODE_COS
, "COS", 1 },
1479 { OPCODE_DDX
, "DDX", 1 },
1480 { OPCODE_DDY
, "DDY", 1 },
1481 { OPCODE_DP3
, "DP3", 2 },
1482 { OPCODE_DP4
, "DP4", 2 },
1483 { OPCODE_DPH
, "DPH", 2 },
1484 { OPCODE_DST
, "DST", 2 },
1485 { OPCODE_END
, "END", 0 },
1486 { OPCODE_EX2
, "EX2", 1 },
1487 { OPCODE_EXP
, "EXP", 1 },
1488 { OPCODE_FLR
, "FLR", 1 },
1489 { OPCODE_FRC
, "FRC", 1 },
1490 { OPCODE_KIL
, "KIL", 1 },
1491 { OPCODE_KIL_NV
, "KIL", 0 },
1492 { OPCODE_LG2
, "LG2", 1 },
1493 { OPCODE_LIT
, "LIT", 1 },
1494 { OPCODE_LOG
, "LOG", 1 },
1495 { OPCODE_LRP
, "LRP", 3 },
1496 { OPCODE_MAD
, "MAD", 3 },
1497 { OPCODE_MAX
, "MAX", 2 },
1498 { OPCODE_MIN
, "MIN", 2 },
1499 { OPCODE_MOV
, "MOV", 1 },
1500 { OPCODE_MUL
, "MUL", 2 },
1501 { OPCODE_PK2H
, "PK2H", 1 },
1502 { OPCODE_PK2US
, "PK2US", 1 },
1503 { OPCODE_PK4B
, "PK4B", 1 },
1504 { OPCODE_PK4UB
, "PK4UB", 1 },
1505 { OPCODE_POW
, "POW", 2 },
1506 { OPCODE_POPA
, "POPA", 0 },
1507 { OPCODE_PRINT
, "PRINT", 1 },
1508 { OPCODE_PUSHA
, "PUSHA", 0 },
1509 { OPCODE_RCC
, "RCC", 1 },
1510 { OPCODE_RCP
, "RCP", 1 },
1511 { OPCODE_RET
, "RET", 1 },
1512 { OPCODE_RFL
, "RFL", 1 },
1513 { OPCODE_RSQ
, "RSQ", 1 },
1514 { OPCODE_SCS
, "SCS", 1 },
1515 { OPCODE_SEQ
, "SEQ", 2 },
1516 { OPCODE_SFL
, "SFL", 0 },
1517 { OPCODE_SGE
, "SGE", 2 },
1518 { OPCODE_SGT
, "SGT", 2 },
1519 { OPCODE_SIN
, "SIN", 1 },
1520 { OPCODE_SLE
, "SLE", 2 },
1521 { OPCODE_SLT
, "SLT", 2 },
1522 { OPCODE_SNE
, "SNE", 2 },
1523 { OPCODE_SSG
, "SSG", 1 },
1524 { OPCODE_STR
, "STR", 0 },
1525 { OPCODE_SUB
, "SUB", 2 },
1526 { OPCODE_SWZ
, "SWZ", 1 },
1527 { OPCODE_TEX
, "TEX", 1 },
1528 { OPCODE_TXB
, "TXB", 1 },
1529 { OPCODE_TXD
, "TXD", 3 },
1530 { OPCODE_TXL
, "TXL", 1 },
1531 { OPCODE_TXP
, "TXP", 1 },
1532 { OPCODE_TXP_NV
, "TXP", 1 },
1533 { OPCODE_UP2H
, "UP2H", 1 },
1534 { OPCODE_UP2US
, "UP2US", 1 },
1535 { OPCODE_UP4B
, "UP4B", 1 },
1536 { OPCODE_UP4UB
, "UP4UB", 1 },
1537 { OPCODE_X2D
, "X2D", 3 },
1538 { OPCODE_XPD
, "XPD", 2 }
1543 * Return the number of src registers for the given instruction/opcode.
1546 _mesa_num_inst_src_regs(enum prog_opcode opcode
)
1548 ASSERT(opcode
== InstInfo
[opcode
].Opcode
);
1549 return InstInfo
[opcode
].NumSrcRegs
;
1554 * Return string name for given program opcode.
1557 _mesa_opcode_string(enum prog_opcode opcode
)
1559 ASSERT(opcode
< MAX_OPCODE
);
1560 return InstInfo
[opcode
].Name
;
1564 * Return string name for given program/register file.
1567 program_file_string(enum register_file f
)
1570 case PROGRAM_TEMPORARY
:
1572 case PROGRAM_LOCAL_PARAM
:
1574 case PROGRAM_ENV_PARAM
:
1576 case PROGRAM_STATE_VAR
:
1580 case PROGRAM_OUTPUT
:
1582 case PROGRAM_NAMED_PARAM
:
1584 case PROGRAM_CONSTANT
:
1586 case PROGRAM_WRITE_ONLY
:
1587 return "WRITE_ONLY";
1588 case PROGRAM_ADDRESS
:
1597 * Return a string representation of the given swizzle word.
1598 * If extended is true, use extended (comma-separated) format.
1601 swizzle_string(GLuint swizzle
, GLuint negateBase
, GLboolean extended
)
1603 static const char swz
[] = "xyzw01";
1607 if (!extended
&& swizzle
== SWIZZLE_NOOP
&& negateBase
== 0)
1608 return ""; /* no swizzle/negation */
1613 if (negateBase
& 0x1)
1615 s
[i
++] = swz
[GET_SWZ(swizzle
, 0)];
1621 if (negateBase
& 0x2)
1623 s
[i
++] = swz
[GET_SWZ(swizzle
, 1)];
1629 if (negateBase
& 0x4)
1631 s
[i
++] = swz
[GET_SWZ(swizzle
, 2)];
1637 if (negateBase
& 0x8)
1639 s
[i
++] = swz
[GET_SWZ(swizzle
, 3)];
1647 writemask_string(GLuint writeMask
)
1652 if (writeMask
== WRITEMASK_XYZW
)
1656 if (writeMask
& WRITEMASK_X
)
1658 if (writeMask
& WRITEMASK_Y
)
1660 if (writeMask
& WRITEMASK_Z
)
1662 if (writeMask
& WRITEMASK_W
)
1670 print_dst_reg(const struct prog_dst_register
*dstReg
)
1672 _mesa_printf(" %s[%d]%s",
1673 program_file_string((enum register_file
) dstReg
->File
),
1675 writemask_string(dstReg
->WriteMask
));
1679 print_src_reg(const struct prog_src_register
*srcReg
)
1681 _mesa_printf("%s[%d]%s",
1682 program_file_string((enum register_file
) srcReg
->File
),
1684 swizzle_string(srcReg
->Swizzle
,
1685 srcReg
->NegateBase
, GL_FALSE
));
1689 _mesa_print_alu_instruction(const struct prog_instruction
*inst
,
1690 const char *opcode_string
,
1695 _mesa_printf("%s", opcode_string
);
1697 /* frag prog only */
1698 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1699 _mesa_printf("_SAT");
1701 if (inst
->DstReg
.File
!= PROGRAM_UNDEFINED
) {
1702 _mesa_printf(" %s[%d]%s",
1703 program_file_string((enum register_file
) inst
->DstReg
.File
),
1705 writemask_string(inst
->DstReg
.WriteMask
));
1711 for (j
= 0; j
< numRegs
; j
++) {
1712 print_src_reg(inst
->SrcReg
+ j
);
1713 if (j
+ 1 < numRegs
)
1717 _mesa_printf(";\n");
1722 * Print a single vertex/fragment program instruction.
1725 _mesa_print_instruction(const struct prog_instruction
*inst
)
1727 switch (inst
->Opcode
) {
1729 _mesa_printf("PRINT '%s'", inst
->Data
);
1730 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1732 _mesa_printf("%s[%d]%s",
1733 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1734 inst
->SrcReg
[0].Index
,
1735 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1736 inst
->SrcReg
[0].NegateBase
, GL_FALSE
));
1738 _mesa_printf(";\n");
1741 _mesa_printf("SWZ");
1742 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1743 _mesa_printf("_SAT");
1744 print_dst_reg(&inst
->DstReg
);
1745 _mesa_printf("%s[%d], %s;\n",
1746 program_file_string((enum register_file
) inst
->SrcReg
[0].File
),
1747 inst
->SrcReg
[0].Index
,
1748 swizzle_string(inst
->SrcReg
[0].Swizzle
,
1749 inst
->SrcReg
[0].NegateBase
, GL_TRUE
));
1754 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
1755 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1756 _mesa_printf("_SAT");
1758 print_dst_reg(&inst
->DstReg
);
1760 print_src_reg(&inst
->SrcReg
[0]);
1761 _mesa_printf(", texture[%d], ", inst
->TexSrcUnit
);
1762 switch (inst
->TexSrcTarget
) {
1763 case TEXTURE_1D_INDEX
: _mesa_printf("1D"); break;
1764 case TEXTURE_2D_INDEX
: _mesa_printf("2D"); break;
1765 case TEXTURE_3D_INDEX
: _mesa_printf("3D"); break;
1766 case TEXTURE_CUBE_INDEX
: _mesa_printf("CUBE"); break;
1767 case TEXTURE_RECT_INDEX
: _mesa_printf("RECT"); break;
1774 _mesa_printf("ARL addr.x, ");
1775 print_src_reg(&inst
->SrcReg
[0]);
1776 _mesa_printf(";\n");
1779 _mesa_printf("END;\n");
1781 /* XXX may need for other special-case instructions */
1783 /* typical alu instruction */
1784 _mesa_print_alu_instruction(inst
,
1785 _mesa_opcode_string(inst
->Opcode
),
1786 _mesa_num_inst_src_regs(inst
->Opcode
));
1793 * Print a vertx/fragment program to stdout.
1794 * XXX this function could be greatly improved.
1797 _mesa_print_program(const struct gl_program
*prog
)
1800 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1801 _mesa_printf("%3d: ", i
);
1802 _mesa_print_instruction(prog
->Instructions
+ i
);
1808 * Print all of a program's parameters.
1811 _mesa_print_program_parameters(GLcontext
*ctx
, const struct gl_program
*prog
)
1815 _mesa_printf("NumInstructions=%d\n", prog
->NumInstructions
);
1816 _mesa_printf("NumTemporaries=%d\n", prog
->NumTemporaries
);
1817 _mesa_printf("NumParameters=%d\n", prog
->NumParameters
);
1818 _mesa_printf("NumAttributes=%d\n", prog
->NumAttributes
);
1819 _mesa_printf("NumAddressRegs=%d\n", prog
->NumAddressRegs
);
1821 _mesa_load_state_parameters(ctx
, prog
->Parameters
);
1824 _mesa_printf("Local Params:\n");
1825 for (i
= 0; i
< MAX_PROGRAM_LOCAL_PARAMS
; i
++){
1826 const GLfloat
*p
= prog
->LocalParams
[i
];
1827 _mesa_printf("%2d: %f, %f, %f, %f\n", i
, p
[0], p
[1], p
[2], p
[3]);
1831 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++){
1832 struct gl_program_parameter
*param
= prog
->Parameters
->Parameters
+ i
;
1833 const GLfloat
*v
= prog
->Parameters
->ParameterValues
[i
];
1834 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1835 i
, param
->Name
, v
[0], v
[1], v
[2], v
[3]);
1841 * Mixing ARB and NV vertex/fragment programs can be tricky.
1842 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
1843 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
1844 * The two different fragment program targets are supposed to be compatible
1845 * to some extent (see GL_ARB_fragment_program spec).
1846 * This function does the compatibility check.
1849 compatible_program_targets(GLenum t1
, GLenum t2
)
1853 if (t1
== GL_FRAGMENT_PROGRAM_ARB
&& t2
== GL_FRAGMENT_PROGRAM_NV
)
1855 if (t1
== GL_FRAGMENT_PROGRAM_NV
&& t2
== GL_FRAGMENT_PROGRAM_ARB
)
1862 /**********************************************************************/
1864 /**********************************************************************/
1868 * Bind a program (make it current)
1869 * \note Called from the GL API dispatcher by both glBindProgramNV
1870 * and glBindProgramARB.
1873 _mesa_BindProgram(GLenum target
, GLuint id
)
1875 struct gl_program
*curProg
, *newProg
;
1876 GET_CURRENT_CONTEXT(ctx
);
1877 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1879 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
);
1881 /* Error-check target and get curProg */
1882 if ((target
== GL_VERTEX_PROGRAM_ARB
) && /* == GL_VERTEX_PROGRAM_NV */
1883 (ctx
->Extensions
.NV_vertex_program
||
1884 ctx
->Extensions
.ARB_vertex_program
)) {
1885 curProg
= &ctx
->VertexProgram
.Current
->Base
;
1887 else if ((target
== GL_FRAGMENT_PROGRAM_NV
1888 && ctx
->Extensions
.NV_fragment_program
) ||
1889 (target
== GL_FRAGMENT_PROGRAM_ARB
1890 && ctx
->Extensions
.ARB_fragment_program
)) {
1891 curProg
= &ctx
->FragmentProgram
.Current
->Base
;
1894 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindProgramNV/ARB(target)");
1899 * Get pointer to new program to bind.
1900 * NOTE: binding to a non-existant program is not an error.
1901 * That's supposed to be caught in glBegin.
1904 /* Bind a default program */
1906 if (target
== GL_VERTEX_PROGRAM_ARB
) /* == GL_VERTEX_PROGRAM_NV */
1907 newProg
= ctx
->Shared
->DefaultVertexProgram
;
1909 newProg
= ctx
->Shared
->DefaultFragmentProgram
;
1912 /* Bind a user program */
1913 newProg
= _mesa_lookup_program(ctx
, id
);
1914 if (!newProg
|| newProg
== &_mesa_DummyProgram
) {
1915 /* allocate a new program now */
1916 newProg
= ctx
->Driver
.NewProgram(ctx
, target
, id
);
1918 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindProgramNV/ARB");
1921 _mesa_HashInsert(ctx
->Shared
->Programs
, id
, newProg
);
1923 else if (!compatible_program_targets(newProg
->Target
, target
)) {
1924 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1925 "glBindProgramNV/ARB(target mismatch)");
1930 /** All error checking is complete now **/
1932 if (curProg
->Id
== id
) {
1933 /* binding same program - no change */
1937 /* unbind/delete oldProg */
1938 if (curProg
->Id
!= 0) {
1939 /* decrement refcount on previously bound fragment program */
1940 curProg
->RefCount
--;
1941 /* and delete if refcount goes below one */
1942 if (curProg
->RefCount
<= 0) {
1943 /* the program ID was already removed from the hash table */
1944 ctx
->Driver
.DeleteProgram(ctx
, curProg
);
1949 if (target
== GL_VERTEX_PROGRAM_ARB
) { /* == GL_VERTEX_PROGRAM_NV */
1950 if (ctx
->VertexProgram
._Current
== ctx
->VertexProgram
.Current
)
1951 ctx
->VertexProgram
._Current
= (struct gl_vertex_program
*) newProg
;
1952 ctx
->VertexProgram
.Current
= (struct gl_vertex_program
*) newProg
;
1954 else if (target
== GL_FRAGMENT_PROGRAM_NV
||
1955 target
== GL_FRAGMENT_PROGRAM_ARB
) {
1956 if (ctx
->FragmentProgram
._Current
== ctx
->FragmentProgram
.Current
)
1957 ctx
->FragmentProgram
._Current
= (struct gl_fragment_program
*) newProg
;
1958 ctx
->FragmentProgram
.Current
= (struct gl_fragment_program
*) newProg
;
1960 newProg
->RefCount
++;
1962 /* Never null pointers */
1963 ASSERT(ctx
->VertexProgram
.Current
);
1964 ASSERT(ctx
->FragmentProgram
.Current
);
1966 if (ctx
->Driver
.BindProgram
)
1967 ctx
->Driver
.BindProgram(ctx
, target
, newProg
);
1972 * Delete a list of programs.
1973 * \note Not compiled into display lists.
1974 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1977 _mesa_DeletePrograms(GLsizei n
, const GLuint
*ids
)
1980 GET_CURRENT_CONTEXT(ctx
);
1981 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1984 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDeleteProgramsNV" );
1988 for (i
= 0; i
< n
; i
++) {
1990 struct gl_program
*prog
= _mesa_lookup_program(ctx
, ids
[i
]);
1991 if (prog
== &_mesa_DummyProgram
) {
1992 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
1995 /* Unbind program if necessary */
1996 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| /* == GL_VERTEX_PROGRAM_NV */
1997 prog
->Target
== GL_VERTEX_STATE_PROGRAM_NV
) {
1998 if (ctx
->VertexProgram
.Current
&&
1999 ctx
->VertexProgram
.Current
->Base
.Id
== ids
[i
]) {
2000 /* unbind this currently bound program */
2001 _mesa_BindProgram(prog
->Target
, 0);
2004 else if (prog
->Target
== GL_FRAGMENT_PROGRAM_NV
||
2005 prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
2006 if (ctx
->FragmentProgram
.Current
&&
2007 ctx
->FragmentProgram
.Current
->Base
.Id
== ids
[i
]) {
2008 /* unbind this currently bound program */
2009 _mesa_BindProgram(prog
->Target
, 0);
2013 _mesa_problem(ctx
, "bad target in glDeleteProgramsNV");
2016 /* The ID is immediately available for re-use now */
2017 _mesa_HashRemove(ctx
->Shared
->Programs
, ids
[i
]);
2019 if (prog
->RefCount
<= 0) {
2020 ctx
->Driver
.DeleteProgram(ctx
, prog
);
2029 * Generate a list of new program identifiers.
2030 * \note Not compiled into display lists.
2031 * \note Called by both glGenProgramsNV and glGenProgramsARB.
2034 _mesa_GenPrograms(GLsizei n
, GLuint
*ids
)
2038 GET_CURRENT_CONTEXT(ctx
);
2039 ASSERT_OUTSIDE_BEGIN_END(ctx
);
2042 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPrograms");
2049 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->Programs
, n
);
2051 /* Insert pointer to dummy program as placeholder */
2052 for (i
= 0; i
< (GLuint
) n
; i
++) {
2053 _mesa_HashInsert(ctx
->Shared
->Programs
, first
+ i
, &_mesa_DummyProgram
);
2056 /* Return the program names */
2057 for (i
= 0; i
< (GLuint
) n
; i
++) {
2063 /**********************************************************************/
2064 /* GL_MESA_program_debug extension */
2065 /**********************************************************************/
2069 GLAPI
void GLAPIENTRY
2070 glProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
2073 _mesa_ProgramCallbackMESA(target
, callback
, data
);
2078 _mesa_ProgramCallbackMESA(GLenum target
, GLprogramcallbackMESA callback
,
2081 GET_CURRENT_CONTEXT(ctx
);
2084 case GL_FRAGMENT_PROGRAM_ARB
:
2085 if (!ctx
->Extensions
.ARB_fragment_program
) {
2086 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2089 ctx
->FragmentProgram
.Callback
= callback
;
2090 ctx
->FragmentProgram
.CallbackData
= data
;
2092 case GL_FRAGMENT_PROGRAM_NV
:
2093 if (!ctx
->Extensions
.NV_fragment_program
) {
2094 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2097 ctx
->FragmentProgram
.Callback
= callback
;
2098 ctx
->FragmentProgram
.CallbackData
= data
;
2100 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
2101 if (!ctx
->Extensions
.ARB_vertex_program
&&
2102 !ctx
->Extensions
.NV_vertex_program
) {
2103 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2106 ctx
->VertexProgram
.Callback
= callback
;
2107 ctx
->VertexProgram
.CallbackData
= data
;
2110 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProgramCallbackMESA(target)");
2117 GLAPI
void GLAPIENTRY
2118 glGetProgramRegisterfvMESA(GLenum target
,
2119 GLsizei len
, const GLubyte
*registerName
,
2122 _mesa_GetProgramRegisterfvMESA(target
, len
, registerName
, v
);
2127 _mesa_GetProgramRegisterfvMESA(GLenum target
,
2128 GLsizei len
, const GLubyte
*registerName
,
2132 GET_CURRENT_CONTEXT(ctx
);
2134 /* We _should_ be inside glBegin/glEnd */
2136 if (ctx
->Driver
.CurrentExecPrimitive
== PRIM_OUTSIDE_BEGIN_END
) {
2137 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetProgramRegisterfvMESA");
2142 /* make null-terminated copy of registerName */
2143 len
= MIN2((unsigned int) len
, sizeof(reg
) - 1);
2144 _mesa_memcpy(reg
, registerName
, len
);
2148 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
2149 if (!ctx
->Extensions
.ARB_vertex_program
&&
2150 !ctx
->Extensions
.NV_vertex_program
) {
2151 _mesa_error(ctx
, GL_INVALID_ENUM
,
2152 "glGetProgramRegisterfvMESA(target)");
2155 if (!ctx
->VertexProgram
._Enabled
) {
2156 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2157 "glGetProgramRegisterfvMESA");
2160 /* GL_NV_vertex_program */
2161 if (reg
[0] == 'R') {
2163 GLint i
= _mesa_atoi(reg
+ 1);
2164 if (i
>= (GLint
)ctx
->Const
.VertexProgram
.MaxTemps
) {
2165 _mesa_error(ctx
, GL_INVALID_VALUE
,
2166 "glGetProgramRegisterfvMESA(registerName)");
2170 ctx
->Driver
.GetVertexProgramRegister(ctx
, PROGRAM_TEMPORARY
, i
, v
);
2173 else if (reg
[0] == 'v' && reg
[1] == '[') {
2174 /* Vertex Input attribute */
2176 for (i
= 0; i
< ctx
->Const
.VertexProgram
.MaxAttribs
; i
++) {
2177 const char *name
= _mesa_nv_vertex_input_register_name(i
);
2179 _mesa_sprintf(number
, "%d", i
);
2180 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0 ||
2181 _mesa_strncmp(reg
+ 2, number
, _mesa_strlen(number
)) == 0) {
2183 ctx
->Driver
.GetVertexProgramRegister(ctx
, PROGRAM_INPUT
,
2189 _mesa_error(ctx
, GL_INVALID_VALUE
,
2190 "glGetProgramRegisterfvMESA(registerName)");
2193 else if (reg
[0] == 'o' && reg
[1] == '[') {
2194 /* Vertex output attribute */
2196 /* GL_ARB_vertex_program */
2197 else if (_mesa_strncmp(reg
, "vertex.", 7) == 0) {
2201 _mesa_error(ctx
, GL_INVALID_VALUE
,
2202 "glGetProgramRegisterfvMESA(registerName)");
2206 case GL_FRAGMENT_PROGRAM_ARB
:
2207 if (!ctx
->Extensions
.ARB_fragment_program
) {
2208 _mesa_error(ctx
, GL_INVALID_ENUM
,
2209 "glGetProgramRegisterfvMESA(target)");
2212 if (!ctx
->FragmentProgram
._Enabled
) {
2213 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2214 "glGetProgramRegisterfvMESA");
2219 case GL_FRAGMENT_PROGRAM_NV
:
2220 if (!ctx
->Extensions
.NV_fragment_program
) {
2221 _mesa_error(ctx
, GL_INVALID_ENUM
,
2222 "glGetProgramRegisterfvMESA(target)");
2225 if (!ctx
->FragmentProgram
._Enabled
) {
2226 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2227 "glGetProgramRegisterfvMESA");
2230 if (reg
[0] == 'R') {
2232 GLint i
= _mesa_atoi(reg
+ 1);
2233 if (i
>= (GLint
)ctx
->Const
.FragmentProgram
.MaxTemps
) {
2234 _mesa_error(ctx
, GL_INVALID_VALUE
,
2235 "glGetProgramRegisterfvMESA(registerName)");
2238 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_TEMPORARY
,
2241 else if (reg
[0] == 'f' && reg
[1] == '[') {
2242 /* Fragment input attribute */
2244 for (i
= 0; i
< ctx
->Const
.FragmentProgram
.MaxAttribs
; i
++) {
2245 const char *name
= _mesa_nv_fragment_input_register_name(i
);
2246 if (_mesa_strncmp(reg
+ 2, name
, 4) == 0) {
2247 ctx
->Driver
.GetFragmentProgramRegister(ctx
,
2248 PROGRAM_INPUT
, i
, v
);
2252 _mesa_error(ctx
, GL_INVALID_VALUE
,
2253 "glGetProgramRegisterfvMESA(registerName)");
2256 else if (_mesa_strcmp(reg
, "o[COLR]") == 0) {
2257 /* Fragment output color */
2258 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2259 FRAG_RESULT_COLR
, v
);
2261 else if (_mesa_strcmp(reg
, "o[COLH]") == 0) {
2262 /* Fragment output color */
2263 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2264 FRAG_RESULT_COLH
, v
);
2266 else if (_mesa_strcmp(reg
, "o[DEPR]") == 0) {
2267 /* Fragment output depth */
2268 ctx
->Driver
.GetFragmentProgramRegister(ctx
, PROGRAM_OUTPUT
,
2269 FRAG_RESULT_DEPR
, v
);
2272 /* try user-defined identifiers */
2273 const GLfloat
*value
= _mesa_lookup_parameter_value(
2274 ctx
->FragmentProgram
.Current
->Base
.Parameters
, -1, reg
);
2279 _mesa_error(ctx
, GL_INVALID_VALUE
,
2280 "glGetProgramRegisterfvMESA(registerName)");
2286 _mesa_error(ctx
, GL_INVALID_ENUM
,
2287 "glGetProgramRegisterfvMESA(target)");