fixes for C++ warnings/errors
[mesa.git] / src / mesa / shader / program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /**
26 * \file program.c
27 * Vertex and fragment program support functions.
28 * \author Brian Paul
29 */
30
31
32 #include "glheader.h"
33 #include "context.h"
34 #include "hash.h"
35 #include "imports.h"
36 #include "macros.h"
37 #include "mtypes.h"
38 #include "program.h"
39 #include "nvfragparse.h"
40 #include "program_instruction.h"
41 #include "nvvertparse.h"
42 #include "atifragshader.h"
43
44
45 static const char *
46 make_state_string(const GLint stateTokens[6]);
47
48 static GLbitfield
49 make_state_flags(const GLint state[]);
50
51
52 /**********************************************************************/
53 /* Utility functions */
54 /**********************************************************************/
55
56
57 /* A pointer to this dummy program is put into the hash table when
58 * glGenPrograms is called.
59 */
60 struct gl_program _mesa_DummyProgram;
61
62
63 /**
64 * Init context's vertex/fragment program state
65 */
66 void
67 _mesa_init_program(GLcontext *ctx)
68 {
69 GLuint i;
70
71 ctx->Program.ErrorPos = -1;
72 ctx->Program.ErrorString = _mesa_strdup("");
73
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;
84 }
85 #endif
86
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++;
92 #endif
93
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++;
100 #endif
101 }
102
103
104 /**
105 * Free a context's vertex/fragment program state
106 */
107 void
108 _mesa_free_program_data(GLcontext *ctx)
109 {
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));
115 }
116 #endif
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));
122 }
123 #endif
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);
130 }
131 }
132 #endif
133 _mesa_free((void *) ctx->Program.ErrorString);
134 }
135
136
137
138
139 /**
140 * Set the vertex/fragment program error state (position and error string).
141 * This is generally called from within the parsers.
142 */
143 void
144 _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
145 {
146 ctx->Program.ErrorPos = pos;
147 _mesa_free((void *) ctx->Program.ErrorString);
148 if (!string)
149 string = "";
150 ctx->Program.ErrorString = _mesa_strdup(string);
151 }
152
153
154 /**
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
157 * _mesa_free().
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'.
163 */
164 const GLubyte *
165 _mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
166 GLint *line, GLint *col)
167 {
168 const GLubyte *lineStart = string;
169 const GLubyte *p = string;
170 GLubyte *s;
171 int len;
172
173 *line = 1;
174
175 while (p != pos) {
176 if (*p == (GLubyte) '\n') {
177 (*line)++;
178 lineStart = p + 1;
179 }
180 p++;
181 }
182
183 *col = (pos - lineStart) + 1;
184
185 /* return copy of this line */
186 while (*p != 0 && *p != '\n')
187 p++;
188 len = p - lineStart;
189 s = (GLubyte *) _mesa_malloc(len + 1);
190 _mesa_memcpy(s, lineStart, len);
191 s[len] = 0;
192
193 return s;
194 }
195
196
197 /**
198 * Initialize a new vertex/fragment program object.
199 */
200 static struct gl_program *
201 _mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
202 GLenum target, GLuint id)
203 {
204 (void) ctx;
205 if (prog) {
206 prog->Id = id;
207 prog->Target = target;
208 prog->Resident = GL_TRUE;
209 prog->RefCount = 1;
210 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
211 }
212
213 return prog;
214 }
215
216
217 /**
218 * Initialize a new fragment program object.
219 */
220 struct gl_program *
221 _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
222 GLenum target, GLuint id)
223 {
224 if (prog)
225 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
226 else
227 return NULL;
228 }
229
230
231 /**
232 * Initialize a new vertex program object.
233 */
234 struct gl_program *
235 _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
236 GLenum target, GLuint id)
237 {
238 if (prog)
239 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
240 else
241 return NULL;
242 }
243
244
245 /**
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.
251 *
252 * \param ctx context
253 * \param id program id/number
254 * \param target program target/type
255 * \return pointer to new program object
256 */
257 struct gl_program *
258 _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
259 {
260 switch (target) {
261 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
262 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
263 target, id );
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),
268 target, id );
269 default:
270 _mesa_problem(ctx, "bad target in _mesa_new_program");
271 return NULL;
272 }
273 }
274
275
276 /**
277 * Delete a program and remove it from the hash table, ignoring the
278 * reference count.
279 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
280 * by a device driver function.
281 */
282 void
283 _mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
284 {
285 (void) ctx;
286 ASSERT(prog);
287
288 if (prog == &_mesa_DummyProgram)
289 return;
290
291 if (prog->String)
292 _mesa_free(prog->String);
293
294 if (prog->Instructions) {
295 GLuint i;
296 for (i = 0; i < prog->NumInstructions; i++) {
297 if (prog->Instructions[i].Data)
298 _mesa_free(prog->Instructions[i].Data);
299 }
300 _mesa_free(prog->Instructions);
301 }
302
303 if (prog->Parameters) {
304 _mesa_free_parameter_list(prog->Parameters);
305 }
306
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;
310 if (vprog->TnlData)
311 _mesa_free(vprog->TnlData);
312 }
313
314 _mesa_free(prog);
315 }
316
317
318 /**
319 * Return the gl_program object for a given ID.
320 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
321 * casts elsewhere.
322 */
323 struct gl_program *
324 _mesa_lookup_program(GLcontext *ctx, GLuint id)
325 {
326 if (id)
327 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
328 else
329 return NULL;
330 }
331
332
333 /**********************************************************************/
334 /* Program parameter functions */
335 /**********************************************************************/
336
337 struct gl_program_parameter_list *
338 _mesa_new_parameter_list(void)
339 {
340 return (struct gl_program_parameter_list *)
341 _mesa_calloc(sizeof(struct gl_program_parameter_list));
342 }
343
344
345 /**
346 * Free a parameter list and all its parameters
347 */
348 void
349 _mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
350 {
351 GLuint i;
352 for (i = 0; i < paramList->NumParameters; i++) {
353 if (paramList->Parameters[i].Name)
354 _mesa_free((void *) paramList->Parameters[i].Name);
355 }
356 _mesa_free(paramList->Parameters);
357 if (paramList->ParameterValues)
358 _mesa_align_free(paramList->ParameterValues);
359 _mesa_free(paramList);
360 }
361
362
363 /**
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)
370 */
371 static GLint
372 add_parameter(struct gl_program_parameter_list *paramList,
373 const char *name, const GLfloat values[4],
374 enum register_file type)
375 {
376 const GLuint n = paramList->NumParameters;
377
378 if (n == paramList->Size) {
379 /* Need to grow the parameter list array */
380 if (paramList->Size == 0)
381 paramList->Size = 8;
382 else
383 paramList->Size *= 2;
384
385 /* realloc arrays */
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));
390
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 */
395 16);
396 }
397
398 if (!paramList->Parameters ||
399 !paramList->ParameterValues) {
400 /* out of memory */
401 paramList->NumParameters = 0;
402 paramList->Size = 0;
403 return -1;
404 }
405 else {
406 paramList->NumParameters = n + 1;
407
408 _mesa_memset(&paramList->Parameters[n], 0,
409 sizeof(struct gl_program_parameter));
410
411 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
412 paramList->Parameters[n].Type = type;
413 if (values)
414 COPY_4V(paramList->ParameterValues[n], values);
415 return (GLint) n;
416 }
417 }
418
419
420 /**
421 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
422 * \return index of the new entry in the parameter list
423 */
424 GLint
425 _mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
426 const char *name, const GLfloat values[4])
427 {
428 return add_parameter(paramList, name, values, PROGRAM_NAMED_PARAM);
429 }
430
431
432 /**
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 };
436 *
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
441 */
442 GLint
443 _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
444 const char *name, const GLfloat values[4],
445 GLuint size)
446 {
447 #if 0 /* disable this for now -- we need to save the name! */
448 GLuint pos, swizzle;
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)) {
452 return pos;
453 }
454 #endif
455 return add_parameter(paramList, name, values, PROGRAM_CONSTANT);
456 }
457
458
459 /**
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 };
463 *
464 * \param paramList the parameter list
465 * \param values four float values
466 * \return index/position of the new parameter in the parameter list.
467 */
468 GLint
469 _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
470 const GLfloat values[4], GLuint size)
471 {
472 GLuint pos, swizzle;
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)) {
476 return pos;
477 }
478 return add_parameter(paramList, NULL, values, PROGRAM_CONSTANT);
479 }
480
481
482 /**
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;
486 *
487 * \param paramList the parameter list
488 * \param state an array of 6 state tokens
489 * \return index of the new parameter.
490 */
491 GLint
492 _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
493 const GLint *stateTokens)
494 {
495 /* XXX we should probably search the current parameter list to see if
496 * the new state reference is already present.
497 */
498 GLint index;
499 const char *name = make_state_string(stateTokens);
500
501 index = add_parameter(paramList, name, NULL, PROGRAM_STATE_VAR);
502 if (index >= 0) {
503 GLuint i;
504 for (i = 0; i < 6; i++) {
505 paramList->Parameters[index].StateIndexes[i]
506 = (enum state_index) stateTokens[i];
507 }
508 paramList->StateFlags |= make_state_flags(stateTokens);
509 }
510
511 /* free name string here since we duplicated it in add_parameter() */
512 _mesa_free((void *) name);
513
514 return index;
515 }
516
517
518 /**
519 * Lookup a parameter value by name in the given parameter list.
520 * \return pointer to the float[4] values.
521 */
522 GLfloat *
523 _mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
524 GLsizei nameLen, const char *name)
525 {
526 GLuint i;
527
528 if (!paramList)
529 return NULL;
530
531 if (nameLen == -1) {
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];
537 }
538 }
539 else {
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];
546 }
547 }
548 return NULL;
549 }
550
551
552 /**
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.
559 */
560 GLint
561 _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
562 GLsizei nameLen, const char *name)
563 {
564 GLint i;
565
566 if (!paramList)
567 return -1;
568
569 if (nameLen == -1) {
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)
574 return i;
575 }
576 }
577 else {
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)
583 return i;
584 }
585 }
586 return -1;
587 }
588
589
590 /**
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
600 */
601 GLboolean
602 _mesa_lookup_parameter_constant(const struct gl_program_parameter_list *paramList,
603 const GLfloat v[], GLsizei vSize,
604 GLuint *posOut, GLuint *swizzleOut)
605 {
606 GLuint i;
607
608 assert(vSize >= 1);
609 assert(vSize <= 4);
610
611 if (!paramList)
612 return -1;
613
614 for (i = 0; i < paramList->NumParameters; i++) {
615 if (paramList->Parameters[i].Type == PROGRAM_CONSTANT) {
616 const GLint maxShift = 4 - vSize;
617 GLint shift, j;
618 for (shift = 0; shift <= maxShift; shift++) {
619 GLint matched = 0;
620 GLuint swizzle[4];
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]) {
626 matched++;
627 swizzle[j] = shift + j;
628 }
629 }
630 if (matched == vSize) {
631 /* found! */
632 *posOut = i;
633 *swizzleOut = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
634 swizzle[2], swizzle[3]);
635 return GL_TRUE;
636 }
637 }
638 }
639 }
640
641 return GL_FALSE;
642 }
643
644
645 /**
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.
651 */
652 static void
653 _mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
654 GLfloat *value)
655 {
656 switch (state[0]) {
657 case STATE_MATERIAL:
658 {
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().
667 */
668 /* state[2] is the material attribute */
669 switch (state[2]) {
670 case STATE_AMBIENT:
671 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_AMBIENT + face]);
672 return;
673 case STATE_DIFFUSE:
674 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_DIFFUSE + face]);
675 return;
676 case STATE_SPECULAR:
677 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_SPECULAR + face]);
678 return;
679 case STATE_EMISSION:
680 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_EMISSION + face]);
681 return;
682 case STATE_SHININESS:
683 value[0] = mat->Attrib[MAT_ATTRIB_FRONT_SHININESS + face][0];
684 value[1] = 0.0F;
685 value[2] = 0.0F;
686 value[3] = 1.0F;
687 return;
688 default:
689 _mesa_problem(ctx, "Invalid material state in fetch_state");
690 return;
691 }
692 }
693 case STATE_LIGHT:
694 {
695 /* state[1] is the light number */
696 const GLuint ln = (GLuint) state[1];
697 /* state[2] is the light attribute */
698 switch (state[2]) {
699 case STATE_AMBIENT:
700 COPY_4V(value, ctx->Light.Light[ln].Ambient);
701 return;
702 case STATE_DIFFUSE:
703 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
704 return;
705 case STATE_SPECULAR:
706 COPY_4V(value, ctx->Light.Light[ln].Specular);
707 return;
708 case STATE_POSITION:
709 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
710 return;
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;
716 return;
717 case STATE_SPOT_DIRECTION:
718 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
719 value[3] = ctx->Light.Light[ln]._CosCutoff;
720 return;
721 case STATE_HALF:
722 {
723 GLfloat eye_z[] = {0, 0, 1};
724
725 /* Compute infinite half angle vector:
726 * half-vector = light_position + (0, 0, 1)
727 * and then normalize. w = 0
728 *
729 * light.EyePosition.w should be 0 for infinite lights.
730 */
731 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
732 NORMALIZE_3FV(value);
733 value[3] = 0;
734 }
735 return;
736 case STATE_POSITION_NORMALIZED:
737 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
738 NORMALIZE_3FV( value );
739 return;
740 default:
741 _mesa_problem(ctx, "Invalid light state in fetch_state");
742 return;
743 }
744 }
745 case STATE_LIGHTMODEL_AMBIENT:
746 COPY_4V(value, ctx->Light.Model.Ambient);
747 return;
748 case STATE_LIGHTMODEL_SCENECOLOR:
749 if (state[1] == 0) {
750 /* front */
751 GLint i;
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];
756 }
757 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
758 }
759 else {
760 /* back */
761 GLint i;
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];
766 }
767 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
768 }
769 return;
770 case STATE_LIGHTPROD:
771 {
772 const GLuint ln = (GLuint) state[1];
773 const GLuint face = (GLuint) state[2];
774 GLint i;
775 ASSERT(face == 0 || face == 1);
776 switch (state[3]) {
777 case STATE_AMBIENT:
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];
781 }
782 /* [3] = material alpha */
783 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
784 return;
785 case STATE_DIFFUSE:
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];
789 }
790 /* [3] = material alpha */
791 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
792 return;
793 case STATE_SPECULAR:
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];
797 }
798 /* [3] = material alpha */
799 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
800 return;
801 default:
802 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
803 return;
804 }
805 }
806 case STATE_TEXGEN:
807 {
808 /* state[1] is the texture unit */
809 const GLuint unit = (GLuint) state[1];
810 /* state[2] is the texgen attribute */
811 switch (state[2]) {
812 case STATE_TEXGEN_EYE_S:
813 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
814 return;
815 case STATE_TEXGEN_EYE_T:
816 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
817 return;
818 case STATE_TEXGEN_EYE_R:
819 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
820 return;
821 case STATE_TEXGEN_EYE_Q:
822 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
823 return;
824 case STATE_TEXGEN_OBJECT_S:
825 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
826 return;
827 case STATE_TEXGEN_OBJECT_T:
828 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
829 return;
830 case STATE_TEXGEN_OBJECT_R:
831 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
832 return;
833 case STATE_TEXGEN_OBJECT_Q:
834 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
835 return;
836 default:
837 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
838 return;
839 }
840 }
841 case STATE_TEXENV_COLOR:
842 {
843 /* state[1] is the texture unit */
844 const GLuint unit = (GLuint) state[1];
845 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
846 }
847 return;
848 case STATE_FOG_COLOR:
849 COPY_4V(value, ctx->Fog.Color);
850 return;
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);
856 return;
857 case STATE_CLIPPLANE:
858 {
859 const GLuint plane = (GLuint) state[1];
860 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
861 }
862 return;
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;
868 return;
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];
873 value[3] = 1.0F;
874 return;
875 case STATE_MATRIX:
876 {
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 */
882
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];
889 const GLfloat *m;
890 GLuint row, i;
891 if (mat == STATE_MODELVIEW) {
892 matrix = ctx->ModelviewMatrixStack.Top;
893 }
894 else if (mat == STATE_PROJECTION) {
895 matrix = ctx->ProjectionMatrixStack.Top;
896 }
897 else if (mat == STATE_MVP) {
898 matrix = &ctx->_ModelProjectMatrix;
899 }
900 else if (mat == STATE_TEXTURE) {
901 matrix = ctx->TextureMatrixStack[index].Top;
902 }
903 else if (mat == STATE_PROGRAM) {
904 matrix = ctx->ProgramMatrixStack[index].Top;
905 }
906 else {
907 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
908 return;
909 }
910 if (modifier == STATE_MATRIX_INVERSE ||
911 modifier == STATE_MATRIX_INVTRANS) {
912 /* Be sure inverse is up to date:
913 */
914 _math_matrix_alloc_inv( (GLmatrix *) matrix );
915 _math_matrix_analyse( (GLmatrix*) matrix );
916 m = matrix->inv;
917 }
918 else {
919 m = matrix->m;
920 }
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];
928 }
929 }
930 else {
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];
936 }
937 }
938 }
939 return;
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 */
944 value[3] = 0;
945 return;
946 case STATE_FRAGMENT_PROGRAM:
947 {
948 /* state[1] = {STATE_ENV, STATE_LOCAL} */
949 /* state[2] = parameter index */
950 const int idx = (int) state[2];
951 switch (state[1]) {
952 case STATE_ENV:
953 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
954 break;
955 case STATE_LOCAL:
956 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
957 break;
958 default:
959 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
960 return;
961 }
962 }
963 return;
964
965 case STATE_VERTEX_PROGRAM:
966 {
967 /* state[1] = {STATE_ENV, STATE_LOCAL} */
968 /* state[2] = parameter index */
969 const int idx = (int) state[2];
970 switch (state[1]) {
971 case STATE_ENV:
972 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
973 break;
974 case STATE_LOCAL:
975 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
976 break;
977 default:
978 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
979 return;
980 }
981 }
982 return;
983
984 case STATE_INTERNAL:
985 {
986 switch (state[1]) {
987 case STATE_NORMAL_SCALE:
988 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
989 break;
990 case STATE_TEXRECT_SCALE: {
991 const int unit = (int) state[2];
992 const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
993 if (texObj) {
994 struct gl_texture_image *texImage = texObj->Image[0][0];
995 ASSIGN_4V(value, 1.0 / texImage->Width, 1.0 / texImage->Height, 0, 1);
996 }
997 break;
998 }
999 default:
1000 /* unknown state indexes are silently ignored
1001 * should be handled by the driver.
1002 */
1003 return;
1004 }
1005 }
1006 return;
1007
1008 default:
1009 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
1010 return;
1011 }
1012 }
1013
1014
1015 /**
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.
1021 */
1022 static GLbitfield
1023 make_state_flags(const GLint state[])
1024 {
1025 switch (state[0]) {
1026 case STATE_MATERIAL:
1027 case STATE_LIGHT:
1028 case STATE_LIGHTMODEL_AMBIENT:
1029 case STATE_LIGHTMODEL_SCENECOLOR:
1030 case STATE_LIGHTPROD:
1031 return _NEW_LIGHT;
1032
1033 case STATE_TEXGEN:
1034 case STATE_TEXENV_COLOR:
1035 return _NEW_TEXTURE;
1036
1037 case STATE_FOG_COLOR:
1038 case STATE_FOG_PARAMS:
1039 return _NEW_FOG;
1040
1041 case STATE_CLIPPLANE:
1042 return _NEW_TRANSFORM;
1043
1044 case STATE_POINT_SIZE:
1045 case STATE_POINT_ATTENUATION:
1046 return _NEW_POINT;
1047
1048 case STATE_MATRIX:
1049 switch (state[1]) {
1050 case STATE_MODELVIEW:
1051 return _NEW_MODELVIEW;
1052 case STATE_PROJECTION:
1053 return _NEW_PROJECTION;
1054 case STATE_MVP:
1055 return _NEW_MODELVIEW | _NEW_PROJECTION;
1056 case STATE_TEXTURE:
1057 return _NEW_TEXTURE_MATRIX;
1058 case STATE_PROGRAM:
1059 return _NEW_TRACK_MATRIX;
1060 default:
1061 _mesa_problem(NULL, "unexpected matrix in make_state_flags()");
1062 return 0;
1063 }
1064
1065 case STATE_DEPTH_RANGE:
1066 return _NEW_VIEWPORT;
1067
1068 case STATE_FRAGMENT_PROGRAM:
1069 case STATE_VERTEX_PROGRAM:
1070 return _NEW_PROGRAM;
1071
1072 case STATE_INTERNAL:
1073 switch (state[1]) {
1074 case STATE_NORMAL_SCALE:
1075 return _NEW_MODELVIEW;
1076 case STATE_TEXRECT_SCALE:
1077 return _NEW_TEXTURE;
1078 default:
1079 /* unknown state indexes are silently ignored and
1080 * no flag set, since it is handled by the driver.
1081 */
1082 return 0;
1083 }
1084
1085 default:
1086 _mesa_problem(NULL, "unexpected state[0] in make_state_flags()");
1087 return 0;
1088 }
1089 }
1090
1091
1092 static void
1093 append(char *dst, const char *src)
1094 {
1095 while (*dst)
1096 dst++;
1097 while (*src)
1098 *dst++ = *src++;
1099 *dst = 0;
1100 }
1101
1102
1103 static void
1104 append_token(char *dst, enum state_index k)
1105 {
1106 switch (k) {
1107 case STATE_MATERIAL:
1108 append(dst, "material.");
1109 break;
1110 case STATE_LIGHT:
1111 append(dst, "light");
1112 break;
1113 case STATE_LIGHTMODEL_AMBIENT:
1114 append(dst, "lightmodel.ambient");
1115 break;
1116 case STATE_LIGHTMODEL_SCENECOLOR:
1117 break;
1118 case STATE_LIGHTPROD:
1119 append(dst, "lightprod");
1120 break;
1121 case STATE_TEXGEN:
1122 append(dst, "texgen");
1123 break;
1124 case STATE_FOG_COLOR:
1125 append(dst, "fog.color");
1126 break;
1127 case STATE_FOG_PARAMS:
1128 append(dst, "fog.params");
1129 break;
1130 case STATE_CLIPPLANE:
1131 append(dst, "clip");
1132 break;
1133 case STATE_POINT_SIZE:
1134 append(dst, "point.size");
1135 break;
1136 case STATE_POINT_ATTENUATION:
1137 append(dst, "point.attenuation");
1138 break;
1139 case STATE_MATRIX:
1140 append(dst, "matrix.");
1141 break;
1142 case STATE_MODELVIEW:
1143 append(dst, "modelview");
1144 break;
1145 case STATE_PROJECTION:
1146 append(dst, "projection");
1147 break;
1148 case STATE_MVP:
1149 append(dst, "mvp");
1150 break;
1151 case STATE_TEXTURE:
1152 append(dst, "texture");
1153 break;
1154 case STATE_PROGRAM:
1155 append(dst, "program");
1156 break;
1157 case STATE_MATRIX_INVERSE:
1158 append(dst, ".inverse");
1159 break;
1160 case STATE_MATRIX_TRANSPOSE:
1161 append(dst, ".transpose");
1162 break;
1163 case STATE_MATRIX_INVTRANS:
1164 append(dst, ".invtrans");
1165 break;
1166 case STATE_AMBIENT:
1167 append(dst, "ambient");
1168 break;
1169 case STATE_DIFFUSE:
1170 append(dst, "diffuse");
1171 break;
1172 case STATE_SPECULAR:
1173 append(dst, "specular");
1174 break;
1175 case STATE_EMISSION:
1176 append(dst, "emission");
1177 break;
1178 case STATE_SHININESS:
1179 append(dst, "shininess");
1180 break;
1181 case STATE_HALF:
1182 append(dst, "half");
1183 break;
1184 case STATE_POSITION:
1185 append(dst, ".position");
1186 break;
1187 case STATE_ATTENUATION:
1188 append(dst, ".attenuation");
1189 break;
1190 case STATE_SPOT_DIRECTION:
1191 append(dst, ".spot.direction");
1192 break;
1193 case STATE_TEXGEN_EYE_S:
1194 append(dst, "eye.s");
1195 break;
1196 case STATE_TEXGEN_EYE_T:
1197 append(dst, "eye.t");
1198 break;
1199 case STATE_TEXGEN_EYE_R:
1200 append(dst, "eye.r");
1201 break;
1202 case STATE_TEXGEN_EYE_Q:
1203 append(dst, "eye.q");
1204 break;
1205 case STATE_TEXGEN_OBJECT_S:
1206 append(dst, "object.s");
1207 break;
1208 case STATE_TEXGEN_OBJECT_T:
1209 append(dst, "object.t");
1210 break;
1211 case STATE_TEXGEN_OBJECT_R:
1212 append(dst, "object.r");
1213 break;
1214 case STATE_TEXGEN_OBJECT_Q:
1215 append(dst, "object.q");
1216 break;
1217 case STATE_TEXENV_COLOR:
1218 append(dst, "texenv");
1219 break;
1220 case STATE_DEPTH_RANGE:
1221 append(dst, "depth.range");
1222 break;
1223 case STATE_VERTEX_PROGRAM:
1224 case STATE_FRAGMENT_PROGRAM:
1225 break;
1226 case STATE_ENV:
1227 append(dst, "env");
1228 break;
1229 case STATE_LOCAL:
1230 append(dst, "local");
1231 break;
1232 case STATE_INTERNAL:
1233 case STATE_NORMAL_SCALE:
1234 case STATE_POSITION_NORMALIZED:
1235 append(dst, "(internal)");
1236 break;
1237 default:
1238 ;
1239 }
1240 }
1241
1242 static void
1243 append_face(char *dst, GLint face)
1244 {
1245 if (face == 0)
1246 append(dst, "front.");
1247 else
1248 append(dst, "back.");
1249 }
1250
1251 static void
1252 append_index(char *dst, GLint index)
1253 {
1254 char s[20];
1255 _mesa_sprintf(s, "[%d].", index);
1256 append(dst, s);
1257 }
1258
1259 /**
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.
1263 */
1264 static const char *
1265 make_state_string(const GLint state[6])
1266 {
1267 char str[1000] = "";
1268 char tmp[30];
1269
1270 append(str, "state.");
1271 append_token(str, (enum state_index) state[0]);
1272
1273 switch (state[0]) {
1274 case STATE_MATERIAL:
1275 append_face(str, state[1]);
1276 append_token(str, (enum state_index) state[2]);
1277 break;
1278 case STATE_LIGHT:
1279 append(str, "light");
1280 append_index(str, state[1]); /* light number [i]. */
1281 append_token(str, (enum state_index) state[2]); /* coefficients */
1282 break;
1283 case STATE_LIGHTMODEL_AMBIENT:
1284 append(str, "lightmodel.ambient");
1285 break;
1286 case STATE_LIGHTMODEL_SCENECOLOR:
1287 if (state[1] == 0) {
1288 append(str, "lightmodel.front.scenecolor");
1289 }
1290 else {
1291 append(str, "lightmodel.back.scenecolor");
1292 }
1293 break;
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]);
1298 break;
1299 case STATE_TEXGEN:
1300 append_index(str, state[1]); /* tex unit [i] */
1301 append_token(str, (enum state_index) state[2]); /* plane coef */
1302 break;
1303 case STATE_TEXENV_COLOR:
1304 append_index(str, state[1]); /* tex unit [i] */
1305 append(str, "color");
1306 break;
1307 case STATE_FOG_COLOR:
1308 case STATE_FOG_PARAMS:
1309 break;
1310 case STATE_CLIPPLANE:
1311 append_index(str, state[1]); /* plane [i] */
1312 append(str, "plane");
1313 break;
1314 case STATE_POINT_SIZE:
1315 case STATE_POINT_ATTENUATION:
1316 break;
1317 case STATE_MATRIX:
1318 {
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);
1330 if (index)
1331 append_index(str, index);
1332 if (modifier)
1333 append_token(str, modifier);
1334 if (first == last)
1335 _mesa_sprintf(tmp, ".row[%d]", first);
1336 else
1337 _mesa_sprintf(tmp, ".row[%d..%d]", first, last);
1338 append(str, tmp);
1339 }
1340 break;
1341 case STATE_DEPTH_RANGE:
1342 break;
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]);
1349 break;
1350 case STATE_INTERNAL:
1351 break;
1352 default:
1353 _mesa_problem(NULL, "Invalid state in make_state_string");
1354 break;
1355 }
1356
1357 return _mesa_strdup(str);
1358 }
1359
1360
1361 /**
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.
1366 */
1367 void
1368 _mesa_load_state_parameters(GLcontext *ctx,
1369 struct gl_program_parameter_list *paramList)
1370 {
1371 GLuint i;
1372
1373 if (!paramList)
1374 return;
1375
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]);
1381 }
1382 }
1383 }
1384
1385
1386 /**
1387 * Initialize program instruction fields to defaults.
1388 * \param inst first instruction to initialize
1389 * \param count number of instructions to initialize
1390 */
1391 void
1392 _mesa_init_instructions(struct prog_instruction *inst, GLuint count)
1393 {
1394 GLuint i;
1395
1396 _mesa_bzero(inst, count * sizeof(struct prog_instruction));
1397
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;
1405
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;
1410
1411 inst[i].SaturateMode = SATURATE_OFF;
1412 inst[i].Precision = FLOAT32;
1413 }
1414 }
1415
1416
1417 /**
1418 * Allocate an array of program instructions.
1419 * \param numInst number of instructions
1420 * \return pointer to instruction memory
1421 */
1422 struct prog_instruction *
1423 _mesa_alloc_instructions(GLuint numInst)
1424 {
1425 return (struct prog_instruction *)
1426 _mesa_calloc(numInst * sizeof(struct prog_instruction));
1427 }
1428
1429
1430 /**
1431 * Reallocate memory storing an array of program instructions.
1432 * This is used when we need to append additional instructions onto an
1433 * program.
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.
1438 */
1439 struct prog_instruction *
1440 _mesa_realloc_instructions(struct prog_instruction *oldInst,
1441 GLuint numOldInst, GLuint numNewInst)
1442 {
1443 struct prog_instruction *newInst;
1444
1445 newInst = (struct prog_instruction *)
1446 _mesa_realloc(oldInst,
1447 numOldInst * sizeof(struct prog_instruction),
1448 numNewInst * sizeof(struct prog_instruction));
1449
1450 return newInst;
1451 }
1452
1453
1454 /**
1455 * Basic info about each instruction
1456 */
1457 struct instruction_info
1458 {
1459 enum prog_opcode Opcode;
1460 const char *Name;
1461 GLuint NumSrcRegs;
1462 };
1463
1464 /**
1465 * Instruction info
1466 * \note Opcode should equal array index!
1467 */
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 }
1539 };
1540
1541
1542 /**
1543 * Return the number of src registers for the given instruction/opcode.
1544 */
1545 GLuint
1546 _mesa_num_inst_src_regs(enum prog_opcode opcode)
1547 {
1548 ASSERT(opcode == InstInfo[opcode].Opcode);
1549 return InstInfo[opcode].NumSrcRegs;
1550 }
1551
1552
1553 /**
1554 * Return string name for given program opcode.
1555 */
1556 const char *
1557 _mesa_opcode_string(enum prog_opcode opcode)
1558 {
1559 ASSERT(opcode < MAX_OPCODE);
1560 return InstInfo[opcode].Name;
1561 }
1562
1563 /**
1564 * Return string name for given program/register file.
1565 */
1566 static const char *
1567 program_file_string(enum register_file f)
1568 {
1569 switch (f) {
1570 case PROGRAM_TEMPORARY:
1571 return "TEMP";
1572 case PROGRAM_LOCAL_PARAM:
1573 return "LOCAL";
1574 case PROGRAM_ENV_PARAM:
1575 return "ENV";
1576 case PROGRAM_STATE_VAR:
1577 return "STATE";
1578 case PROGRAM_INPUT:
1579 return "INPUT";
1580 case PROGRAM_OUTPUT:
1581 return "OUTPUT";
1582 case PROGRAM_NAMED_PARAM:
1583 return "NAMED";
1584 case PROGRAM_CONSTANT:
1585 return "CONST";
1586 case PROGRAM_WRITE_ONLY:
1587 return "WRITE_ONLY";
1588 case PROGRAM_ADDRESS:
1589 return "ADDR";
1590 default:
1591 return "!unkown!";
1592 }
1593 }
1594
1595
1596 /**
1597 * Return a string representation of the given swizzle word.
1598 * If extended is true, use extended (comma-separated) format.
1599 */
1600 static const char *
1601 swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
1602 {
1603 static const char swz[] = "xyzw01";
1604 static char s[20];
1605 GLuint i = 0;
1606
1607 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
1608 return ""; /* no swizzle/negation */
1609
1610 if (!extended)
1611 s[i++] = '.';
1612
1613 if (negateBase & 0x1)
1614 s[i++] = '-';
1615 s[i++] = swz[GET_SWZ(swizzle, 0)];
1616
1617 if (extended) {
1618 s[i++] = ',';
1619 }
1620
1621 if (negateBase & 0x2)
1622 s[i++] = '-';
1623 s[i++] = swz[GET_SWZ(swizzle, 1)];
1624
1625 if (extended) {
1626 s[i++] = ',';
1627 }
1628
1629 if (negateBase & 0x4)
1630 s[i++] = '-';
1631 s[i++] = swz[GET_SWZ(swizzle, 2)];
1632
1633 if (extended) {
1634 s[i++] = ',';
1635 }
1636
1637 if (negateBase & 0x8)
1638 s[i++] = '-';
1639 s[i++] = swz[GET_SWZ(swizzle, 3)];
1640
1641 s[i] = 0;
1642 return s;
1643 }
1644
1645
1646 static const char *
1647 writemask_string(GLuint writeMask)
1648 {
1649 static char s[10];
1650 GLuint i = 0;
1651
1652 if (writeMask == WRITEMASK_XYZW)
1653 return "";
1654
1655 s[i++] = '.';
1656 if (writeMask & WRITEMASK_X)
1657 s[i++] = 'x';
1658 if (writeMask & WRITEMASK_Y)
1659 s[i++] = 'y';
1660 if (writeMask & WRITEMASK_Z)
1661 s[i++] = 'z';
1662 if (writeMask & WRITEMASK_W)
1663 s[i++] = 'w';
1664
1665 s[i] = 0;
1666 return s;
1667 }
1668
1669 static void
1670 print_dst_reg(const struct prog_dst_register *dstReg)
1671 {
1672 _mesa_printf(" %s[%d]%s",
1673 program_file_string((enum register_file) dstReg->File),
1674 dstReg->Index,
1675 writemask_string(dstReg->WriteMask));
1676 }
1677
1678 static void
1679 print_src_reg(const struct prog_src_register *srcReg)
1680 {
1681 _mesa_printf("%s[%d]%s",
1682 program_file_string((enum register_file) srcReg->File),
1683 srcReg->Index,
1684 swizzle_string(srcReg->Swizzle,
1685 srcReg->NegateBase, GL_FALSE));
1686 }
1687
1688 void
1689 _mesa_print_alu_instruction(const struct prog_instruction *inst,
1690 const char *opcode_string,
1691 GLuint numRegs)
1692 {
1693 GLuint j;
1694
1695 _mesa_printf("%s", opcode_string);
1696
1697 /* frag prog only */
1698 if (inst->SaturateMode == SATURATE_ZERO_ONE)
1699 _mesa_printf("_SAT");
1700
1701 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
1702 _mesa_printf(" %s[%d]%s",
1703 program_file_string((enum register_file) inst->DstReg.File),
1704 inst->DstReg.Index,
1705 writemask_string(inst->DstReg.WriteMask));
1706 }
1707
1708 if (numRegs > 0)
1709 _mesa_printf(", ");
1710
1711 for (j = 0; j < numRegs; j++) {
1712 print_src_reg(inst->SrcReg + j);
1713 if (j + 1 < numRegs)
1714 _mesa_printf(", ");
1715 }
1716
1717 _mesa_printf(";\n");
1718 }
1719
1720
1721 /**
1722 * Print a single vertex/fragment program instruction.
1723 */
1724 void
1725 _mesa_print_instruction(const struct prog_instruction *inst)
1726 {
1727 switch (inst->Opcode) {
1728 case OPCODE_PRINT:
1729 _mesa_printf("PRINT '%s'", inst->Data);
1730 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
1731 _mesa_printf(", ");
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));
1737 }
1738 _mesa_printf(";\n");
1739 break;
1740 case OPCODE_SWZ:
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));
1750 break;
1751 case OPCODE_TEX:
1752 case OPCODE_TXP:
1753 case OPCODE_TXB:
1754 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
1755 if (inst->SaturateMode == SATURATE_ZERO_ONE)
1756 _mesa_printf("_SAT");
1757 _mesa_printf(" ");
1758 print_dst_reg(&inst->DstReg);
1759 _mesa_printf(", ");
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;
1768 default:
1769 ;
1770 }
1771 _mesa_printf("\n");
1772 break;
1773 case OPCODE_ARL:
1774 _mesa_printf("ARL addr.x, ");
1775 print_src_reg(&inst->SrcReg[0]);
1776 _mesa_printf(";\n");
1777 break;
1778 case OPCODE_END:
1779 _mesa_printf("END;\n");
1780 break;
1781 /* XXX may need for other special-case instructions */
1782 default:
1783 /* typical alu instruction */
1784 _mesa_print_alu_instruction(inst,
1785 _mesa_opcode_string(inst->Opcode),
1786 _mesa_num_inst_src_regs(inst->Opcode));
1787 break;
1788 }
1789 }
1790
1791
1792 /**
1793 * Print a vertx/fragment program to stdout.
1794 * XXX this function could be greatly improved.
1795 */
1796 void
1797 _mesa_print_program(const struct gl_program *prog)
1798 {
1799 GLuint i;
1800 for (i = 0; i < prog->NumInstructions; i++) {
1801 _mesa_printf("%3d: ", i);
1802 _mesa_print_instruction(prog->Instructions + i);
1803 }
1804 }
1805
1806
1807 /**
1808 * Print all of a program's parameters.
1809 */
1810 void
1811 _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
1812 {
1813 GLuint i;
1814
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);
1820
1821 _mesa_load_state_parameters(ctx, prog->Parameters);
1822
1823 #if 0
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]);
1828 }
1829 #endif
1830
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]);
1836 }
1837 }
1838
1839
1840 /**
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.
1847 */
1848 static GLboolean
1849 compatible_program_targets(GLenum t1, GLenum t2)
1850 {
1851 if (t1 == t2)
1852 return GL_TRUE;
1853 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
1854 return GL_TRUE;
1855 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
1856 return GL_TRUE;
1857 return GL_FALSE;
1858 }
1859
1860
1861
1862 /**********************************************************************/
1863 /* API functions */
1864 /**********************************************************************/
1865
1866
1867 /**
1868 * Bind a program (make it current)
1869 * \note Called from the GL API dispatcher by both glBindProgramNV
1870 * and glBindProgramARB.
1871 */
1872 void GLAPIENTRY
1873 _mesa_BindProgram(GLenum target, GLuint id)
1874 {
1875 struct gl_program *curProg, *newProg;
1876 GET_CURRENT_CONTEXT(ctx);
1877 ASSERT_OUTSIDE_BEGIN_END(ctx);
1878
1879 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1880
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;
1886 }
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;
1892 }
1893 else {
1894 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1895 return;
1896 }
1897
1898 /*
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.
1902 */
1903 if (id == 0) {
1904 /* Bind a default program */
1905 newProg = NULL;
1906 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
1907 newProg = ctx->Shared->DefaultVertexProgram;
1908 else
1909 newProg = ctx->Shared->DefaultFragmentProgram;
1910 }
1911 else {
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);
1917 if (!newProg) {
1918 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1919 return;
1920 }
1921 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
1922 }
1923 else if (!compatible_program_targets(newProg->Target, target)) {
1924 _mesa_error(ctx, GL_INVALID_OPERATION,
1925 "glBindProgramNV/ARB(target mismatch)");
1926 return;
1927 }
1928 }
1929
1930 /** All error checking is complete now **/
1931
1932 if (curProg->Id == id) {
1933 /* binding same program - no change */
1934 return;
1935 }
1936
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);
1945 }
1946 }
1947
1948 /* bind newProg */
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;
1953 }
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;
1959 }
1960 newProg->RefCount++;
1961
1962 /* Never null pointers */
1963 ASSERT(ctx->VertexProgram.Current);
1964 ASSERT(ctx->FragmentProgram.Current);
1965
1966 if (ctx->Driver.BindProgram)
1967 ctx->Driver.BindProgram(ctx, target, newProg);
1968 }
1969
1970
1971 /**
1972 * Delete a list of programs.
1973 * \note Not compiled into display lists.
1974 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1975 */
1976 void GLAPIENTRY
1977 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1978 {
1979 GLint i;
1980 GET_CURRENT_CONTEXT(ctx);
1981 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1982
1983 if (n < 0) {
1984 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1985 return;
1986 }
1987
1988 for (i = 0; i < n; i++) {
1989 if (ids[i] != 0) {
1990 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
1991 if (prog == &_mesa_DummyProgram) {
1992 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1993 }
1994 else if (prog) {
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);
2002 }
2003 }
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);
2010 }
2011 }
2012 else {
2013 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
2014 return;
2015 }
2016 /* The ID is immediately available for re-use now */
2017 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
2018 prog->RefCount--;
2019 if (prog->RefCount <= 0) {
2020 ctx->Driver.DeleteProgram(ctx, prog);
2021 }
2022 }
2023 }
2024 }
2025 }
2026
2027
2028 /**
2029 * Generate a list of new program identifiers.
2030 * \note Not compiled into display lists.
2031 * \note Called by both glGenProgramsNV and glGenProgramsARB.
2032 */
2033 void GLAPIENTRY
2034 _mesa_GenPrograms(GLsizei n, GLuint *ids)
2035 {
2036 GLuint first;
2037 GLuint i;
2038 GET_CURRENT_CONTEXT(ctx);
2039 ASSERT_OUTSIDE_BEGIN_END(ctx);
2040
2041 if (n < 0) {
2042 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
2043 return;
2044 }
2045
2046 if (!ids)
2047 return;
2048
2049 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
2050
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);
2054 }
2055
2056 /* Return the program names */
2057 for (i = 0; i < (GLuint) n; i++) {
2058 ids[i] = first + i;
2059 }
2060 }
2061
2062
2063 /**********************************************************************/
2064 /* GL_MESA_program_debug extension */
2065 /**********************************************************************/
2066
2067
2068 /* XXX temporary */
2069 GLAPI void GLAPIENTRY
2070 glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
2071 GLvoid *data)
2072 {
2073 _mesa_ProgramCallbackMESA(target, callback, data);
2074 }
2075
2076
2077 void
2078 _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
2079 GLvoid *data)
2080 {
2081 GET_CURRENT_CONTEXT(ctx);
2082
2083 switch (target) {
2084 case GL_FRAGMENT_PROGRAM_ARB:
2085 if (!ctx->Extensions.ARB_fragment_program) {
2086 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2087 return;
2088 }
2089 ctx->FragmentProgram.Callback = callback;
2090 ctx->FragmentProgram.CallbackData = data;
2091 break;
2092 case GL_FRAGMENT_PROGRAM_NV:
2093 if (!ctx->Extensions.NV_fragment_program) {
2094 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2095 return;
2096 }
2097 ctx->FragmentProgram.Callback = callback;
2098 ctx->FragmentProgram.CallbackData = data;
2099 break;
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)");
2104 return;
2105 }
2106 ctx->VertexProgram.Callback = callback;
2107 ctx->VertexProgram.CallbackData = data;
2108 break;
2109 default:
2110 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2111 return;
2112 }
2113 }
2114
2115
2116 /* XXX temporary */
2117 GLAPI void GLAPIENTRY
2118 glGetProgramRegisterfvMESA(GLenum target,
2119 GLsizei len, const GLubyte *registerName,
2120 GLfloat *v)
2121 {
2122 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
2123 }
2124
2125
2126 void
2127 _mesa_GetProgramRegisterfvMESA(GLenum target,
2128 GLsizei len, const GLubyte *registerName,
2129 GLfloat *v)
2130 {
2131 char reg[1000];
2132 GET_CURRENT_CONTEXT(ctx);
2133
2134 /* We _should_ be inside glBegin/glEnd */
2135 #if 0
2136 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
2137 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
2138 return;
2139 }
2140 #endif
2141
2142 /* make null-terminated copy of registerName */
2143 len = MIN2((unsigned int) len, sizeof(reg) - 1);
2144 _mesa_memcpy(reg, registerName, len);
2145 reg[len] = 0;
2146
2147 switch (target) {
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)");
2153 return;
2154 }
2155 if (!ctx->VertexProgram._Enabled) {
2156 _mesa_error(ctx, GL_INVALID_OPERATION,
2157 "glGetProgramRegisterfvMESA");
2158 return;
2159 }
2160 /* GL_NV_vertex_program */
2161 if (reg[0] == 'R') {
2162 /* Temp register */
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)");
2167 return;
2168 }
2169 #if 0 /* FIX ME */
2170 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_TEMPORARY, i, v);
2171 #endif
2172 }
2173 else if (reg[0] == 'v' && reg[1] == '[') {
2174 /* Vertex Input attribute */
2175 GLuint i;
2176 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
2177 const char *name = _mesa_nv_vertex_input_register_name(i);
2178 char number[10];
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) {
2182 #if 0 /* FIX ME */
2183 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_INPUT,
2184 i, v);
2185 #endif
2186 return;
2187 }
2188 }
2189 _mesa_error(ctx, GL_INVALID_VALUE,
2190 "glGetProgramRegisterfvMESA(registerName)");
2191 return;
2192 }
2193 else if (reg[0] == 'o' && reg[1] == '[') {
2194 /* Vertex output attribute */
2195 }
2196 /* GL_ARB_vertex_program */
2197 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
2198
2199 }
2200 else {
2201 _mesa_error(ctx, GL_INVALID_VALUE,
2202 "glGetProgramRegisterfvMESA(registerName)");
2203 return;
2204 }
2205 break;
2206 case GL_FRAGMENT_PROGRAM_ARB:
2207 if (!ctx->Extensions.ARB_fragment_program) {
2208 _mesa_error(ctx, GL_INVALID_ENUM,
2209 "glGetProgramRegisterfvMESA(target)");
2210 return;
2211 }
2212 if (!ctx->FragmentProgram._Enabled) {
2213 _mesa_error(ctx, GL_INVALID_OPERATION,
2214 "glGetProgramRegisterfvMESA");
2215 return;
2216 }
2217 /* XXX to do */
2218 break;
2219 case GL_FRAGMENT_PROGRAM_NV:
2220 if (!ctx->Extensions.NV_fragment_program) {
2221 _mesa_error(ctx, GL_INVALID_ENUM,
2222 "glGetProgramRegisterfvMESA(target)");
2223 return;
2224 }
2225 if (!ctx->FragmentProgram._Enabled) {
2226 _mesa_error(ctx, GL_INVALID_OPERATION,
2227 "glGetProgramRegisterfvMESA");
2228 return;
2229 }
2230 if (reg[0] == 'R') {
2231 /* Temp register */
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)");
2236 return;
2237 }
2238 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_TEMPORARY,
2239 i, v);
2240 }
2241 else if (reg[0] == 'f' && reg[1] == '[') {
2242 /* Fragment input attribute */
2243 GLuint i;
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);
2249 return;
2250 }
2251 }
2252 _mesa_error(ctx, GL_INVALID_VALUE,
2253 "glGetProgramRegisterfvMESA(registerName)");
2254 return;
2255 }
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);
2260 }
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);
2265 }
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);
2270 }
2271 else {
2272 /* try user-defined identifiers */
2273 const GLfloat *value = _mesa_lookup_parameter_value(
2274 ctx->FragmentProgram.Current->Base.Parameters, -1, reg);
2275 if (value) {
2276 COPY_4V(v, value);
2277 }
2278 else {
2279 _mesa_error(ctx, GL_INVALID_VALUE,
2280 "glGetProgramRegisterfvMESA(registerName)");
2281 return;
2282 }
2283 }
2284 break;
2285 default:
2286 _mesa_error(ctx, GL_INVALID_ENUM,
2287 "glGetProgramRegisterfvMESA(target)");
2288 return;
2289 }
2290 }