Checkpoint of work for new GLSL compiler back-end. Lots of assorted changes.
[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 _mesa_bzero(prog, sizeof(*prog));
207 prog->Id = id;
208 prog->Target = target;
209 prog->Resident = GL_TRUE;
210 prog->RefCount = 1;
211 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
212 }
213
214 return prog;
215 }
216
217
218 /**
219 * Initialize a new fragment program object.
220 */
221 struct gl_program *
222 _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
223 GLenum target, GLuint id)
224 {
225 if (prog)
226 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
227 else
228 return NULL;
229 }
230
231
232 /**
233 * Initialize a new vertex program object.
234 */
235 struct gl_program *
236 _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
237 GLenum target, GLuint id)
238 {
239 if (prog)
240 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
241 else
242 return NULL;
243 }
244
245
246 /**
247 * Allocate and initialize a new fragment/vertex program object but
248 * don't put it into the program hash table. Called via
249 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
250 * device driver function to implement OO deriviation with additional
251 * types not understood by this function.
252 *
253 * \param ctx context
254 * \param id program id/number
255 * \param target program target/type
256 * \return pointer to new program object
257 */
258 struct gl_program *
259 _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
260 {
261 switch (target) {
262 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
263 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
264 target, id );
265 case GL_FRAGMENT_PROGRAM_NV:
266 case GL_FRAGMENT_PROGRAM_ARB:
267 return _mesa_init_fragment_program(ctx,
268 CALLOC_STRUCT(gl_fragment_program),
269 target, id );
270 default:
271 _mesa_problem(ctx, "bad target in _mesa_new_program");
272 return NULL;
273 }
274 }
275
276
277 /**
278 * Delete a program and remove it from the hash table, ignoring the
279 * reference count.
280 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
281 * by a device driver function.
282 */
283 void
284 _mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
285 {
286 (void) ctx;
287 ASSERT(prog);
288
289 if (prog == &_mesa_DummyProgram)
290 return;
291
292 if (prog->String)
293 _mesa_free(prog->String);
294
295 if (prog->Instructions) {
296 GLuint i;
297 for (i = 0; i < prog->NumInstructions; i++) {
298 if (prog->Instructions[i].Data)
299 _mesa_free(prog->Instructions[i].Data);
300 }
301 _mesa_free(prog->Instructions);
302 }
303
304 if (prog->Parameters) {
305 _mesa_free_parameter_list(prog->Parameters);
306 }
307
308 if (prog->Varying) {
309 _mesa_free_parameter_list(prog->Varying);
310 }
311
312 /* XXX this is a little ugly */
313 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
314 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
315 if (vprog->TnlData)
316 _mesa_free(vprog->TnlData);
317 }
318
319 _mesa_free(prog);
320 }
321
322
323 /**
324 * Return the gl_program object for a given ID.
325 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
326 * casts elsewhere.
327 */
328 struct gl_program *
329 _mesa_lookup_program(GLcontext *ctx, GLuint id)
330 {
331 if (id)
332 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
333 else
334 return NULL;
335 }
336
337
338 /**********************************************************************/
339 /* Program parameter functions */
340 /**********************************************************************/
341
342 struct gl_program_parameter_list *
343 _mesa_new_parameter_list(void)
344 {
345 return (struct gl_program_parameter_list *)
346 _mesa_calloc(sizeof(struct gl_program_parameter_list));
347 }
348
349
350 /**
351 * Free a parameter list and all its parameters
352 */
353 void
354 _mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
355 {
356 GLuint i;
357 for (i = 0; i < paramList->NumParameters; i++) {
358 if (paramList->Parameters[i].Name)
359 _mesa_free((void *) paramList->Parameters[i].Name);
360 }
361 _mesa_free(paramList->Parameters);
362 if (paramList->ParameterValues)
363 _mesa_align_free(paramList->ParameterValues);
364 _mesa_free(paramList);
365 }
366
367
368 /**
369 * Add a new parameter to a parameter list.
370 * \param paramList the list to add the parameter to
371 * \param name the parameter name, will be duplicated/copied!
372 * \param values initial parameter value, up to 4 GLfloats
373 * \param size number of elements in 'values' vector (1..4)
374 * \param type type of parameter, such as
375 * \return index of new parameter in the list, or -1 if error (out of mem)
376 */
377 static GLint
378 add_parameter(struct gl_program_parameter_list *paramList,
379 const char *name, const GLfloat values[4], GLuint size,
380 enum register_file type)
381 {
382 const GLuint n = paramList->NumParameters;
383
384 if (n == paramList->Size) {
385 /* Need to grow the parameter list array */
386 if (paramList->Size == 0)
387 paramList->Size = 8;
388 else
389 paramList->Size *= 2;
390
391 /* realloc arrays */
392 paramList->Parameters = (struct gl_program_parameter *)
393 _mesa_realloc(paramList->Parameters,
394 n * sizeof(struct gl_program_parameter),
395 paramList->Size * sizeof(struct gl_program_parameter));
396
397 paramList->ParameterValues = (GLfloat (*)[4])
398 _mesa_align_realloc(paramList->ParameterValues, /* old buf */
399 n * 4 * sizeof(GLfloat), /* old size */
400 paramList->Size * 4 *sizeof(GLfloat), /* new sz */
401 16);
402 }
403
404 if (!paramList->Parameters ||
405 !paramList->ParameterValues) {
406 /* out of memory */
407 paramList->NumParameters = 0;
408 paramList->Size = 0;
409 return -1;
410 }
411 else {
412 paramList->NumParameters = n + 1;
413
414 _mesa_memset(&paramList->Parameters[n], 0,
415 sizeof(struct gl_program_parameter));
416
417 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
418 paramList->Parameters[n].Type = type;
419 if (values)
420 COPY_4V(paramList->ParameterValues[n], values);
421 return (GLint) n;
422 }
423 }
424
425
426 /**
427 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
428 * \return index of the new entry in the parameter list
429 */
430 GLint
431 _mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
432 const char *name, const GLfloat values[4])
433 {
434 return add_parameter(paramList, name, values, 4, PROGRAM_NAMED_PARAM);
435 }
436
437
438 /**
439 * Add a new named constant to the parameter list.
440 * This will be used when the program contains something like this:
441 * PARAM myVals = { 0, 1, 2, 3 };
442 *
443 * \param paramList the parameter list
444 * \param name the name for the constant
445 * \param values four float values
446 * \return index/position of the new parameter in the parameter list
447 */
448 GLint
449 _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
450 const char *name, const GLfloat values[4],
451 GLuint size)
452 {
453 #if 0 /* disable this for now -- we need to save the name! */
454 GLint pos;
455 GLuint swizzle;
456 ASSERT(size == 4); /* XXX future feature */
457 /* check if we already have this constant */
458 if (_mesa_lookup_parameter_constant(paramList, values, 4, &pos, &swizzle)) {
459 return pos;
460 }
461 #endif
462 size = 4; /** XXX fix */
463 return add_parameter(paramList, name, values, size, PROGRAM_CONSTANT);
464 }
465
466
467 /**
468 * Add a new unnamed constant to the parameter list.
469 * This will be used when the program contains something like this:
470 * MOV r, { 0, 1, 2, 3 };
471 *
472 * \param paramList the parameter list
473 * \param values four float values
474 * \param swizzleOut returns swizzle mask for accessing the constant
475 * \return index/position of the new parameter in the parameter list.
476 */
477 GLint
478 _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
479 const GLfloat values[4], GLuint size,
480 GLuint *swizzleOut)
481 {
482 GLint pos;
483 GLuint swizzle;
484 ASSERT(size >= 1);
485 ASSERT(size <= 4);
486 size = 4; /* XXX temporary */
487 /* check if we already have this constant */
488 if (_mesa_lookup_parameter_constant(paramList, values,
489 size, &pos, &swizzle)) {
490 return pos;
491 }
492 return add_parameter(paramList, NULL, values, size, PROGRAM_CONSTANT);
493 }
494
495
496 GLint
497 _mesa_add_uniform(struct gl_program_parameter_list *paramList,
498 const char *name, GLuint size)
499 {
500 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
501 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_UNIFORM) {
502 /* already in list */
503 return i;
504 }
505 else {
506 assert(size == 4);
507 i = add_parameter(paramList, name, NULL, size, PROGRAM_UNIFORM);
508 return i;
509 }
510 }
511
512
513 GLint
514 _mesa_add_varying(struct gl_program_parameter_list *paramList,
515 const char *name, GLuint size)
516 {
517 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
518 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_VARYING) {
519 /* already in list */
520 return i;
521 }
522 else {
523 assert(size == 4);
524 i = add_parameter(paramList, name, NULL, size, PROGRAM_VARYING);
525 return i;
526 }
527 }
528
529
530
531
532 #if 0 /* not used yet */
533 /**
534 * Returns the number of 4-component registers needed to store a piece
535 * of GL state. For matrices this may be as many as 4 registers,
536 * everything else needs
537 * just 1 register.
538 */
539 static GLuint
540 sizeof_state_reference(const GLint *stateTokens)
541 {
542 if (stateTokens[0] == STATE_MATRIX) {
543 GLuint rows = stateTokens[4] - stateTokens[3] + 1;
544 assert(rows >= 1);
545 assert(rows <= 4);
546 return rows;
547 }
548 else {
549 return 1;
550 }
551 }
552 #endif
553
554
555 /**
556 * Add a new state reference to the parameter list.
557 * This will be used when the program contains something like this:
558 * PARAM ambient = state.material.front.ambient;
559 *
560 * \param paramList the parameter list
561 * \param state an array of 6 state tokens
562 * \return index of the new parameter.
563 */
564 GLint
565 _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
566 const GLint *stateTokens)
567 {
568 const GLuint size = 4; /* XXX fix */
569 const char *name;
570 GLint index;
571
572 /* Check if the state reference is already in the list */
573 for (index = 0; index < paramList->NumParameters; index++) {
574 GLuint i, match = 0;
575 for (i = 0; i < 6; i++) {
576 if (paramList->Parameters[index].StateIndexes[i] == stateTokens[i]) {
577 match++;
578 }
579 else {
580 break;
581 }
582 }
583 if (match == 6) {
584 /* this state reference is already in the parameter list */
585 return index;
586 }
587 }
588
589 name = make_state_string(stateTokens);
590 index = add_parameter(paramList, name, NULL, size, PROGRAM_STATE_VAR);
591 if (index >= 0) {
592 GLuint i;
593 for (i = 0; i < 6; i++) {
594 paramList->Parameters[index].StateIndexes[i]
595 = (gl_state_index) stateTokens[i];
596 }
597 paramList->StateFlags |= make_state_flags(stateTokens);
598 }
599
600 /* free name string here since we duplicated it in add_parameter() */
601 _mesa_free((void *) name);
602
603 return index;
604 }
605
606
607 /**
608 * Lookup a parameter value by name in the given parameter list.
609 * \return pointer to the float[4] values.
610 */
611 GLfloat *
612 _mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
613 GLsizei nameLen, const char *name)
614 {
615 GLuint i = _mesa_lookup_parameter_index(paramList, nameLen, name);
616 if (i < 0)
617 return NULL;
618 else
619 return paramList->ParameterValues[i];
620 }
621
622
623 /**
624 * Given a program parameter name, find its position in the list of parameters.
625 * \param paramList the parameter list to search
626 * \param nameLen length of name (in chars).
627 * If length is negative, assume that name is null-terminated.
628 * \param name the name to search for
629 * \return index of parameter in the list.
630 */
631 GLint
632 _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
633 GLsizei nameLen, const char *name)
634 {
635 GLint i;
636
637 if (!paramList)
638 return -1;
639
640 if (nameLen == -1) {
641 /* name is null-terminated */
642 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
643 if (paramList->Parameters[i].Name &&
644 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
645 return i;
646 }
647 }
648 else {
649 /* name is not null-terminated, use nameLen */
650 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
651 if (paramList->Parameters[i].Name &&
652 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
653 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
654 return i;
655 }
656 }
657 return -1;
658 }
659
660
661 /**
662 * Look for a float vector in the given parameter list. The float vector
663 * may be of length 1, 2, 3 or 4.
664 * \param paramList the parameter list to search
665 * \param v the float vector to search for
666 * \param size number of element in v
667 * \param posOut returns the position of the constant, if found
668 * \param swizzleOut returns a swizzle mask describing location of the
669 * vector elements if found
670 * \return GL_TRUE if found, GL_FALSE if not found
671 */
672 GLboolean
673 _mesa_lookup_parameter_constant(const struct gl_program_parameter_list *paramList,
674 const GLfloat v[], GLsizei vSize,
675 GLint *posOut, GLuint *swizzleOut)
676 {
677 GLuint i;
678
679 assert(vSize >= 1);
680 assert(vSize <= 4);
681
682 if (!paramList)
683 return -1;
684
685 for (i = 0; i < paramList->NumParameters; i++) {
686 if (paramList->Parameters[i].Type == PROGRAM_CONSTANT) {
687 const GLint maxShift = 4 - vSize;
688 GLint shift, j;
689 for (shift = 0; shift <= maxShift; shift++) {
690 GLint matched = 0;
691 GLuint swizzle[4];
692 swizzle[0] = swizzle[1] = swizzle[2] = swizzle[3] = 0;
693 /* XXX we could do out-of-order swizzle matches too, someday */
694 for (j = 0; j < vSize; j++) {
695 assert(shift + j < 4);
696 if (paramList->ParameterValues[i][shift + j] == v[j]) {
697 matched++;
698 swizzle[j] = shift + j;
699 ASSERT(swizzle[j] >= SWIZZLE_X);
700 ASSERT(swizzle[j] <= SWIZZLE_W);
701 }
702 }
703 if (matched == vSize) {
704 /* found! */
705 *posOut = i;
706 *swizzleOut = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
707 swizzle[2], swizzle[3]);
708 return GL_TRUE;
709 }
710 }
711 }
712 }
713
714 *posOut = -1;
715 return GL_FALSE;
716 }
717
718
719 /**
720 * Use the list of tokens in the state[] array to find global GL state
721 * and return it in <value>. Usually, four values are returned in <value>
722 * but matrix queries may return as many as 16 values.
723 * This function is used for ARB vertex/fragment programs.
724 * The program parser will produce the state[] values.
725 */
726 static void
727 _mesa_fetch_state(GLcontext *ctx, const gl_state_index state[],
728 GLfloat *value)
729 {
730 switch (state[0]) {
731 case STATE_MATERIAL:
732 {
733 /* state[1] is either 0=front or 1=back side */
734 const GLuint face = (GLuint) state[1];
735 const struct gl_material *mat = &ctx->Light.Material;
736 ASSERT(face == 0 || face == 1);
737 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
738 ASSERT(MAT_ATTRIB_FRONT_AMBIENT + 1 == MAT_ATTRIB_BACK_AMBIENT);
739 /* XXX we could get rid of this switch entirely with a little
740 * work in arbprogparse.c's parse_state_single_item().
741 */
742 /* state[2] is the material attribute */
743 switch (state[2]) {
744 case STATE_AMBIENT:
745 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_AMBIENT + face]);
746 return;
747 case STATE_DIFFUSE:
748 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_DIFFUSE + face]);
749 return;
750 case STATE_SPECULAR:
751 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_SPECULAR + face]);
752 return;
753 case STATE_EMISSION:
754 COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_EMISSION + face]);
755 return;
756 case STATE_SHININESS:
757 value[0] = mat->Attrib[MAT_ATTRIB_FRONT_SHININESS + face][0];
758 value[1] = 0.0F;
759 value[2] = 0.0F;
760 value[3] = 1.0F;
761 return;
762 default:
763 _mesa_problem(ctx, "Invalid material state in fetch_state");
764 return;
765 }
766 }
767 case STATE_LIGHT:
768 {
769 /* state[1] is the light number */
770 const GLuint ln = (GLuint) state[1];
771 /* state[2] is the light attribute */
772 switch (state[2]) {
773 case STATE_AMBIENT:
774 COPY_4V(value, ctx->Light.Light[ln].Ambient);
775 return;
776 case STATE_DIFFUSE:
777 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
778 return;
779 case STATE_SPECULAR:
780 COPY_4V(value, ctx->Light.Light[ln].Specular);
781 return;
782 case STATE_POSITION:
783 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
784 return;
785 case STATE_ATTENUATION:
786 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
787 value[1] = ctx->Light.Light[ln].LinearAttenuation;
788 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
789 value[3] = ctx->Light.Light[ln].SpotExponent;
790 return;
791 case STATE_SPOT_DIRECTION:
792 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
793 value[3] = ctx->Light.Light[ln]._CosCutoff;
794 return;
795 case STATE_HALF:
796 {
797 GLfloat eye_z[] = {0, 0, 1};
798
799 /* Compute infinite half angle vector:
800 * half-vector = light_position + (0, 0, 1)
801 * and then normalize. w = 0
802 *
803 * light.EyePosition.w should be 0 for infinite lights.
804 */
805 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
806 NORMALIZE_3FV(value);
807 value[3] = 0;
808 }
809 return;
810 case STATE_POSITION_NORMALIZED:
811 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
812 NORMALIZE_3FV( value );
813 return;
814 default:
815 _mesa_problem(ctx, "Invalid light state in fetch_state");
816 return;
817 }
818 }
819 case STATE_LIGHTMODEL_AMBIENT:
820 COPY_4V(value, ctx->Light.Model.Ambient);
821 return;
822 case STATE_LIGHTMODEL_SCENECOLOR:
823 if (state[1] == 0) {
824 /* front */
825 GLint i;
826 for (i = 0; i < 3; i++) {
827 value[i] = ctx->Light.Model.Ambient[i]
828 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
829 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
830 }
831 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
832 }
833 else {
834 /* back */
835 GLint i;
836 for (i = 0; i < 3; i++) {
837 value[i] = ctx->Light.Model.Ambient[i]
838 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
839 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
840 }
841 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
842 }
843 return;
844 case STATE_LIGHTPROD:
845 {
846 const GLuint ln = (GLuint) state[1];
847 const GLuint face = (GLuint) state[2];
848 GLint i;
849 ASSERT(face == 0 || face == 1);
850 switch (state[3]) {
851 case STATE_AMBIENT:
852 for (i = 0; i < 3; i++) {
853 value[i] = ctx->Light.Light[ln].Ambient[i] *
854 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
855 }
856 /* [3] = material alpha */
857 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
858 return;
859 case STATE_DIFFUSE:
860 for (i = 0; i < 3; i++) {
861 value[i] = ctx->Light.Light[ln].Diffuse[i] *
862 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
863 }
864 /* [3] = material alpha */
865 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
866 return;
867 case STATE_SPECULAR:
868 for (i = 0; i < 3; i++) {
869 value[i] = ctx->Light.Light[ln].Specular[i] *
870 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
871 }
872 /* [3] = material alpha */
873 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
874 return;
875 default:
876 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
877 return;
878 }
879 }
880 case STATE_TEXGEN:
881 {
882 /* state[1] is the texture unit */
883 const GLuint unit = (GLuint) state[1];
884 /* state[2] is the texgen attribute */
885 switch (state[2]) {
886 case STATE_TEXGEN_EYE_S:
887 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
888 return;
889 case STATE_TEXGEN_EYE_T:
890 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
891 return;
892 case STATE_TEXGEN_EYE_R:
893 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
894 return;
895 case STATE_TEXGEN_EYE_Q:
896 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
897 return;
898 case STATE_TEXGEN_OBJECT_S:
899 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
900 return;
901 case STATE_TEXGEN_OBJECT_T:
902 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
903 return;
904 case STATE_TEXGEN_OBJECT_R:
905 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
906 return;
907 case STATE_TEXGEN_OBJECT_Q:
908 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
909 return;
910 default:
911 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
912 return;
913 }
914 }
915 case STATE_TEXENV_COLOR:
916 {
917 /* state[1] is the texture unit */
918 const GLuint unit = (GLuint) state[1];
919 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
920 }
921 return;
922 case STATE_FOG_COLOR:
923 COPY_4V(value, ctx->Fog.Color);
924 return;
925 case STATE_FOG_PARAMS:
926 value[0] = ctx->Fog.Density;
927 value[1] = ctx->Fog.Start;
928 value[2] = ctx->Fog.End;
929 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
930 return;
931 case STATE_CLIPPLANE:
932 {
933 const GLuint plane = (GLuint) state[1];
934 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
935 }
936 return;
937 case STATE_POINT_SIZE:
938 value[0] = ctx->Point.Size;
939 value[1] = ctx->Point.MinSize;
940 value[2] = ctx->Point.MaxSize;
941 value[3] = ctx->Point.Threshold;
942 return;
943 case STATE_POINT_ATTENUATION:
944 value[0] = ctx->Point.Params[0];
945 value[1] = ctx->Point.Params[1];
946 value[2] = ctx->Point.Params[2];
947 value[3] = 1.0F;
948 return;
949 case STATE_MATRIX:
950 {
951 /* state[1] = modelview, projection, texture, etc. */
952 /* state[2] = which texture matrix or program matrix */
953 /* state[3] = first row to fetch */
954 /* state[4] = last row to fetch */
955 /* state[5] = transpose, inverse or invtrans */
956
957 const GLmatrix *matrix;
958 const gl_state_index mat = state[1];
959 const GLuint index = (GLuint) state[2];
960 const GLuint firstRow = (GLuint) state[3];
961 const GLuint lastRow = (GLuint) state[4];
962 const gl_state_index modifier = state[5];
963 const GLfloat *m;
964 GLuint row, i;
965 if (mat == STATE_MODELVIEW) {
966 matrix = ctx->ModelviewMatrixStack.Top;
967 }
968 else if (mat == STATE_PROJECTION) {
969 matrix = ctx->ProjectionMatrixStack.Top;
970 }
971 else if (mat == STATE_MVP) {
972 matrix = &ctx->_ModelProjectMatrix;
973 }
974 else if (mat == STATE_TEXTURE) {
975 matrix = ctx->TextureMatrixStack[index].Top;
976 }
977 else if (mat == STATE_PROGRAM) {
978 matrix = ctx->ProgramMatrixStack[index].Top;
979 }
980 else {
981 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
982 return;
983 }
984 if (modifier == STATE_MATRIX_INVERSE ||
985 modifier == STATE_MATRIX_INVTRANS) {
986 /* Be sure inverse is up to date:
987 */
988 _math_matrix_alloc_inv( (GLmatrix *) matrix );
989 _math_matrix_analyse( (GLmatrix*) matrix );
990 m = matrix->inv;
991 }
992 else {
993 m = matrix->m;
994 }
995 if (modifier == STATE_MATRIX_TRANSPOSE ||
996 modifier == STATE_MATRIX_INVTRANS) {
997 for (i = 0, row = firstRow; row <= lastRow; row++) {
998 value[i++] = m[row * 4 + 0];
999 value[i++] = m[row * 4 + 1];
1000 value[i++] = m[row * 4 + 2];
1001 value[i++] = m[row * 4 + 3];
1002 }
1003 }
1004 else {
1005 for (i = 0, row = firstRow; row <= lastRow; row++) {
1006 value[i++] = m[row + 0];
1007 value[i++] = m[row + 4];
1008 value[i++] = m[row + 8];
1009 value[i++] = m[row + 12];
1010 }
1011 }
1012 }
1013 return;
1014 case STATE_DEPTH_RANGE:
1015 value[0] = ctx->Viewport.Near; /* near */
1016 value[1] = ctx->Viewport.Far; /* far */
1017 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
1018 value[3] = 0;
1019 return;
1020 case STATE_FRAGMENT_PROGRAM:
1021 {
1022 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1023 /* state[2] = parameter index */
1024 const int idx = (int) state[2];
1025 switch (state[1]) {
1026 case STATE_ENV:
1027 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
1028 break;
1029 case STATE_LOCAL:
1030 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
1031 break;
1032 default:
1033 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
1034 return;
1035 }
1036 }
1037 return;
1038
1039 case STATE_VERTEX_PROGRAM:
1040 {
1041 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1042 /* state[2] = parameter index */
1043 const int idx = (int) state[2];
1044 switch (state[1]) {
1045 case STATE_ENV:
1046 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
1047 break;
1048 case STATE_LOCAL:
1049 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
1050 break;
1051 default:
1052 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
1053 return;
1054 }
1055 }
1056 return;
1057
1058 case STATE_INTERNAL:
1059 {
1060 switch (state[1]) {
1061 case STATE_NORMAL_SCALE:
1062 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
1063 break;
1064 case STATE_TEXRECT_SCALE: {
1065 const int unit = (int) state[2];
1066 const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
1067 if (texObj) {
1068 struct gl_texture_image *texImage = texObj->Image[0][0];
1069 ASSIGN_4V(value, 1.0 / texImage->Width, 1.0 / texImage->Height, 0, 1);
1070 }
1071 break;
1072 }
1073 default:
1074 /* unknown state indexes are silently ignored
1075 * should be handled by the driver.
1076 */
1077 return;
1078 }
1079 }
1080 return;
1081
1082 default:
1083 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
1084 return;
1085 }
1086 }
1087
1088
1089 /**
1090 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
1091 * indicate that the given context state may have changed.
1092 * The bitmask is used during validation to determine if we need to update
1093 * vertex/fragment program parameters (like "state.material.color") when
1094 * some GL state has changed.
1095 */
1096 static GLbitfield
1097 make_state_flags(const GLint state[])
1098 {
1099 switch (state[0]) {
1100 case STATE_MATERIAL:
1101 case STATE_LIGHT:
1102 case STATE_LIGHTMODEL_AMBIENT:
1103 case STATE_LIGHTMODEL_SCENECOLOR:
1104 case STATE_LIGHTPROD:
1105 return _NEW_LIGHT;
1106
1107 case STATE_TEXGEN:
1108 case STATE_TEXENV_COLOR:
1109 return _NEW_TEXTURE;
1110
1111 case STATE_FOG_COLOR:
1112 case STATE_FOG_PARAMS:
1113 return _NEW_FOG;
1114
1115 case STATE_CLIPPLANE:
1116 return _NEW_TRANSFORM;
1117
1118 case STATE_POINT_SIZE:
1119 case STATE_POINT_ATTENUATION:
1120 return _NEW_POINT;
1121
1122 case STATE_MATRIX:
1123 switch (state[1]) {
1124 case STATE_MODELVIEW:
1125 return _NEW_MODELVIEW;
1126 case STATE_PROJECTION:
1127 return _NEW_PROJECTION;
1128 case STATE_MVP:
1129 return _NEW_MODELVIEW | _NEW_PROJECTION;
1130 case STATE_TEXTURE:
1131 return _NEW_TEXTURE_MATRIX;
1132 case STATE_PROGRAM:
1133 return _NEW_TRACK_MATRIX;
1134 default:
1135 _mesa_problem(NULL, "unexpected matrix in make_state_flags()");
1136 return 0;
1137 }
1138
1139 case STATE_DEPTH_RANGE:
1140 return _NEW_VIEWPORT;
1141
1142 case STATE_FRAGMENT_PROGRAM:
1143 case STATE_VERTEX_PROGRAM:
1144 return _NEW_PROGRAM;
1145
1146 case STATE_INTERNAL:
1147 switch (state[1]) {
1148 case STATE_NORMAL_SCALE:
1149 return _NEW_MODELVIEW;
1150 case STATE_TEXRECT_SCALE:
1151 return _NEW_TEXTURE;
1152 default:
1153 /* unknown state indexes are silently ignored and
1154 * no flag set, since it is handled by the driver.
1155 */
1156 return 0;
1157 }
1158
1159 default:
1160 _mesa_problem(NULL, "unexpected state[0] in make_state_flags()");
1161 return 0;
1162 }
1163 }
1164
1165
1166 static void
1167 append(char *dst, const char *src)
1168 {
1169 while (*dst)
1170 dst++;
1171 while (*src)
1172 *dst++ = *src++;
1173 *dst = 0;
1174 }
1175
1176
1177 static void
1178 append_token(char *dst, gl_state_index k)
1179 {
1180 switch (k) {
1181 case STATE_MATERIAL:
1182 append(dst, "material.");
1183 break;
1184 case STATE_LIGHT:
1185 append(dst, "light");
1186 break;
1187 case STATE_LIGHTMODEL_AMBIENT:
1188 append(dst, "lightmodel.ambient");
1189 break;
1190 case STATE_LIGHTMODEL_SCENECOLOR:
1191 break;
1192 case STATE_LIGHTPROD:
1193 append(dst, "lightprod");
1194 break;
1195 case STATE_TEXGEN:
1196 append(dst, "texgen");
1197 break;
1198 case STATE_FOG_COLOR:
1199 append(dst, "fog.color");
1200 break;
1201 case STATE_FOG_PARAMS:
1202 append(dst, "fog.params");
1203 break;
1204 case STATE_CLIPPLANE:
1205 append(dst, "clip");
1206 break;
1207 case STATE_POINT_SIZE:
1208 append(dst, "point.size");
1209 break;
1210 case STATE_POINT_ATTENUATION:
1211 append(dst, "point.attenuation");
1212 break;
1213 case STATE_MATRIX:
1214 append(dst, "matrix.");
1215 break;
1216 case STATE_MODELVIEW:
1217 append(dst, "modelview");
1218 break;
1219 case STATE_PROJECTION:
1220 append(dst, "projection");
1221 break;
1222 case STATE_MVP:
1223 append(dst, "mvp");
1224 break;
1225 case STATE_TEXTURE:
1226 append(dst, "texture");
1227 break;
1228 case STATE_PROGRAM:
1229 append(dst, "program");
1230 break;
1231 case STATE_MATRIX_INVERSE:
1232 append(dst, ".inverse");
1233 break;
1234 case STATE_MATRIX_TRANSPOSE:
1235 append(dst, ".transpose");
1236 break;
1237 case STATE_MATRIX_INVTRANS:
1238 append(dst, ".invtrans");
1239 break;
1240 case STATE_AMBIENT:
1241 append(dst, "ambient");
1242 break;
1243 case STATE_DIFFUSE:
1244 append(dst, "diffuse");
1245 break;
1246 case STATE_SPECULAR:
1247 append(dst, "specular");
1248 break;
1249 case STATE_EMISSION:
1250 append(dst, "emission");
1251 break;
1252 case STATE_SHININESS:
1253 append(dst, "shininess");
1254 break;
1255 case STATE_HALF:
1256 append(dst, "half");
1257 break;
1258 case STATE_POSITION:
1259 append(dst, ".position");
1260 break;
1261 case STATE_ATTENUATION:
1262 append(dst, ".attenuation");
1263 break;
1264 case STATE_SPOT_DIRECTION:
1265 append(dst, ".spot.direction");
1266 break;
1267 case STATE_TEXGEN_EYE_S:
1268 append(dst, "eye.s");
1269 break;
1270 case STATE_TEXGEN_EYE_T:
1271 append(dst, "eye.t");
1272 break;
1273 case STATE_TEXGEN_EYE_R:
1274 append(dst, "eye.r");
1275 break;
1276 case STATE_TEXGEN_EYE_Q:
1277 append(dst, "eye.q");
1278 break;
1279 case STATE_TEXGEN_OBJECT_S:
1280 append(dst, "object.s");
1281 break;
1282 case STATE_TEXGEN_OBJECT_T:
1283 append(dst, "object.t");
1284 break;
1285 case STATE_TEXGEN_OBJECT_R:
1286 append(dst, "object.r");
1287 break;
1288 case STATE_TEXGEN_OBJECT_Q:
1289 append(dst, "object.q");
1290 break;
1291 case STATE_TEXENV_COLOR:
1292 append(dst, "texenv");
1293 break;
1294 case STATE_DEPTH_RANGE:
1295 append(dst, "depth.range");
1296 break;
1297 case STATE_VERTEX_PROGRAM:
1298 case STATE_FRAGMENT_PROGRAM:
1299 break;
1300 case STATE_ENV:
1301 append(dst, "env");
1302 break;
1303 case STATE_LOCAL:
1304 append(dst, "local");
1305 break;
1306 case STATE_INTERNAL:
1307 case STATE_NORMAL_SCALE:
1308 case STATE_POSITION_NORMALIZED:
1309 append(dst, "(internal)");
1310 break;
1311 default:
1312 ;
1313 }
1314 }
1315
1316 static void
1317 append_face(char *dst, GLint face)
1318 {
1319 if (face == 0)
1320 append(dst, "front.");
1321 else
1322 append(dst, "back.");
1323 }
1324
1325 static void
1326 append_index(char *dst, GLint index)
1327 {
1328 char s[20];
1329 _mesa_sprintf(s, "[%d].", index);
1330 append(dst, s);
1331 }
1332
1333 /**
1334 * Make a string from the given state vector.
1335 * For example, return "state.matrix.texture[2].inverse".
1336 * Use _mesa_free() to deallocate the string.
1337 */
1338 static const char *
1339 make_state_string(const GLint state[6])
1340 {
1341 char str[1000] = "";
1342 char tmp[30];
1343
1344 append(str, "state.");
1345 append_token(str, (gl_state_index) state[0]);
1346
1347 switch (state[0]) {
1348 case STATE_MATERIAL:
1349 append_face(str, state[1]);
1350 append_token(str, (gl_state_index) state[2]);
1351 break;
1352 case STATE_LIGHT:
1353 append(str, "light");
1354 append_index(str, state[1]); /* light number [i]. */
1355 append_token(str, (gl_state_index) state[2]); /* coefficients */
1356 break;
1357 case STATE_LIGHTMODEL_AMBIENT:
1358 append(str, "lightmodel.ambient");
1359 break;
1360 case STATE_LIGHTMODEL_SCENECOLOR:
1361 if (state[1] == 0) {
1362 append(str, "lightmodel.front.scenecolor");
1363 }
1364 else {
1365 append(str, "lightmodel.back.scenecolor");
1366 }
1367 break;
1368 case STATE_LIGHTPROD:
1369 append_index(str, state[1]); /* light number [i]. */
1370 append_face(str, state[2]);
1371 append_token(str, (gl_state_index) state[3]);
1372 break;
1373 case STATE_TEXGEN:
1374 append_index(str, state[1]); /* tex unit [i] */
1375 append_token(str, (gl_state_index) state[2]); /* plane coef */
1376 break;
1377 case STATE_TEXENV_COLOR:
1378 append_index(str, state[1]); /* tex unit [i] */
1379 append(str, "color");
1380 break;
1381 case STATE_FOG_COLOR:
1382 case STATE_FOG_PARAMS:
1383 break;
1384 case STATE_CLIPPLANE:
1385 append_index(str, state[1]); /* plane [i] */
1386 append(str, "plane");
1387 break;
1388 case STATE_POINT_SIZE:
1389 case STATE_POINT_ATTENUATION:
1390 break;
1391 case STATE_MATRIX:
1392 {
1393 /* state[1] = modelview, projection, texture, etc. */
1394 /* state[2] = which texture matrix or program matrix */
1395 /* state[3] = first row to fetch */
1396 /* state[4] = last row to fetch */
1397 /* state[5] = transpose, inverse or invtrans */
1398 const gl_state_index mat = (gl_state_index) state[1];
1399 const GLuint index = (GLuint) state[2];
1400 const GLuint firstRow = (GLuint) state[3];
1401 const GLuint lastRow = (GLuint) state[4];
1402 const gl_state_index modifier = (gl_state_index) state[5];
1403 append_token(str, mat);
1404 if (index)
1405 append_index(str, index);
1406 if (modifier)
1407 append_token(str, modifier);
1408 if (firstRow == lastRow)
1409 _mesa_sprintf(tmp, ".row[%d]", firstRow);
1410 else
1411 _mesa_sprintf(tmp, ".row[%d..%d]", firstRow, lastRow);
1412 append(str, tmp);
1413 }
1414 break;
1415 case STATE_DEPTH_RANGE:
1416 break;
1417 case STATE_FRAGMENT_PROGRAM:
1418 case STATE_VERTEX_PROGRAM:
1419 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1420 /* state[2] = parameter index */
1421 append_token(str, (gl_state_index) state[1]);
1422 append_index(str, state[2]);
1423 break;
1424 case STATE_INTERNAL:
1425 break;
1426 default:
1427 _mesa_problem(NULL, "Invalid state in make_state_string");
1428 break;
1429 }
1430
1431 return _mesa_strdup(str);
1432 }
1433
1434
1435 /**
1436 * Loop over all the parameters in a parameter list. If the parameter
1437 * is a GL state reference, look up the current value of that state
1438 * variable and put it into the parameter's Value[4] array.
1439 * This would be called at glBegin time when using a fragment program.
1440 */
1441 void
1442 _mesa_load_state_parameters(GLcontext *ctx,
1443 struct gl_program_parameter_list *paramList)
1444 {
1445 GLuint i;
1446
1447 if (!paramList)
1448 return;
1449
1450 for (i = 0; i < paramList->NumParameters; i++) {
1451 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1452 _mesa_fetch_state(ctx,
1453 paramList->Parameters[i].StateIndexes,
1454 paramList->ParameterValues[i]);
1455 }
1456 }
1457 }
1458
1459
1460 /**
1461 * Initialize program instruction fields to defaults.
1462 * \param inst first instruction to initialize
1463 * \param count number of instructions to initialize
1464 */
1465 void
1466 _mesa_init_instructions(struct prog_instruction *inst, GLuint count)
1467 {
1468 GLuint i;
1469
1470 _mesa_bzero(inst, count * sizeof(struct prog_instruction));
1471
1472 for (i = 0; i < count; i++) {
1473 inst[i].SrcReg[0].File = PROGRAM_UNDEFINED;
1474 inst[i].SrcReg[0].Swizzle = SWIZZLE_NOOP;
1475 inst[i].SrcReg[1].File = PROGRAM_UNDEFINED;
1476 inst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP;
1477 inst[i].SrcReg[2].File = PROGRAM_UNDEFINED;
1478 inst[i].SrcReg[2].Swizzle = SWIZZLE_NOOP;
1479
1480 inst[i].DstReg.File = PROGRAM_UNDEFINED;
1481 inst[i].DstReg.WriteMask = WRITEMASK_XYZW;
1482 inst[i].DstReg.CondMask = COND_TR;
1483 inst[i].DstReg.CondSwizzle = SWIZZLE_NOOP;
1484
1485 inst[i].SaturateMode = SATURATE_OFF;
1486 inst[i].Precision = FLOAT32;
1487 }
1488 }
1489
1490
1491 /**
1492 * Allocate an array of program instructions.
1493 * \param numInst number of instructions
1494 * \return pointer to instruction memory
1495 */
1496 struct prog_instruction *
1497 _mesa_alloc_instructions(GLuint numInst)
1498 {
1499 return (struct prog_instruction *)
1500 _mesa_calloc(numInst * sizeof(struct prog_instruction));
1501 }
1502
1503
1504 /**
1505 * Reallocate memory storing an array of program instructions.
1506 * This is used when we need to append additional instructions onto an
1507 * program.
1508 * \param oldInst pointer to first of old/src instructions
1509 * \param numOldInst number of instructions at <oldInst>
1510 * \param numNewInst desired size of new instruction array.
1511 * \return pointer to start of new instruction array.
1512 */
1513 struct prog_instruction *
1514 _mesa_realloc_instructions(struct prog_instruction *oldInst,
1515 GLuint numOldInst, GLuint numNewInst)
1516 {
1517 struct prog_instruction *newInst;
1518
1519 newInst = (struct prog_instruction *)
1520 _mesa_realloc(oldInst,
1521 numOldInst * sizeof(struct prog_instruction),
1522 numNewInst * sizeof(struct prog_instruction));
1523
1524 return newInst;
1525 }
1526
1527
1528 /**
1529 * Basic info about each instruction
1530 */
1531 struct instruction_info
1532 {
1533 gl_inst_opcode Opcode;
1534 const char *Name;
1535 GLuint NumSrcRegs;
1536 };
1537
1538 /**
1539 * Instruction info
1540 * \note Opcode should equal array index!
1541 */
1542 static const struct instruction_info InstInfo[MAX_OPCODE] = {
1543 { OPCODE_NOP, "NOP", 0 },
1544 { OPCODE_ABS, "ABS", 1 },
1545 { OPCODE_ADD, "ADD", 2 },
1546 { OPCODE_ARA, "ARA", 1 },
1547 { OPCODE_ARL, "ARL", 1 },
1548 { OPCODE_ARL_NV, "ARL", 1 },
1549 { OPCODE_ARR, "ARL", 1 },
1550 { OPCODE_BRA, "BRA", 0 },
1551 { OPCODE_CAL, "CAL", 0 },
1552 { OPCODE_CMP, "CMP", 3 },
1553 { OPCODE_COS, "COS", 1 },
1554 { OPCODE_DDX, "DDX", 1 },
1555 { OPCODE_DDY, "DDY", 1 },
1556 { OPCODE_DP3, "DP3", 2 },
1557 { OPCODE_DP4, "DP4", 2 },
1558 { OPCODE_DPH, "DPH", 2 },
1559 { OPCODE_DST, "DST", 2 },
1560 { OPCODE_END, "END", 0 },
1561 { OPCODE_EX2, "EX2", 1 },
1562 { OPCODE_EXP, "EXP", 1 },
1563 { OPCODE_FLR, "FLR", 1 },
1564 { OPCODE_FRC, "FRC", 1 },
1565 { OPCODE_KIL, "KIL", 1 },
1566 { OPCODE_KIL_NV, "KIL", 0 },
1567 { OPCODE_LG2, "LG2", 1 },
1568 { OPCODE_LIT, "LIT", 1 },
1569 { OPCODE_LOG, "LOG", 1 },
1570 { OPCODE_LRP, "LRP", 3 },
1571 { OPCODE_MAD, "MAD", 3 },
1572 { OPCODE_MAX, "MAX", 2 },
1573 { OPCODE_MIN, "MIN", 2 },
1574 { OPCODE_MOV, "MOV", 1 },
1575 { OPCODE_MUL, "MUL", 2 },
1576 { OPCODE_PK2H, "PK2H", 1 },
1577 { OPCODE_PK2US, "PK2US", 1 },
1578 { OPCODE_PK4B, "PK4B", 1 },
1579 { OPCODE_PK4UB, "PK4UB", 1 },
1580 { OPCODE_POW, "POW", 2 },
1581 { OPCODE_POPA, "POPA", 0 },
1582 { OPCODE_PRINT, "PRINT", 1 },
1583 { OPCODE_PUSHA, "PUSHA", 0 },
1584 { OPCODE_RCC, "RCC", 1 },
1585 { OPCODE_RCP, "RCP", 1 },
1586 { OPCODE_RET, "RET", 0 },
1587 { OPCODE_RFL, "RFL", 1 },
1588 { OPCODE_RSQ, "RSQ", 1 },
1589 { OPCODE_SCS, "SCS", 1 },
1590 { OPCODE_SEQ, "SEQ", 2 },
1591 { OPCODE_SFL, "SFL", 0 },
1592 { OPCODE_SGE, "SGE", 2 },
1593 { OPCODE_SGT, "SGT", 2 },
1594 { OPCODE_SIN, "SIN", 1 },
1595 { OPCODE_SLE, "SLE", 2 },
1596 { OPCODE_SLT, "SLT", 2 },
1597 { OPCODE_SNE, "SNE", 2 },
1598 { OPCODE_SSG, "SSG", 1 },
1599 { OPCODE_STR, "STR", 0 },
1600 { OPCODE_SUB, "SUB", 2 },
1601 { OPCODE_SWZ, "SWZ", 1 },
1602 { OPCODE_TEX, "TEX", 1 },
1603 { OPCODE_TXB, "TXB", 1 },
1604 { OPCODE_TXD, "TXD", 3 },
1605 { OPCODE_TXL, "TXL", 1 },
1606 { OPCODE_TXP, "TXP", 1 },
1607 { OPCODE_TXP_NV, "TXP", 1 },
1608 { OPCODE_UP2H, "UP2H", 1 },
1609 { OPCODE_UP2US, "UP2US", 1 },
1610 { OPCODE_UP4B, "UP4B", 1 },
1611 { OPCODE_UP4UB, "UP4UB", 1 },
1612 { OPCODE_X2D, "X2D", 3 },
1613 { OPCODE_XPD, "XPD", 2 }
1614 };
1615
1616
1617 /**
1618 * Return the number of src registers for the given instruction/opcode.
1619 */
1620 GLuint
1621 _mesa_num_inst_src_regs(gl_inst_opcode opcode)
1622 {
1623 ASSERT(opcode == InstInfo[opcode].Opcode);
1624 ASSERT(OPCODE_XPD == InstInfo[OPCODE_XPD].Opcode);
1625 return InstInfo[opcode].NumSrcRegs;
1626 }
1627
1628
1629 /**
1630 * Return string name for given program opcode.
1631 */
1632 const char *
1633 _mesa_opcode_string(gl_inst_opcode opcode)
1634 {
1635 ASSERT(opcode < MAX_OPCODE);
1636 return InstInfo[opcode].Name;
1637 }
1638
1639 /**
1640 * Return string name for given program/register file.
1641 */
1642 static const char *
1643 program_file_string(enum register_file f)
1644 {
1645 switch (f) {
1646 case PROGRAM_TEMPORARY:
1647 return "TEMP";
1648 case PROGRAM_LOCAL_PARAM:
1649 return "LOCAL";
1650 case PROGRAM_ENV_PARAM:
1651 return "ENV";
1652 case PROGRAM_STATE_VAR:
1653 return "STATE";
1654 case PROGRAM_INPUT:
1655 return "INPUT";
1656 case PROGRAM_OUTPUT:
1657 return "OUTPUT";
1658 case PROGRAM_NAMED_PARAM:
1659 return "NAMED";
1660 case PROGRAM_CONSTANT:
1661 return "CONST";
1662 case PROGRAM_UNIFORM:
1663 return "UNIFORM";
1664 case PROGRAM_VARYING:
1665 return "VARYING";
1666 case PROGRAM_WRITE_ONLY:
1667 return "WRITE_ONLY";
1668 case PROGRAM_ADDRESS:
1669 return "ADDR";
1670 default:
1671 return "Unknown program file!";
1672 }
1673 }
1674
1675
1676 /**
1677 * Return a string representation of the given swizzle word.
1678 * If extended is true, use extended (comma-separated) format.
1679 */
1680 static const char *
1681 swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
1682 {
1683 static const char swz[] = "xyzw01";
1684 static char s[20];
1685 GLuint i = 0;
1686
1687 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
1688 return ""; /* no swizzle/negation */
1689
1690 if (!extended)
1691 s[i++] = '.';
1692
1693 if (negateBase & 0x1)
1694 s[i++] = '-';
1695 s[i++] = swz[GET_SWZ(swizzle, 0)];
1696
1697 if (extended) {
1698 s[i++] = ',';
1699 }
1700
1701 if (negateBase & 0x2)
1702 s[i++] = '-';
1703 s[i++] = swz[GET_SWZ(swizzle, 1)];
1704
1705 if (extended) {
1706 s[i++] = ',';
1707 }
1708
1709 if (negateBase & 0x4)
1710 s[i++] = '-';
1711 s[i++] = swz[GET_SWZ(swizzle, 2)];
1712
1713 if (extended) {
1714 s[i++] = ',';
1715 }
1716
1717 if (negateBase & 0x8)
1718 s[i++] = '-';
1719 s[i++] = swz[GET_SWZ(swizzle, 3)];
1720
1721 s[i] = 0;
1722 return s;
1723 }
1724
1725
1726 static const char *
1727 writemask_string(GLuint writeMask)
1728 {
1729 static char s[10];
1730 GLuint i = 0;
1731
1732 if (writeMask == WRITEMASK_XYZW)
1733 return "";
1734
1735 s[i++] = '.';
1736 if (writeMask & WRITEMASK_X)
1737 s[i++] = 'x';
1738 if (writeMask & WRITEMASK_Y)
1739 s[i++] = 'y';
1740 if (writeMask & WRITEMASK_Z)
1741 s[i++] = 'z';
1742 if (writeMask & WRITEMASK_W)
1743 s[i++] = 'w';
1744
1745 s[i] = 0;
1746 return s;
1747 }
1748
1749 static void
1750 print_dst_reg(const struct prog_dst_register *dstReg)
1751 {
1752 _mesa_printf(" %s[%d]%s",
1753 program_file_string((enum register_file) dstReg->File),
1754 dstReg->Index,
1755 writemask_string(dstReg->WriteMask));
1756 }
1757
1758 static void
1759 print_src_reg(const struct prog_src_register *srcReg)
1760 {
1761 _mesa_printf("%s[%d]%s",
1762 program_file_string((enum register_file) srcReg->File),
1763 srcReg->Index,
1764 swizzle_string(srcReg->Swizzle,
1765 srcReg->NegateBase, GL_FALSE));
1766 }
1767
1768 static void
1769 print_comment(const struct prog_instruction *inst)
1770 {
1771 if (inst->Comment)
1772 _mesa_printf("; # %s\n", inst->Comment);
1773 else
1774 _mesa_printf(";\n");
1775 }
1776
1777
1778 void
1779 _mesa_print_alu_instruction(const struct prog_instruction *inst,
1780 const char *opcode_string,
1781 GLuint numRegs)
1782 {
1783 GLuint j;
1784
1785 _mesa_printf("%s", opcode_string);
1786
1787 /* frag prog only */
1788 if (inst->SaturateMode == SATURATE_ZERO_ONE)
1789 _mesa_printf("_SAT");
1790
1791 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
1792 _mesa_printf(" %s[%d]%s",
1793 program_file_string((enum register_file) inst->DstReg.File),
1794 inst->DstReg.Index,
1795 writemask_string(inst->DstReg.WriteMask));
1796 }
1797 else {
1798 _mesa_printf(" ???");
1799 }
1800
1801 if (numRegs > 0)
1802 _mesa_printf(", ");
1803
1804 for (j = 0; j < numRegs; j++) {
1805 print_src_reg(inst->SrcReg + j);
1806 if (j + 1 < numRegs)
1807 _mesa_printf(", ");
1808 }
1809
1810 if (inst->Comment)
1811 _mesa_printf(" # %s", inst->Comment);
1812
1813 print_comment(inst);
1814 }
1815
1816
1817 /**
1818 * Print a single vertex/fragment program instruction.
1819 */
1820 void
1821 _mesa_print_instruction(const struct prog_instruction *inst)
1822 {
1823 switch (inst->Opcode) {
1824 case OPCODE_PRINT:
1825 _mesa_printf("PRINT '%s'", inst->Data);
1826 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
1827 _mesa_printf(", ");
1828 _mesa_printf("%s[%d]%s",
1829 program_file_string((enum register_file) inst->SrcReg[0].File),
1830 inst->SrcReg[0].Index,
1831 swizzle_string(inst->SrcReg[0].Swizzle,
1832 inst->SrcReg[0].NegateBase, GL_FALSE));
1833 }
1834 if (inst->Comment)
1835 _mesa_printf(" # %s", inst->Comment);
1836 print_comment(inst);
1837 break;
1838 case OPCODE_SWZ:
1839 _mesa_printf("SWZ");
1840 if (inst->SaturateMode == SATURATE_ZERO_ONE)
1841 _mesa_printf("_SAT");
1842 print_dst_reg(&inst->DstReg);
1843 _mesa_printf("%s[%d], %s",
1844 program_file_string((enum register_file) inst->SrcReg[0].File),
1845 inst->SrcReg[0].Index,
1846 swizzle_string(inst->SrcReg[0].Swizzle,
1847 inst->SrcReg[0].NegateBase, GL_TRUE));
1848 print_comment(inst);
1849 break;
1850 case OPCODE_TEX:
1851 case OPCODE_TXP:
1852 case OPCODE_TXB:
1853 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
1854 if (inst->SaturateMode == SATURATE_ZERO_ONE)
1855 _mesa_printf("_SAT");
1856 _mesa_printf(" ");
1857 print_dst_reg(&inst->DstReg);
1858 _mesa_printf(", ");
1859 print_src_reg(&inst->SrcReg[0]);
1860 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
1861 switch (inst->TexSrcTarget) {
1862 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
1863 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
1864 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
1865 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
1866 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
1867 default:
1868 ;
1869 }
1870 print_comment(inst);
1871 break;
1872 case OPCODE_ARL:
1873 _mesa_printf("ARL addr.x, ");
1874 print_src_reg(&inst->SrcReg[0]);
1875 print_comment(inst);
1876 break;
1877 case OPCODE_BRA:
1878 _mesa_printf("BRA %u", inst->BranchTarget);
1879 print_comment(inst);
1880 break;
1881 case OPCODE_CAL:
1882 _mesa_printf("CAL %u", inst->BranchTarget);
1883 print_comment(inst);
1884 break;
1885 case OPCODE_END:
1886 _mesa_printf("END");
1887 print_comment(inst);
1888 break;
1889 /* XXX may need other special-case instructions */
1890 default:
1891 /* typical alu instruction */
1892 _mesa_print_alu_instruction(inst,
1893 _mesa_opcode_string(inst->Opcode),
1894 _mesa_num_inst_src_regs(inst->Opcode));
1895 break;
1896 }
1897 }
1898
1899
1900 /**
1901 * Print a vertx/fragment program to stdout.
1902 * XXX this function could be greatly improved.
1903 */
1904 void
1905 _mesa_print_program(const struct gl_program *prog)
1906 {
1907 GLuint i;
1908 for (i = 0; i < prog->NumInstructions; i++) {
1909 _mesa_printf("%3d: ", i);
1910 _mesa_print_instruction(prog->Instructions + i);
1911 }
1912 }
1913
1914
1915 /**
1916 * Print all of a program's parameters.
1917 */
1918 void
1919 _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
1920 {
1921 GLint i;
1922
1923 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
1924 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
1925 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
1926 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
1927 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
1928
1929 _mesa_load_state_parameters(ctx, prog->Parameters);
1930
1931 #if 0
1932 _mesa_printf("Local Params:\n");
1933 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
1934 const GLfloat *p = prog->LocalParams[i];
1935 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
1936 }
1937 #endif
1938
1939 for (i = 0; i < prog->Parameters->NumParameters; i++){
1940 struct gl_program_parameter *param = prog->Parameters->Parameters + i;
1941 const GLfloat *v = prog->Parameters->ParameterValues[i];
1942 _mesa_printf("param[%d] %s %s = {%.3f, %.3f, %.3f, %.3f};\n",
1943 i,
1944 program_file_string(prog->Parameters->Parameters[i].Type),
1945 param->Name, v[0], v[1], v[2], v[3]);
1946 }
1947 }
1948
1949
1950 /**
1951 * Mixing ARB and NV vertex/fragment programs can be tricky.
1952 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
1953 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
1954 * The two different fragment program targets are supposed to be compatible
1955 * to some extent (see GL_ARB_fragment_program spec).
1956 * This function does the compatibility check.
1957 */
1958 static GLboolean
1959 compatible_program_targets(GLenum t1, GLenum t2)
1960 {
1961 if (t1 == t2)
1962 return GL_TRUE;
1963 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
1964 return GL_TRUE;
1965 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
1966 return GL_TRUE;
1967 return GL_FALSE;
1968 }
1969
1970
1971
1972 /**********************************************************************/
1973 /* API functions */
1974 /**********************************************************************/
1975
1976
1977 /**
1978 * Bind a program (make it current)
1979 * \note Called from the GL API dispatcher by both glBindProgramNV
1980 * and glBindProgramARB.
1981 */
1982 void GLAPIENTRY
1983 _mesa_BindProgram(GLenum target, GLuint id)
1984 {
1985 struct gl_program *curProg, *newProg;
1986 GET_CURRENT_CONTEXT(ctx);
1987 ASSERT_OUTSIDE_BEGIN_END(ctx);
1988
1989 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1990
1991 /* Error-check target and get curProg */
1992 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
1993 (ctx->Extensions.NV_vertex_program ||
1994 ctx->Extensions.ARB_vertex_program)) {
1995 curProg = &ctx->VertexProgram.Current->Base;
1996 }
1997 else if ((target == GL_FRAGMENT_PROGRAM_NV
1998 && ctx->Extensions.NV_fragment_program) ||
1999 (target == GL_FRAGMENT_PROGRAM_ARB
2000 && ctx->Extensions.ARB_fragment_program)) {
2001 curProg = &ctx->FragmentProgram.Current->Base;
2002 }
2003 else {
2004 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
2005 return;
2006 }
2007
2008 /*
2009 * Get pointer to new program to bind.
2010 * NOTE: binding to a non-existant program is not an error.
2011 * That's supposed to be caught in glBegin.
2012 */
2013 if (id == 0) {
2014 /* Bind a default program */
2015 newProg = NULL;
2016 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
2017 newProg = ctx->Shared->DefaultVertexProgram;
2018 else
2019 newProg = ctx->Shared->DefaultFragmentProgram;
2020 }
2021 else {
2022 /* Bind a user program */
2023 newProg = _mesa_lookup_program(ctx, id);
2024 if (!newProg || newProg == &_mesa_DummyProgram) {
2025 /* allocate a new program now */
2026 newProg = ctx->Driver.NewProgram(ctx, target, id);
2027 if (!newProg) {
2028 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
2029 return;
2030 }
2031 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
2032 }
2033 else if (!compatible_program_targets(newProg->Target, target)) {
2034 _mesa_error(ctx, GL_INVALID_OPERATION,
2035 "glBindProgramNV/ARB(target mismatch)");
2036 return;
2037 }
2038 }
2039
2040 /** All error checking is complete now **/
2041
2042 if (curProg->Id == id) {
2043 /* binding same program - no change */
2044 return;
2045 }
2046
2047 /* unbind/delete oldProg */
2048 if (curProg->Id != 0) {
2049 /* decrement refcount on previously bound fragment program */
2050 curProg->RefCount--;
2051 /* and delete if refcount goes below one */
2052 if (curProg->RefCount <= 0) {
2053 /* the program ID was already removed from the hash table */
2054 ctx->Driver.DeleteProgram(ctx, curProg);
2055 }
2056 }
2057
2058 /* bind newProg */
2059 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
2060 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
2061 }
2062 else if (target == GL_FRAGMENT_PROGRAM_NV ||
2063 target == GL_FRAGMENT_PROGRAM_ARB) {
2064 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
2065 }
2066 newProg->RefCount++;
2067
2068 /* Never null pointers */
2069 ASSERT(ctx->VertexProgram.Current);
2070 ASSERT(ctx->FragmentProgram.Current);
2071
2072 if (ctx->Driver.BindProgram)
2073 ctx->Driver.BindProgram(ctx, target, newProg);
2074 }
2075
2076
2077 /**
2078 * Delete a list of programs.
2079 * \note Not compiled into display lists.
2080 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
2081 */
2082 void GLAPIENTRY
2083 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
2084 {
2085 GLint i;
2086 GET_CURRENT_CONTEXT(ctx);
2087 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2088
2089 if (n < 0) {
2090 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
2091 return;
2092 }
2093
2094 for (i = 0; i < n; i++) {
2095 if (ids[i] != 0) {
2096 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
2097 if (prog == &_mesa_DummyProgram) {
2098 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
2099 }
2100 else if (prog) {
2101 /* Unbind program if necessary */
2102 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
2103 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
2104 if (ctx->VertexProgram.Current &&
2105 ctx->VertexProgram.Current->Base.Id == ids[i]) {
2106 /* unbind this currently bound program */
2107 _mesa_BindProgram(prog->Target, 0);
2108 }
2109 }
2110 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
2111 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
2112 if (ctx->FragmentProgram.Current &&
2113 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
2114 /* unbind this currently bound program */
2115 _mesa_BindProgram(prog->Target, 0);
2116 }
2117 }
2118 else {
2119 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
2120 return;
2121 }
2122 /* The ID is immediately available for re-use now */
2123 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
2124 prog->RefCount--;
2125 if (prog->RefCount <= 0) {
2126 ctx->Driver.DeleteProgram(ctx, prog);
2127 }
2128 }
2129 }
2130 }
2131 }
2132
2133
2134 /**
2135 * Generate a list of new program identifiers.
2136 * \note Not compiled into display lists.
2137 * \note Called by both glGenProgramsNV and glGenProgramsARB.
2138 */
2139 void GLAPIENTRY
2140 _mesa_GenPrograms(GLsizei n, GLuint *ids)
2141 {
2142 GLuint first;
2143 GLuint i;
2144 GET_CURRENT_CONTEXT(ctx);
2145 ASSERT_OUTSIDE_BEGIN_END(ctx);
2146
2147 if (n < 0) {
2148 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
2149 return;
2150 }
2151
2152 if (!ids)
2153 return;
2154
2155 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
2156
2157 /* Insert pointer to dummy program as placeholder */
2158 for (i = 0; i < (GLuint) n; i++) {
2159 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
2160 }
2161
2162 /* Return the program names */
2163 for (i = 0; i < (GLuint) n; i++) {
2164 ids[i] = first + i;
2165 }
2166 }
2167
2168
2169 /**********************************************************************/
2170 /* GL_MESA_program_debug extension */
2171 /**********************************************************************/
2172
2173
2174 /* XXX temporary */
2175 GLAPI void GLAPIENTRY
2176 glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
2177 GLvoid *data)
2178 {
2179 _mesa_ProgramCallbackMESA(target, callback, data);
2180 }
2181
2182
2183 void
2184 _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
2185 GLvoid *data)
2186 {
2187 GET_CURRENT_CONTEXT(ctx);
2188
2189 switch (target) {
2190 case GL_FRAGMENT_PROGRAM_ARB:
2191 if (!ctx->Extensions.ARB_fragment_program) {
2192 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2193 return;
2194 }
2195 ctx->FragmentProgram.Callback = callback;
2196 ctx->FragmentProgram.CallbackData = data;
2197 break;
2198 case GL_FRAGMENT_PROGRAM_NV:
2199 if (!ctx->Extensions.NV_fragment_program) {
2200 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2201 return;
2202 }
2203 ctx->FragmentProgram.Callback = callback;
2204 ctx->FragmentProgram.CallbackData = data;
2205 break;
2206 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
2207 if (!ctx->Extensions.ARB_vertex_program &&
2208 !ctx->Extensions.NV_vertex_program) {
2209 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2210 return;
2211 }
2212 ctx->VertexProgram.Callback = callback;
2213 ctx->VertexProgram.CallbackData = data;
2214 break;
2215 default:
2216 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2217 return;
2218 }
2219 }
2220
2221
2222 /* XXX temporary */
2223 GLAPI void GLAPIENTRY
2224 glGetProgramRegisterfvMESA(GLenum target,
2225 GLsizei len, const GLubyte *registerName,
2226 GLfloat *v)
2227 {
2228 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
2229 }
2230
2231
2232 void
2233 _mesa_GetProgramRegisterfvMESA(GLenum target,
2234 GLsizei len, const GLubyte *registerName,
2235 GLfloat *v)
2236 {
2237 char reg[1000];
2238 GET_CURRENT_CONTEXT(ctx);
2239
2240 /* We _should_ be inside glBegin/glEnd */
2241 #if 0
2242 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
2243 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
2244 return;
2245 }
2246 #endif
2247
2248 /* make null-terminated copy of registerName */
2249 len = MIN2((unsigned int) len, sizeof(reg) - 1);
2250 _mesa_memcpy(reg, registerName, len);
2251 reg[len] = 0;
2252
2253 switch (target) {
2254 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
2255 if (!ctx->Extensions.ARB_vertex_program &&
2256 !ctx->Extensions.NV_vertex_program) {
2257 _mesa_error(ctx, GL_INVALID_ENUM,
2258 "glGetProgramRegisterfvMESA(target)");
2259 return;
2260 }
2261 if (!ctx->VertexProgram._Enabled) {
2262 _mesa_error(ctx, GL_INVALID_OPERATION,
2263 "glGetProgramRegisterfvMESA");
2264 return;
2265 }
2266 /* GL_NV_vertex_program */
2267 if (reg[0] == 'R') {
2268 /* Temp register */
2269 GLint i = _mesa_atoi(reg + 1);
2270 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
2271 _mesa_error(ctx, GL_INVALID_VALUE,
2272 "glGetProgramRegisterfvMESA(registerName)");
2273 return;
2274 }
2275 #if 0 /* FIX ME */
2276 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_TEMPORARY, i, v);
2277 #endif
2278 }
2279 else if (reg[0] == 'v' && reg[1] == '[') {
2280 /* Vertex Input attribute */
2281 GLuint i;
2282 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
2283 const char *name = _mesa_nv_vertex_input_register_name(i);
2284 char number[10];
2285 _mesa_sprintf(number, "%d", i);
2286 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
2287 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
2288 #if 0 /* FIX ME */
2289 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_INPUT,
2290 i, v);
2291 #endif
2292 return;
2293 }
2294 }
2295 _mesa_error(ctx, GL_INVALID_VALUE,
2296 "glGetProgramRegisterfvMESA(registerName)");
2297 return;
2298 }
2299 else if (reg[0] == 'o' && reg[1] == '[') {
2300 /* Vertex output attribute */
2301 }
2302 /* GL_ARB_vertex_program */
2303 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
2304
2305 }
2306 else {
2307 _mesa_error(ctx, GL_INVALID_VALUE,
2308 "glGetProgramRegisterfvMESA(registerName)");
2309 return;
2310 }
2311 break;
2312 case GL_FRAGMENT_PROGRAM_ARB:
2313 if (!ctx->Extensions.ARB_fragment_program) {
2314 _mesa_error(ctx, GL_INVALID_ENUM,
2315 "glGetProgramRegisterfvMESA(target)");
2316 return;
2317 }
2318 if (!ctx->FragmentProgram._Enabled) {
2319 _mesa_error(ctx, GL_INVALID_OPERATION,
2320 "glGetProgramRegisterfvMESA");
2321 return;
2322 }
2323 /* XXX to do */
2324 break;
2325 case GL_FRAGMENT_PROGRAM_NV:
2326 if (!ctx->Extensions.NV_fragment_program) {
2327 _mesa_error(ctx, GL_INVALID_ENUM,
2328 "glGetProgramRegisterfvMESA(target)");
2329 return;
2330 }
2331 if (!ctx->FragmentProgram._Enabled) {
2332 _mesa_error(ctx, GL_INVALID_OPERATION,
2333 "glGetProgramRegisterfvMESA");
2334 return;
2335 }
2336 if (reg[0] == 'R') {
2337 /* Temp register */
2338 GLint i = _mesa_atoi(reg + 1);
2339 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
2340 _mesa_error(ctx, GL_INVALID_VALUE,
2341 "glGetProgramRegisterfvMESA(registerName)");
2342 return;
2343 }
2344 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_TEMPORARY,
2345 i, v);
2346 }
2347 else if (reg[0] == 'f' && reg[1] == '[') {
2348 /* Fragment input attribute */
2349 GLuint i;
2350 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
2351 const char *name = _mesa_nv_fragment_input_register_name(i);
2352 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
2353 ctx->Driver.GetFragmentProgramRegister(ctx,
2354 PROGRAM_INPUT, i, v);
2355 return;
2356 }
2357 }
2358 _mesa_error(ctx, GL_INVALID_VALUE,
2359 "glGetProgramRegisterfvMESA(registerName)");
2360 return;
2361 }
2362 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
2363 /* Fragment output color */
2364 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
2365 FRAG_RESULT_COLR, v);
2366 }
2367 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
2368 /* Fragment output color */
2369 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
2370 FRAG_RESULT_COLH, v);
2371 }
2372 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
2373 /* Fragment output depth */
2374 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
2375 FRAG_RESULT_DEPR, v);
2376 }
2377 else {
2378 /* try user-defined identifiers */
2379 const GLfloat *value = _mesa_lookup_parameter_value(
2380 ctx->FragmentProgram.Current->Base.Parameters, -1, reg);
2381 if (value) {
2382 COPY_4V(v, value);
2383 }
2384 else {
2385 _mesa_error(ctx, GL_INVALID_VALUE,
2386 "glGetProgramRegisterfvMESA(registerName)");
2387 return;
2388 }
2389 }
2390 break;
2391 default:
2392 _mesa_error(ctx, GL_INVALID_ENUM,
2393 "glGetProgramRegisterfvMESA(target)");
2394 return;
2395 }
2396 }