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