Make it easier for drivers to create "subclasses" of the existing
[mesa.git] / src / mesa / main / program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.0
4 *
5 * Copyright (C) 1999-2004 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 "nvfragprog.h"
41 #include "nvvertparse.h"
42
43
44 /**********************************************************************/
45 /* Utility functions */
46 /**********************************************************************/
47
48
49 /**
50 * Init context's program state
51 */
52 void
53 _mesa_init_program(GLcontext *ctx)
54 {
55 GLuint i;
56
57 ctx->Program.ErrorPos = -1;
58 ctx->Program.ErrorString = _mesa_strdup("");
59
60 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
61 ctx->VertexProgram.Enabled = GL_FALSE;
62 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
63 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
64 ctx->VertexProgram.Current = NULL;
65 ctx->VertexProgram.Current = (struct vertex_program *) ctx->Shared->DefaultVertexProgram;
66 assert(ctx->VertexProgram.Current);
67 ctx->VertexProgram.Current->Base.RefCount++;
68 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
69 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
70 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
71 }
72 #endif
73
74 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
75 ctx->FragmentProgram.Enabled = GL_FALSE;
76 ctx->FragmentProgram.Current = (struct fragment_program *) ctx->Shared->DefaultFragmentProgram;
77 assert(ctx->FragmentProgram.Current);
78 ctx->FragmentProgram.Current->Base.RefCount++;
79 #endif
80 }
81
82
83 /**
84 * Set the vertex/fragment program error state (position and error string).
85 * This is generally called from within the parsers.
86 */
87 void
88 _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
89 {
90 ctx->Program.ErrorPos = pos;
91 _mesa_free((void *) ctx->Program.ErrorString);
92 if (!string)
93 string = "";
94 ctx->Program.ErrorString = _mesa_strdup(string);
95 }
96
97
98 /**
99 * Find the line number and column for 'pos' within 'string'.
100 * Return a copy of the line which contains 'pos'. Free the line with
101 * _mesa_free().
102 * \param string the program string
103 * \param pos the position within the string
104 * \param line returns the line number corresponding to 'pos'.
105 * \param col returns the column number corresponding to 'pos'.
106 * \return copy of the line containing 'pos'.
107 */
108 const GLubyte *
109 _mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
110 GLint *line, GLint *col)
111 {
112 const GLubyte *lineStart = string;
113 const GLubyte *p = string;
114 GLubyte *s;
115 int len;
116
117 *line = 1;
118
119 while (p != pos) {
120 if (*p == (GLubyte) '\n') {
121 (*line)++;
122 lineStart = p + 1;
123 }
124 p++;
125 }
126
127 *col = (pos - lineStart) + 1;
128
129 /* return copy of this line */
130 while (*p != 0 && *p != '\n')
131 p++;
132 len = p - lineStart;
133 s = (GLubyte *) _mesa_malloc(len + 1);
134 _mesa_memcpy(s, lineStart, len);
135 s[len] = 0;
136
137 return s;
138 }
139
140
141 static struct program * _mesa_init_program_struct( GLcontext *ctx,
142 struct program *prog,
143 GLenum target, GLuint id)
144 {
145 if (prog) {
146 prog->Id = id;
147 prog->Target = target;
148 prog->Resident = GL_TRUE;
149 prog->RefCount = 1;
150 }
151
152 fprintf(stderr, "%s %x %x\n", __FUNCTION__, target, prog);
153 return prog;
154 }
155
156 struct program * _mesa_init_fragment_program( GLcontext *ctx,
157 struct fragment_program *prog,
158 GLenum target, GLuint id)
159 {
160 if (prog)
161 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
162 else
163 return NULL;
164 }
165
166 struct program * _mesa_init_vertex_program( GLcontext *ctx,
167 struct vertex_program *prog,
168 GLenum target, GLuint id)
169 {
170 if (prog)
171 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
172 else
173 return NULL;
174 }
175
176
177 /**
178 * Allocate and initialize a new fragment/vertex program object but
179 * don't put it into the program hash table. Called via
180 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
181 * device driver function to implement OO deriviation with additional
182 * types not understood by this function.
183 *
184 * \param ctx context
185 * \param id program id/number
186 * \param target program target/type
187 * \return pointer to new program object
188 */
189 struct program *
190 _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
191 {
192 fprintf(stderr, "%s\n", __FUNCTION__);
193 switch (target) {
194 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
195 return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program),
196 target, id );
197
198 case GL_FRAGMENT_PROGRAM_NV:
199 case GL_FRAGMENT_PROGRAM_ARB:
200 return _mesa_init_fragment_program( ctx, CALLOC_STRUCT(fragment_program),
201 target, id );
202
203 default:
204 _mesa_problem(ctx, "bad target in _mesa_new_program");
205 return NULL;
206 }
207 }
208
209
210 /**
211 * Delete a program and remove it from the hash table, ignoring the
212 * reference count.
213 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
214 * by a device driver function.
215 */
216 void
217 _mesa_delete_program(GLcontext *ctx, struct program *prog)
218 {
219 ASSERT(prog);
220
221 if (prog->String)
222 _mesa_free(prog->String);
223 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
224 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
225 struct vertex_program *vprog = (struct vertex_program *) prog;
226 if (vprog->Instructions)
227 _mesa_free(vprog->Instructions);
228 }
229 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
230 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
231 struct fragment_program *fprog = (struct fragment_program *) prog;
232 if (fprog->Instructions)
233 _mesa_free(fprog->Instructions);
234 if (fprog->Parameters) {
235 _mesa_free_parameter_list(fprog->Parameters);
236 }
237 }
238 _mesa_free(prog);
239 }
240
241
242
243 /**********************************************************************/
244 /* Program parameter functions */
245 /**********************************************************************/
246
247 struct program_parameter_list *
248 _mesa_new_parameter_list(void)
249 {
250 return (struct program_parameter_list *)
251 _mesa_calloc(sizeof(struct program_parameter_list));
252 }
253
254
255 /**
256 * Free a parameter list and all its parameters
257 */
258 void
259 _mesa_free_parameter_list(struct program_parameter_list *paramList)
260 {
261 _mesa_free_parameters(paramList);
262 _mesa_free(paramList);
263 }
264
265
266 /**
267 * Free all the parameters in the given list, but don't free the
268 * paramList structure itself.
269 */
270 void
271 _mesa_free_parameters(struct program_parameter_list *paramList)
272 {
273 GLuint i;
274 for (i = 0; i < paramList->NumParameters; i++) {
275 _mesa_free((void *) paramList->Parameters[i].Name);
276 }
277 _mesa_free(paramList->Parameters);
278 paramList->NumParameters = 0;
279 paramList->Parameters = NULL;
280 }
281
282
283 /**
284 * Helper function used by the functions below.
285 */
286 static GLint
287 add_parameter(struct program_parameter_list *paramList,
288 const char *name, const GLfloat values[4],
289 enum parameter_type type)
290 {
291 const GLuint n = paramList->NumParameters;
292
293 paramList->Parameters = (struct program_parameter *)
294 _mesa_realloc(paramList->Parameters,
295 n * sizeof(struct program_parameter),
296 (n + 1) * sizeof(struct program_parameter));
297 if (!paramList->Parameters) {
298 /* out of memory */
299 paramList->NumParameters = 0;
300 return -1;
301 }
302 else {
303 paramList->NumParameters = n + 1;
304 paramList->Parameters[n].Name = _mesa_strdup(name);
305 paramList->Parameters[n].Type = type;
306 if (values)
307 COPY_4V(paramList->Parameters[n].Values, values);
308 return (GLint) n;
309 }
310 }
311
312
313 /**
314 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
315 * \return index of the new entry in the parameter list
316 */
317 GLint
318 _mesa_add_named_parameter(struct program_parameter_list *paramList,
319 const char *name, const GLfloat values[4])
320 {
321 return add_parameter(paramList, name, values, NAMED_PARAMETER);
322 }
323
324
325 /**
326 * Add a new unnamed constant to the parameter list.
327 * \param paramList - the parameter list
328 * \param values - four float values
329 * \return index of the new parameter.
330 */
331 GLint
332 _mesa_add_named_constant(struct program_parameter_list *paramList,
333 const char *name, const GLfloat values[4])
334 {
335 return add_parameter(paramList, name, values, CONSTANT);
336 }
337
338
339 /**
340 * Add a new unnamed constant to the parameter list.
341 * \param paramList - the parameter list
342 * \param values - four float values
343 * \return index of the new parameter.
344 */
345 GLint
346 _mesa_add_unnamed_constant(struct program_parameter_list *paramList,
347 const GLfloat values[4])
348 {
349 /* generate a new dummy name */
350 static GLuint n = 0;
351 char name[20];
352 _mesa_sprintf(name, "constant%d", n);
353 n++;
354 /* store it */
355 return add_parameter(paramList, name, values, CONSTANT);
356 }
357
358
359 /**
360 * Add a new state reference to the parameter list.
361 * \param paramList - the parameter list
362 * \param state - an array of 6 state tokens
363 *
364 * \return index of the new parameter.
365 */
366 GLint
367 _mesa_add_state_reference(struct program_parameter_list *paramList,
368 GLint *stateTokens)
369 {
370 /* XXX Should we parse <stateString> here and produce the parameter's
371 * list of STATE_* tokens here, or in the parser?
372 */
373 GLint a, idx;
374
375 idx = add_parameter(paramList, "Some State", NULL, STATE);
376
377 for (a=0; a<6; a++)
378 paramList->Parameters[idx].StateIndexes[a] = (enum state_index) stateTokens[a];
379
380 return idx;
381 }
382
383
384 /**
385 * Lookup a parameter value by name in the given parameter list.
386 * \return pointer to the float[4] values.
387 */
388 GLfloat *
389 _mesa_lookup_parameter_value(struct program_parameter_list *paramList,
390 GLsizei nameLen, const char *name)
391 {
392 GLuint i;
393
394 if (nameLen == -1) {
395 /* name is null-terminated */
396 for (i = 0; i < paramList->NumParameters; i++) {
397 if (_mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
398 return paramList->Parameters[i].Values;
399 }
400 }
401 else {
402 /* name is not null-terminated, use nameLen */
403 for (i = 0; i < paramList->NumParameters; i++) {
404 if (_mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
405 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
406 return paramList->Parameters[i].Values;
407 }
408 }
409 return NULL;
410 }
411
412
413 /**
414 * Lookup a parameter index by name in the given parameter list.
415 * \return index of parameter in the list.
416 */
417 GLint
418 _mesa_lookup_parameter_index(struct program_parameter_list *paramList,
419 GLsizei nameLen, const char *name)
420 {
421 GLint i;
422
423 if (nameLen == -1) {
424 /* name is null-terminated */
425 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
426 if (_mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
427 return i;
428 }
429 }
430 else {
431 /* name is not null-terminated, use nameLen */
432 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
433 if (_mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
434 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
435 return i;
436 }
437 }
438 return -1;
439 }
440
441
442 /**
443 * Use the list of tokens in the state[] array to find global GL state
444 * and return it in <value>. Usually, four values are returned in <value>
445 * but matrix queries may return as many as 16 values.
446 * This function is used for ARB vertex/fragment programs.
447 * The program parser will produce the state[] values.
448 */
449 static void
450 _mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
451 GLfloat *value)
452 {
453 switch (state[0]) {
454 case STATE_MATERIAL:
455 {
456 /* state[1] is either 0=front or 1=back side */
457 const GLuint face = (GLuint) state[1];
458 /* state[2] is the material attribute */
459 switch (state[2]) {
460 case STATE_AMBIENT:
461 if (face == 0)
462 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
463 else
464 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
465 return;
466 case STATE_DIFFUSE:
467 if (face == 0)
468 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
469 else
470 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
471 return;
472 case STATE_SPECULAR:
473 if (face == 0)
474 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
475 else
476 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
477 return;
478 case STATE_EMISSION:
479 if (face == 0)
480 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
481 else
482 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
483 return;
484 case STATE_SHININESS:
485 if (face == 0)
486 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
487 else
488 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
489 value[1] = 0.0F;
490 value[2] = 0.0F;
491 value[3] = 1.0F;
492 return;
493 default:
494 _mesa_problem(ctx, "Invalid material state in fetch_state");
495 return;
496 }
497 };
498 return;
499 case STATE_LIGHT:
500 {
501 /* state[1] is the light number */
502 const GLuint ln = (GLuint) state[1];
503 /* state[2] is the light attribute */
504 switch (state[2]) {
505 case STATE_AMBIENT:
506 COPY_4V(value, ctx->Light.Light[ln].Ambient);
507 return;
508 case STATE_DIFFUSE:
509 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
510 return;
511 case STATE_SPECULAR:
512 COPY_4V(value, ctx->Light.Light[ln].Specular);
513 return;
514 case STATE_POSITION:
515 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
516 return;
517 case STATE_ATTENUATION:
518 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
519 value[1] = ctx->Light.Light[ln].LinearAttenuation;
520 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
521 value[3] = ctx->Light.Light[ln].SpotExponent;
522 return;
523 case STATE_SPOT_DIRECTION:
524 COPY_4V(value, ctx->Light.Light[ln].EyeDirection);
525 return;
526 case STATE_HALF:
527 {
528 GLfloat eye_z[] = {0, 0, 1};
529
530 /* Compute infinite half angle vector:
531 * half-vector = light_position + (0, 0, 1)
532 * and then normalize. w = 0
533 *
534 * light.EyePosition.w should be 0 for infinite lights.
535 */
536 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
537 NORMALIZE_3FV(value);
538 value[3] = 0;
539 }
540 return;
541 default:
542 _mesa_problem(ctx, "Invalid light state in fetch_state");
543 return;
544 }
545 }
546 return;
547 case STATE_LIGHTMODEL_AMBIENT:
548 COPY_4V(value, ctx->Light.Model.Ambient);
549 return;
550 case STATE_LIGHTMODEL_SCENECOLOR:
551 if (state[1] == 0) {
552 /* front */
553 GLint i;
554 for (i = 0; i < 4; i++) {
555 value[i] = ctx->Light.Model.Ambient[i]
556 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
557 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
558 }
559 }
560 else {
561 /* back */
562 GLint i;
563 for (i = 0; i < 4; i++) {
564 value[i] = ctx->Light.Model.Ambient[i]
565 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
566 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
567 }
568 }
569 return;
570 case STATE_LIGHTPROD:
571 {
572 const GLuint ln = (GLuint) state[1];
573 const GLuint face = (GLuint) state[2];
574 GLint i;
575 ASSERT(face == 0 || face == 1);
576 switch (state[3]) {
577 case STATE_AMBIENT:
578 for (i = 0; i < 3; i++) {
579 value[i] = ctx->Light.Light[ln].Ambient[i] *
580 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
581 }
582 /* [3] = material alpha */
583 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
584 return;
585 case STATE_DIFFUSE:
586 for (i = 0; i < 3; i++) {
587 value[i] = ctx->Light.Light[ln].Diffuse[i] *
588 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
589 }
590 /* [3] = material alpha */
591 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
592 return;
593 case STATE_SPECULAR:
594 for (i = 0; i < 3; i++) {
595 value[i] = ctx->Light.Light[ln].Specular[i] *
596 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
597 }
598 /* [3] = material alpha */
599 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
600 return;
601 default:
602 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
603 return;
604 }
605 }
606 return;
607 case STATE_TEXGEN:
608 {
609 /* state[1] is the texture unit */
610 const GLuint unit = (GLuint) state[1];
611 /* state[2] is the texgen attribute */
612 switch (state[2]) {
613 case STATE_TEXGEN_EYE_S:
614 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
615 return;
616 case STATE_TEXGEN_EYE_T:
617 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
618 return;
619 case STATE_TEXGEN_EYE_R:
620 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
621 return;
622 case STATE_TEXGEN_EYE_Q:
623 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
624 return;
625 case STATE_TEXGEN_OBJECT_S:
626 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
627 return;
628 case STATE_TEXGEN_OBJECT_T:
629 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
630 return;
631 case STATE_TEXGEN_OBJECT_R:
632 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
633 return;
634 case STATE_TEXGEN_OBJECT_Q:
635 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
636 return;
637 default:
638 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
639 return;
640 }
641 }
642 return;
643 case STATE_TEXENV_COLOR:
644 {
645 /* state[1] is the texture unit */
646 const GLuint unit = (GLuint) state[1];
647 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
648 }
649 return;
650 case STATE_FOG_COLOR:
651 COPY_4V(value, ctx->Fog.Color);
652 return;
653 case STATE_FOG_PARAMS:
654 value[0] = ctx->Fog.Density;
655 value[1] = ctx->Fog.Start;
656 value[2] = ctx->Fog.End;
657 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
658 return;
659 case STATE_CLIPPLANE:
660 {
661 const GLuint plane = (GLuint) state[1];
662 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
663 }
664 return;
665 case STATE_POINT_SIZE:
666 value[0] = ctx->Point.Size;
667 value[1] = ctx->Point.MinSize;
668 value[2] = ctx->Point.MaxSize;
669 value[3] = ctx->Point.Threshold;
670 return;
671 case STATE_POINT_ATTENUATION:
672 value[0] = ctx->Point.Params[0];
673 value[1] = ctx->Point.Params[1];
674 value[2] = ctx->Point.Params[2];
675 value[3] = 1.0F;
676 return;
677 case STATE_MATRIX:
678 {
679 /* state[1] = modelview, projection, texture, etc. */
680 /* state[2] = which texture matrix or program matrix */
681 /* state[3] = first column to fetch */
682 /* state[4] = last column to fetch */
683 /* state[5] = transpose, inverse or invtrans */
684
685 const GLmatrix *matrix;
686 const enum state_index mat = state[1];
687 const GLuint index = (GLuint) state[2];
688 const GLuint first = (GLuint) state[3];
689 const GLuint last = (GLuint) state[4];
690 const enum state_index modifier = state[5];
691 const GLfloat *m;
692 GLuint row, i;
693 if (mat == STATE_MODELVIEW) {
694 matrix = ctx->ModelviewMatrixStack.Top;
695 }
696 else if (mat == STATE_PROJECTION) {
697 matrix = ctx->ProjectionMatrixStack.Top;
698 }
699 else if (mat == STATE_MVP) {
700 matrix = &ctx->_ModelProjectMatrix;
701 }
702 else if (mat == STATE_TEXTURE) {
703 matrix = ctx->TextureMatrixStack[index].Top;
704 }
705 else if (mat == STATE_PROGRAM) {
706 matrix = ctx->ProgramMatrixStack[index].Top;
707 }
708 else {
709 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
710 return;
711 }
712 if (modifier == STATE_MATRIX_INVERSE ||
713 modifier == STATE_MATRIX_INVTRANS) {
714 /* XXX be sure inverse is up to date */
715 m = matrix->inv;
716 }
717 else {
718 m = matrix->m;
719 }
720 if (modifier == STATE_MATRIX_TRANSPOSE ||
721 modifier == STATE_MATRIX_INVTRANS) {
722 for (i = 0, row = first; row <= last; row++) {
723 value[i++] = m[row * 4 + 0];
724 value[i++] = m[row * 4 + 1];
725 value[i++] = m[row * 4 + 2];
726 value[i++] = m[row * 4 + 3];
727 }
728 }
729 else {
730 for (i = 0, row = first; row <= last; row++) {
731 value[i++] = m[row + 0];
732 value[i++] = m[row + 4];
733 value[i++] = m[row + 8];
734 value[i++] = m[row + 12];
735 }
736 }
737 }
738 return;
739 case STATE_DEPTH_RANGE:
740 value[0] = ctx->Viewport.Near; /* near */
741 value[1] = ctx->Viewport.Far; /* far */
742 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
743 value[3] = 0;
744 return;
745 case STATE_FRAGMENT_PROGRAM:
746 {
747 /* state[1] = {STATE_ENV, STATE_LOCAL} */
748 /* state[2] = parameter index */
749 int idx = state[2];
750
751 switch (state[1]) {
752 case STATE_ENV:
753 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
754 break;
755
756 case STATE_LOCAL:
757 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
758 break;
759 default:
760 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
761 return;
762 }
763 }
764 return;
765
766 case STATE_VERTEX_PROGRAM:
767 {
768 /* state[1] = {STATE_ENV, STATE_LOCAL} */
769 /* state[2] = parameter index */
770 int idx = state[2];
771
772 switch (state[1]) {
773 case STATE_ENV:
774 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
775 break;
776
777 case STATE_LOCAL:
778 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
779 break;
780 default:
781 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
782 return;
783 }
784 }
785 return;
786 default:
787 _mesa_problem(ctx, "Invalid state in fetch_state");
788 return;
789 }
790 }
791
792
793 /**
794 * Loop over all the parameters in a parameter list. If the parameter
795 * is a GL state reference, look up the current value of that state
796 * variable and put it into the parameter's Value[4] array.
797 * This would be called at glBegin time when using a fragment program.
798 */
799 void
800 _mesa_load_state_parameters(GLcontext *ctx,
801 struct program_parameter_list *paramList)
802 {
803 GLuint i;
804 for (i = 0; i < paramList->NumParameters; i++) {
805 if (paramList->Parameters[i].Type == STATE) {
806 _mesa_fetch_state(ctx, paramList->Parameters[i].StateIndexes,
807 paramList->Parameters[i].Values);
808 }
809 }
810 }
811
812
813
814 /**********************************************************************/
815 /* API functions */
816 /**********************************************************************/
817
818
819 /**
820 * Bind a program (make it current)
821 * \note Called from the GL API dispatcher by both glBindProgramNV
822 * and glBindProgramARB.
823 */
824 void GLAPIENTRY
825 _mesa_BindProgram(GLenum target, GLuint id)
826 {
827 struct program *prog;
828 GET_CURRENT_CONTEXT(ctx);
829 ASSERT_OUTSIDE_BEGIN_END(ctx);
830
831 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
832
833 if ((target == GL_VERTEX_PROGRAM_NV
834 && ctx->Extensions.NV_vertex_program) ||
835 (target == GL_VERTEX_PROGRAM_ARB
836 && ctx->Extensions.ARB_vertex_program)) {
837 if (ctx->VertexProgram.Current &&
838 ctx->VertexProgram.Current->Base.Id == id)
839 return;
840 /* decrement refcount on previously bound vertex program */
841 if (ctx->VertexProgram.Current) {
842 ctx->VertexProgram.Current->Base.RefCount--;
843 /* and delete if refcount goes below one */
844 if (ctx->VertexProgram.Current->Base.RefCount <= 0) {
845 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
846 _mesa_HashRemove(ctx->Shared->Programs, id);
847 }
848 }
849 }
850 else if ((target == GL_FRAGMENT_PROGRAM_NV
851 && ctx->Extensions.NV_fragment_program) ||
852 (target == GL_FRAGMENT_PROGRAM_ARB
853 && ctx->Extensions.ARB_fragment_program)) {
854 if (ctx->FragmentProgram.Current &&
855 ctx->FragmentProgram.Current->Base.Id == id)
856 return;
857 /* decrement refcount on previously bound fragment program */
858 if (ctx->FragmentProgram.Current) {
859 ctx->FragmentProgram.Current->Base.RefCount--;
860 /* and delete if refcount goes below one */
861 if (ctx->FragmentProgram.Current->Base.RefCount <= 0) {
862 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
863 _mesa_HashRemove(ctx->Shared->Programs, id);
864 }
865 }
866 }
867 else {
868 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
869 return;
870 }
871
872 /* NOTE: binding to a non-existant program is not an error.
873 * That's supposed to be caught in glBegin.
874 */
875 if (id == 0) {
876 /* default program */
877 prog = NULL;
878 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
879 prog = ctx->Shared->DefaultVertexProgram;
880 else
881 prog = ctx->Shared->DefaultFragmentProgram;
882 }
883 else {
884 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
885 if (prog) {
886 if (prog->Target == 0) {
887 /* prog was allocated with glGenProgramsNV */
888 prog->Target = target;
889 }
890 else if (prog->Target != target) {
891 _mesa_error(ctx, GL_INVALID_OPERATION,
892 "glBindProgramNV/ARB(target mismatch)");
893 return;
894 }
895 }
896 else {
897 /* allocate a new program now */
898 prog = ctx->Driver.NewProgram(ctx, target, id);
899 if (!prog) {
900 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
901 return;
902 }
903 prog->Id = id;
904 prog->Target = target;
905 prog->Resident = GL_TRUE;
906 prog->RefCount = 1;
907 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
908 }
909 }
910
911 /* bind now */
912 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
913 ctx->VertexProgram.Current = (struct vertex_program *) prog;
914 }
915 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
916 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
917 }
918
919 if (prog)
920 prog->RefCount++;
921
922 if (ctx->Driver.BindProgram)
923 ctx->Driver.BindProgram(ctx, target, prog);
924 }
925
926
927 /**
928 * Delete a list of programs.
929 * \note Not compiled into display lists.
930 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
931 */
932 void GLAPIENTRY
933 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
934 {
935 GLint i;
936 GET_CURRENT_CONTEXT(ctx);
937 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
938
939 if (n < 0) {
940 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
941 return;
942 }
943
944 for (i = 0; i < n; i++) {
945 if (ids[i] != 0) {
946 struct program *prog = (struct program *)
947 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
948 if (prog) {
949 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
950 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
951 if (ctx->VertexProgram.Current &&
952 ctx->VertexProgram.Current->Base.Id == ids[i]) {
953 /* unbind this currently bound program */
954 _mesa_BindProgram(prog->Target, 0);
955 }
956 }
957 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
958 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
959 if (ctx->FragmentProgram.Current &&
960 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
961 /* unbind this currently bound program */
962 _mesa_BindProgram(prog->Target, 0);
963 }
964 }
965 else {
966 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
967 return;
968 }
969 prog->RefCount--;
970 if (prog->RefCount <= 0) {
971 ctx->Driver.DeleteProgram(ctx, prog);
972 }
973 }
974 else {
975 /* This is necessary as we can't tell from HashLookup
976 * whether the entry exists with data == 0, or if it
977 * doesn't exist at all. As GenPrograms creates the first
978 * case below, need to call Remove() to avoid memory leak:
979 */
980 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
981 }
982 }
983 }
984 }
985
986
987 /**
988 * Generate a list of new program identifiers.
989 * \note Not compiled into display lists.
990 * \note Called by both glGenProgramsNV and glGenProgramsARB.
991 */
992 void GLAPIENTRY
993 _mesa_GenPrograms(GLsizei n, GLuint *ids)
994 {
995 GLuint first;
996 GLuint i;
997 GET_CURRENT_CONTEXT(ctx);
998 ASSERT_OUTSIDE_BEGIN_END(ctx);
999
1000 if (n < 0) {
1001 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1002 return;
1003 }
1004
1005 if (!ids)
1006 return;
1007
1008 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1009
1010 for (i = 0; i < (GLuint) n; i++) {
1011 _mesa_HashInsert(ctx->Shared->Programs, first + i, 0);
1012 }
1013
1014 /* Return the program names */
1015 for (i = 0; i < (GLuint) n; i++) {
1016 ids[i] = first + i;
1017 }
1018 }
1019
1020
1021 /**
1022 * Determine if id names a program.
1023 * \note Not compiled into display lists.
1024 * \note Called from both glIsProgramNV and glIsProgramARB.
1025 * \param id is the program identifier
1026 * \return GL_TRUE if id is a program, else GL_FALSE.
1027 */
1028 GLboolean GLAPIENTRY
1029 _mesa_IsProgram(GLuint id)
1030 {
1031 GET_CURRENT_CONTEXT(ctx);
1032 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1033
1034 if (id == 0)
1035 return GL_FALSE;
1036
1037 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1038 return GL_TRUE;
1039 else
1040 return GL_FALSE;
1041 }
1042
1043
1044
1045 /**********************************************************************/
1046 /* GL_MESA_program_debug extension */
1047 /**********************************************************************/
1048
1049
1050 /* XXX temporary */
1051 void
1052 glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1053 GLvoid *data)
1054 {
1055 _mesa_ProgramCallbackMESA(target, callback, data);
1056 }
1057
1058
1059 void
1060 _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1061 GLvoid *data)
1062 {
1063 GET_CURRENT_CONTEXT(ctx);
1064
1065 switch (target) {
1066 case GL_FRAGMENT_PROGRAM_ARB:
1067 if (!ctx->Extensions.ARB_fragment_program) {
1068 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1069 return;
1070 }
1071 ctx->FragmentProgram.Callback = callback;
1072 ctx->FragmentProgram.CallbackData = data;
1073 break;
1074 case GL_FRAGMENT_PROGRAM_NV:
1075 if (!ctx->Extensions.NV_fragment_program) {
1076 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1077 return;
1078 }
1079 ctx->FragmentProgram.Callback = callback;
1080 ctx->FragmentProgram.CallbackData = data;
1081 break;
1082 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1083 if (!ctx->Extensions.ARB_vertex_program &&
1084 !ctx->Extensions.NV_vertex_program) {
1085 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1086 return;
1087 }
1088 ctx->VertexProgram.Callback = callback;
1089 ctx->VertexProgram.CallbackData = data;
1090 break;
1091 default:
1092 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1093 return;
1094 }
1095 }
1096
1097
1098 /* XXX temporary */
1099 void
1100 glGetProgramRegisterfvMESA(GLenum target,
1101 GLsizei len, const GLubyte *registerName,
1102 GLfloat *v)
1103 {
1104 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1105 }
1106
1107
1108 void
1109 _mesa_GetProgramRegisterfvMESA(GLenum target,
1110 GLsizei len, const GLubyte *registerName,
1111 GLfloat *v)
1112 {
1113 char reg[1000];
1114 GET_CURRENT_CONTEXT(ctx);
1115
1116 /* We _should_ be inside glBegin/glEnd */
1117 #if 0
1118 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1119 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1120 return;
1121 }
1122 #endif
1123
1124 /* make null-terminated copy of registerName */
1125 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1126 _mesa_memcpy(reg, registerName, len);
1127 reg[len] = 0;
1128
1129 switch (target) {
1130 case GL_VERTEX_PROGRAM_NV:
1131 if (!ctx->Extensions.ARB_vertex_program &&
1132 !ctx->Extensions.NV_vertex_program) {
1133 _mesa_error(ctx, GL_INVALID_ENUM,
1134 "glGetProgramRegisterfvMESA(target)");
1135 return;
1136 }
1137 if (!ctx->VertexProgram.Enabled) {
1138 _mesa_error(ctx, GL_INVALID_OPERATION,
1139 "glGetProgramRegisterfvMESA");
1140 return;
1141 }
1142 /* GL_NV_vertex_program */
1143 if (reg[0] == 'R') {
1144 /* Temp register */
1145 GLint i = _mesa_atoi(reg + 1);
1146 if (i >= (GLint)ctx->Const.MaxVertexProgramTemps) {
1147 _mesa_error(ctx, GL_INVALID_VALUE,
1148 "glGetProgramRegisterfvMESA(registerName)");
1149 return;
1150 }
1151 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1152 }
1153 else if (reg[0] == 'v' && reg[1] == '[') {
1154 /* Vertex Input attribute */
1155 GLuint i;
1156 for (i = 0; i < ctx->Const.MaxVertexProgramAttribs; i++) {
1157 const char *name = _mesa_nv_vertex_input_register_name(i);
1158 char number[10];
1159 sprintf(number, "%d", i);
1160 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1161 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1162 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1163 return;
1164 }
1165 }
1166 _mesa_error(ctx, GL_INVALID_VALUE,
1167 "glGetProgramRegisterfvMESA(registerName)");
1168 return;
1169 }
1170 else if (reg[0] == 'o' && reg[1] == '[') {
1171 /* Vertex output attribute */
1172 }
1173 /* GL_ARB_vertex_program */
1174 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1175
1176 }
1177 else {
1178 _mesa_error(ctx, GL_INVALID_VALUE,
1179 "glGetProgramRegisterfvMESA(registerName)");
1180 return;
1181 }
1182 break;
1183 case GL_FRAGMENT_PROGRAM_ARB:
1184 if (!ctx->Extensions.ARB_fragment_program) {
1185 _mesa_error(ctx, GL_INVALID_ENUM,
1186 "glGetProgramRegisterfvMESA(target)");
1187 return;
1188 }
1189 if (!ctx->FragmentProgram.Enabled) {
1190 _mesa_error(ctx, GL_INVALID_OPERATION,
1191 "glGetProgramRegisterfvMESA");
1192 return;
1193 }
1194 /* XXX to do */
1195 break;
1196 case GL_FRAGMENT_PROGRAM_NV:
1197 if (!ctx->Extensions.NV_fragment_program) {
1198 _mesa_error(ctx, GL_INVALID_ENUM,
1199 "glGetProgramRegisterfvMESA(target)");
1200 return;
1201 }
1202 if (!ctx->FragmentProgram.Enabled) {
1203 _mesa_error(ctx, GL_INVALID_OPERATION,
1204 "glGetProgramRegisterfvMESA");
1205 return;
1206 }
1207 if (reg[0] == 'R') {
1208 /* Temp register */
1209 GLint i = _mesa_atoi(reg + 1);
1210 if (i >= (GLint)ctx->Const.MaxFragmentProgramTemps) {
1211 _mesa_error(ctx, GL_INVALID_VALUE,
1212 "glGetProgramRegisterfvMESA(registerName)");
1213 return;
1214 }
1215 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1216 }
1217 else if (reg[0] == 'f' && reg[1] == '[') {
1218 /* Fragment input attribute */
1219 GLuint i;
1220 for (i = 0; i < ctx->Const.MaxFragmentProgramAttribs; i++) {
1221 const char *name = _mesa_nv_fragment_input_register_name(i);
1222 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1223 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1224 return;
1225 }
1226 }
1227 _mesa_error(ctx, GL_INVALID_VALUE,
1228 "glGetProgramRegisterfvMESA(registerName)");
1229 return;
1230 }
1231 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1232 /* Fragment output color */
1233 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR]);
1234 }
1235 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1236 /* Fragment output color */
1237 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLH]);
1238 }
1239 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1240 /* Fragment output depth */
1241 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR]);
1242 }
1243 else {
1244 /* try user-defined identifiers */
1245 const GLfloat *value = _mesa_lookup_parameter_value(
1246 ctx->FragmentProgram.Current->Parameters, -1, reg);
1247 if (value) {
1248 COPY_4V(v, value);
1249 }
1250 else {
1251 _mesa_error(ctx, GL_INVALID_VALUE,
1252 "glGetProgramRegisterfvMESA(registerName)");
1253 return;
1254 }
1255 }
1256 break;
1257 default:
1258 _mesa_error(ctx, GL_INVALID_ENUM,
1259 "glGetProgramRegisterfvMESA(target)");
1260 return;
1261 }
1262
1263 }