Merge git://proxy01.pd.intel.com:9419/git/mesa/mesa into crestline
[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 case STATE_FOG_PARAMS_OPTIMIZED:
1000 /* for simpler per-vertex/pixel fog calcs. POW
1001 (for EXP/EXP2 fog) might be more expensive than EX2 on some hw,
1002 plus it needs another constant (e) anyway. Linear fog can now be
1003 done with a single MAD.
1004 linear: fogcoord * -1/(end-start) + end/(end-start)
1005 exp: 2^-(density/ln(2) * fogcoord)
1006 exp2: 2^-((density/(ln(2)^2) * fogcoord)^2) */
1007 value[0] = -1.0F / (ctx->Fog.End - ctx->Fog.Start);
1008 value[1] = ctx->Fog.End / (ctx->Fog.End - ctx->Fog.Start);
1009 value[2] = ctx->Fog.Density * ONE_DIV_LN2;
1010 value[3] = ctx->Fog.Density * ONE_DIV_SQRT_LN2;
1011 break;
1012 case STATE_SPOT_DIR_NORMALIZED: {
1013 /* here, state[2] is the light number */
1014 /* pre-normalize spot dir */
1015 const GLuint ln = (GLuint) state[2];
1016 value[0] = ctx->Light.Light[ln].EyeDirection[0];
1017 value[1] = ctx->Light.Light[ln].EyeDirection[1];
1018 value[2] = ctx->Light.Light[ln].EyeDirection[2];
1019 NORMALIZE_3FV(value);
1020 value[3] = ctx->Light.Light[ln]._CosCutoff;
1021 break;
1022 }
1023 default:
1024 /* unknown state indexes are silently ignored
1025 * should be handled by the driver.
1026 */
1027 return;
1028 }
1029 }
1030 return;
1031
1032 default:
1033 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
1034 return;
1035 }
1036 }
1037
1038
1039 /**
1040 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
1041 * indicate that the given context state may have changed.
1042 * The bitmask is used during validation to determine if we need to update
1043 * vertex/fragment program parameters (like "state.material.color") when
1044 * some GL state has changed.
1045 */
1046 static GLbitfield
1047 make_state_flags(const GLint state[])
1048 {
1049 switch (state[0]) {
1050 case STATE_MATERIAL:
1051 case STATE_LIGHT:
1052 case STATE_LIGHTMODEL_AMBIENT:
1053 case STATE_LIGHTMODEL_SCENECOLOR:
1054 case STATE_LIGHTPROD:
1055 return _NEW_LIGHT;
1056
1057 case STATE_TEXGEN:
1058 case STATE_TEXENV_COLOR:
1059 return _NEW_TEXTURE;
1060
1061 case STATE_FOG_COLOR:
1062 case STATE_FOG_PARAMS:
1063 return _NEW_FOG;
1064
1065 case STATE_CLIPPLANE:
1066 return _NEW_TRANSFORM;
1067
1068 case STATE_POINT_SIZE:
1069 case STATE_POINT_ATTENUATION:
1070 return _NEW_POINT;
1071
1072 case STATE_MATRIX:
1073 switch (state[1]) {
1074 case STATE_MODELVIEW:
1075 return _NEW_MODELVIEW;
1076 case STATE_PROJECTION:
1077 return _NEW_PROJECTION;
1078 case STATE_MVP:
1079 return _NEW_MODELVIEW | _NEW_PROJECTION;
1080 case STATE_TEXTURE:
1081 return _NEW_TEXTURE_MATRIX;
1082 case STATE_PROGRAM:
1083 return _NEW_TRACK_MATRIX;
1084 default:
1085 _mesa_problem(NULL, "unexpected matrix in make_state_flags()");
1086 return 0;
1087 }
1088
1089 case STATE_DEPTH_RANGE:
1090 return _NEW_VIEWPORT;
1091
1092 case STATE_FRAGMENT_PROGRAM:
1093 case STATE_VERTEX_PROGRAM:
1094 return _NEW_PROGRAM;
1095
1096 case STATE_INTERNAL:
1097 switch (state[1]) {
1098 case STATE_NORMAL_SCALE:
1099 return _NEW_MODELVIEW;
1100 case STATE_TEXRECT_SCALE:
1101 return _NEW_TEXTURE;
1102 case STATE_FOG_PARAMS_OPTIMIZED:
1103 return _NEW_FOG;
1104 case STATE_SPOT_DIR_NORMALIZED:
1105 return _NEW_LIGHT;
1106 default:
1107 /* unknown state indexes are silently ignored and
1108 * no flag set, since it is handled by the driver.
1109 */
1110 return 0;
1111 }
1112
1113 default:
1114 _mesa_problem(NULL, "unexpected state[0] in make_state_flags()");
1115 return 0;
1116 }
1117 }
1118
1119
1120 static void
1121 append(char *dst, const char *src)
1122 {
1123 while (*dst)
1124 dst++;
1125 while (*src)
1126 *dst++ = *src++;
1127 *dst = 0;
1128 }
1129
1130
1131 static void
1132 append_token(char *dst, enum state_index k)
1133 {
1134 switch (k) {
1135 case STATE_MATERIAL:
1136 append(dst, "material.");
1137 break;
1138 case STATE_LIGHT:
1139 append(dst, "light");
1140 break;
1141 case STATE_LIGHTMODEL_AMBIENT:
1142 append(dst, "lightmodel.ambient");
1143 break;
1144 case STATE_LIGHTMODEL_SCENECOLOR:
1145 break;
1146 case STATE_LIGHTPROD:
1147 append(dst, "lightprod");
1148 break;
1149 case STATE_TEXGEN:
1150 append(dst, "texgen");
1151 break;
1152 case STATE_FOG_COLOR:
1153 append(dst, "fog.color");
1154 break;
1155 case STATE_FOG_PARAMS:
1156 append(dst, "fog.params");
1157 break;
1158 case STATE_CLIPPLANE:
1159 append(dst, "clip");
1160 break;
1161 case STATE_POINT_SIZE:
1162 append(dst, "point.size");
1163 break;
1164 case STATE_POINT_ATTENUATION:
1165 append(dst, "point.attenuation");
1166 break;
1167 case STATE_MATRIX:
1168 append(dst, "matrix.");
1169 break;
1170 case STATE_MODELVIEW:
1171 append(dst, "modelview");
1172 break;
1173 case STATE_PROJECTION:
1174 append(dst, "projection");
1175 break;
1176 case STATE_MVP:
1177 append(dst, "mvp");
1178 break;
1179 case STATE_TEXTURE:
1180 append(dst, "texture");
1181 break;
1182 case STATE_PROGRAM:
1183 append(dst, "program");
1184 break;
1185 case STATE_MATRIX_INVERSE:
1186 append(dst, ".inverse");
1187 break;
1188 case STATE_MATRIX_TRANSPOSE:
1189 append(dst, ".transpose");
1190 break;
1191 case STATE_MATRIX_INVTRANS:
1192 append(dst, ".invtrans");
1193 break;
1194 case STATE_AMBIENT:
1195 append(dst, "ambient");
1196 break;
1197 case STATE_DIFFUSE:
1198 append(dst, "diffuse");
1199 break;
1200 case STATE_SPECULAR:
1201 append(dst, "specular");
1202 break;
1203 case STATE_EMISSION:
1204 append(dst, "emission");
1205 break;
1206 case STATE_SHININESS:
1207 append(dst, "shininess");
1208 break;
1209 case STATE_HALF:
1210 append(dst, "half");
1211 break;
1212 case STATE_POSITION:
1213 append(dst, ".position");
1214 break;
1215 case STATE_ATTENUATION:
1216 append(dst, ".attenuation");
1217 break;
1218 case STATE_SPOT_DIRECTION:
1219 append(dst, ".spot.direction");
1220 break;
1221 case STATE_TEXGEN_EYE_S:
1222 append(dst, "eye.s");
1223 break;
1224 case STATE_TEXGEN_EYE_T:
1225 append(dst, "eye.t");
1226 break;
1227 case STATE_TEXGEN_EYE_R:
1228 append(dst, "eye.r");
1229 break;
1230 case STATE_TEXGEN_EYE_Q:
1231 append(dst, "eye.q");
1232 break;
1233 case STATE_TEXGEN_OBJECT_S:
1234 append(dst, "object.s");
1235 break;
1236 case STATE_TEXGEN_OBJECT_T:
1237 append(dst, "object.t");
1238 break;
1239 case STATE_TEXGEN_OBJECT_R:
1240 append(dst, "object.r");
1241 break;
1242 case STATE_TEXGEN_OBJECT_Q:
1243 append(dst, "object.q");
1244 break;
1245 case STATE_TEXENV_COLOR:
1246 append(dst, "texenv");
1247 break;
1248 case STATE_DEPTH_RANGE:
1249 append(dst, "depth.range");
1250 break;
1251 case STATE_VERTEX_PROGRAM:
1252 case STATE_FRAGMENT_PROGRAM:
1253 break;
1254 case STATE_ENV:
1255 append(dst, "env");
1256 break;
1257 case STATE_LOCAL:
1258 append(dst, "local");
1259 break;
1260 case STATE_INTERNAL:
1261 case STATE_NORMAL_SCALE:
1262 case STATE_POSITION_NORMALIZED:
1263 case STATE_FOG_PARAMS_OPTIMIZED:
1264 case STATE_SPOT_DIR_NORMALIZED:
1265 append(dst, "(internal)");
1266 break;
1267 default:
1268 ;
1269 }
1270 }
1271
1272 static void
1273 append_face(char *dst, GLint face)
1274 {
1275 if (face == 0)
1276 append(dst, "front.");
1277 else
1278 append(dst, "back.");
1279 }
1280
1281 static void
1282 append_index(char *dst, GLint index)
1283 {
1284 char s[20];
1285 _mesa_sprintf(s, "[%d].", index);
1286 append(dst, s);
1287 }
1288
1289 /**
1290 * Make a string from the given state vector.
1291 * For example, return "state.matrix.texture[2].inverse".
1292 * Use _mesa_free() to deallocate the string.
1293 */
1294 static const char *
1295 make_state_string(const GLint state[6])
1296 {
1297 char str[1000] = "";
1298 char tmp[30];
1299
1300 append(str, "state.");
1301 append_token(str, (enum state_index) state[0]);
1302
1303 switch (state[0]) {
1304 case STATE_MATERIAL:
1305 append_face(str, state[1]);
1306 append_token(str, (enum state_index) state[2]);
1307 break;
1308 case STATE_LIGHT:
1309 append(str, "light");
1310 append_index(str, state[1]); /* light number [i]. */
1311 append_token(str, (enum state_index) state[2]); /* coefficients */
1312 break;
1313 case STATE_LIGHTMODEL_AMBIENT:
1314 append(str, "lightmodel.ambient");
1315 break;
1316 case STATE_LIGHTMODEL_SCENECOLOR:
1317 if (state[1] == 0) {
1318 append(str, "lightmodel.front.scenecolor");
1319 }
1320 else {
1321 append(str, "lightmodel.back.scenecolor");
1322 }
1323 break;
1324 case STATE_LIGHTPROD:
1325 append_index(str, state[1]); /* light number [i]. */
1326 append_face(str, state[2]);
1327 append_token(str, (enum state_index) state[3]);
1328 break;
1329 case STATE_TEXGEN:
1330 append_index(str, state[1]); /* tex unit [i] */
1331 append_token(str, (enum state_index) state[2]); /* plane coef */
1332 break;
1333 case STATE_TEXENV_COLOR:
1334 append_index(str, state[1]); /* tex unit [i] */
1335 append(str, "color");
1336 break;
1337 case STATE_FOG_COLOR:
1338 case STATE_FOG_PARAMS:
1339 break;
1340 case STATE_CLIPPLANE:
1341 append_index(str, state[1]); /* plane [i] */
1342 append(str, "plane");
1343 break;
1344 case STATE_POINT_SIZE:
1345 case STATE_POINT_ATTENUATION:
1346 break;
1347 case STATE_MATRIX:
1348 {
1349 /* state[1] = modelview, projection, texture, etc. */
1350 /* state[2] = which texture matrix or program matrix */
1351 /* state[3] = first column to fetch */
1352 /* state[4] = last column to fetch */
1353 /* state[5] = transpose, inverse or invtrans */
1354 const enum state_index mat = (enum state_index) state[1];
1355 const GLuint index = (GLuint) state[2];
1356 const GLuint first = (GLuint) state[3];
1357 const GLuint last = (GLuint) state[4];
1358 const enum state_index modifier = (enum state_index) state[5];
1359 append_token(str, mat);
1360 if (index)
1361 append_index(str, index);
1362 if (modifier)
1363 append_token(str, modifier);
1364 if (first == last)
1365 _mesa_sprintf(tmp, ".row[%d]", first);
1366 else
1367 _mesa_sprintf(tmp, ".row[%d..%d]", first, last);
1368 append(str, tmp);
1369 }
1370 break;
1371 case STATE_DEPTH_RANGE:
1372 break;
1373 case STATE_FRAGMENT_PROGRAM:
1374 case STATE_VERTEX_PROGRAM:
1375 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1376 /* state[2] = parameter index */
1377 append_token(str, (enum state_index) state[1]);
1378 append_index(str, state[2]);
1379 break;
1380 case STATE_INTERNAL:
1381 break;
1382 default:
1383 _mesa_problem(NULL, "Invalid state in make_state_string");
1384 break;
1385 }
1386
1387 return _mesa_strdup(str);
1388 }
1389
1390
1391 /**
1392 * Loop over all the parameters in a parameter list. If the parameter
1393 * is a GL state reference, look up the current value of that state
1394 * variable and put it into the parameter's Value[4] array.
1395 * This would be called at glBegin time when using a fragment program.
1396 */
1397 void
1398 _mesa_load_state_parameters(GLcontext *ctx,
1399 struct gl_program_parameter_list *paramList)
1400 {
1401 GLuint i;
1402
1403 if (!paramList)
1404 return;
1405
1406 for (i = 0; i < paramList->NumParameters; i++) {
1407 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1408 _mesa_fetch_state(ctx,
1409 paramList->Parameters[i].StateIndexes,
1410 paramList->ParameterValues[i]);
1411 }
1412 }
1413 }
1414
1415
1416 /**
1417 * Initialize program instruction fields to defaults.
1418 * \param inst first instruction to initialize
1419 * \param count number of instructions to initialize
1420 */
1421 void
1422 _mesa_init_instructions(struct prog_instruction *inst, GLuint count)
1423 {
1424 GLuint i;
1425
1426 _mesa_bzero(inst, count * sizeof(struct prog_instruction));
1427
1428 for (i = 0; i < count; i++) {
1429 inst[i].SrcReg[0].File = PROGRAM_UNDEFINED;
1430 inst[i].SrcReg[0].Swizzle = SWIZZLE_NOOP;
1431 inst[i].SrcReg[1].File = PROGRAM_UNDEFINED;
1432 inst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP;
1433 inst[i].SrcReg[2].File = PROGRAM_UNDEFINED;
1434 inst[i].SrcReg[2].Swizzle = SWIZZLE_NOOP;
1435
1436 inst[i].DstReg.File = PROGRAM_UNDEFINED;
1437 inst[i].DstReg.WriteMask = WRITEMASK_XYZW;
1438 inst[i].DstReg.CondMask = COND_TR;
1439 inst[i].DstReg.CondSwizzle = SWIZZLE_NOOP;
1440
1441 inst[i].SaturateMode = SATURATE_OFF;
1442 inst[i].Precision = FLOAT32;
1443 }
1444 }
1445
1446
1447 /**
1448 * Allocate an array of program instructions.
1449 * \param numInst number of instructions
1450 * \return pointer to instruction memory
1451 */
1452 struct prog_instruction *
1453 _mesa_alloc_instructions(GLuint numInst)
1454 {
1455 return (struct prog_instruction *)
1456 _mesa_calloc(numInst * sizeof(struct prog_instruction));
1457 }
1458
1459
1460 /**
1461 * Reallocate memory storing an array of program instructions.
1462 * This is used when we need to append additional instructions onto an
1463 * program.
1464 * \param oldInst pointer to first of old/src instructions
1465 * \param numOldInst number of instructions at <oldInst>
1466 * \param numNewInst desired size of new instruction array.
1467 * \return pointer to start of new instruction array.
1468 */
1469 struct prog_instruction *
1470 _mesa_realloc_instructions(struct prog_instruction *oldInst,
1471 GLuint numOldInst, GLuint numNewInst)
1472 {
1473 struct prog_instruction *newInst;
1474
1475 newInst = (struct prog_instruction *)
1476 _mesa_realloc(oldInst,
1477 numOldInst * sizeof(struct prog_instruction),
1478 numNewInst * sizeof(struct prog_instruction));
1479
1480 return newInst;
1481 }
1482
1483
1484 /**
1485 * Basic info about each instruction
1486 */
1487 struct instruction_info
1488 {
1489 enum prog_opcode Opcode;
1490 const char *Name;
1491 GLuint NumSrcRegs;
1492 };
1493
1494 /**
1495 * Instruction info
1496 * \note Opcode should equal array index!
1497 */
1498 static const struct instruction_info InstInfo[MAX_OPCODE] = {
1499 { OPCODE_ABS, "ABS", 1 },
1500 { OPCODE_ADD, "ADD", 2 },
1501 { OPCODE_ARA, "ARA", 1 },
1502 { OPCODE_ARL, "ARL", 1 },
1503 { OPCODE_ARL_NV, "ARL", 1 },
1504 { OPCODE_ARR, "ARL", 1 },
1505 { OPCODE_BRA, "BRA", 1 },
1506 { OPCODE_CAL, "CAL", 1 },
1507 { OPCODE_CMP, "CMP", 3 },
1508 { OPCODE_COS, "COS", 1 },
1509 { OPCODE_DDX, "DDX", 1 },
1510 { OPCODE_DDY, "DDY", 1 },
1511 { OPCODE_DP3, "DP3", 2 },
1512 { OPCODE_DP4, "DP4", 2 },
1513 { OPCODE_DPH, "DPH", 2 },
1514 { OPCODE_DST, "DST", 2 },
1515 { OPCODE_END, "END", 0 },
1516 { OPCODE_EX2, "EX2", 1 },
1517 { OPCODE_EXP, "EXP", 1 },
1518 { OPCODE_FLR, "FLR", 1 },
1519 { OPCODE_FRC, "FRC", 1 },
1520 { OPCODE_KIL, "KIL", 1 },
1521 { OPCODE_KIL_NV, "KIL", 0 },
1522 { OPCODE_LG2, "LG2", 1 },
1523 { OPCODE_LIT, "LIT", 1 },
1524 { OPCODE_LOG, "LOG", 1 },
1525 { OPCODE_LRP, "LRP", 3 },
1526 { OPCODE_MAD, "MAD", 3 },
1527 { OPCODE_MAX, "MAX", 2 },
1528 { OPCODE_MIN, "MIN", 2 },
1529 { OPCODE_MOV, "MOV", 1 },
1530 { OPCODE_MUL, "MUL", 2 },
1531 { OPCODE_PK2H, "PK2H", 1 },
1532 { OPCODE_PK2US, "PK2US", 1 },
1533 { OPCODE_PK4B, "PK4B", 1 },
1534 { OPCODE_PK4UB, "PK4UB", 1 },
1535 { OPCODE_POW, "POW", 2 },
1536 { OPCODE_POPA, "POPA", 0 },
1537 { OPCODE_PRINT, "PRINT", 1 },
1538 { OPCODE_PUSHA, "PUSHA", 0 },
1539 { OPCODE_RCC, "RCC", 1 },
1540 { OPCODE_RCP, "RCP", 1 },
1541 { OPCODE_RET, "RET", 1 },
1542 { OPCODE_RFL, "RFL", 1 },
1543 { OPCODE_RSQ, "RSQ", 1 },
1544 { OPCODE_SCS, "SCS", 1 },
1545 { OPCODE_SEQ, "SEQ", 2 },
1546 { OPCODE_SFL, "SFL", 0 },
1547 { OPCODE_SGE, "SGE", 2 },
1548 { OPCODE_SGT, "SGT", 2 },
1549 { OPCODE_SIN, "SIN", 1 },
1550 { OPCODE_SLE, "SLE", 2 },
1551 { OPCODE_SLT, "SLT", 2 },
1552 { OPCODE_SNE, "SNE", 2 },
1553 { OPCODE_SSG, "SSG", 1 },
1554 { OPCODE_STR, "STR", 0 },
1555 { OPCODE_SUB, "SUB", 2 },
1556 { OPCODE_SWZ, "SWZ", 1 },
1557 { OPCODE_TEX, "TEX", 1 },
1558 { OPCODE_TXB, "TXB", 1 },
1559 { OPCODE_TXD, "TXD", 3 },
1560 { OPCODE_TXL, "TXL", 1 },
1561 { OPCODE_TXP, "TXP", 1 },
1562 { OPCODE_TXP_NV, "TXP", 1 },
1563 { OPCODE_UP2H, "UP2H", 1 },
1564 { OPCODE_UP2US, "UP2US", 1 },
1565 { OPCODE_UP4B, "UP4B", 1 },
1566 { OPCODE_UP4UB, "UP4UB", 1 },
1567 { OPCODE_X2D, "X2D", 3 },
1568 { OPCODE_XPD, "XPD", 2 }
1569 };
1570
1571
1572 /**
1573 * Return the number of src registers for the given instruction/opcode.
1574 */
1575 GLuint
1576 _mesa_num_inst_src_regs(enum prog_opcode opcode)
1577 {
1578 ASSERT(opcode == InstInfo[opcode].Opcode);
1579 return InstInfo[opcode].NumSrcRegs;
1580 }
1581
1582
1583 /**
1584 * Return string name for given program opcode.
1585 */
1586 const char *
1587 _mesa_opcode_string(enum prog_opcode opcode)
1588 {
1589 ASSERT(opcode < MAX_OPCODE);
1590 return InstInfo[opcode].Name;
1591 }
1592
1593 /**
1594 * Return string name for given program/register file.
1595 */
1596 static const char *
1597 program_file_string(enum register_file f)
1598 {
1599 switch (f) {
1600 case PROGRAM_TEMPORARY:
1601 return "TEMP";
1602 case PROGRAM_LOCAL_PARAM:
1603 return "LOCAL";
1604 case PROGRAM_ENV_PARAM:
1605 return "ENV";
1606 case PROGRAM_STATE_VAR:
1607 return "STATE";
1608 case PROGRAM_INPUT:
1609 return "INPUT";
1610 case PROGRAM_OUTPUT:
1611 return "OUTPUT";
1612 case PROGRAM_NAMED_PARAM:
1613 return "NAMED";
1614 case PROGRAM_CONSTANT:
1615 return "CONST";
1616 case PROGRAM_WRITE_ONLY:
1617 return "WRITE_ONLY";
1618 case PROGRAM_ADDRESS:
1619 return "ADDR";
1620 default:
1621 return "!unkown!";
1622 }
1623 }
1624
1625
1626 /**
1627 * Return a string representation of the given swizzle word.
1628 * If extended is true, use extended (comma-separated) format.
1629 */
1630 static const char *
1631 swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
1632 {
1633 static const char swz[] = "xyzw01";
1634 static char s[20];
1635 GLuint i = 0;
1636
1637 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
1638 return ""; /* no swizzle/negation */
1639
1640 if (!extended)
1641 s[i++] = '.';
1642
1643 if (negateBase & 0x1)
1644 s[i++] = '-';
1645 s[i++] = swz[GET_SWZ(swizzle, 0)];
1646
1647 if (extended) {
1648 s[i++] = ',';
1649 }
1650
1651 if (negateBase & 0x2)
1652 s[i++] = '-';
1653 s[i++] = swz[GET_SWZ(swizzle, 1)];
1654
1655 if (extended) {
1656 s[i++] = ',';
1657 }
1658
1659 if (negateBase & 0x4)
1660 s[i++] = '-';
1661 s[i++] = swz[GET_SWZ(swizzle, 2)];
1662
1663 if (extended) {
1664 s[i++] = ',';
1665 }
1666
1667 if (negateBase & 0x8)
1668 s[i++] = '-';
1669 s[i++] = swz[GET_SWZ(swizzle, 3)];
1670
1671 s[i] = 0;
1672 return s;
1673 }
1674
1675
1676 static const char *
1677 writemask_string(GLuint writeMask)
1678 {
1679 static char s[10];
1680 GLuint i = 0;
1681
1682 if (writeMask == WRITEMASK_XYZW)
1683 return "";
1684
1685 s[i++] = '.';
1686 if (writeMask & WRITEMASK_X)
1687 s[i++] = 'x';
1688 if (writeMask & WRITEMASK_Y)
1689 s[i++] = 'y';
1690 if (writeMask & WRITEMASK_Z)
1691 s[i++] = 'z';
1692 if (writeMask & WRITEMASK_W)
1693 s[i++] = 'w';
1694
1695 s[i] = 0;
1696 return s;
1697 }
1698
1699 static void
1700 print_dst_reg(const struct prog_dst_register *dstReg)
1701 {
1702 _mesa_printf(" %s[%d]%s",
1703 program_file_string((enum register_file) dstReg->File),
1704 dstReg->Index,
1705 writemask_string(dstReg->WriteMask));
1706 }
1707
1708 static void
1709 print_src_reg(const struct prog_src_register *srcReg)
1710 {
1711 _mesa_printf("%s[%d]%s",
1712 program_file_string((enum register_file) srcReg->File),
1713 srcReg->Index,
1714 swizzle_string(srcReg->Swizzle,
1715 srcReg->NegateBase, GL_FALSE));
1716 }
1717
1718 void
1719 _mesa_print_alu_instruction(const struct prog_instruction *inst,
1720 const char *opcode_string,
1721 GLuint numRegs)
1722 {
1723 GLuint j;
1724
1725 _mesa_printf("%s", opcode_string);
1726
1727 /* frag prog only */
1728 if (inst->SaturateMode == SATURATE_ZERO_ONE)
1729 _mesa_printf("_SAT");
1730
1731 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
1732 _mesa_printf(" %s[%d]%s",
1733 program_file_string((enum register_file) inst->DstReg.File),
1734 inst->DstReg.Index,
1735 writemask_string(inst->DstReg.WriteMask));
1736 }
1737
1738 if (numRegs > 0)
1739 _mesa_printf(", ");
1740
1741 for (j = 0; j < numRegs; j++) {
1742 print_src_reg(inst->SrcReg + j);
1743 if (j + 1 < numRegs)
1744 _mesa_printf(", ");
1745 }
1746
1747 _mesa_printf(";\n");
1748 }
1749
1750
1751 /**
1752 * Print a single vertex/fragment program instruction.
1753 */
1754 void
1755 _mesa_print_instruction(const struct prog_instruction *inst)
1756 {
1757 switch (inst->Opcode) {
1758 case OPCODE_PRINT:
1759 _mesa_printf("PRINT '%s'", inst->Data);
1760 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
1761 _mesa_printf(", ");
1762 _mesa_printf("%s[%d]%s",
1763 program_file_string((enum register_file) inst->SrcReg[0].File),
1764 inst->SrcReg[0].Index,
1765 swizzle_string(inst->SrcReg[0].Swizzle,
1766 inst->SrcReg[0].NegateBase, GL_FALSE));
1767 }
1768 _mesa_printf(";\n");
1769 break;
1770 case OPCODE_SWZ:
1771 _mesa_printf("SWZ");
1772 if (inst->SaturateMode == SATURATE_ZERO_ONE)
1773 _mesa_printf("_SAT");
1774 print_dst_reg(&inst->DstReg);
1775 _mesa_printf("%s[%d], %s;\n",
1776 program_file_string((enum register_file) inst->SrcReg[0].File),
1777 inst->SrcReg[0].Index,
1778 swizzle_string(inst->SrcReg[0].Swizzle,
1779 inst->SrcReg[0].NegateBase, GL_TRUE));
1780 break;
1781 case OPCODE_TEX:
1782 case OPCODE_TXP:
1783 case OPCODE_TXB:
1784 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
1785 if (inst->SaturateMode == SATURATE_ZERO_ONE)
1786 _mesa_printf("_SAT");
1787 _mesa_printf(" ");
1788 print_dst_reg(&inst->DstReg);
1789 _mesa_printf(", ");
1790 print_src_reg(&inst->SrcReg[0]);
1791 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
1792 switch (inst->TexSrcTarget) {
1793 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
1794 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
1795 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
1796 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
1797 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
1798 default:
1799 ;
1800 }
1801 _mesa_printf("\n");
1802 break;
1803 case OPCODE_ARL:
1804 _mesa_printf("ARL addr.x, ");
1805 print_src_reg(&inst->SrcReg[0]);
1806 _mesa_printf(";\n");
1807 break;
1808 case OPCODE_END:
1809 _mesa_printf("END;\n");
1810 break;
1811 /* XXX may need for other special-case instructions */
1812 default:
1813 /* typical alu instruction */
1814 _mesa_print_alu_instruction(inst,
1815 _mesa_opcode_string(inst->Opcode),
1816 _mesa_num_inst_src_regs(inst->Opcode));
1817 break;
1818 }
1819 }
1820
1821
1822 /**
1823 * Print a vertx/fragment program to stdout.
1824 * XXX this function could be greatly improved.
1825 */
1826 void
1827 _mesa_print_program(const struct gl_program *prog)
1828 {
1829 GLuint i;
1830 for (i = 0; i < prog->NumInstructions; i++) {
1831 _mesa_printf("%3d: ", i);
1832 _mesa_print_instruction(prog->Instructions + i);
1833 }
1834 }
1835
1836
1837 /**
1838 * Print all of a program's parameters.
1839 */
1840 void
1841 _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
1842 {
1843 GLuint i;
1844
1845 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
1846 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
1847 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
1848 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
1849 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
1850
1851 _mesa_load_state_parameters(ctx, prog->Parameters);
1852
1853 #if 0
1854 _mesa_printf("Local Params:\n");
1855 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
1856 const GLfloat *p = prog->LocalParams[i];
1857 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
1858 }
1859 #endif
1860
1861 for (i = 0; i < prog->Parameters->NumParameters; i++){
1862 struct gl_program_parameter *param = prog->Parameters->Parameters + i;
1863 const GLfloat *v = prog->Parameters->ParameterValues[i];
1864 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1865 i, param->Name, v[0], v[1], v[2], v[3]);
1866 }
1867 }
1868
1869
1870 /**
1871 * Mixing ARB and NV vertex/fragment programs can be tricky.
1872 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
1873 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
1874 * The two different fragment program targets are supposed to be compatible
1875 * to some extent (see GL_ARB_fragment_program spec).
1876 * This function does the compatibility check.
1877 */
1878 static GLboolean
1879 compatible_program_targets(GLenum t1, GLenum t2)
1880 {
1881 if (t1 == t2)
1882 return GL_TRUE;
1883 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
1884 return GL_TRUE;
1885 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
1886 return GL_TRUE;
1887 return GL_FALSE;
1888 }
1889
1890
1891
1892 /**********************************************************************/
1893 /* API functions */
1894 /**********************************************************************/
1895
1896
1897 /**
1898 * Bind a program (make it current)
1899 * \note Called from the GL API dispatcher by both glBindProgramNV
1900 * and glBindProgramARB.
1901 */
1902 void GLAPIENTRY
1903 _mesa_BindProgram(GLenum target, GLuint id)
1904 {
1905 struct gl_program *curProg, *newProg;
1906 GET_CURRENT_CONTEXT(ctx);
1907 ASSERT_OUTSIDE_BEGIN_END(ctx);
1908
1909 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1910
1911 /* Error-check target and get curProg */
1912 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
1913 (ctx->Extensions.NV_vertex_program ||
1914 ctx->Extensions.ARB_vertex_program)) {
1915 curProg = &ctx->VertexProgram.Current->Base;
1916 }
1917 else if ((target == GL_FRAGMENT_PROGRAM_NV
1918 && ctx->Extensions.NV_fragment_program) ||
1919 (target == GL_FRAGMENT_PROGRAM_ARB
1920 && ctx->Extensions.ARB_fragment_program)) {
1921 curProg = &ctx->FragmentProgram.Current->Base;
1922 }
1923 else {
1924 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1925 return;
1926 }
1927
1928 /*
1929 * Get pointer to new program to bind.
1930 * NOTE: binding to a non-existant program is not an error.
1931 * That's supposed to be caught in glBegin.
1932 */
1933 if (id == 0) {
1934 /* Bind a default program */
1935 newProg = NULL;
1936 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
1937 newProg = ctx->Shared->DefaultVertexProgram;
1938 else
1939 newProg = ctx->Shared->DefaultFragmentProgram;
1940 }
1941 else {
1942 /* Bind a user program */
1943 newProg = _mesa_lookup_program(ctx, id);
1944 if (!newProg || newProg == &_mesa_DummyProgram) {
1945 /* allocate a new program now */
1946 newProg = ctx->Driver.NewProgram(ctx, target, id);
1947 if (!newProg) {
1948 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1949 return;
1950 }
1951 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
1952 }
1953 else if (!compatible_program_targets(newProg->Target, target)) {
1954 _mesa_error(ctx, GL_INVALID_OPERATION,
1955 "glBindProgramNV/ARB(target mismatch)");
1956 return;
1957 }
1958 }
1959
1960 /** All error checking is complete now **/
1961
1962 if (curProg->Id == id) {
1963 /* binding same program - no change */
1964 return;
1965 }
1966
1967 /* unbind/delete oldProg */
1968 if (curProg->Id != 0) {
1969 /* decrement refcount on previously bound fragment program */
1970 curProg->RefCount--;
1971 /* and delete if refcount goes below one */
1972 if (curProg->RefCount <= 0) {
1973 /* the program ID was already removed from the hash table */
1974 ctx->Driver.DeleteProgram(ctx, curProg);
1975 }
1976 }
1977
1978 /* bind newProg */
1979 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
1980 if (ctx->VertexProgram._Current == ctx->VertexProgram.Current)
1981 ctx->VertexProgram._Current = (struct gl_vertex_program *) newProg;
1982 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
1983 }
1984 else if (target == GL_FRAGMENT_PROGRAM_NV ||
1985 target == GL_FRAGMENT_PROGRAM_ARB) {
1986 if (ctx->FragmentProgram._Current == ctx->FragmentProgram.Current)
1987 ctx->FragmentProgram._Current = (struct gl_fragment_program *) newProg;
1988 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
1989 }
1990 newProg->RefCount++;
1991
1992 /* Never null pointers */
1993 ASSERT(ctx->VertexProgram.Current);
1994 ASSERT(ctx->FragmentProgram.Current);
1995
1996 if (ctx->Driver.BindProgram)
1997 ctx->Driver.BindProgram(ctx, target, newProg);
1998 }
1999
2000
2001 /**
2002 * Delete a list of programs.
2003 * \note Not compiled into display lists.
2004 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
2005 */
2006 void GLAPIENTRY
2007 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
2008 {
2009 GLint i;
2010 GET_CURRENT_CONTEXT(ctx);
2011 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2012
2013 if (n < 0) {
2014 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
2015 return;
2016 }
2017
2018 for (i = 0; i < n; i++) {
2019 if (ids[i] != 0) {
2020 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
2021 if (prog == &_mesa_DummyProgram) {
2022 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
2023 }
2024 else if (prog) {
2025 /* Unbind program if necessary */
2026 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
2027 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
2028 if (ctx->VertexProgram.Current &&
2029 ctx->VertexProgram.Current->Base.Id == ids[i]) {
2030 /* unbind this currently bound program */
2031 _mesa_BindProgram(prog->Target, 0);
2032 }
2033 }
2034 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
2035 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
2036 if (ctx->FragmentProgram.Current &&
2037 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
2038 /* unbind this currently bound program */
2039 _mesa_BindProgram(prog->Target, 0);
2040 }
2041 }
2042 else {
2043 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
2044 return;
2045 }
2046 /* The ID is immediately available for re-use now */
2047 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
2048 prog->RefCount--;
2049 if (prog->RefCount <= 0) {
2050 ctx->Driver.DeleteProgram(ctx, prog);
2051 }
2052 }
2053 }
2054 }
2055 }
2056
2057
2058 /**
2059 * Generate a list of new program identifiers.
2060 * \note Not compiled into display lists.
2061 * \note Called by both glGenProgramsNV and glGenProgramsARB.
2062 */
2063 void GLAPIENTRY
2064 _mesa_GenPrograms(GLsizei n, GLuint *ids)
2065 {
2066 GLuint first;
2067 GLuint i;
2068 GET_CURRENT_CONTEXT(ctx);
2069 ASSERT_OUTSIDE_BEGIN_END(ctx);
2070
2071 if (n < 0) {
2072 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
2073 return;
2074 }
2075
2076 if (!ids)
2077 return;
2078
2079 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
2080
2081 /* Insert pointer to dummy program as placeholder */
2082 for (i = 0; i < (GLuint) n; i++) {
2083 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
2084 }
2085
2086 /* Return the program names */
2087 for (i = 0; i < (GLuint) n; i++) {
2088 ids[i] = first + i;
2089 }
2090 }
2091
2092
2093 /**********************************************************************/
2094 /* GL_MESA_program_debug extension */
2095 /**********************************************************************/
2096
2097
2098 /* XXX temporary */
2099 GLAPI void GLAPIENTRY
2100 glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
2101 GLvoid *data)
2102 {
2103 _mesa_ProgramCallbackMESA(target, callback, data);
2104 }
2105
2106
2107 void
2108 _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
2109 GLvoid *data)
2110 {
2111 GET_CURRENT_CONTEXT(ctx);
2112
2113 switch (target) {
2114 case GL_FRAGMENT_PROGRAM_ARB:
2115 if (!ctx->Extensions.ARB_fragment_program) {
2116 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2117 return;
2118 }
2119 ctx->FragmentProgram.Callback = callback;
2120 ctx->FragmentProgram.CallbackData = data;
2121 break;
2122 case GL_FRAGMENT_PROGRAM_NV:
2123 if (!ctx->Extensions.NV_fragment_program) {
2124 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2125 return;
2126 }
2127 ctx->FragmentProgram.Callback = callback;
2128 ctx->FragmentProgram.CallbackData = data;
2129 break;
2130 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
2131 if (!ctx->Extensions.ARB_vertex_program &&
2132 !ctx->Extensions.NV_vertex_program) {
2133 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2134 return;
2135 }
2136 ctx->VertexProgram.Callback = callback;
2137 ctx->VertexProgram.CallbackData = data;
2138 break;
2139 default:
2140 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
2141 return;
2142 }
2143 }
2144
2145
2146 /* XXX temporary */
2147 GLAPI void GLAPIENTRY
2148 glGetProgramRegisterfvMESA(GLenum target,
2149 GLsizei len, const GLubyte *registerName,
2150 GLfloat *v)
2151 {
2152 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
2153 }
2154
2155
2156 void
2157 _mesa_GetProgramRegisterfvMESA(GLenum target,
2158 GLsizei len, const GLubyte *registerName,
2159 GLfloat *v)
2160 {
2161 char reg[1000];
2162 GET_CURRENT_CONTEXT(ctx);
2163
2164 /* We _should_ be inside glBegin/glEnd */
2165 #if 0
2166 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
2167 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
2168 return;
2169 }
2170 #endif
2171
2172 /* make null-terminated copy of registerName */
2173 len = MIN2((unsigned int) len, sizeof(reg) - 1);
2174 _mesa_memcpy(reg, registerName, len);
2175 reg[len] = 0;
2176
2177 switch (target) {
2178 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
2179 if (!ctx->Extensions.ARB_vertex_program &&
2180 !ctx->Extensions.NV_vertex_program) {
2181 _mesa_error(ctx, GL_INVALID_ENUM,
2182 "glGetProgramRegisterfvMESA(target)");
2183 return;
2184 }
2185 if (!ctx->VertexProgram._Enabled) {
2186 _mesa_error(ctx, GL_INVALID_OPERATION,
2187 "glGetProgramRegisterfvMESA");
2188 return;
2189 }
2190 /* GL_NV_vertex_program */
2191 if (reg[0] == 'R') {
2192 /* Temp register */
2193 GLint i = _mesa_atoi(reg + 1);
2194 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
2195 _mesa_error(ctx, GL_INVALID_VALUE,
2196 "glGetProgramRegisterfvMESA(registerName)");
2197 return;
2198 }
2199 #if 0 /* FIX ME */
2200 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_TEMPORARY, i, v);
2201 #endif
2202 }
2203 else if (reg[0] == 'v' && reg[1] == '[') {
2204 /* Vertex Input attribute */
2205 GLuint i;
2206 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
2207 const char *name = _mesa_nv_vertex_input_register_name(i);
2208 char number[10];
2209 _mesa_sprintf(number, "%d", i);
2210 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
2211 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
2212 #if 0 /* FIX ME */
2213 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_INPUT,
2214 i, v);
2215 #endif
2216 return;
2217 }
2218 }
2219 _mesa_error(ctx, GL_INVALID_VALUE,
2220 "glGetProgramRegisterfvMESA(registerName)");
2221 return;
2222 }
2223 else if (reg[0] == 'o' && reg[1] == '[') {
2224 /* Vertex output attribute */
2225 }
2226 /* GL_ARB_vertex_program */
2227 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
2228
2229 }
2230 else {
2231 _mesa_error(ctx, GL_INVALID_VALUE,
2232 "glGetProgramRegisterfvMESA(registerName)");
2233 return;
2234 }
2235 break;
2236 case GL_FRAGMENT_PROGRAM_ARB:
2237 if (!ctx->Extensions.ARB_fragment_program) {
2238 _mesa_error(ctx, GL_INVALID_ENUM,
2239 "glGetProgramRegisterfvMESA(target)");
2240 return;
2241 }
2242 if (!ctx->FragmentProgram._Enabled) {
2243 _mesa_error(ctx, GL_INVALID_OPERATION,
2244 "glGetProgramRegisterfvMESA");
2245 return;
2246 }
2247 /* XXX to do */
2248 break;
2249 case GL_FRAGMENT_PROGRAM_NV:
2250 if (!ctx->Extensions.NV_fragment_program) {
2251 _mesa_error(ctx, GL_INVALID_ENUM,
2252 "glGetProgramRegisterfvMESA(target)");
2253 return;
2254 }
2255 if (!ctx->FragmentProgram._Enabled) {
2256 _mesa_error(ctx, GL_INVALID_OPERATION,
2257 "glGetProgramRegisterfvMESA");
2258 return;
2259 }
2260 if (reg[0] == 'R') {
2261 /* Temp register */
2262 GLint i = _mesa_atoi(reg + 1);
2263 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
2264 _mesa_error(ctx, GL_INVALID_VALUE,
2265 "glGetProgramRegisterfvMESA(registerName)");
2266 return;
2267 }
2268 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_TEMPORARY,
2269 i, v);
2270 }
2271 else if (reg[0] == 'f' && reg[1] == '[') {
2272 /* Fragment input attribute */
2273 GLuint i;
2274 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
2275 const char *name = _mesa_nv_fragment_input_register_name(i);
2276 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
2277 ctx->Driver.GetFragmentProgramRegister(ctx,
2278 PROGRAM_INPUT, i, v);
2279 return;
2280 }
2281 }
2282 _mesa_error(ctx, GL_INVALID_VALUE,
2283 "glGetProgramRegisterfvMESA(registerName)");
2284 return;
2285 }
2286 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
2287 /* Fragment output color */
2288 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
2289 FRAG_RESULT_COLR, v);
2290 }
2291 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
2292 /* Fragment output color */
2293 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
2294 FRAG_RESULT_COLH, v);
2295 }
2296 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
2297 /* Fragment output depth */
2298 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
2299 FRAG_RESULT_DEPR, v);
2300 }
2301 else {
2302 /* try user-defined identifiers */
2303 const GLfloat *value = _mesa_lookup_parameter_value(
2304 ctx->FragmentProgram.Current->Base.Parameters, -1, reg);
2305 if (value) {
2306 COPY_4V(v, value);
2307 }
2308 else {
2309 _mesa_error(ctx, GL_INVALID_VALUE,
2310 "glGetProgramRegisterfvMESA(registerName)");
2311 return;
2312 }
2313 }
2314 break;
2315 default:
2316 _mesa_error(ctx, GL_INVALID_ENUM,
2317 "glGetProgramRegisterfvMESA(target)");
2318 return;
2319 }
2320 }