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