Reimplement the post-increment/decrement functions.
[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 "nvfragparse.h"
39 #include "program.h"
40 #include "prog_parameter.h"
41 #include "prog_instruction.h"
42 #include "prog_statevars.h"
43 #include "nvvertparse.h"
44 #include "atifragshader.h"
45
46
47
48 /**********************************************************************/
49 /* Utility functions */
50 /**********************************************************************/
51
52
53 /* A pointer to this dummy program is put into the hash table when
54 * glGenPrograms is called.
55 */
56 struct gl_program _mesa_DummyProgram;
57
58
59 /**
60 * Init context's vertex/fragment program state
61 */
62 void
63 _mesa_init_program(GLcontext *ctx)
64 {
65 GLuint i;
66
67 ctx->Program.ErrorPos = -1;
68 ctx->Program.ErrorString = _mesa_strdup("");
69
70 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
71 ctx->VertexProgram.Enabled = GL_FALSE;
72 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
73 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
74 ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram;
75 assert(ctx->VertexProgram.Current);
76 ctx->VertexProgram.Current->Base.RefCount++;
77 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
78 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
79 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
80 }
81 #endif
82
83 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
84 ctx->FragmentProgram.Enabled = GL_FALSE;
85 ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram;
86 assert(ctx->FragmentProgram.Current);
87 ctx->FragmentProgram.Current->Base.RefCount++;
88 #endif
89
90 /* XXX probably move this stuff */
91 #if FEATURE_ATI_fragment_shader
92 ctx->ATIFragmentShader.Enabled = GL_FALSE;
93 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
94 assert(ctx->ATIFragmentShader.Current);
95 ctx->ATIFragmentShader.Current->RefCount++;
96 #endif
97 }
98
99
100 /**
101 * Free a context's vertex/fragment program state
102 */
103 void
104 _mesa_free_program_data(GLcontext *ctx)
105 {
106 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
107 if (ctx->VertexProgram.Current) {
108 ctx->VertexProgram.Current->Base.RefCount--;
109 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
110 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
111 }
112 #endif
113 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
114 if (ctx->FragmentProgram.Current) {
115 ctx->FragmentProgram.Current->Base.RefCount--;
116 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
117 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
118 }
119 #endif
120 /* XXX probably move this stuff */
121 #if FEATURE_ATI_fragment_shader
122 if (ctx->ATIFragmentShader.Current) {
123 ctx->ATIFragmentShader.Current->RefCount--;
124 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
125 _mesa_free(ctx->ATIFragmentShader.Current);
126 }
127 }
128 #endif
129 _mesa_free((void *) ctx->Program.ErrorString);
130 }
131
132
133
134
135 /**
136 * Set the vertex/fragment program error state (position and error string).
137 * This is generally called from within the parsers.
138 */
139 void
140 _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
141 {
142 ctx->Program.ErrorPos = pos;
143 _mesa_free((void *) ctx->Program.ErrorString);
144 if (!string)
145 string = "";
146 ctx->Program.ErrorString = _mesa_strdup(string);
147 }
148
149
150 /**
151 * Find the line number and column for 'pos' within 'string'.
152 * Return a copy of the line which contains 'pos'. Free the line with
153 * _mesa_free().
154 * \param string the program string
155 * \param pos the position within the string
156 * \param line returns the line number corresponding to 'pos'.
157 * \param col returns the column number corresponding to 'pos'.
158 * \return copy of the line containing 'pos'.
159 */
160 const GLubyte *
161 _mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
162 GLint *line, GLint *col)
163 {
164 const GLubyte *lineStart = string;
165 const GLubyte *p = string;
166 GLubyte *s;
167 int len;
168
169 *line = 1;
170
171 while (p != pos) {
172 if (*p == (GLubyte) '\n') {
173 (*line)++;
174 lineStart = p + 1;
175 }
176 p++;
177 }
178
179 *col = (pos - lineStart) + 1;
180
181 /* return copy of this line */
182 while (*p != 0 && *p != '\n')
183 p++;
184 len = p - lineStart;
185 s = (GLubyte *) _mesa_malloc(len + 1);
186 _mesa_memcpy(s, lineStart, len);
187 s[len] = 0;
188
189 return s;
190 }
191
192
193 /**
194 * Initialize a new vertex/fragment program object.
195 */
196 static struct gl_program *
197 _mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
198 GLenum target, GLuint id)
199 {
200 (void) ctx;
201 if (prog) {
202 _mesa_bzero(prog, sizeof(*prog));
203 prog->Id = id;
204 prog->Target = target;
205 prog->Resident = GL_TRUE;
206 prog->RefCount = 1;
207 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
208 }
209
210 return prog;
211 }
212
213
214 /**
215 * Initialize a new fragment program object.
216 */
217 struct gl_program *
218 _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
219 GLenum target, GLuint id)
220 {
221 if (prog)
222 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
223 else
224 return NULL;
225 }
226
227
228 /**
229 * Initialize a new vertex program object.
230 */
231 struct gl_program *
232 _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
233 GLenum target, GLuint id)
234 {
235 if (prog)
236 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
237 else
238 return NULL;
239 }
240
241
242 /**
243 * Allocate and initialize a new fragment/vertex program object but
244 * don't put it into the program hash table. Called via
245 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
246 * device driver function to implement OO deriviation with additional
247 * types not understood by this function.
248 *
249 * \param ctx context
250 * \param id program id/number
251 * \param target program target/type
252 * \return pointer to new program object
253 */
254 struct gl_program *
255 _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
256 {
257 switch (target) {
258 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
259 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
260 target, id );
261 case GL_FRAGMENT_PROGRAM_NV:
262 case GL_FRAGMENT_PROGRAM_ARB:
263 return _mesa_init_fragment_program(ctx,
264 CALLOC_STRUCT(gl_fragment_program),
265 target, id );
266 default:
267 _mesa_problem(ctx, "bad target in _mesa_new_program");
268 return NULL;
269 }
270 }
271
272
273 /**
274 * Delete a program and remove it from the hash table, ignoring the
275 * reference count.
276 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
277 * by a device driver function.
278 */
279 void
280 _mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
281 {
282 (void) ctx;
283 ASSERT(prog);
284
285 if (prog == &_mesa_DummyProgram)
286 return;
287
288 if (prog->String)
289 _mesa_free(prog->String);
290
291 if (prog->Instructions) {
292 GLuint i;
293 for (i = 0; i < prog->NumInstructions; i++) {
294 if (prog->Instructions[i].Data)
295 _mesa_free(prog->Instructions[i].Data);
296 }
297 _mesa_free(prog->Instructions);
298 }
299
300 if (prog->Parameters) {
301 _mesa_free_parameter_list(prog->Parameters);
302 }
303
304 if (prog->Varying) {
305 _mesa_free_parameter_list(prog->Varying);
306 }
307
308 /* XXX this is a little ugly */
309 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
310 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
311 if (vprog->TnlData)
312 _mesa_free(vprog->TnlData);
313 }
314
315 _mesa_free(prog);
316 }
317
318
319 /**
320 * Return the gl_program object for a given ID.
321 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
322 * casts elsewhere.
323 */
324 struct gl_program *
325 _mesa_lookup_program(GLcontext *ctx, GLuint id)
326 {
327 if (id)
328 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
329 else
330 return NULL;
331 }
332
333
334 /**
335 * Return a copy of a program.
336 * XXX Problem here if the program object is actually OO-derivation
337 * made by a device driver.
338 */
339 struct gl_program *
340 _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
341 {
342 struct gl_program *clone;
343
344 clone = _mesa_new_program(ctx, prog->Target, prog->Id);
345 if (!clone)
346 return NULL;
347
348 assert(clone->Target == prog->Target);
349 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
350 clone->RefCount = 1;
351 clone->Format = prog->Format;
352 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
353 if (!clone->Instructions) {
354 _mesa_delete_program(ctx, clone);
355 return NULL;
356 }
357 memcpy(clone->Instructions, prog->Instructions,
358 prog->NumInstructions * sizeof(struct prog_instruction));
359 clone->InputsRead = prog->InputsRead;
360 clone->OutputsWritten = prog->OutputsWritten;
361 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
362
363 if (prog->Parameters)
364 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
365 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
366 if (prog->Varying)
367 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
368 if (prog->Attributes)
369 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
370 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
371 clone->NumInstructions = prog->NumInstructions;
372 clone->NumTemporaries = prog->NumTemporaries;
373 clone->NumParameters = prog->NumParameters;
374 clone->NumAttributes = prog->NumAttributes;
375 clone->NumAddressRegs = prog->NumAddressRegs;
376 clone->NumNativeInstructions = prog->NumNativeInstructions;
377 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
378 clone->NumNativeParameters = prog->NumNativeParameters;
379 clone->NumNativeAttributes = prog->NumNativeAttributes;
380 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
381 clone->NumAluInstructions = prog->NumAluInstructions;
382 clone->NumTexInstructions = prog->NumTexInstructions;
383 clone->NumTexIndirections = prog->NumTexIndirections;
384 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
385 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
386 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
387
388 switch (prog->Target) {
389 case GL_VERTEX_PROGRAM_ARB:
390 {
391 const struct gl_vertex_program *vp
392 = (const struct gl_vertex_program *) prog;
393 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
394 vpc->IsPositionInvariant = vp->IsPositionInvariant;
395 }
396 break;
397 case GL_FRAGMENT_PROGRAM_ARB:
398 {
399 const struct gl_fragment_program *fp
400 = (const struct gl_fragment_program *) prog;
401 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
402 fpc->FogOption = fp->FogOption;
403 fpc->UsesKill = fp->UsesKill;
404 }
405 break;
406 default:
407 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
408 }
409
410 return clone;
411 }
412
413
414
415 /**
416 * Mixing ARB and NV vertex/fragment programs can be tricky.
417 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
418 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
419 * The two different fragment program targets are supposed to be compatible
420 * to some extent (see GL_ARB_fragment_program spec).
421 * This function does the compatibility check.
422 */
423 static GLboolean
424 compatible_program_targets(GLenum t1, GLenum t2)
425 {
426 if (t1 == t2)
427 return GL_TRUE;
428 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
429 return GL_TRUE;
430 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
431 return GL_TRUE;
432 return GL_FALSE;
433 }
434
435
436
437 /**********************************************************************/
438 /* API functions */
439 /**********************************************************************/
440
441
442 /**
443 * Bind a program (make it current)
444 * \note Called from the GL API dispatcher by both glBindProgramNV
445 * and glBindProgramARB.
446 */
447 void GLAPIENTRY
448 _mesa_BindProgram(GLenum target, GLuint id)
449 {
450 struct gl_program *curProg, *newProg;
451 GET_CURRENT_CONTEXT(ctx);
452 ASSERT_OUTSIDE_BEGIN_END(ctx);
453
454 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
455
456 /* Error-check target and get curProg */
457 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
458 (ctx->Extensions.NV_vertex_program ||
459 ctx->Extensions.ARB_vertex_program)) {
460 curProg = &ctx->VertexProgram.Current->Base;
461 }
462 else if ((target == GL_FRAGMENT_PROGRAM_NV
463 && ctx->Extensions.NV_fragment_program) ||
464 (target == GL_FRAGMENT_PROGRAM_ARB
465 && ctx->Extensions.ARB_fragment_program)) {
466 curProg = &ctx->FragmentProgram.Current->Base;
467 }
468 else {
469 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
470 return;
471 }
472
473 /*
474 * Get pointer to new program to bind.
475 * NOTE: binding to a non-existant program is not an error.
476 * That's supposed to be caught in glBegin.
477 */
478 if (id == 0) {
479 /* Bind a default program */
480 newProg = NULL;
481 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
482 newProg = ctx->Shared->DefaultVertexProgram;
483 else
484 newProg = ctx->Shared->DefaultFragmentProgram;
485 }
486 else {
487 /* Bind a user program */
488 newProg = _mesa_lookup_program(ctx, id);
489 if (!newProg || newProg == &_mesa_DummyProgram) {
490 /* allocate a new program now */
491 newProg = ctx->Driver.NewProgram(ctx, target, id);
492 if (!newProg) {
493 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
494 return;
495 }
496 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
497 }
498 else if (!compatible_program_targets(newProg->Target, target)) {
499 _mesa_error(ctx, GL_INVALID_OPERATION,
500 "glBindProgramNV/ARB(target mismatch)");
501 return;
502 }
503 }
504
505 /** All error checking is complete now **/
506
507 if (curProg->Id == id) {
508 /* binding same program - no change */
509 return;
510 }
511
512 /* unbind/delete oldProg */
513 if (curProg->Id != 0) {
514 /* decrement refcount on previously bound fragment program */
515 curProg->RefCount--;
516 /* and delete if refcount goes below one */
517 if (curProg->RefCount <= 0) {
518 /* the program ID was already removed from the hash table */
519 ctx->Driver.DeleteProgram(ctx, curProg);
520 }
521 }
522
523 /* bind newProg */
524 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
525 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
526 }
527 else if (target == GL_FRAGMENT_PROGRAM_NV ||
528 target == GL_FRAGMENT_PROGRAM_ARB) {
529 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
530 }
531 newProg->RefCount++;
532
533 /* Never null pointers */
534 ASSERT(ctx->VertexProgram.Current);
535 ASSERT(ctx->FragmentProgram.Current);
536
537 if (ctx->Driver.BindProgram)
538 ctx->Driver.BindProgram(ctx, target, newProg);
539 }
540
541
542 /**
543 * Delete a list of programs.
544 * \note Not compiled into display lists.
545 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
546 */
547 void GLAPIENTRY
548 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
549 {
550 GLint i;
551 GET_CURRENT_CONTEXT(ctx);
552 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
553
554 if (n < 0) {
555 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
556 return;
557 }
558
559 for (i = 0; i < n; i++) {
560 if (ids[i] != 0) {
561 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
562 if (prog == &_mesa_DummyProgram) {
563 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
564 }
565 else if (prog) {
566 /* Unbind program if necessary */
567 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
568 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
569 if (ctx->VertexProgram.Current &&
570 ctx->VertexProgram.Current->Base.Id == ids[i]) {
571 /* unbind this currently bound program */
572 _mesa_BindProgram(prog->Target, 0);
573 }
574 }
575 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
576 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
577 if (ctx->FragmentProgram.Current &&
578 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
579 /* unbind this currently bound program */
580 _mesa_BindProgram(prog->Target, 0);
581 }
582 }
583 else {
584 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
585 return;
586 }
587 /* The ID is immediately available for re-use now */
588 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
589 prog->RefCount--;
590 if (prog->RefCount <= 0) {
591 ctx->Driver.DeleteProgram(ctx, prog);
592 }
593 }
594 }
595 }
596 }
597
598
599 /**
600 * Generate a list of new program identifiers.
601 * \note Not compiled into display lists.
602 * \note Called by both glGenProgramsNV and glGenProgramsARB.
603 */
604 void GLAPIENTRY
605 _mesa_GenPrograms(GLsizei n, GLuint *ids)
606 {
607 GLuint first;
608 GLuint i;
609 GET_CURRENT_CONTEXT(ctx);
610 ASSERT_OUTSIDE_BEGIN_END(ctx);
611
612 if (n < 0) {
613 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
614 return;
615 }
616
617 if (!ids)
618 return;
619
620 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
621
622 /* Insert pointer to dummy program as placeholder */
623 for (i = 0; i < (GLuint) n; i++) {
624 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
625 }
626
627 /* Return the program names */
628 for (i = 0; i < (GLuint) n; i++) {
629 ids[i] = first + i;
630 }
631 }
632
633
634 /**********************************************************************/
635 /* GL_MESA_program_debug extension */
636 /**********************************************************************/
637
638
639 /* XXX temporary */
640 GLAPI void GLAPIENTRY
641 glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
642 GLvoid *data)
643 {
644 _mesa_ProgramCallbackMESA(target, callback, data);
645 }
646
647
648 void
649 _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
650 GLvoid *data)
651 {
652 GET_CURRENT_CONTEXT(ctx);
653
654 switch (target) {
655 case GL_FRAGMENT_PROGRAM_ARB:
656 if (!ctx->Extensions.ARB_fragment_program) {
657 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
658 return;
659 }
660 ctx->FragmentProgram.Callback = callback;
661 ctx->FragmentProgram.CallbackData = data;
662 break;
663 case GL_FRAGMENT_PROGRAM_NV:
664 if (!ctx->Extensions.NV_fragment_program) {
665 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
666 return;
667 }
668 ctx->FragmentProgram.Callback = callback;
669 ctx->FragmentProgram.CallbackData = data;
670 break;
671 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
672 if (!ctx->Extensions.ARB_vertex_program &&
673 !ctx->Extensions.NV_vertex_program) {
674 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
675 return;
676 }
677 ctx->VertexProgram.Callback = callback;
678 ctx->VertexProgram.CallbackData = data;
679 break;
680 default:
681 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
682 return;
683 }
684 }
685
686
687 /* XXX temporary */
688 GLAPI void GLAPIENTRY
689 glGetProgramRegisterfvMESA(GLenum target,
690 GLsizei len, const GLubyte *registerName,
691 GLfloat *v)
692 {
693 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
694 }
695
696
697 void
698 _mesa_GetProgramRegisterfvMESA(GLenum target,
699 GLsizei len, const GLubyte *registerName,
700 GLfloat *v)
701 {
702 char reg[1000];
703 GET_CURRENT_CONTEXT(ctx);
704
705 /* We _should_ be inside glBegin/glEnd */
706 #if 0
707 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
708 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
709 return;
710 }
711 #endif
712
713 /* make null-terminated copy of registerName */
714 len = MIN2((unsigned int) len, sizeof(reg) - 1);
715 _mesa_memcpy(reg, registerName, len);
716 reg[len] = 0;
717
718 switch (target) {
719 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
720 if (!ctx->Extensions.ARB_vertex_program &&
721 !ctx->Extensions.NV_vertex_program) {
722 _mesa_error(ctx, GL_INVALID_ENUM,
723 "glGetProgramRegisterfvMESA(target)");
724 return;
725 }
726 if (!ctx->VertexProgram._Enabled) {
727 _mesa_error(ctx, GL_INVALID_OPERATION,
728 "glGetProgramRegisterfvMESA");
729 return;
730 }
731 /* GL_NV_vertex_program */
732 if (reg[0] == 'R') {
733 /* Temp register */
734 GLint i = _mesa_atoi(reg + 1);
735 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
736 _mesa_error(ctx, GL_INVALID_VALUE,
737 "glGetProgramRegisterfvMESA(registerName)");
738 return;
739 }
740 #if 0 /* FIX ME */
741 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_TEMPORARY, i, v);
742 #endif
743 }
744 else if (reg[0] == 'v' && reg[1] == '[') {
745 /* Vertex Input attribute */
746 GLuint i;
747 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
748 const char *name = _mesa_nv_vertex_input_register_name(i);
749 char number[10];
750 _mesa_sprintf(number, "%d", i);
751 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
752 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
753 #if 0 /* FIX ME */
754 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_INPUT,
755 i, v);
756 #endif
757 return;
758 }
759 }
760 _mesa_error(ctx, GL_INVALID_VALUE,
761 "glGetProgramRegisterfvMESA(registerName)");
762 return;
763 }
764 else if (reg[0] == 'o' && reg[1] == '[') {
765 /* Vertex output attribute */
766 }
767 /* GL_ARB_vertex_program */
768 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
769
770 }
771 else {
772 _mesa_error(ctx, GL_INVALID_VALUE,
773 "glGetProgramRegisterfvMESA(registerName)");
774 return;
775 }
776 break;
777 case GL_FRAGMENT_PROGRAM_ARB:
778 if (!ctx->Extensions.ARB_fragment_program) {
779 _mesa_error(ctx, GL_INVALID_ENUM,
780 "glGetProgramRegisterfvMESA(target)");
781 return;
782 }
783 if (!ctx->FragmentProgram._Enabled) {
784 _mesa_error(ctx, GL_INVALID_OPERATION,
785 "glGetProgramRegisterfvMESA");
786 return;
787 }
788 /* XXX to do */
789 break;
790 case GL_FRAGMENT_PROGRAM_NV:
791 if (!ctx->Extensions.NV_fragment_program) {
792 _mesa_error(ctx, GL_INVALID_ENUM,
793 "glGetProgramRegisterfvMESA(target)");
794 return;
795 }
796 if (!ctx->FragmentProgram._Enabled) {
797 _mesa_error(ctx, GL_INVALID_OPERATION,
798 "glGetProgramRegisterfvMESA");
799 return;
800 }
801 if (reg[0] == 'R') {
802 /* Temp register */
803 GLint i = _mesa_atoi(reg + 1);
804 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
805 _mesa_error(ctx, GL_INVALID_VALUE,
806 "glGetProgramRegisterfvMESA(registerName)");
807 return;
808 }
809 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_TEMPORARY,
810 i, v);
811 }
812 else if (reg[0] == 'f' && reg[1] == '[') {
813 /* Fragment input attribute */
814 GLuint i;
815 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
816 const char *name = _mesa_nv_fragment_input_register_name(i);
817 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
818 ctx->Driver.GetFragmentProgramRegister(ctx,
819 PROGRAM_INPUT, i, v);
820 return;
821 }
822 }
823 _mesa_error(ctx, GL_INVALID_VALUE,
824 "glGetProgramRegisterfvMESA(registerName)");
825 return;
826 }
827 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
828 /* Fragment output color */
829 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
830 FRAG_RESULT_COLR, v);
831 }
832 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
833 /* Fragment output color */
834 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
835 FRAG_RESULT_COLH, v);
836 }
837 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
838 /* Fragment output depth */
839 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
840 FRAG_RESULT_DEPR, v);
841 }
842 else {
843 /* try user-defined identifiers */
844 const GLfloat *value = _mesa_lookup_parameter_value(
845 ctx->FragmentProgram.Current->Base.Parameters, -1, reg);
846 if (value) {
847 COPY_4V(v, value);
848 }
849 else {
850 _mesa_error(ctx, GL_INVALID_VALUE,
851 "glGetProgramRegisterfvMESA(registerName)");
852 return;
853 }
854 }
855 break;
856 default:
857 _mesa_error(ctx, GL_INVALID_ENUM,
858 "glGetProgramRegisterfvMESA(target)");
859 return;
860 }
861 }