blob: a77c447e284cb1d981b20e4b40e9addd348affad [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian Paul16241622005-11-03 02:26:47 +00003 * Version: 6.5
Michal Krol2861e732004-03-29 11:09:34 +00004 *
Brian Paul16241622005-11-03 02:26:47 +00005 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
Michal Krol2861e732004-03-29 11:09:34 +00006 *
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"
Brian Paul7e807512005-11-05 17:10:45 +000040#include "program_instruction.h"
Michal Krol2861e732004-03-29 11:09:34 +000041#include "nvvertparse.h"
Roland Scheideggerf519a772005-09-02 01:11:53 +000042#include "atifragshader.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
Dave Airlie7f752fe2004-12-19 03:06:59 +000086
87#if FEATURE_ATI_fragment_shader
88 ctx->ATIFragmentShader.Enabled = GL_FALSE;
89 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
90 assert(ctx->ATIFragmentShader.Current);
91 ctx->ATIFragmentShader.Current->Base.RefCount++;
92#endif
Michal Krol2861e732004-03-29 11:09:34 +000093}
94
95
96/**
Brian Paul21841f02004-08-14 14:28:11 +000097 * Free a context's vertex/fragment program state
98 */
99void
100_mesa_free_program_data(GLcontext *ctx)
101{
102#if FEATURE_NV_vertex_program
103 if (ctx->VertexProgram.Current) {
104 ctx->VertexProgram.Current->Base.RefCount--;
105 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
106 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
107 }
108#endif
109#if FEATURE_NV_fragment_program
110 if (ctx->FragmentProgram.Current) {
111 ctx->FragmentProgram.Current->Base.RefCount--;
112 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
113 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
114 }
115#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000116#if FEATURE_ATI_fragment_shader
117 if (ctx->ATIFragmentShader.Current) {
118 ctx->ATIFragmentShader.Current->Base.RefCount--;
119 if (ctx->ATIFragmentShader.Current->Base.RefCount <= 0)
120 ctx->Driver.DeleteProgram(ctx, &(ctx->ATIFragmentShader.Current->Base));
121 }
122#endif
Brian Paul21841f02004-08-14 14:28:11 +0000123 _mesa_free((void *) ctx->Program.ErrorString);
124}
125
126
127
128
129/**
Michal Krol2861e732004-03-29 11:09:34 +0000130 * Set the vertex/fragment program error state (position and error string).
131 * This is generally called from within the parsers.
132 */
133void
134_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
135{
136 ctx->Program.ErrorPos = pos;
137 _mesa_free((void *) ctx->Program.ErrorString);
138 if (!string)
139 string = "";
140 ctx->Program.ErrorString = _mesa_strdup(string);
141}
142
143
144/**
145 * Find the line number and column for 'pos' within 'string'.
146 * Return a copy of the line which contains 'pos'. Free the line with
147 * _mesa_free().
148 * \param string the program string
149 * \param pos the position within the string
150 * \param line returns the line number corresponding to 'pos'.
151 * \param col returns the column number corresponding to 'pos'.
152 * \return copy of the line containing 'pos'.
153 */
154const GLubyte *
155_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
156 GLint *line, GLint *col)
157{
158 const GLubyte *lineStart = string;
159 const GLubyte *p = string;
160 GLubyte *s;
161 int len;
162
163 *line = 1;
164
165 while (p != pos) {
166 if (*p == (GLubyte) '\n') {
167 (*line)++;
168 lineStart = p + 1;
169 }
170 p++;
171 }
172
173 *col = (pos - lineStart) + 1;
174
175 /* return copy of this line */
176 while (*p != 0 && *p != '\n')
177 p++;
178 len = p - lineStart;
179 s = (GLubyte *) _mesa_malloc(len + 1);
180 _mesa_memcpy(s, lineStart, len);
181 s[len] = 0;
182
183 return s;
184}
185
186
Brian Paul765f1a12004-09-14 22:28:27 +0000187/**
188 * Initialize a new vertex/fragment program object.
189 */
190static struct program *
191_mesa_init_program_struct( GLcontext *ctx, struct program *prog,
192 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000193{
Brian Paula6c423d2004-08-25 15:59:48 +0000194 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000195 if (prog) {
196 prog->Id = id;
197 prog->Target = target;
198 prog->Resident = GL_TRUE;
199 prog->RefCount = 1;
200 }
201
202 return prog;
203}
204
Brian Paul765f1a12004-09-14 22:28:27 +0000205
206/**
207 * Initialize a new fragment program object.
208 */
209struct program *
210_mesa_init_fragment_program( GLcontext *ctx, struct fragment_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
Brian Paul765f1a12004-09-14 22:28:27 +0000219
220/**
221 * Initialize a new vertex program object.
222 */
223struct program *
224_mesa_init_vertex_program( GLcontext *ctx, struct vertex_program *prog,
225 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000226{
227 if (prog)
228 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
229 else
230 return NULL;
231}
232
Dave Airlie7f752fe2004-12-19 03:06:59 +0000233/**
234 * Initialize a new ATI fragment shader object.
235 */
236struct program *
Brian Paulcdb65412005-01-11 15:56:47 +0000237_mesa_init_ati_fragment_shader( GLcontext *ctx,
238 struct ati_fragment_shader *prog,
239 GLenum target, GLuint id )
Dave Airlie7f752fe2004-12-19 03:06:59 +0000240{
241 if (prog)
242 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
243 else
244 return NULL;
245}
246
247
Michal Krol2861e732004-03-29 11:09:34 +0000248
249/**
250 * Allocate and initialize a new fragment/vertex program object but
251 * don't put it into the program hash table. Called via
252 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
253 * device driver function to implement OO deriviation with additional
254 * types not understood by this function.
255 *
256 * \param ctx context
257 * \param id program id/number
258 * \param target program target/type
259 * \return pointer to new program object
260 */
261struct program *
262_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
263{
264 switch (target) {
265 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
266 return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program),
267 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000268 case GL_FRAGMENT_PROGRAM_NV:
269 case GL_FRAGMENT_PROGRAM_ARB:
270 return _mesa_init_fragment_program( ctx, CALLOC_STRUCT(fragment_program),
271 target, id );
Dave Airlie7f752fe2004-12-19 03:06:59 +0000272 case GL_FRAGMENT_SHADER_ATI:
273 return _mesa_init_ati_fragment_shader( ctx, CALLOC_STRUCT(ati_fragment_shader),
274 target, id );
275
Michal Krol2861e732004-03-29 11:09:34 +0000276 default:
277 _mesa_problem(ctx, "bad target in _mesa_new_program");
278 return NULL;
279 }
280}
281
282
283/**
284 * Delete a program and remove it from the hash table, ignoring the
285 * reference count.
286 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
287 * by a device driver function.
288 */
289void
290_mesa_delete_program(GLcontext *ctx, struct program *prog)
291{
Brian Paula6c423d2004-08-25 15:59:48 +0000292 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000293 ASSERT(prog);
294
295 if (prog->String)
296 _mesa_free(prog->String);
297 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
298 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
299 struct vertex_program *vprog = (struct vertex_program *) prog;
Brian Paul575700f2004-12-16 03:07:18 +0000300 if (vprog->Instructions) {
301 GLuint i;
302 for (i = 0; i < vprog->Base.NumInstructions; i++) {
303 if (vprog->Instructions[i].Data)
304 _mesa_free(vprog->Instructions[i].Data);
305 }
Michal Krol2861e732004-03-29 11:09:34 +0000306 _mesa_free(vprog->Instructions);
Brian Paul575700f2004-12-16 03:07:18 +0000307 }
Brian Paul21841f02004-08-14 14:28:11 +0000308 if (vprog->Parameters)
309 _mesa_free_parameter_list(vprog->Parameters);
Michal Krol2861e732004-03-29 11:09:34 +0000310 }
311 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
312 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
313 struct fragment_program *fprog = (struct fragment_program *) prog;
Brian Paul2a5afe32004-12-18 16:18:00 +0000314 if (fprog->Instructions) {
315 GLuint i;
316 for (i = 0; i < fprog->Base.NumInstructions; i++) {
317 if (fprog->Instructions[i].Data)
318 _mesa_free(fprog->Instructions[i].Data);
319 }
Michal Krol2861e732004-03-29 11:09:34 +0000320 _mesa_free(fprog->Instructions);
Brian Paul2a5afe32004-12-18 16:18:00 +0000321 }
Brian Paul21841f02004-08-14 14:28:11 +0000322 if (fprog->Parameters)
Michal Krol2861e732004-03-29 11:09:34 +0000323 _mesa_free_parameter_list(fprog->Parameters);
Michal Krol2861e732004-03-29 11:09:34 +0000324 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000325 else if (prog->Target == GL_FRAGMENT_SHADER_ATI) {
326 struct ati_fragment_shader *atifs = (struct ati_fragment_shader *)prog;
Roland Scheideggerf519a772005-09-02 01:11:53 +0000327 GLuint i;
328 for (i = 0; i < MAX_NUM_PASSES_ATI; i++) {
329 if (atifs->Instructions[i])
330 _mesa_free(atifs->Instructions[i]);
331 if (atifs->SetupInst[i])
332 _mesa_free(atifs->SetupInst[i]);
333 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000334 }
335
Michal Krol2861e732004-03-29 11:09:34 +0000336 _mesa_free(prog);
337}
338
339
340
341/**********************************************************************/
342/* Program parameter functions */
343/**********************************************************************/
344
345struct program_parameter_list *
346_mesa_new_parameter_list(void)
347{
348 return (struct program_parameter_list *)
349 _mesa_calloc(sizeof(struct program_parameter_list));
350}
351
352
353/**
354 * Free a parameter list and all its parameters
355 */
356void
357_mesa_free_parameter_list(struct program_parameter_list *paramList)
358{
359 _mesa_free_parameters(paramList);
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000360 _mesa_free(paramList->Parameters);
Keith Whitwell9899f582005-06-08 21:57:45 +0000361 if (paramList->ParameterValues)
362 ALIGN_FREE(paramList->ParameterValues);
Michal Krol2861e732004-03-29 11:09:34 +0000363 _mesa_free(paramList);
364}
365
366
367/**
368 * Free all the parameters in the given list, but don't free the
369 * paramList structure itself.
370 */
371void
372_mesa_free_parameters(struct program_parameter_list *paramList)
373{
374 GLuint i;
375 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000376 if (paramList->Parameters[i].Name)
377 _mesa_free((void *) paramList->Parameters[i].Name);
Michal Krol2861e732004-03-29 11:09:34 +0000378 }
Michal Krol2861e732004-03-29 11:09:34 +0000379 paramList->NumParameters = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000380}
381
382
383/**
384 * Helper function used by the functions below.
Brian Paul16241622005-11-03 02:26:47 +0000385 * \return index of new parameter in the list, or -1 if error (out of mem)
Michal Krol2861e732004-03-29 11:09:34 +0000386 */
387static GLint
388add_parameter(struct program_parameter_list *paramList,
389 const char *name, const GLfloat values[4],
Brian Paul613e1ad2005-11-05 02:15:21 +0000390 enum register_file type)
Michal Krol2861e732004-03-29 11:09:34 +0000391{
392 const GLuint n = paramList->NumParameters;
393
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000394 if (n == paramList->Size) {
Keith Whitwell9899f582005-06-08 21:57:45 +0000395 GLfloat (*tmp)[4];
396
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000397 paramList->Size *= 2;
398 if (!paramList->Size)
Keith Whitwell9899f582005-06-08 21:57:45 +0000399 paramList->Size = 8;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000400
401 paramList->Parameters = (struct program_parameter *)
402 _mesa_realloc(paramList->Parameters,
403 n * sizeof(struct program_parameter),
404 paramList->Size * sizeof(struct program_parameter));
Keith Whitwell9899f582005-06-08 21:57:45 +0000405
406 tmp = paramList->ParameterValues;
407 paramList->ParameterValues = ALIGN_MALLOC(paramList->Size * 4 * sizeof(GLfloat), 16);
408 if (tmp) {
409 _mesa_memcpy(paramList->ParameterValues, tmp,
410 n * 4 * sizeof(GLfloat));
411 ALIGN_FREE(tmp);
412 }
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000413 }
Keith Whitwell7c26b612005-04-21 14:46:57 +0000414
415 if (!paramList->Parameters ||
416 !paramList->ParameterValues) {
Michal Krol2861e732004-03-29 11:09:34 +0000417 /* out of memory */
418 paramList->NumParameters = 0;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000419 paramList->Size = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000420 return -1;
421 }
422 else {
423 paramList->NumParameters = n + 1;
Keith Whitwella42fe192005-05-10 18:22:19 +0000424
425 _mesa_memset(&paramList->Parameters[n], 0,
426 sizeof(struct program_parameter));
427
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000428 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000429 paramList->Parameters[n].Type = type;
430 if (values)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000431 COPY_4V(paramList->ParameterValues[n], values);
Michal Krol2861e732004-03-29 11:09:34 +0000432 return (GLint) n;
433 }
434}
435
436
437/**
438 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
439 * \return index of the new entry in the parameter list
440 */
441GLint
442_mesa_add_named_parameter(struct program_parameter_list *paramList,
443 const char *name, const GLfloat values[4])
444{
Brian Paul613e1ad2005-11-05 02:15:21 +0000445 return add_parameter(paramList, name, values, PROGRAM_NAMED_PARAM);
Michal Krol2861e732004-03-29 11:09:34 +0000446}
447
448
449/**
450 * Add a new unnamed constant to the parameter list.
451 * \param paramList - the parameter list
452 * \param values - four float values
453 * \return index of the new parameter.
454 */
455GLint
456_mesa_add_named_constant(struct program_parameter_list *paramList,
457 const char *name, const GLfloat values[4])
458{
Brian Paul613e1ad2005-11-05 02:15:21 +0000459 return add_parameter(paramList, name, values, PROGRAM_CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000460}
461
462
463/**
464 * Add a new unnamed constant to the parameter list.
465 * \param paramList - the parameter list
466 * \param values - four float values
467 * \return index of the new parameter.
468 */
469GLint
470_mesa_add_unnamed_constant(struct program_parameter_list *paramList,
471 const GLfloat values[4])
472{
Brian Paul613e1ad2005-11-05 02:15:21 +0000473 return add_parameter(paramList, NULL, values, PROGRAM_CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000474}
475
476
477/**
478 * Add a new state reference to the parameter list.
479 * \param paramList - the parameter list
480 * \param state - an array of 6 state tokens
481 *
482 * \return index of the new parameter.
483 */
484GLint
485_mesa_add_state_reference(struct program_parameter_list *paramList,
Brian Paul16241622005-11-03 02:26:47 +0000486 const GLint *stateTokens)
Michal Krol2861e732004-03-29 11:09:34 +0000487{
Brian Paul16241622005-11-03 02:26:47 +0000488 /* XXX we should probably search the current parameter list to see if
489 * the new state reference is already present.
Michal Krol2861e732004-03-29 11:09:34 +0000490 */
Brian Paul16241622005-11-03 02:26:47 +0000491 GLint index;
Michal Krol2861e732004-03-29 11:09:34 +0000492
Brian Paul613e1ad2005-11-05 02:15:21 +0000493 index = add_parameter(paramList, NULL, NULL, PROGRAM_STATE_VAR);
Brian Paul16241622005-11-03 02:26:47 +0000494 if (index >= 0) {
495 GLuint i;
496 for (i = 0; i < 6; i++)
497 paramList->Parameters[index].StateIndexes[i]
498 = (enum state_index) stateTokens[i];
499 }
Michal Krol2861e732004-03-29 11:09:34 +0000500
Brian Paul16241622005-11-03 02:26:47 +0000501 return index;
Michal Krol2861e732004-03-29 11:09:34 +0000502}
503
504
505/**
506 * Lookup a parameter value by name in the given parameter list.
507 * \return pointer to the float[4] values.
508 */
509GLfloat *
510_mesa_lookup_parameter_value(struct program_parameter_list *paramList,
511 GLsizei nameLen, const char *name)
512{
513 GLuint i;
514
515 if (!paramList)
516 return NULL;
517
518 if (nameLen == -1) {
519 /* name is null-terminated */
520 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000521 if (paramList->Parameters[i].Name &&
522 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000523 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000524 }
525 }
526 else {
527 /* name is not null-terminated, use nameLen */
528 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000529 if (paramList->Parameters[i].Name &&
530 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000531 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000532 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000533 }
534 }
535 return NULL;
536}
537
538
539/**
540 * Lookup a parameter index by name in the given parameter list.
541 * \return index of parameter in the list.
542 */
543GLint
544_mesa_lookup_parameter_index(struct program_parameter_list *paramList,
545 GLsizei nameLen, const char *name)
546{
547 GLint i;
548
549 if (!paramList)
550 return -1;
551
552 if (nameLen == -1) {
553 /* name is null-terminated */
554 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000555 if (paramList->Parameters[i].Name &&
556 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Michal Krol2861e732004-03-29 11:09:34 +0000557 return i;
558 }
559 }
560 else {
561 /* name is not null-terminated, use nameLen */
562 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000563 if (paramList->Parameters[i].Name &&
564 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000565 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
566 return i;
567 }
568 }
569 return -1;
570}
571
572
573/**
574 * Use the list of tokens in the state[] array to find global GL state
575 * and return it in <value>. Usually, four values are returned in <value>
576 * but matrix queries may return as many as 16 values.
577 * This function is used for ARB vertex/fragment programs.
578 * The program parser will produce the state[] values.
579 */
580static void
581_mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
582 GLfloat *value)
583{
584 switch (state[0]) {
585 case STATE_MATERIAL:
586 {
587 /* state[1] is either 0=front or 1=back side */
588 const GLuint face = (GLuint) state[1];
589 /* state[2] is the material attribute */
590 switch (state[2]) {
591 case STATE_AMBIENT:
592 if (face == 0)
593 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
594 else
595 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
596 return;
597 case STATE_DIFFUSE:
598 if (face == 0)
599 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
600 else
601 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
602 return;
603 case STATE_SPECULAR:
604 if (face == 0)
605 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
606 else
607 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
608 return;
609 case STATE_EMISSION:
610 if (face == 0)
611 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
612 else
613 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
614 return;
615 case STATE_SHININESS:
616 if (face == 0)
617 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
618 else
619 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
620 value[1] = 0.0F;
621 value[2] = 0.0F;
622 value[3] = 1.0F;
623 return;
624 default:
625 _mesa_problem(ctx, "Invalid material state in fetch_state");
626 return;
627 }
Alan Hourihane22ae6332004-12-02 13:29:40 +0000628 }
Michal Krol2861e732004-03-29 11:09:34 +0000629 case STATE_LIGHT:
630 {
631 /* state[1] is the light number */
632 const GLuint ln = (GLuint) state[1];
633 /* state[2] is the light attribute */
634 switch (state[2]) {
635 case STATE_AMBIENT:
636 COPY_4V(value, ctx->Light.Light[ln].Ambient);
637 return;
638 case STATE_DIFFUSE:
639 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
640 return;
641 case STATE_SPECULAR:
642 COPY_4V(value, ctx->Light.Light[ln].Specular);
643 return;
644 case STATE_POSITION:
645 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
646 return;
647 case STATE_ATTENUATION:
648 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
649 value[1] = ctx->Light.Light[ln].LinearAttenuation;
650 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
651 value[3] = ctx->Light.Light[ln].SpotExponent;
652 return;
653 case STATE_SPOT_DIRECTION:
Brian Paul52bf0052005-04-20 23:47:03 +0000654 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
655 value[3] = ctx->Light.Light[ln]._CosCutoff;
Michal Krol2861e732004-03-29 11:09:34 +0000656 return;
657 case STATE_HALF:
658 {
659 GLfloat eye_z[] = {0, 0, 1};
660
661 /* Compute infinite half angle vector:
662 * half-vector = light_position + (0, 0, 1)
663 * and then normalize. w = 0
Keith Whitwell7c26b612005-04-21 14:46:57 +0000664 *
665 * light.EyePosition.w should be 0 for infinite lights.
Michal Krol2861e732004-03-29 11:09:34 +0000666 */
Keith Whitwell7c26b612005-04-21 14:46:57 +0000667 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
668 NORMALIZE_3FV(value);
669 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000670 }
671 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000672 case STATE_POSITION_NORMALIZED:
673 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
674 NORMALIZE_3FV( value );
675 return;
Michal Krol2861e732004-03-29 11:09:34 +0000676 default:
677 _mesa_problem(ctx, "Invalid light state in fetch_state");
678 return;
679 }
680 }
Michal Krol2861e732004-03-29 11:09:34 +0000681 case STATE_LIGHTMODEL_AMBIENT:
682 COPY_4V(value, ctx->Light.Model.Ambient);
683 return;
684 case STATE_LIGHTMODEL_SCENECOLOR:
685 if (state[1] == 0) {
686 /* front */
687 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000688 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000689 value[i] = ctx->Light.Model.Ambient[i]
690 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
691 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
692 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000693 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000694 }
695 else {
696 /* back */
697 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000698 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000699 value[i] = ctx->Light.Model.Ambient[i]
700 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
701 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
702 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000703 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000704 }
705 return;
706 case STATE_LIGHTPROD:
707 {
708 const GLuint ln = (GLuint) state[1];
709 const GLuint face = (GLuint) state[2];
710 GLint i;
711 ASSERT(face == 0 || face == 1);
712 switch (state[3]) {
713 case STATE_AMBIENT:
714 for (i = 0; i < 3; i++) {
715 value[i] = ctx->Light.Light[ln].Ambient[i] *
716 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
717 }
718 /* [3] = material alpha */
719 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
720 return;
721 case STATE_DIFFUSE:
722 for (i = 0; i < 3; i++) {
723 value[i] = ctx->Light.Light[ln].Diffuse[i] *
724 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
725 }
726 /* [3] = material alpha */
727 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
728 return;
729 case STATE_SPECULAR:
730 for (i = 0; i < 3; i++) {
731 value[i] = ctx->Light.Light[ln].Specular[i] *
732 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
733 }
734 /* [3] = material alpha */
735 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
736 return;
737 default:
738 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
739 return;
740 }
741 }
Michal Krol2861e732004-03-29 11:09:34 +0000742 case STATE_TEXGEN:
743 {
744 /* state[1] is the texture unit */
745 const GLuint unit = (GLuint) state[1];
746 /* state[2] is the texgen attribute */
747 switch (state[2]) {
748 case STATE_TEXGEN_EYE_S:
749 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
750 return;
751 case STATE_TEXGEN_EYE_T:
752 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
753 return;
754 case STATE_TEXGEN_EYE_R:
755 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
756 return;
757 case STATE_TEXGEN_EYE_Q:
758 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
759 return;
760 case STATE_TEXGEN_OBJECT_S:
761 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
762 return;
763 case STATE_TEXGEN_OBJECT_T:
764 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
765 return;
766 case STATE_TEXGEN_OBJECT_R:
767 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
768 return;
769 case STATE_TEXGEN_OBJECT_Q:
770 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
771 return;
772 default:
773 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
774 return;
775 }
776 }
Michal Krol2861e732004-03-29 11:09:34 +0000777 case STATE_TEXENV_COLOR:
778 {
779 /* state[1] is the texture unit */
780 const GLuint unit = (GLuint) state[1];
781 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
782 }
783 return;
784 case STATE_FOG_COLOR:
785 COPY_4V(value, ctx->Fog.Color);
786 return;
787 case STATE_FOG_PARAMS:
788 value[0] = ctx->Fog.Density;
789 value[1] = ctx->Fog.Start;
790 value[2] = ctx->Fog.End;
791 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
792 return;
793 case STATE_CLIPPLANE:
794 {
795 const GLuint plane = (GLuint) state[1];
796 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
797 }
798 return;
799 case STATE_POINT_SIZE:
800 value[0] = ctx->Point.Size;
801 value[1] = ctx->Point.MinSize;
802 value[2] = ctx->Point.MaxSize;
803 value[3] = ctx->Point.Threshold;
804 return;
805 case STATE_POINT_ATTENUATION:
806 value[0] = ctx->Point.Params[0];
807 value[1] = ctx->Point.Params[1];
808 value[2] = ctx->Point.Params[2];
809 value[3] = 1.0F;
810 return;
811 case STATE_MATRIX:
812 {
813 /* state[1] = modelview, projection, texture, etc. */
814 /* state[2] = which texture matrix or program matrix */
815 /* state[3] = first column to fetch */
816 /* state[4] = last column to fetch */
817 /* state[5] = transpose, inverse or invtrans */
818
819 const GLmatrix *matrix;
820 const enum state_index mat = state[1];
821 const GLuint index = (GLuint) state[2];
822 const GLuint first = (GLuint) state[3];
823 const GLuint last = (GLuint) state[4];
824 const enum state_index modifier = state[5];
825 const GLfloat *m;
826 GLuint row, i;
827 if (mat == STATE_MODELVIEW) {
828 matrix = ctx->ModelviewMatrixStack.Top;
829 }
830 else if (mat == STATE_PROJECTION) {
831 matrix = ctx->ProjectionMatrixStack.Top;
832 }
833 else if (mat == STATE_MVP) {
834 matrix = &ctx->_ModelProjectMatrix;
835 }
836 else if (mat == STATE_TEXTURE) {
837 matrix = ctx->TextureMatrixStack[index].Top;
838 }
839 else if (mat == STATE_PROGRAM) {
840 matrix = ctx->ProgramMatrixStack[index].Top;
841 }
842 else {
843 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
844 return;
845 }
846 if (modifier == STATE_MATRIX_INVERSE ||
847 modifier == STATE_MATRIX_INVTRANS) {
Keith Whitwell78803b22005-04-15 12:57:23 +0000848 /* Be sure inverse is up to date:
849 */
Jouk Jansen49b1d952005-04-18 13:05:24 +0000850 _math_matrix_analyse( (GLmatrix*) matrix );
Michal Krol2861e732004-03-29 11:09:34 +0000851 m = matrix->inv;
852 }
853 else {
854 m = matrix->m;
855 }
856 if (modifier == STATE_MATRIX_TRANSPOSE ||
857 modifier == STATE_MATRIX_INVTRANS) {
858 for (i = 0, row = first; row <= last; row++) {
859 value[i++] = m[row * 4 + 0];
860 value[i++] = m[row * 4 + 1];
861 value[i++] = m[row * 4 + 2];
862 value[i++] = m[row * 4 + 3];
863 }
864 }
865 else {
866 for (i = 0, row = first; row <= last; row++) {
867 value[i++] = m[row + 0];
868 value[i++] = m[row + 4];
869 value[i++] = m[row + 8];
870 value[i++] = m[row + 12];
871 }
872 }
873 }
874 return;
875 case STATE_DEPTH_RANGE:
Brian Paul824fdf02004-06-29 00:00:06 +0000876 value[0] = ctx->Viewport.Near; /* near */
877 value[1] = ctx->Viewport.Far; /* far */
878 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
879 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000880 return;
881 case STATE_FRAGMENT_PROGRAM:
882 {
883 /* state[1] = {STATE_ENV, STATE_LOCAL} */
884 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000885 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000886 switch (state[1]) {
887 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000888 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000889 break;
Michal Krol2861e732004-03-29 11:09:34 +0000890 case STATE_LOCAL:
891 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000892 break;
Michal Krol2861e732004-03-29 11:09:34 +0000893 default:
894 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
895 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000896 }
897 }
Michal Krol2861e732004-03-29 11:09:34 +0000898 return;
899
900 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000901 {
Michal Krol2861e732004-03-29 11:09:34 +0000902 /* state[1] = {STATE_ENV, STATE_LOCAL} */
903 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000904 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000905 switch (state[1]) {
906 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000907 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000908 break;
Michal Krol2861e732004-03-29 11:09:34 +0000909 case STATE_LOCAL:
910 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000911 break;
Michal Krol2861e732004-03-29 11:09:34 +0000912 default:
913 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
914 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000915 }
916 }
Michal Krol2861e732004-03-29 11:09:34 +0000917 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000918
919 case STATE_INTERNAL:
920 {
921 switch (state[1]) {
922 case STATE_NORMAL_SCALE:
923 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
924 break;
925 default:
926 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
927 return;
928 }
929 }
930 return;
931
Michal Krol2861e732004-03-29 11:09:34 +0000932 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000933 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000934 return;
935 }
936}
937
938
939/**
940 * Loop over all the parameters in a parameter list. If the parameter
941 * is a GL state reference, look up the current value of that state
942 * variable and put it into the parameter's Value[4] array.
943 * This would be called at glBegin time when using a fragment program.
944 */
945void
946_mesa_load_state_parameters(GLcontext *ctx,
947 struct program_parameter_list *paramList)
948{
949 GLuint i;
950
951 if (!paramList)
952 return;
953
954 for (i = 0; i < paramList->NumParameters; i++) {
Brian Paul613e1ad2005-11-05 02:15:21 +0000955 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
Keith Whitwell7c26b612005-04-21 14:46:57 +0000956 _mesa_fetch_state(ctx,
957 paramList->Parameters[i].StateIndexes,
958 paramList->ParameterValues[i]);
Michal Krol2861e732004-03-29 11:09:34 +0000959 }
960 }
961}
962
963
Brian Paul1fcdaf12005-11-05 19:12:36 +0000964/**
965 * Basic info about each instruction
966 */
967struct instruction_info
968{
969 enum prog_opcode Opcode;
970 const char *Name;
971 GLuint NumSrcRegs;
972};
973
974/**
975 * Instruction info
976 * \note Opcode should equal array index!
977 */
978static const struct instruction_info InstInfo[MAX_OPCODE] = {
979 { OPCODE_ABS, "ABS", 1 },
980 { OPCODE_ADD, "ADD", 2 },
981 { OPCODE_ARL, "ARL", 1 },
982 { OPCODE_CMP, "CMP", 3 },
983 { OPCODE_COS, "COS", 1 },
984 { OPCODE_DDX, "DDX", 1 },
985 { OPCODE_DDY, "DDY", 1 },
986 { OPCODE_DP3, "DP3", 2 },
987 { OPCODE_DP4, "DP4", 2 },
988 { OPCODE_DPH, "DPH", 2 },
989 { OPCODE_DST, "DST", 2 },
990 { OPCODE_END, "END", 0 },
991 { OPCODE_EX2, "EX2", 1 },
992 { OPCODE_EXP, "EXP", 1 },
993 { OPCODE_FLR, "FLR", 1 },
994 { OPCODE_FRC, "FRC", 1 },
995 { OPCODE_KIL, "KIL", 1 },
996 { OPCODE_KIL_NV, "KIL", 0 },
997 { OPCODE_LG2, "LG2", 1 },
998 { OPCODE_LIT, "LIT", 1 },
999 { OPCODE_LOG, "LOG", 1 },
1000 { OPCODE_LRP, "LRP", 3 },
1001 { OPCODE_MAD, "MAD", 3 },
1002 { OPCODE_MAX, "MAX", 2 },
1003 { OPCODE_MIN, "MIN", 2 },
1004 { OPCODE_MOV, "MOV", 1 },
1005 { OPCODE_MUL, "MUL", 2 },
1006 { OPCODE_PK2H, "PK2H", 1 },
1007 { OPCODE_PK2US, "PK2US", 1 },
1008 { OPCODE_PK4B, "PK4B", 1 },
1009 { OPCODE_PK4UB, "PK4UB", 1 },
1010 { OPCODE_POW, "POW", 2 },
1011 { OPCODE_PRINT, "PRINT", 1 },
1012 { OPCODE_RCC, "RCC", 1 },
1013 { OPCODE_RCP, "RCP", 1 },
1014 { OPCODE_RFL, "RFL", 1 },
1015 { OPCODE_RSQ, "RSQ", 1 },
1016 { OPCODE_SCS, "SCS", 1 },
1017 { OPCODE_SEQ, "SEQ", 2 },
1018 { OPCODE_SFL, "SFL", 0 },
1019 { OPCODE_SGE, "SGE", 2 },
1020 { OPCODE_SGT, "SGT", 2 },
1021 { OPCODE_SIN, "SIN", 1 },
1022 { OPCODE_SLE, "SLE", 2 },
1023 { OPCODE_SLT, "SLT", 2 },
1024 { OPCODE_SNE, "SNE", 2 },
1025 { OPCODE_STR, "STR", 0 },
1026 { OPCODE_SUB, "SUB", 2 },
1027 { OPCODE_SWZ, "SWZ", 1 },
1028 { OPCODE_TEX, "TEX", 1 },
1029 { OPCODE_TXB, "TXB", 1 },
1030 { OPCODE_TXD, "TXD", 3 },
1031 { OPCODE_TXP, "TXP", 1 },
1032 { OPCODE_TXP_NV, "TXP", 1 },
1033 { OPCODE_UP2H, "UP2H", 1 },
1034 { OPCODE_UP2US, "UP2US", 1 },
1035 { OPCODE_UP4B, "UP4B", 1 },
1036 { OPCODE_UP4UB, "UP4UB", 1 },
1037 { OPCODE_X2D, "X2D", 3 },
1038 { OPCODE_XPD, "XPD", 2 }
1039};
1040
1041
1042/**
1043 * Return the number of src registers for the given instruction/opcode.
1044 */
1045GLuint
1046_mesa_num_inst_src_regs(enum prog_opcode opcode)
1047{
1048 GLuint i;
1049#ifdef DEBUG
1050 for (i = 0; i < MAX_OPCODE; i++) {
1051 ASSERT(i == InstInfo[i].Opcode);
1052 }
1053#endif
1054 for (i = 0; i < MAX_OPCODE; i++) {
1055 if (InstInfo[i].Opcode == opcode) {
1056 return InstInfo[i].NumSrcRegs;
1057 }
1058 }
1059 _mesa_problem(NULL, "invalid opcode in _mesa_num_inst_src_regs");
1060 return 0;
1061}
1062
1063
1064/**
1065 * Return string name for given program opcode.
1066 */
1067const char *
1068_mesa_opcode_string(enum prog_opcode opcode)
1069{
1070 ASSERT(opcode < MAX_OPCODE);
1071 return InstInfo[opcode].Name;
1072}
1073
Brian Paulbf41bc02005-11-05 19:32:36 +00001074/**
1075 * Return string name for given program/register file.
1076 */
Brian Paul30d6a4b2005-11-05 20:18:18 +00001077static const char *
1078program_file_string(enum register_file f)
Brian Paulbf41bc02005-11-05 19:32:36 +00001079{
1080 switch (f) {
1081 case PROGRAM_TEMPORARY:
1082 return "TEMP";
1083 case PROGRAM_LOCAL_PARAM:
1084 return "LOCAL";
1085 case PROGRAM_ENV_PARAM:
1086 return "ENV";
1087 case PROGRAM_STATE_VAR:
1088 return "STATE";
1089 case PROGRAM_INPUT:
1090 return "INPUT";
1091 case PROGRAM_OUTPUT:
1092 return "OUTPUT";
1093 case PROGRAM_NAMED_PARAM:
1094 return "NAMED";
1095 case PROGRAM_CONSTANT:
1096 return "CONST";
1097 case PROGRAM_WRITE_ONLY:
1098 return "WRITE_ONLY";
1099 case PROGRAM_ADDRESS:
1100 return "ADDR";
1101 default:
1102 return "!unkown!";
1103 }
1104}
1105
Michal Krol2861e732004-03-29 11:09:34 +00001106
Brian Paul30d6a4b2005-11-05 20:18:18 +00001107/**
1108 * Return a string representation of the given swizzle word.
1109 */
1110static const char *
1111swizzle_string(GLuint swizzle, GLuint negateBase)
1112{
1113 static const char swz[] = "xyzw01";
1114 static char s[20];
1115 GLuint i = 0;
1116
1117 if (swizzle == SWIZZLE_NOOP && negateBase == 0)
1118 return ""; /* no swizzle/negation */
1119
1120 s[i++] = '.';
1121
1122 if (negateBase & 0x1)
1123 s[i++] = '-';
1124 s[i++] = swz[GET_SWZ(swizzle, 0)];
1125
1126 if (negateBase & 0x2)
1127 s[i++] = '-';
1128 s[i++] = swz[GET_SWZ(swizzle, 1)];
1129
1130 if (negateBase & 0x4)
1131 s[i++] = '-';
1132 s[i++] = swz[GET_SWZ(swizzle, 2)];
1133
1134 if (negateBase & 0x8)
1135 s[i++] = '-';
1136 s[i++] = swz[GET_SWZ(swizzle, 3)];
1137
1138 s[i] = 0;
1139 return s;
1140}
1141
1142
1143static const char *
1144writemask_string(GLuint writeMask)
1145{
1146 static char s[10];
1147 GLuint i = 0;
1148
1149 if (writeMask == WRITEMASK_XYZW)
1150 return "";
1151
1152 s[i++] = '.';
1153 if (writeMask & WRITEMASK_X)
1154 s[i++] = 'x';
1155 if (writeMask & WRITEMASK_Y)
1156 s[i++] = 'y';
1157 if (writeMask & WRITEMASK_Z)
1158 s[i++] = 'z';
1159 if (writeMask & WRITEMASK_W)
1160 s[i++] = 'w';
1161
1162 s[i] = 0;
1163 return s;
1164}
1165
1166
1167/**
1168 * Print a vertx/fragment program to stdout.
1169 * XXX this function could be greatly improved.
1170 */
1171void
1172_mesa_print_program(GLuint count, const struct prog_instruction *inst)
1173{
1174 GLuint i;
1175
1176 for (i = 0; i < count; i++) {
1177 /* inst number */
1178 _mesa_printf("%3d: ", i);
1179
1180 switch (inst[i].Opcode) {
1181 case OPCODE_PRINT:
Brian Paul02df9e12005-11-08 14:42:52 +00001182 _mesa_printf("PRINT '%s'", inst[i].Data);
1183 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
1184 _mesa_printf(", ");
1185 _mesa_printf("%s[%d]%s",
1186 program_file_string(inst[i].SrcReg[0].File),
1187 inst[i].SrcReg[0].Index,
1188 swizzle_string(inst[i].SrcReg[0].Swizzle,
1189 inst[i].SrcReg[0].NegateBase));
1190 }
1191 _mesa_printf(";\n");
Brian Paul30d6a4b2005-11-05 20:18:18 +00001192 break;
1193 /* XXX check for a bunch of other special-case instructions */
1194 default:
1195 /* typical alu instruction */
1196 {
1197 const GLuint numRegs = _mesa_num_inst_src_regs(inst[i].Opcode);
1198 GLuint j;
1199
1200 _mesa_printf("%s", _mesa_opcode_string(inst[i].Opcode));
1201
1202 /* frag prog only */
1203 if (inst[i].Saturate)
1204 _mesa_printf("_SAT");
1205
1206 if (inst[i].DstReg.File != PROGRAM_UNDEFINED) {
1207 _mesa_printf(" %s[%d]%s",
1208 program_file_string(inst[i].DstReg.File),
1209 inst[i].DstReg.Index,
1210 writemask_string(inst[i].DstReg.WriteMask));
1211 }
1212
1213 if (numRegs > 0)
1214 _mesa_printf(", ");
1215
1216 for (j = 0; j < numRegs; j++) {
1217 _mesa_printf("%s[%d]%s",
1218 program_file_string(inst[i].SrcReg[j].File),
1219 inst[i].SrcReg[j].Index,
1220 swizzle_string(inst[i].SrcReg[j].Swizzle,
1221 inst[i].SrcReg[j].NegateBase));
1222 if (j + 1 < numRegs)
1223 _mesa_printf(", ");
1224 }
1225
1226 _mesa_printf(";\n");
1227 }
1228 }
1229 }
1230}
1231
1232
1233
1234
Michal Krol2861e732004-03-29 11:09:34 +00001235/**********************************************************************/
1236/* API functions */
1237/**********************************************************************/
1238
1239
1240/**
1241 * Bind a program (make it current)
1242 * \note Called from the GL API dispatcher by both glBindProgramNV
1243 * and glBindProgramARB.
1244 */
1245void GLAPIENTRY
1246_mesa_BindProgram(GLenum target, GLuint id)
1247{
1248 struct program *prog;
1249 GET_CURRENT_CONTEXT(ctx);
1250 ASSERT_OUTSIDE_BEGIN_END(ctx);
1251
1252 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1253
1254 if ((target == GL_VERTEX_PROGRAM_NV
1255 && ctx->Extensions.NV_vertex_program) ||
1256 (target == GL_VERTEX_PROGRAM_ARB
1257 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001258 /*** Vertex program binding ***/
1259 struct vertex_program *curProg = ctx->VertexProgram.Current;
1260 if (curProg->Base.Id == id) {
1261 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001262 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001263 }
1264 if (curProg->Base.Id != 0) {
1265 /* decrement refcount on previously bound vertex program */
1266 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001267 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001268 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001269 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001270 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001271 }
1272 }
1273 }
1274 else if ((target == GL_FRAGMENT_PROGRAM_NV
1275 && ctx->Extensions.NV_fragment_program) ||
1276 (target == GL_FRAGMENT_PROGRAM_ARB
1277 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001278 /*** Fragment program binding ***/
1279 struct fragment_program *curProg = ctx->FragmentProgram.Current;
1280 if (curProg->Base.Id == id) {
1281 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001282 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001283 }
1284 if (curProg->Base.Id != 0) {
1285 /* decrement refcount on previously bound fragment program */
1286 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001287 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001288 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001289 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001290 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001291 }
1292 }
1293 }
1294 else {
1295 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1296 return;
1297 }
1298
1299 /* NOTE: binding to a non-existant program is not an error.
1300 * That's supposed to be caught in glBegin.
1301 */
1302 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001303 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001304 prog = NULL;
1305 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1306 prog = ctx->Shared->DefaultVertexProgram;
1307 else
1308 prog = ctx->Shared->DefaultFragmentProgram;
1309 }
1310 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001311 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001312 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001313 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001314 /* allocate a new program now */
1315 prog = ctx->Driver.NewProgram(ctx, target, id);
1316 if (!prog) {
1317 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1318 return;
1319 }
Michal Krol2861e732004-03-29 11:09:34 +00001320 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1321 }
Brian Paul765f1a12004-09-14 22:28:27 +00001322 else if (prog->Target != target) {
1323 _mesa_error(ctx, GL_INVALID_OPERATION,
1324 "glBindProgramNV/ARB(target mismatch)");
1325 return;
1326 }
Michal Krol2861e732004-03-29 11:09:34 +00001327 }
1328
1329 /* bind now */
1330 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1331 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1332 }
1333 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1334 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1335 }
1336
Brian Paul765f1a12004-09-14 22:28:27 +00001337 /* Never null pointers */
1338 ASSERT(ctx->VertexProgram.Current);
1339 ASSERT(ctx->FragmentProgram.Current);
1340
Michal Krol2861e732004-03-29 11:09:34 +00001341 if (prog)
1342 prog->RefCount++;
1343
1344 if (ctx->Driver.BindProgram)
1345 ctx->Driver.BindProgram(ctx, target, prog);
1346}
1347
1348
1349/**
1350 * Delete a list of programs.
1351 * \note Not compiled into display lists.
1352 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1353 */
1354void GLAPIENTRY
1355_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1356{
1357 GLint i;
1358 GET_CURRENT_CONTEXT(ctx);
1359 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1360
1361 if (n < 0) {
1362 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1363 return;
1364 }
1365
1366 for (i = 0; i < n; i++) {
1367 if (ids[i] != 0) {
1368 struct program *prog = (struct program *)
1369 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001370 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001371 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1372 }
1373 else if (prog) {
1374 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001375 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1376 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1377 if (ctx->VertexProgram.Current &&
1378 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1379 /* unbind this currently bound program */
1380 _mesa_BindProgram(prog->Target, 0);
1381 }
1382 }
1383 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1384 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1385 if (ctx->FragmentProgram.Current &&
1386 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1387 /* unbind this currently bound program */
1388 _mesa_BindProgram(prog->Target, 0);
1389 }
1390 }
1391 else {
1392 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1393 return;
1394 }
Brian Paulea2943e2005-01-20 04:02:02 +00001395 /* The ID is immediately available for re-use now */
1396 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1397 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001398 if (prog->RefCount <= 0) {
1399 ctx->Driver.DeleteProgram(ctx, prog);
1400 }
1401 }
Michal Krol2861e732004-03-29 11:09:34 +00001402 }
1403 }
1404}
1405
1406
1407/**
1408 * Generate a list of new program identifiers.
1409 * \note Not compiled into display lists.
1410 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1411 */
1412void GLAPIENTRY
1413_mesa_GenPrograms(GLsizei n, GLuint *ids)
1414{
1415 GLuint first;
1416 GLuint i;
1417 GET_CURRENT_CONTEXT(ctx);
1418 ASSERT_OUTSIDE_BEGIN_END(ctx);
1419
1420 if (n < 0) {
1421 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1422 return;
1423 }
1424
1425 if (!ids)
1426 return;
1427
1428 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1429
Brian Paul765f1a12004-09-14 22:28:27 +00001430 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001431 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001432 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001433 }
1434
1435 /* Return the program names */
1436 for (i = 0; i < (GLuint) n; i++) {
1437 ids[i] = first + i;
1438 }
1439}
1440
1441
1442/**
Brian Paul765f1a12004-09-14 22:28:27 +00001443 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001444 * \note Not compiled into display lists.
1445 * \note Called from both glIsProgramNV and glIsProgramARB.
1446 * \param id is the program identifier
1447 * \return GL_TRUE if id is a program, else GL_FALSE.
1448 */
1449GLboolean GLAPIENTRY
1450_mesa_IsProgram(GLuint id)
1451{
1452 GET_CURRENT_CONTEXT(ctx);
1453 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1454
1455 if (id == 0)
1456 return GL_FALSE;
1457
1458 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1459 return GL_TRUE;
1460 else
1461 return GL_FALSE;
1462}
1463
1464
1465
1466/**********************************************************************/
1467/* GL_MESA_program_debug extension */
1468/**********************************************************************/
1469
1470
1471/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001472GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001473glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1474 GLvoid *data)
1475{
1476 _mesa_ProgramCallbackMESA(target, callback, data);
1477}
1478
1479
1480void
1481_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1482 GLvoid *data)
1483{
1484 GET_CURRENT_CONTEXT(ctx);
1485
1486 switch (target) {
1487 case GL_FRAGMENT_PROGRAM_ARB:
1488 if (!ctx->Extensions.ARB_fragment_program) {
1489 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1490 return;
1491 }
1492 ctx->FragmentProgram.Callback = callback;
1493 ctx->FragmentProgram.CallbackData = data;
1494 break;
1495 case GL_FRAGMENT_PROGRAM_NV:
1496 if (!ctx->Extensions.NV_fragment_program) {
1497 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1498 return;
1499 }
1500 ctx->FragmentProgram.Callback = callback;
1501 ctx->FragmentProgram.CallbackData = data;
1502 break;
1503 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1504 if (!ctx->Extensions.ARB_vertex_program &&
1505 !ctx->Extensions.NV_vertex_program) {
1506 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1507 return;
1508 }
1509 ctx->VertexProgram.Callback = callback;
1510 ctx->VertexProgram.CallbackData = data;
1511 break;
1512 default:
1513 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1514 return;
1515 }
1516}
1517
1518
1519/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001520GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001521glGetProgramRegisterfvMESA(GLenum target,
1522 GLsizei len, const GLubyte *registerName,
1523 GLfloat *v)
1524{
1525 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1526}
1527
1528
1529void
1530_mesa_GetProgramRegisterfvMESA(GLenum target,
1531 GLsizei len, const GLubyte *registerName,
1532 GLfloat *v)
1533{
1534 char reg[1000];
1535 GET_CURRENT_CONTEXT(ctx);
1536
1537 /* We _should_ be inside glBegin/glEnd */
1538#if 0
1539 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1540 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1541 return;
1542 }
1543#endif
1544
1545 /* make null-terminated copy of registerName */
1546 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1547 _mesa_memcpy(reg, registerName, len);
1548 reg[len] = 0;
1549
1550 switch (target) {
1551 case GL_VERTEX_PROGRAM_NV:
1552 if (!ctx->Extensions.ARB_vertex_program &&
1553 !ctx->Extensions.NV_vertex_program) {
1554 _mesa_error(ctx, GL_INVALID_ENUM,
1555 "glGetProgramRegisterfvMESA(target)");
1556 return;
1557 }
Brian Paul6d460af2004-04-23 14:16:46 +00001558 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001559 _mesa_error(ctx, GL_INVALID_OPERATION,
1560 "glGetProgramRegisterfvMESA");
1561 return;
1562 }
1563 /* GL_NV_vertex_program */
1564 if (reg[0] == 'R') {
1565 /* Temp register */
1566 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001567 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001568 _mesa_error(ctx, GL_INVALID_VALUE,
1569 "glGetProgramRegisterfvMESA(registerName)");
1570 return;
1571 }
1572 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1573 }
1574 else if (reg[0] == 'v' && reg[1] == '[') {
1575 /* Vertex Input attribute */
1576 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001577 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001578 const char *name = _mesa_nv_vertex_input_register_name(i);
1579 char number[10];
Brian Paulaa206952005-09-16 18:14:24 +00001580 _mesa_sprintf(number, "%d", i);
Michal Krol2861e732004-03-29 11:09:34 +00001581 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1582 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1583 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1584 return;
1585 }
1586 }
1587 _mesa_error(ctx, GL_INVALID_VALUE,
1588 "glGetProgramRegisterfvMESA(registerName)");
1589 return;
1590 }
1591 else if (reg[0] == 'o' && reg[1] == '[') {
1592 /* Vertex output attribute */
1593 }
1594 /* GL_ARB_vertex_program */
1595 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1596
1597 }
1598 else {
1599 _mesa_error(ctx, GL_INVALID_VALUE,
1600 "glGetProgramRegisterfvMESA(registerName)");
1601 return;
1602 }
1603 break;
1604 case GL_FRAGMENT_PROGRAM_ARB:
1605 if (!ctx->Extensions.ARB_fragment_program) {
1606 _mesa_error(ctx, GL_INVALID_ENUM,
1607 "glGetProgramRegisterfvMESA(target)");
1608 return;
1609 }
Brian Paul6d460af2004-04-23 14:16:46 +00001610 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001611 _mesa_error(ctx, GL_INVALID_OPERATION,
1612 "glGetProgramRegisterfvMESA");
1613 return;
1614 }
1615 /* XXX to do */
1616 break;
1617 case GL_FRAGMENT_PROGRAM_NV:
1618 if (!ctx->Extensions.NV_fragment_program) {
1619 _mesa_error(ctx, GL_INVALID_ENUM,
1620 "glGetProgramRegisterfvMESA(target)");
1621 return;
1622 }
Brian Paul6d460af2004-04-23 14:16:46 +00001623 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001624 _mesa_error(ctx, GL_INVALID_OPERATION,
1625 "glGetProgramRegisterfvMESA");
1626 return;
1627 }
1628 if (reg[0] == 'R') {
1629 /* Temp register */
1630 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001631 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001632 _mesa_error(ctx, GL_INVALID_VALUE,
1633 "glGetProgramRegisterfvMESA(registerName)");
1634 return;
1635 }
1636 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1637 }
1638 else if (reg[0] == 'f' && reg[1] == '[') {
1639 /* Fragment input attribute */
1640 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001641 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001642 const char *name = _mesa_nv_fragment_input_register_name(i);
1643 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1644 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1645 return;
1646 }
1647 }
1648 _mesa_error(ctx, GL_INVALID_VALUE,
1649 "glGetProgramRegisterfvMESA(registerName)");
1650 return;
1651 }
1652 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1653 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00001654 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLR]);
Michal Krol2861e732004-03-29 11:09:34 +00001655 }
1656 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
1657 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00001658 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLH]);
Michal Krol2861e732004-03-29 11:09:34 +00001659 }
1660 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
1661 /* Fragment output depth */
Brian Paul90ebb582005-11-02 18:06:12 +00001662 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_DEPR]);
Michal Krol2861e732004-03-29 11:09:34 +00001663 }
1664 else {
1665 /* try user-defined identifiers */
1666 const GLfloat *value = _mesa_lookup_parameter_value(
1667 ctx->FragmentProgram.Current->Parameters, -1, reg);
1668 if (value) {
1669 COPY_4V(v, value);
1670 }
1671 else {
1672 _mesa_error(ctx, GL_INVALID_VALUE,
1673 "glGetProgramRegisterfvMESA(registerName)");
1674 return;
1675 }
1676 }
1677 break;
1678 default:
1679 _mesa_error(ctx, GL_INVALID_ENUM,
1680 "glGetProgramRegisterfvMESA(target)");
1681 return;
1682 }
1683
1684}