blob: 18bebd227abc095c6c02359b9dc78ba292cadc95 [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian Paul6d460af2004-04-23 14:16:46 +00003 * Version: 6.1
Michal Krol2861e732004-03-29 11:09:34 +00004 *
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 */
52void
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;
Michal Krol2861e732004-03-29 11:09:34 +000064 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 */
86void
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 */
107const 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
140static 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
154struct 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
164struct 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 */
187struct 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 */
213void
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
244struct 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 */
255void
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 */
267void
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 */
283static GLint
284add_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 */
314GLint
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 */
328GLint
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 */
342GLint
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 */
363GLint
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 */
385GLfloat *
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 */
417GLint
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 */
452static 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:
Brian Paul824fdf02004-06-29 00:00:06 +0000743 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;
Michal Krol2861e732004-03-29 11:09:34 +0000747 return;
748 case STATE_FRAGMENT_PROGRAM:
749 {
750 /* state[1] = {STATE_ENV, STATE_LOCAL} */
751 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000752 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000753 switch (state[1]) {
754 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000755 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000756 break;
Michal Krol2861e732004-03-29 11:09:34 +0000757 case STATE_LOCAL:
758 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000759 break;
Michal Krol2861e732004-03-29 11:09:34 +0000760 default:
761 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
762 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000763 }
764 }
Michal Krol2861e732004-03-29 11:09:34 +0000765 return;
766
767 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000768 {
Michal Krol2861e732004-03-29 11:09:34 +0000769 /* state[1] = {STATE_ENV, STATE_LOCAL} */
770 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000771 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000772 switch (state[1]) {
773 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000774 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000775 break;
Michal Krol2861e732004-03-29 11:09:34 +0000776 case STATE_LOCAL:
777 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000778 break;
Michal Krol2861e732004-03-29 11:09:34 +0000779 default:
780 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
781 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000782 }
783 }
Michal Krol2861e732004-03-29 11:09:34 +0000784 return;
785 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000786 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000787 return;
788 }
789}
790
791
792/**
793 * Loop over all the parameters in a parameter list. If the parameter
794 * is a GL state reference, look up the current value of that state
795 * variable and put it into the parameter's Value[4] array.
796 * This would be called at glBegin time when using a fragment program.
797 */
798void
799_mesa_load_state_parameters(GLcontext *ctx,
800 struct program_parameter_list *paramList)
801{
802 GLuint i;
803
804 if (!paramList)
805 return;
806
807 for (i = 0; i < paramList->NumParameters; i++) {
808 if (paramList->Parameters[i].Type == STATE) {
809 _mesa_fetch_state(ctx, paramList->Parameters[i].StateIndexes,
810 paramList->Parameters[i].Values);
811 }
812 }
813}
814
815
816
817/**********************************************************************/
818/* API functions */
819/**********************************************************************/
820
821
822/**
823 * Bind a program (make it current)
824 * \note Called from the GL API dispatcher by both glBindProgramNV
825 * and glBindProgramARB.
826 */
827void GLAPIENTRY
828_mesa_BindProgram(GLenum target, GLuint id)
829{
830 struct program *prog;
831 GET_CURRENT_CONTEXT(ctx);
832 ASSERT_OUTSIDE_BEGIN_END(ctx);
833
834 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
835
836 if ((target == GL_VERTEX_PROGRAM_NV
837 && ctx->Extensions.NV_vertex_program) ||
838 (target == GL_VERTEX_PROGRAM_ARB
839 && ctx->Extensions.ARB_vertex_program)) {
840 if (ctx->VertexProgram.Current &&
841 ctx->VertexProgram.Current->Base.Id == id)
842 return;
843 /* decrement refcount on previously bound vertex program */
844 if (ctx->VertexProgram.Current) {
845 ctx->VertexProgram.Current->Base.RefCount--;
846 /* and delete if refcount goes below one */
847 if (ctx->VertexProgram.Current->Base.RefCount <= 0) {
848 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
849 _mesa_HashRemove(ctx->Shared->Programs, id);
850 }
851 }
852 }
853 else if ((target == GL_FRAGMENT_PROGRAM_NV
854 && ctx->Extensions.NV_fragment_program) ||
855 (target == GL_FRAGMENT_PROGRAM_ARB
856 && ctx->Extensions.ARB_fragment_program)) {
857 if (ctx->FragmentProgram.Current &&
858 ctx->FragmentProgram.Current->Base.Id == id)
859 return;
860 /* decrement refcount on previously bound fragment program */
861 if (ctx->FragmentProgram.Current) {
862 ctx->FragmentProgram.Current->Base.RefCount--;
863 /* and delete if refcount goes below one */
864 if (ctx->FragmentProgram.Current->Base.RefCount <= 0) {
865 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
866 _mesa_HashRemove(ctx->Shared->Programs, id);
867 }
868 }
869 }
870 else {
871 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
872 return;
873 }
874
875 /* NOTE: binding to a non-existant program is not an error.
876 * That's supposed to be caught in glBegin.
877 */
878 if (id == 0) {
879 /* default program */
880 prog = NULL;
881 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
882 prog = ctx->Shared->DefaultVertexProgram;
883 else
884 prog = ctx->Shared->DefaultFragmentProgram;
885 }
886 else {
887 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
888 if (prog) {
889 if (prog->Target == 0) {
890 /* prog was allocated with glGenProgramsNV */
891 prog->Target = target;
892 }
893 else if (prog->Target != target) {
894 _mesa_error(ctx, GL_INVALID_OPERATION,
895 "glBindProgramNV/ARB(target mismatch)");
896 return;
897 }
898 }
899 else {
900 /* allocate a new program now */
901 prog = ctx->Driver.NewProgram(ctx, target, id);
902 if (!prog) {
903 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
904 return;
905 }
Michal Krol2861e732004-03-29 11:09:34 +0000906 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
907 }
908 }
909
910 /* bind now */
911 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
912 ctx->VertexProgram.Current = (struct vertex_program *) prog;
913 }
914 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
915 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
916 }
917
918 if (prog)
919 prog->RefCount++;
920
921 if (ctx->Driver.BindProgram)
922 ctx->Driver.BindProgram(ctx, target, prog);
923}
924
925
926/**
927 * Delete a list of programs.
928 * \note Not compiled into display lists.
929 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
930 */
931void GLAPIENTRY
932_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
933{
934 GLint i;
935 GET_CURRENT_CONTEXT(ctx);
936 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
937
938 if (n < 0) {
939 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
940 return;
941 }
942
943 for (i = 0; i < n; i++) {
944 if (ids[i] != 0) {
945 struct program *prog = (struct program *)
946 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
947 if (prog) {
948 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
949 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
950 if (ctx->VertexProgram.Current &&
951 ctx->VertexProgram.Current->Base.Id == ids[i]) {
952 /* unbind this currently bound program */
953 _mesa_BindProgram(prog->Target, 0);
954 }
955 }
956 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
957 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
958 if (ctx->FragmentProgram.Current &&
959 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
960 /* unbind this currently bound program */
961 _mesa_BindProgram(prog->Target, 0);
962 }
963 }
964 else {
965 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
966 return;
967 }
968 prog->RefCount--;
969 if (prog->RefCount <= 0) {
970 ctx->Driver.DeleteProgram(ctx, prog);
971 }
972 }
Brian Paulc9a41562004-06-25 14:46:48 +0000973 /* Always remove entry from hash table.
974 * This is necessary as we can't tell from HashLookup
975 * whether the entry exists with data == 0, or if it
976 * doesn't exist at all. As GenPrograms creates the first
977 * case below, need to call Remove() to avoid memory leak:
978 */
979 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
Michal Krol2861e732004-03-29 11:09:34 +0000980 }
981 }
982}
983
984
985/**
986 * Generate a list of new program identifiers.
987 * \note Not compiled into display lists.
988 * \note Called by both glGenProgramsNV and glGenProgramsARB.
989 */
990void GLAPIENTRY
991_mesa_GenPrograms(GLsizei n, GLuint *ids)
992{
993 GLuint first;
994 GLuint i;
995 GET_CURRENT_CONTEXT(ctx);
996 ASSERT_OUTSIDE_BEGIN_END(ctx);
997
998 if (n < 0) {
999 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1000 return;
1001 }
1002
1003 if (!ids)
1004 return;
1005
1006 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1007
1008 for (i = 0; i < (GLuint) n; i++) {
1009 _mesa_HashInsert(ctx->Shared->Programs, first + i, 0);
1010 }
1011
1012 /* Return the program names */
1013 for (i = 0; i < (GLuint) n; i++) {
1014 ids[i] = first + i;
1015 }
1016}
1017
1018
1019/**
1020 * Determine if id names a program.
1021 * \note Not compiled into display lists.
1022 * \note Called from both glIsProgramNV and glIsProgramARB.
1023 * \param id is the program identifier
1024 * \return GL_TRUE if id is a program, else GL_FALSE.
1025 */
1026GLboolean GLAPIENTRY
1027_mesa_IsProgram(GLuint id)
1028{
1029 GET_CURRENT_CONTEXT(ctx);
1030 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1031
1032 if (id == 0)
1033 return GL_FALSE;
1034
1035 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1036 return GL_TRUE;
1037 else
1038 return GL_FALSE;
1039}
1040
1041
1042
1043/**********************************************************************/
1044/* GL_MESA_program_debug extension */
1045/**********************************************************************/
1046
1047
1048/* XXX temporary */
1049void
1050glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1051 GLvoid *data)
1052{
1053 _mesa_ProgramCallbackMESA(target, callback, data);
1054}
1055
1056
1057void
1058_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1059 GLvoid *data)
1060{
1061 GET_CURRENT_CONTEXT(ctx);
1062
1063 switch (target) {
1064 case GL_FRAGMENT_PROGRAM_ARB:
1065 if (!ctx->Extensions.ARB_fragment_program) {
1066 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1067 return;
1068 }
1069 ctx->FragmentProgram.Callback = callback;
1070 ctx->FragmentProgram.CallbackData = data;
1071 break;
1072 case GL_FRAGMENT_PROGRAM_NV:
1073 if (!ctx->Extensions.NV_fragment_program) {
1074 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1075 return;
1076 }
1077 ctx->FragmentProgram.Callback = callback;
1078 ctx->FragmentProgram.CallbackData = data;
1079 break;
1080 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1081 if (!ctx->Extensions.ARB_vertex_program &&
1082 !ctx->Extensions.NV_vertex_program) {
1083 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1084 return;
1085 }
1086 ctx->VertexProgram.Callback = callback;
1087 ctx->VertexProgram.CallbackData = data;
1088 break;
1089 default:
1090 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1091 return;
1092 }
1093}
1094
1095
1096/* XXX temporary */
1097void
1098glGetProgramRegisterfvMESA(GLenum target,
1099 GLsizei len, const GLubyte *registerName,
1100 GLfloat *v)
1101{
1102 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1103}
1104
1105
1106void
1107_mesa_GetProgramRegisterfvMESA(GLenum target,
1108 GLsizei len, const GLubyte *registerName,
1109 GLfloat *v)
1110{
1111 char reg[1000];
1112 GET_CURRENT_CONTEXT(ctx);
1113
1114 /* We _should_ be inside glBegin/glEnd */
1115#if 0
1116 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1117 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1118 return;
1119 }
1120#endif
1121
1122 /* make null-terminated copy of registerName */
1123 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1124 _mesa_memcpy(reg, registerName, len);
1125 reg[len] = 0;
1126
1127 switch (target) {
1128 case GL_VERTEX_PROGRAM_NV:
1129 if (!ctx->Extensions.ARB_vertex_program &&
1130 !ctx->Extensions.NV_vertex_program) {
1131 _mesa_error(ctx, GL_INVALID_ENUM,
1132 "glGetProgramRegisterfvMESA(target)");
1133 return;
1134 }
Brian Paul6d460af2004-04-23 14:16:46 +00001135 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001136 _mesa_error(ctx, GL_INVALID_OPERATION,
1137 "glGetProgramRegisterfvMESA");
1138 return;
1139 }
1140 /* GL_NV_vertex_program */
1141 if (reg[0] == 'R') {
1142 /* Temp register */
1143 GLint i = _mesa_atoi(reg + 1);
1144 if (i >= (GLint)ctx->Const.MaxVertexProgramTemps) {
1145 _mesa_error(ctx, GL_INVALID_VALUE,
1146 "glGetProgramRegisterfvMESA(registerName)");
1147 return;
1148 }
1149 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1150 }
1151 else if (reg[0] == 'v' && reg[1] == '[') {
1152 /* Vertex Input attribute */
1153 GLuint i;
1154 for (i = 0; i < ctx->Const.MaxVertexProgramAttribs; i++) {
1155 const char *name = _mesa_nv_vertex_input_register_name(i);
1156 char number[10];
1157 sprintf(number, "%d", i);
1158 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1159 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1160 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1161 return;
1162 }
1163 }
1164 _mesa_error(ctx, GL_INVALID_VALUE,
1165 "glGetProgramRegisterfvMESA(registerName)");
1166 return;
1167 }
1168 else if (reg[0] == 'o' && reg[1] == '[') {
1169 /* Vertex output attribute */
1170 }
1171 /* GL_ARB_vertex_program */
1172 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1173
1174 }
1175 else {
1176 _mesa_error(ctx, GL_INVALID_VALUE,
1177 "glGetProgramRegisterfvMESA(registerName)");
1178 return;
1179 }
1180 break;
1181 case GL_FRAGMENT_PROGRAM_ARB:
1182 if (!ctx->Extensions.ARB_fragment_program) {
1183 _mesa_error(ctx, GL_INVALID_ENUM,
1184 "glGetProgramRegisterfvMESA(target)");
1185 return;
1186 }
Brian Paul6d460af2004-04-23 14:16:46 +00001187 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001188 _mesa_error(ctx, GL_INVALID_OPERATION,
1189 "glGetProgramRegisterfvMESA");
1190 return;
1191 }
1192 /* XXX to do */
1193 break;
1194 case GL_FRAGMENT_PROGRAM_NV:
1195 if (!ctx->Extensions.NV_fragment_program) {
1196 _mesa_error(ctx, GL_INVALID_ENUM,
1197 "glGetProgramRegisterfvMESA(target)");
1198 return;
1199 }
Brian Paul6d460af2004-04-23 14:16:46 +00001200 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001201 _mesa_error(ctx, GL_INVALID_OPERATION,
1202 "glGetProgramRegisterfvMESA");
1203 return;
1204 }
1205 if (reg[0] == 'R') {
1206 /* Temp register */
1207 GLint i = _mesa_atoi(reg + 1);
1208 if (i >= (GLint)ctx->Const.MaxFragmentProgramTemps) {
1209 _mesa_error(ctx, GL_INVALID_VALUE,
1210 "glGetProgramRegisterfvMESA(registerName)");
1211 return;
1212 }
1213 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1214 }
1215 else if (reg[0] == 'f' && reg[1] == '[') {
1216 /* Fragment input attribute */
1217 GLuint i;
1218 for (i = 0; i < ctx->Const.MaxFragmentProgramAttribs; i++) {
1219 const char *name = _mesa_nv_fragment_input_register_name(i);
1220 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1221 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1222 return;
1223 }
1224 }
1225 _mesa_error(ctx, GL_INVALID_VALUE,
1226 "glGetProgramRegisterfvMESA(registerName)");
1227 return;
1228 }
1229 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1230 /* Fragment output color */
1231 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR]);
1232 }
1233 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1234 /* Fragment output color */
1235 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLH]);
1236 }
1237 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1238 /* Fragment output depth */
1239 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR]);
1240 }
1241 else {
1242 /* try user-defined identifiers */
1243 const GLfloat *value = _mesa_lookup_parameter_value(
1244 ctx->FragmentProgram.Current->Parameters, -1, reg);
1245 if (value) {
1246 COPY_4V(v, value);
1247 }
1248 else {
1249 _mesa_error(ctx, GL_INVALID_VALUE,
1250 "glGetProgramRegisterfvMESA(registerName)");
1251 return;
1252 }
1253 }
1254 break;
1255 default:
1256 _mesa_error(ctx, GL_INVALID_ENUM,
1257 "glGetProgramRegisterfvMESA(target)");
1258 return;
1259 }
1260
1261}