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