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