blob: 38982de99360d913b8e510d70eb9bfe4af8db3a0 [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
Brian Paul7b98b402005-11-12 23:25:49 +000045static const char *
46make_state_string(const GLint stateTokens[6]);
47
48
Michal Krol2861e732004-03-29 11:09:34 +000049/**********************************************************************/
50/* Utility functions */
51/**********************************************************************/
52
53
Brian Paul765f1a12004-09-14 22:28:27 +000054/* A pointer to this dummy program is put into the hash table when
55 * glGenPrograms is called.
56 */
Brian Paul9ca83922004-10-02 15:16:59 +000057struct program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000058
59
Michal Krol2861e732004-03-29 11:09:34 +000060/**
Brian Paul21841f02004-08-14 14:28:11 +000061 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000062 */
63void
64_mesa_init_program(GLcontext *ctx)
65{
66 GLuint i;
67
68 ctx->Program.ErrorPos = -1;
69 ctx->Program.ErrorString = _mesa_strdup("");
70
71#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
72 ctx->VertexProgram.Enabled = GL_FALSE;
73 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
74 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Michal Krol2861e732004-03-29 11:09:34 +000075 ctx->VertexProgram.Current = (struct vertex_program *) ctx->Shared->DefaultVertexProgram;
76 assert(ctx->VertexProgram.Current);
77 ctx->VertexProgram.Current->Base.RefCount++;
78 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
79 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
80 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
81 }
82#endif
83
84#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
85 ctx->FragmentProgram.Enabled = GL_FALSE;
86 ctx->FragmentProgram.Current = (struct fragment_program *) ctx->Shared->DefaultFragmentProgram;
87 assert(ctx->FragmentProgram.Current);
88 ctx->FragmentProgram.Current->Base.RefCount++;
89#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000090
Brian Paul63d68302005-11-19 16:43:04 +000091 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000092#if FEATURE_ATI_fragment_shader
93 ctx->ATIFragmentShader.Enabled = GL_FALSE;
94 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
95 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +000096 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +000097#endif
Michal Krol2861e732004-03-29 11:09:34 +000098}
99
100
101/**
Brian Paul21841f02004-08-14 14:28:11 +0000102 * Free a context's vertex/fragment program state
103 */
104void
105_mesa_free_program_data(GLcontext *ctx)
106{
107#if FEATURE_NV_vertex_program
108 if (ctx->VertexProgram.Current) {
109 ctx->VertexProgram.Current->Base.RefCount--;
110 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
111 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
112 }
113#endif
114#if FEATURE_NV_fragment_program
115 if (ctx->FragmentProgram.Current) {
116 ctx->FragmentProgram.Current->Base.RefCount--;
117 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
118 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
119 }
120#endif
Brian Paul63d68302005-11-19 16:43:04 +0000121 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000122#if FEATURE_ATI_fragment_shader
123 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000124 ctx->ATIFragmentShader.Current->RefCount--;
125 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
126 _mesa_free(ctx->ATIFragmentShader.Current);
127 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000128 }
129#endif
Brian Paul21841f02004-08-14 14:28:11 +0000130 _mesa_free((void *) ctx->Program.ErrorString);
131}
132
133
134
135
136/**
Michal Krol2861e732004-03-29 11:09:34 +0000137 * Set the vertex/fragment program error state (position and error string).
138 * This is generally called from within the parsers.
139 */
140void
141_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
142{
143 ctx->Program.ErrorPos = pos;
144 _mesa_free((void *) ctx->Program.ErrorString);
145 if (!string)
146 string = "";
147 ctx->Program.ErrorString = _mesa_strdup(string);
148}
149
150
151/**
152 * Find the line number and column for 'pos' within 'string'.
153 * Return a copy of the line which contains 'pos'. Free the line with
154 * _mesa_free().
155 * \param string the program string
156 * \param pos the position within the string
157 * \param line returns the line number corresponding to 'pos'.
158 * \param col returns the column number corresponding to 'pos'.
159 * \return copy of the line containing 'pos'.
160 */
161const GLubyte *
162_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
163 GLint *line, GLint *col)
164{
165 const GLubyte *lineStart = string;
166 const GLubyte *p = string;
167 GLubyte *s;
168 int len;
169
170 *line = 1;
171
172 while (p != pos) {
173 if (*p == (GLubyte) '\n') {
174 (*line)++;
175 lineStart = p + 1;
176 }
177 p++;
178 }
179
180 *col = (pos - lineStart) + 1;
181
182 /* return copy of this line */
183 while (*p != 0 && *p != '\n')
184 p++;
185 len = p - lineStart;
186 s = (GLubyte *) _mesa_malloc(len + 1);
187 _mesa_memcpy(s, lineStart, len);
188 s[len] = 0;
189
190 return s;
191}
192
193
Brian Paul765f1a12004-09-14 22:28:27 +0000194/**
195 * Initialize a new vertex/fragment program object.
196 */
197static struct program *
198_mesa_init_program_struct( GLcontext *ctx, struct program *prog,
199 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000200{
Brian Paula6c423d2004-08-25 15:59:48 +0000201 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000202 if (prog) {
203 prog->Id = id;
204 prog->Target = target;
205 prog->Resident = GL_TRUE;
206 prog->RefCount = 1;
207 }
208
209 return prog;
210}
211
Brian Paul765f1a12004-09-14 22:28:27 +0000212
213/**
214 * Initialize a new fragment program object.
215 */
216struct program *
217_mesa_init_fragment_program( GLcontext *ctx, struct fragment_program *prog,
218 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000219{
220 if (prog)
221 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
222 else
223 return NULL;
224}
225
Brian Paul765f1a12004-09-14 22:28:27 +0000226
227/**
228 * Initialize a new vertex program object.
229 */
230struct program *
231_mesa_init_vertex_program( GLcontext *ctx, struct vertex_program *prog,
232 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000233{
234 if (prog)
235 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
236 else
237 return NULL;
238}
239
240
241/**
242 * Allocate and initialize a new fragment/vertex program object but
243 * don't put it into the program hash table. Called via
244 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
245 * device driver function to implement OO deriviation with additional
246 * types not understood by this function.
247 *
248 * \param ctx context
249 * \param id program id/number
250 * \param target program target/type
251 * \return pointer to new program object
252 */
253struct program *
254_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
255{
256 switch (target) {
257 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
258 return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program),
259 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000260 case GL_FRAGMENT_PROGRAM_NV:
261 case GL_FRAGMENT_PROGRAM_ARB:
262 return _mesa_init_fragment_program( ctx, CALLOC_STRUCT(fragment_program),
263 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000264 default:
265 _mesa_problem(ctx, "bad target in _mesa_new_program");
266 return NULL;
267 }
268}
269
270
271/**
272 * Delete a program and remove it from the hash table, ignoring the
273 * reference count.
274 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
275 * by a device driver function.
276 */
277void
278_mesa_delete_program(GLcontext *ctx, struct program *prog)
279{
Brian Paula6c423d2004-08-25 15:59:48 +0000280 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000281 ASSERT(prog);
282
283 if (prog->String)
284 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000285
286 if (prog->Instructions) {
287 GLuint i;
288 for (i = 0; i < prog->NumInstructions; i++) {
289 if (prog->Instructions[i].Data)
290 _mesa_free(prog->Instructions[i].Data);
Brian Paul575700f2004-12-16 03:07:18 +0000291 }
Brian Paulde997602005-11-12 17:53:14 +0000292 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000293 }
Brian Paulde997602005-11-12 17:53:14 +0000294
295 if (prog->Parameters)
296 _mesa_free_parameter_list(prog->Parameters);
297
Michal Krol2861e732004-03-29 11:09:34 +0000298 _mesa_free(prog);
299}
300
301
302
303/**********************************************************************/
304/* Program parameter functions */
305/**********************************************************************/
306
307struct program_parameter_list *
308_mesa_new_parameter_list(void)
309{
310 return (struct program_parameter_list *)
311 _mesa_calloc(sizeof(struct program_parameter_list));
312}
313
314
315/**
316 * Free a parameter list and all its parameters
317 */
318void
319_mesa_free_parameter_list(struct program_parameter_list *paramList)
320{
321 _mesa_free_parameters(paramList);
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000322 _mesa_free(paramList->Parameters);
Keith Whitwell9899f582005-06-08 21:57:45 +0000323 if (paramList->ParameterValues)
324 ALIGN_FREE(paramList->ParameterValues);
Michal Krol2861e732004-03-29 11:09:34 +0000325 _mesa_free(paramList);
326}
327
328
329/**
330 * Free all the parameters in the given list, but don't free the
331 * paramList structure itself.
332 */
333void
334_mesa_free_parameters(struct program_parameter_list *paramList)
335{
336 GLuint i;
337 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000338 if (paramList->Parameters[i].Name)
339 _mesa_free((void *) paramList->Parameters[i].Name);
Michal Krol2861e732004-03-29 11:09:34 +0000340 }
Michal Krol2861e732004-03-29 11:09:34 +0000341 paramList->NumParameters = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000342}
343
344
345/**
346 * Helper function used by the functions below.
Brian Paul16241622005-11-03 02:26:47 +0000347 * \return index of new parameter in the list, or -1 if error (out of mem)
Michal Krol2861e732004-03-29 11:09:34 +0000348 */
349static GLint
350add_parameter(struct program_parameter_list *paramList,
351 const char *name, const GLfloat values[4],
Brian Paul613e1ad2005-11-05 02:15:21 +0000352 enum register_file type)
Michal Krol2861e732004-03-29 11:09:34 +0000353{
354 const GLuint n = paramList->NumParameters;
355
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000356 if (n == paramList->Size) {
Keith Whitwell9899f582005-06-08 21:57:45 +0000357 GLfloat (*tmp)[4];
358
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000359 paramList->Size *= 2;
360 if (!paramList->Size)
Keith Whitwell9899f582005-06-08 21:57:45 +0000361 paramList->Size = 8;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000362
363 paramList->Parameters = (struct program_parameter *)
364 _mesa_realloc(paramList->Parameters,
365 n * sizeof(struct program_parameter),
366 paramList->Size * sizeof(struct program_parameter));
Keith Whitwell9899f582005-06-08 21:57:45 +0000367
368 tmp = paramList->ParameterValues;
369 paramList->ParameterValues = ALIGN_MALLOC(paramList->Size * 4 * sizeof(GLfloat), 16);
370 if (tmp) {
371 _mesa_memcpy(paramList->ParameterValues, tmp,
372 n * 4 * sizeof(GLfloat));
373 ALIGN_FREE(tmp);
374 }
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000375 }
Keith Whitwell7c26b612005-04-21 14:46:57 +0000376
377 if (!paramList->Parameters ||
378 !paramList->ParameterValues) {
Michal Krol2861e732004-03-29 11:09:34 +0000379 /* out of memory */
380 paramList->NumParameters = 0;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000381 paramList->Size = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000382 return -1;
383 }
384 else {
385 paramList->NumParameters = n + 1;
Keith Whitwella42fe192005-05-10 18:22:19 +0000386
387 _mesa_memset(&paramList->Parameters[n], 0,
388 sizeof(struct program_parameter));
389
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000390 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000391 paramList->Parameters[n].Type = type;
392 if (values)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000393 COPY_4V(paramList->ParameterValues[n], values);
Michal Krol2861e732004-03-29 11:09:34 +0000394 return (GLint) n;
395 }
396}
397
398
399/**
400 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
401 * \return index of the new entry in the parameter list
402 */
403GLint
404_mesa_add_named_parameter(struct program_parameter_list *paramList,
405 const char *name, const GLfloat values[4])
406{
Brian Paul613e1ad2005-11-05 02:15:21 +0000407 return add_parameter(paramList, name, values, PROGRAM_NAMED_PARAM);
Michal Krol2861e732004-03-29 11:09:34 +0000408}
409
410
411/**
412 * Add a new unnamed constant to the parameter list.
413 * \param paramList - the parameter list
414 * \param values - four float values
415 * \return index of the new parameter.
416 */
417GLint
418_mesa_add_named_constant(struct program_parameter_list *paramList,
419 const char *name, const GLfloat values[4])
420{
Brian Paul613e1ad2005-11-05 02:15:21 +0000421 return add_parameter(paramList, name, values, PROGRAM_CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000422}
423
424
425/**
426 * Add a new unnamed constant to the parameter list.
427 * \param paramList - the parameter list
428 * \param values - four float values
429 * \return index of the new parameter.
430 */
431GLint
432_mesa_add_unnamed_constant(struct program_parameter_list *paramList,
433 const GLfloat values[4])
434{
Brian Paul613e1ad2005-11-05 02:15:21 +0000435 return add_parameter(paramList, NULL, values, PROGRAM_CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000436}
437
438
439/**
440 * Add a new state reference to the parameter list.
441 * \param paramList - the parameter list
442 * \param state - an array of 6 state tokens
443 *
444 * \return index of the new parameter.
445 */
446GLint
447_mesa_add_state_reference(struct program_parameter_list *paramList,
Brian Paul16241622005-11-03 02:26:47 +0000448 const GLint *stateTokens)
Michal Krol2861e732004-03-29 11:09:34 +0000449{
Brian Paul16241622005-11-03 02:26:47 +0000450 /* XXX we should probably search the current parameter list to see if
451 * the new state reference is already present.
Michal Krol2861e732004-03-29 11:09:34 +0000452 */
Brian Paul16241622005-11-03 02:26:47 +0000453 GLint index;
Brian Paul7b98b402005-11-12 23:25:49 +0000454 const char *name = make_state_string(stateTokens);
Michal Krol2861e732004-03-29 11:09:34 +0000455
Brian Paul7b98b402005-11-12 23:25:49 +0000456 index = add_parameter(paramList, name, NULL, PROGRAM_STATE_VAR);
Brian Paul16241622005-11-03 02:26:47 +0000457 if (index >= 0) {
458 GLuint i;
459 for (i = 0; i < 6; i++)
460 paramList->Parameters[index].StateIndexes[i]
461 = (enum state_index) stateTokens[i];
462 }
Michal Krol2861e732004-03-29 11:09:34 +0000463
Brian Paul16241622005-11-03 02:26:47 +0000464 return index;
Michal Krol2861e732004-03-29 11:09:34 +0000465}
466
467
468/**
469 * Lookup a parameter value by name in the given parameter list.
470 * \return pointer to the float[4] values.
471 */
472GLfloat *
473_mesa_lookup_parameter_value(struct program_parameter_list *paramList,
474 GLsizei nameLen, const char *name)
475{
476 GLuint i;
477
478 if (!paramList)
479 return NULL;
480
481 if (nameLen == -1) {
482 /* name is null-terminated */
483 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000484 if (paramList->Parameters[i].Name &&
485 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000486 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000487 }
488 }
489 else {
490 /* name is not null-terminated, use nameLen */
491 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000492 if (paramList->Parameters[i].Name &&
493 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000494 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000495 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000496 }
497 }
498 return NULL;
499}
500
501
502/**
503 * Lookup a parameter index by name in the given parameter list.
504 * \return index of parameter in the list.
505 */
506GLint
507_mesa_lookup_parameter_index(struct program_parameter_list *paramList,
508 GLsizei nameLen, const char *name)
509{
510 GLint i;
511
512 if (!paramList)
513 return -1;
514
515 if (nameLen == -1) {
516 /* name is null-terminated */
517 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000518 if (paramList->Parameters[i].Name &&
519 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Michal Krol2861e732004-03-29 11:09:34 +0000520 return i;
521 }
522 }
523 else {
524 /* name is not null-terminated, use nameLen */
525 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000526 if (paramList->Parameters[i].Name &&
527 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000528 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
529 return i;
530 }
531 }
532 return -1;
533}
534
535
536/**
537 * Use the list of tokens in the state[] array to find global GL state
538 * and return it in <value>. Usually, four values are returned in <value>
539 * but matrix queries may return as many as 16 values.
540 * This function is used for ARB vertex/fragment programs.
541 * The program parser will produce the state[] values.
542 */
543static void
544_mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
545 GLfloat *value)
546{
547 switch (state[0]) {
548 case STATE_MATERIAL:
549 {
550 /* state[1] is either 0=front or 1=back side */
551 const GLuint face = (GLuint) state[1];
552 /* state[2] is the material attribute */
553 switch (state[2]) {
554 case STATE_AMBIENT:
555 if (face == 0)
556 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
557 else
558 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
559 return;
560 case STATE_DIFFUSE:
561 if (face == 0)
562 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
563 else
564 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
565 return;
566 case STATE_SPECULAR:
567 if (face == 0)
568 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
569 else
570 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
571 return;
572 case STATE_EMISSION:
573 if (face == 0)
574 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
575 else
576 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
577 return;
578 case STATE_SHININESS:
579 if (face == 0)
580 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
581 else
582 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
583 value[1] = 0.0F;
584 value[2] = 0.0F;
585 value[3] = 1.0F;
586 return;
587 default:
588 _mesa_problem(ctx, "Invalid material state in fetch_state");
589 return;
590 }
Alan Hourihane22ae6332004-12-02 13:29:40 +0000591 }
Michal Krol2861e732004-03-29 11:09:34 +0000592 case STATE_LIGHT:
593 {
594 /* state[1] is the light number */
595 const GLuint ln = (GLuint) state[1];
596 /* state[2] is the light attribute */
597 switch (state[2]) {
598 case STATE_AMBIENT:
599 COPY_4V(value, ctx->Light.Light[ln].Ambient);
600 return;
601 case STATE_DIFFUSE:
602 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
603 return;
604 case STATE_SPECULAR:
605 COPY_4V(value, ctx->Light.Light[ln].Specular);
606 return;
607 case STATE_POSITION:
608 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
609 return;
610 case STATE_ATTENUATION:
611 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
612 value[1] = ctx->Light.Light[ln].LinearAttenuation;
613 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
614 value[3] = ctx->Light.Light[ln].SpotExponent;
615 return;
616 case STATE_SPOT_DIRECTION:
Brian Paul52bf0052005-04-20 23:47:03 +0000617 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
618 value[3] = ctx->Light.Light[ln]._CosCutoff;
Michal Krol2861e732004-03-29 11:09:34 +0000619 return;
620 case STATE_HALF:
621 {
622 GLfloat eye_z[] = {0, 0, 1};
623
624 /* Compute infinite half angle vector:
625 * half-vector = light_position + (0, 0, 1)
626 * and then normalize. w = 0
Keith Whitwell7c26b612005-04-21 14:46:57 +0000627 *
628 * light.EyePosition.w should be 0 for infinite lights.
Michal Krol2861e732004-03-29 11:09:34 +0000629 */
Keith Whitwell7c26b612005-04-21 14:46:57 +0000630 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
631 NORMALIZE_3FV(value);
632 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000633 }
634 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000635 case STATE_POSITION_NORMALIZED:
636 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
637 NORMALIZE_3FV( value );
638 return;
Michal Krol2861e732004-03-29 11:09:34 +0000639 default:
640 _mesa_problem(ctx, "Invalid light state in fetch_state");
641 return;
642 }
643 }
Michal Krol2861e732004-03-29 11:09:34 +0000644 case STATE_LIGHTMODEL_AMBIENT:
645 COPY_4V(value, ctx->Light.Model.Ambient);
646 return;
647 case STATE_LIGHTMODEL_SCENECOLOR:
648 if (state[1] == 0) {
649 /* front */
650 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000651 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000652 value[i] = ctx->Light.Model.Ambient[i]
653 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
654 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
655 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000656 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000657 }
658 else {
659 /* back */
660 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000661 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000662 value[i] = ctx->Light.Model.Ambient[i]
663 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
664 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
665 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000666 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000667 }
668 return;
669 case STATE_LIGHTPROD:
670 {
671 const GLuint ln = (GLuint) state[1];
672 const GLuint face = (GLuint) state[2];
673 GLint i;
674 ASSERT(face == 0 || face == 1);
675 switch (state[3]) {
676 case STATE_AMBIENT:
677 for (i = 0; i < 3; i++) {
678 value[i] = ctx->Light.Light[ln].Ambient[i] *
679 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
680 }
681 /* [3] = material alpha */
682 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
683 return;
684 case STATE_DIFFUSE:
685 for (i = 0; i < 3; i++) {
686 value[i] = ctx->Light.Light[ln].Diffuse[i] *
687 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
688 }
689 /* [3] = material alpha */
690 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
691 return;
692 case STATE_SPECULAR:
693 for (i = 0; i < 3; i++) {
694 value[i] = ctx->Light.Light[ln].Specular[i] *
695 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
696 }
697 /* [3] = material alpha */
698 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
699 return;
700 default:
701 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
702 return;
703 }
704 }
Michal Krol2861e732004-03-29 11:09:34 +0000705 case STATE_TEXGEN:
706 {
707 /* state[1] is the texture unit */
708 const GLuint unit = (GLuint) state[1];
709 /* state[2] is the texgen attribute */
710 switch (state[2]) {
711 case STATE_TEXGEN_EYE_S:
712 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
713 return;
714 case STATE_TEXGEN_EYE_T:
715 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
716 return;
717 case STATE_TEXGEN_EYE_R:
718 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
719 return;
720 case STATE_TEXGEN_EYE_Q:
721 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
722 return;
723 case STATE_TEXGEN_OBJECT_S:
724 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
725 return;
726 case STATE_TEXGEN_OBJECT_T:
727 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
728 return;
729 case STATE_TEXGEN_OBJECT_R:
730 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
731 return;
732 case STATE_TEXGEN_OBJECT_Q:
733 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
734 return;
735 default:
736 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
737 return;
738 }
739 }
Michal Krol2861e732004-03-29 11:09:34 +0000740 case STATE_TEXENV_COLOR:
741 {
742 /* state[1] is the texture unit */
743 const GLuint unit = (GLuint) state[1];
744 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
745 }
746 return;
747 case STATE_FOG_COLOR:
748 COPY_4V(value, ctx->Fog.Color);
749 return;
750 case STATE_FOG_PARAMS:
751 value[0] = ctx->Fog.Density;
752 value[1] = ctx->Fog.Start;
753 value[2] = ctx->Fog.End;
754 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
755 return;
756 case STATE_CLIPPLANE:
757 {
758 const GLuint plane = (GLuint) state[1];
759 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
760 }
761 return;
762 case STATE_POINT_SIZE:
763 value[0] = ctx->Point.Size;
764 value[1] = ctx->Point.MinSize;
765 value[2] = ctx->Point.MaxSize;
766 value[3] = ctx->Point.Threshold;
767 return;
768 case STATE_POINT_ATTENUATION:
769 value[0] = ctx->Point.Params[0];
770 value[1] = ctx->Point.Params[1];
771 value[2] = ctx->Point.Params[2];
772 value[3] = 1.0F;
773 return;
774 case STATE_MATRIX:
775 {
776 /* state[1] = modelview, projection, texture, etc. */
777 /* state[2] = which texture matrix or program matrix */
778 /* state[3] = first column to fetch */
779 /* state[4] = last column to fetch */
780 /* state[5] = transpose, inverse or invtrans */
781
782 const GLmatrix *matrix;
783 const enum state_index mat = state[1];
784 const GLuint index = (GLuint) state[2];
785 const GLuint first = (GLuint) state[3];
786 const GLuint last = (GLuint) state[4];
787 const enum state_index modifier = state[5];
788 const GLfloat *m;
789 GLuint row, i;
790 if (mat == STATE_MODELVIEW) {
791 matrix = ctx->ModelviewMatrixStack.Top;
792 }
793 else if (mat == STATE_PROJECTION) {
794 matrix = ctx->ProjectionMatrixStack.Top;
795 }
796 else if (mat == STATE_MVP) {
797 matrix = &ctx->_ModelProjectMatrix;
798 }
799 else if (mat == STATE_TEXTURE) {
800 matrix = ctx->TextureMatrixStack[index].Top;
801 }
802 else if (mat == STATE_PROGRAM) {
803 matrix = ctx->ProgramMatrixStack[index].Top;
804 }
805 else {
806 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
807 return;
808 }
809 if (modifier == STATE_MATRIX_INVERSE ||
810 modifier == STATE_MATRIX_INVTRANS) {
Keith Whitwell78803b22005-04-15 12:57:23 +0000811 /* Be sure inverse is up to date:
812 */
Jouk Jansen49b1d952005-04-18 13:05:24 +0000813 _math_matrix_analyse( (GLmatrix*) matrix );
Michal Krol2861e732004-03-29 11:09:34 +0000814 m = matrix->inv;
815 }
816 else {
817 m = matrix->m;
818 }
819 if (modifier == STATE_MATRIX_TRANSPOSE ||
820 modifier == STATE_MATRIX_INVTRANS) {
821 for (i = 0, row = first; row <= last; row++) {
822 value[i++] = m[row * 4 + 0];
823 value[i++] = m[row * 4 + 1];
824 value[i++] = m[row * 4 + 2];
825 value[i++] = m[row * 4 + 3];
826 }
827 }
828 else {
829 for (i = 0, row = first; row <= last; row++) {
830 value[i++] = m[row + 0];
831 value[i++] = m[row + 4];
832 value[i++] = m[row + 8];
833 value[i++] = m[row + 12];
834 }
835 }
836 }
837 return;
838 case STATE_DEPTH_RANGE:
Brian Paul824fdf02004-06-29 00:00:06 +0000839 value[0] = ctx->Viewport.Near; /* near */
840 value[1] = ctx->Viewport.Far; /* far */
841 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
842 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000843 return;
844 case STATE_FRAGMENT_PROGRAM:
845 {
846 /* state[1] = {STATE_ENV, STATE_LOCAL} */
847 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000848 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000849 switch (state[1]) {
850 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000851 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000852 break;
Michal Krol2861e732004-03-29 11:09:34 +0000853 case STATE_LOCAL:
854 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000855 break;
Michal Krol2861e732004-03-29 11:09:34 +0000856 default:
857 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
858 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000859 }
860 }
Michal Krol2861e732004-03-29 11:09:34 +0000861 return;
862
863 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000864 {
Michal Krol2861e732004-03-29 11:09:34 +0000865 /* state[1] = {STATE_ENV, STATE_LOCAL} */
866 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000867 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000868 switch (state[1]) {
869 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000870 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000871 break;
Michal Krol2861e732004-03-29 11:09:34 +0000872 case STATE_LOCAL:
873 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000874 break;
Michal Krol2861e732004-03-29 11:09:34 +0000875 default:
876 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
877 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000878 }
879 }
Michal Krol2861e732004-03-29 11:09:34 +0000880 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000881
882 case STATE_INTERNAL:
883 {
884 switch (state[1]) {
885 case STATE_NORMAL_SCALE:
886 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
887 break;
888 default:
889 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
890 return;
891 }
892 }
893 return;
894
Michal Krol2861e732004-03-29 11:09:34 +0000895 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000896 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000897 return;
898 }
899}
900
901
Brian Paul7b98b402005-11-12 23:25:49 +0000902static void
903append(char *dst, const char *src)
904{
905 while (*dst)
906 dst++;
907 while (*src)
908 *dst++ = *src++;
909 *dst = 0;
910}
911
912static void
913append_token(char *dst, enum state_index k)
914{
915 switch (k) {
916 case STATE_MATERIAL:
917 append(dst, "material.");
918 break;
919 case STATE_LIGHT:
920 append(dst, "light");
921 break;
922 case STATE_LIGHTMODEL_AMBIENT:
923 append(dst, "lightmodel.ambient");
924 break;
925 case STATE_LIGHTMODEL_SCENECOLOR:
926 break;
927 case STATE_LIGHTPROD:
928 append(dst, "lightprod");
929 break;
930 case STATE_TEXGEN:
931 append(dst, "texgen");
932 break;
933 case STATE_FOG_COLOR:
934 append(dst, "fog.color");
935 break;
936 case STATE_FOG_PARAMS:
937 append(dst, "fog.params");
938 break;
939 case STATE_CLIPPLANE:
940 append(dst, "clip");
941 break;
942 case STATE_POINT_SIZE:
943 append(dst, "point.size");
944 break;
945 case STATE_POINT_ATTENUATION:
946 append(dst, "point.attenuation");
947 break;
948 case STATE_MATRIX:
949 append(dst, "matrix.");
950 break;
951 case STATE_MODELVIEW:
952 append(dst, "modelview");
953 break;
954 case STATE_PROJECTION:
955 append(dst, "projection");
956 break;
957 case STATE_MVP:
958 append(dst, "mvp");
959 break;
960 case STATE_TEXTURE:
961 append(dst, "texture");
962 break;
963 case STATE_PROGRAM:
964 append(dst, "program");
965 break;
966 case STATE_MATRIX_INVERSE:
967 append(dst, ".inverse");
968 break;
969 case STATE_MATRIX_TRANSPOSE:
970 append(dst, ".transpose");
971 break;
972 case STATE_MATRIX_INVTRANS:
973 append(dst, ".invtrans");
974 break;
975 case STATE_AMBIENT:
976 append(dst, "ambient");
977 break;
978 case STATE_DIFFUSE:
979 append(dst, "diffuse");
980 break;
981 case STATE_SPECULAR:
982 append(dst, "specular");
983 break;
984 case STATE_EMISSION:
985 append(dst, "emission");
986 break;
987 case STATE_SHININESS:
988 append(dst, "shininess");
989 break;
990 case STATE_HALF:
991 append(dst, "half");
992 break;
993 case STATE_POSITION:
994 append(dst, ".position");
995 break;
996 case STATE_ATTENUATION:
997 append(dst, ".attenuation");
998 break;
999 case STATE_SPOT_DIRECTION:
1000 append(dst, ".spot.direction");
1001 break;
1002 case STATE_TEXGEN_EYE_S:
1003 append(dst, "eye.s");
1004 break;
1005 case STATE_TEXGEN_EYE_T:
1006 append(dst, "eye.t");
1007 break;
1008 case STATE_TEXGEN_EYE_R:
1009 append(dst, "eye.r");
1010 break;
1011 case STATE_TEXGEN_EYE_Q:
1012 append(dst, "eye.q");
1013 break;
1014 case STATE_TEXGEN_OBJECT_S:
1015 append(dst, "object.s");
1016 break;
1017 case STATE_TEXGEN_OBJECT_T:
1018 append(dst, "object.t");
1019 break;
1020 case STATE_TEXGEN_OBJECT_R:
1021 append(dst, "object.r");
1022 break;
1023 case STATE_TEXGEN_OBJECT_Q:
1024 append(dst, "object.q");
1025 break;
1026 case STATE_TEXENV_COLOR:
1027 append(dst, "texenv");
1028 break;
1029 case STATE_DEPTH_RANGE:
1030 append(dst, "depth.range");
1031 break;
1032 case STATE_VERTEX_PROGRAM:
1033 case STATE_FRAGMENT_PROGRAM:
1034 break;
1035 case STATE_ENV:
1036 append(dst, "env");
1037 break;
1038 case STATE_LOCAL:
1039 append(dst, "local");
1040 break;
1041 case STATE_INTERNAL:
1042 case STATE_NORMAL_SCALE:
1043 case STATE_POSITION_NORMALIZED:
1044 append(dst, "(internal)");
1045 break;
1046 default:
1047 ;
1048 }
1049}
1050
1051static void
1052append_face(char *dst, GLint face)
1053{
1054 if (face == 0)
1055 append(dst, "front.");
1056 else
1057 append(dst, "back.");
1058}
1059
1060static void
1061append_index(char *dst, GLint index)
1062{
1063 char s[20];
1064 _mesa_sprintf(s, "[%d].", index);
1065 append(dst, s);
1066}
1067
1068/**
1069 * Make a string from the given state vector.
1070 * For example, return "state.matrix.texture[2].inverse".
1071 */
1072static const char *
1073make_state_string(const GLint state[6])
1074{
1075 char str[1000] = "";
1076 char tmp[30];
1077
1078 append(str, "state.");
1079 append_token(str, state[0]);
1080
1081 switch (state[0]) {
1082 case STATE_MATERIAL:
1083 append_face(str, state[1]);
1084 append_token(str, state[2]);
1085 break;
1086 case STATE_LIGHT:
1087 append(str, "light");
1088 append_index(str, state[1]); /* light number [i]. */
1089 append_token(str, state[2]); /* coefficients */
1090 break;
1091 case STATE_LIGHTMODEL_AMBIENT:
1092 append(str, "lightmodel.ambient");
1093 break;
1094 case STATE_LIGHTMODEL_SCENECOLOR:
1095 if (state[1] == 0) {
1096 append(str, "lightmodel.front.scenecolor");
1097 }
1098 else {
1099 append(str, "lightmodel.back.scenecolor");
1100 }
1101 break;
1102 case STATE_LIGHTPROD:
1103 append_index(str, state[1]); /* light number [i]. */
1104 append_face(str, state[2]);
1105 append_token(str, state[3]);
1106 break;
1107 case STATE_TEXGEN:
1108 append_index(str, state[1]); /* tex unit [i] */
1109 append_token(str, state[2]); /* plane coef */
1110 break;
1111 case STATE_TEXENV_COLOR:
1112 append_index(str, state[1]); /* tex unit [i] */
1113 append(str, "color");
1114 break;
1115 case STATE_FOG_COLOR:
1116 case STATE_FOG_PARAMS:
1117 break;
1118 case STATE_CLIPPLANE:
1119 append_index(str, state[1]); /* plane [i] */
1120 append(str, "plane");
1121 break;
1122 case STATE_POINT_SIZE:
1123 case STATE_POINT_ATTENUATION:
1124 break;
1125 case STATE_MATRIX:
1126 {
1127 /* state[1] = modelview, projection, texture, etc. */
1128 /* state[2] = which texture matrix or program matrix */
1129 /* state[3] = first column to fetch */
1130 /* state[4] = last column to fetch */
1131 /* state[5] = transpose, inverse or invtrans */
1132 const enum state_index mat = state[1];
1133 const GLuint index = (GLuint) state[2];
1134 const GLuint first = (GLuint) state[3];
1135 const GLuint last = (GLuint) state[4];
1136 const enum state_index modifier = state[5];
1137 append_token(str, mat);
1138 if (index)
1139 append_index(str, index);
1140 if (modifier)
1141 append_token(str, modifier);
1142 if (first == last)
1143 _mesa_sprintf(tmp, ".row[%d]", first);
1144 else
1145 _mesa_sprintf(tmp, ".row[%d..%d]", first, last);
1146 append(str, tmp);
1147 }
1148 break;
1149 case STATE_DEPTH_RANGE:
1150 break;
1151 case STATE_FRAGMENT_PROGRAM:
1152 case STATE_VERTEX_PROGRAM:
1153 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1154 /* state[2] = parameter index */
1155 append_token(str, state[1]);
1156 append_index(str, state[2]);
1157 break;
1158 case STATE_INTERNAL:
1159 break;
1160 default:
1161 _mesa_problem(NULL, "Invalid state in maka_state_string");
1162 break;
1163 }
1164
1165 return _mesa_strdup(str);
1166}
1167
1168
Michal Krol2861e732004-03-29 11:09:34 +00001169/**
1170 * Loop over all the parameters in a parameter list. If the parameter
1171 * is a GL state reference, look up the current value of that state
1172 * variable and put it into the parameter's Value[4] array.
1173 * This would be called at glBegin time when using a fragment program.
1174 */
1175void
1176_mesa_load_state_parameters(GLcontext *ctx,
1177 struct program_parameter_list *paramList)
1178{
1179 GLuint i;
1180
1181 if (!paramList)
1182 return;
1183
1184 for (i = 0; i < paramList->NumParameters; i++) {
Brian Paul613e1ad2005-11-05 02:15:21 +00001185 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00001186 _mesa_fetch_state(ctx,
1187 paramList->Parameters[i].StateIndexes,
1188 paramList->ParameterValues[i]);
Michal Krol2861e732004-03-29 11:09:34 +00001189 }
1190 }
1191}
1192
1193
Brian Paul1fcdaf12005-11-05 19:12:36 +00001194/**
1195 * Basic info about each instruction
1196 */
1197struct instruction_info
1198{
1199 enum prog_opcode Opcode;
1200 const char *Name;
1201 GLuint NumSrcRegs;
1202};
1203
1204/**
1205 * Instruction info
1206 * \note Opcode should equal array index!
1207 */
1208static const struct instruction_info InstInfo[MAX_OPCODE] = {
1209 { OPCODE_ABS, "ABS", 1 },
1210 { OPCODE_ADD, "ADD", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001211 { OPCODE_ARA, "ARA", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001212 { OPCODE_ARL, "ARL", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001213 { OPCODE_ARL_NV, "ARL", 1 },
1214 { OPCODE_ARR, "ARL", 1 },
1215 { OPCODE_BRA, "BRA", 1 },
1216 { OPCODE_CAL, "CAL", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001217 { OPCODE_CMP, "CMP", 3 },
1218 { OPCODE_COS, "COS", 1 },
1219 { OPCODE_DDX, "DDX", 1 },
1220 { OPCODE_DDY, "DDY", 1 },
1221 { OPCODE_DP3, "DP3", 2 },
1222 { OPCODE_DP4, "DP4", 2 },
1223 { OPCODE_DPH, "DPH", 2 },
1224 { OPCODE_DST, "DST", 2 },
1225 { OPCODE_END, "END", 0 },
1226 { OPCODE_EX2, "EX2", 1 },
1227 { OPCODE_EXP, "EXP", 1 },
1228 { OPCODE_FLR, "FLR", 1 },
1229 { OPCODE_FRC, "FRC", 1 },
1230 { OPCODE_KIL, "KIL", 1 },
1231 { OPCODE_KIL_NV, "KIL", 0 },
1232 { OPCODE_LG2, "LG2", 1 },
1233 { OPCODE_LIT, "LIT", 1 },
1234 { OPCODE_LOG, "LOG", 1 },
1235 { OPCODE_LRP, "LRP", 3 },
1236 { OPCODE_MAD, "MAD", 3 },
1237 { OPCODE_MAX, "MAX", 2 },
1238 { OPCODE_MIN, "MIN", 2 },
1239 { OPCODE_MOV, "MOV", 1 },
1240 { OPCODE_MUL, "MUL", 2 },
1241 { OPCODE_PK2H, "PK2H", 1 },
1242 { OPCODE_PK2US, "PK2US", 1 },
1243 { OPCODE_PK4B, "PK4B", 1 },
1244 { OPCODE_PK4UB, "PK4UB", 1 },
1245 { OPCODE_POW, "POW", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001246 { OPCODE_POPA, "POPA", 0 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001247 { OPCODE_PRINT, "PRINT", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001248 { OPCODE_PUSHA, "PUSHA", 0 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001249 { OPCODE_RCC, "RCC", 1 },
1250 { OPCODE_RCP, "RCP", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001251 { OPCODE_RET, "RET", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001252 { OPCODE_RFL, "RFL", 1 },
1253 { OPCODE_RSQ, "RSQ", 1 },
1254 { OPCODE_SCS, "SCS", 1 },
1255 { OPCODE_SEQ, "SEQ", 2 },
1256 { OPCODE_SFL, "SFL", 0 },
1257 { OPCODE_SGE, "SGE", 2 },
1258 { OPCODE_SGT, "SGT", 2 },
1259 { OPCODE_SIN, "SIN", 1 },
1260 { OPCODE_SLE, "SLE", 2 },
1261 { OPCODE_SLT, "SLT", 2 },
1262 { OPCODE_SNE, "SNE", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001263 { OPCODE_SSG, "SSG", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001264 { OPCODE_STR, "STR", 0 },
1265 { OPCODE_SUB, "SUB", 2 },
1266 { OPCODE_SWZ, "SWZ", 1 },
1267 { OPCODE_TEX, "TEX", 1 },
1268 { OPCODE_TXB, "TXB", 1 },
1269 { OPCODE_TXD, "TXD", 3 },
Ian Romanick4884db62005-11-08 22:40:26 +00001270 { OPCODE_TXL, "TXL", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001271 { OPCODE_TXP, "TXP", 1 },
1272 { OPCODE_TXP_NV, "TXP", 1 },
1273 { OPCODE_UP2H, "UP2H", 1 },
1274 { OPCODE_UP2US, "UP2US", 1 },
1275 { OPCODE_UP4B, "UP4B", 1 },
1276 { OPCODE_UP4UB, "UP4UB", 1 },
1277 { OPCODE_X2D, "X2D", 3 },
1278 { OPCODE_XPD, "XPD", 2 }
1279};
1280
1281
1282/**
1283 * Return the number of src registers for the given instruction/opcode.
1284 */
1285GLuint
1286_mesa_num_inst_src_regs(enum prog_opcode opcode)
1287{
1288 GLuint i;
1289#ifdef DEBUG
1290 for (i = 0; i < MAX_OPCODE; i++) {
1291 ASSERT(i == InstInfo[i].Opcode);
1292 }
1293#endif
1294 for (i = 0; i < MAX_OPCODE; i++) {
1295 if (InstInfo[i].Opcode == opcode) {
1296 return InstInfo[i].NumSrcRegs;
1297 }
1298 }
1299 _mesa_problem(NULL, "invalid opcode in _mesa_num_inst_src_regs");
1300 return 0;
1301}
1302
1303
1304/**
1305 * Return string name for given program opcode.
1306 */
1307const char *
1308_mesa_opcode_string(enum prog_opcode opcode)
1309{
1310 ASSERT(opcode < MAX_OPCODE);
1311 return InstInfo[opcode].Name;
1312}
1313
Brian Paulbf41bc02005-11-05 19:32:36 +00001314/**
1315 * Return string name for given program/register file.
1316 */
Brian Paul30d6a4b2005-11-05 20:18:18 +00001317static const char *
1318program_file_string(enum register_file f)
Brian Paulbf41bc02005-11-05 19:32:36 +00001319{
1320 switch (f) {
1321 case PROGRAM_TEMPORARY:
1322 return "TEMP";
1323 case PROGRAM_LOCAL_PARAM:
1324 return "LOCAL";
1325 case PROGRAM_ENV_PARAM:
1326 return "ENV";
1327 case PROGRAM_STATE_VAR:
1328 return "STATE";
1329 case PROGRAM_INPUT:
1330 return "INPUT";
1331 case PROGRAM_OUTPUT:
1332 return "OUTPUT";
1333 case PROGRAM_NAMED_PARAM:
1334 return "NAMED";
1335 case PROGRAM_CONSTANT:
1336 return "CONST";
1337 case PROGRAM_WRITE_ONLY:
1338 return "WRITE_ONLY";
1339 case PROGRAM_ADDRESS:
1340 return "ADDR";
1341 default:
1342 return "!unkown!";
1343 }
1344}
1345
Michal Krol2861e732004-03-29 11:09:34 +00001346
Brian Paul30d6a4b2005-11-05 20:18:18 +00001347/**
1348 * Return a string representation of the given swizzle word.
Brian Paul7b98b402005-11-12 23:25:49 +00001349 * If extended is true, use extended (comma-separated) format.
Brian Paul30d6a4b2005-11-05 20:18:18 +00001350 */
1351static const char *
Brian Paul7b98b402005-11-12 23:25:49 +00001352swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001353{
1354 static const char swz[] = "xyzw01";
1355 static char s[20];
1356 GLuint i = 0;
1357
Brian Paul7b98b402005-11-12 23:25:49 +00001358 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001359 return ""; /* no swizzle/negation */
1360
Brian Paul7b98b402005-11-12 23:25:49 +00001361 if (!extended)
1362 s[i++] = '.';
Brian Paul30d6a4b2005-11-05 20:18:18 +00001363
1364 if (negateBase & 0x1)
1365 s[i++] = '-';
1366 s[i++] = swz[GET_SWZ(swizzle, 0)];
1367
Brian Paul7b98b402005-11-12 23:25:49 +00001368 if (extended) {
1369 s[i++] = ',';
1370 }
1371
Brian Paul30d6a4b2005-11-05 20:18:18 +00001372 if (negateBase & 0x2)
1373 s[i++] = '-';
1374 s[i++] = swz[GET_SWZ(swizzle, 1)];
1375
Brian Paul7b98b402005-11-12 23:25:49 +00001376 if (extended) {
1377 s[i++] = ',';
1378 }
1379
Brian Paul30d6a4b2005-11-05 20:18:18 +00001380 if (negateBase & 0x4)
1381 s[i++] = '-';
1382 s[i++] = swz[GET_SWZ(swizzle, 2)];
1383
Brian Paul7b98b402005-11-12 23:25:49 +00001384 if (extended) {
1385 s[i++] = ',';
1386 }
1387
Brian Paul30d6a4b2005-11-05 20:18:18 +00001388 if (negateBase & 0x8)
1389 s[i++] = '-';
1390 s[i++] = swz[GET_SWZ(swizzle, 3)];
1391
1392 s[i] = 0;
1393 return s;
1394}
1395
1396
1397static const char *
1398writemask_string(GLuint writeMask)
1399{
1400 static char s[10];
1401 GLuint i = 0;
1402
1403 if (writeMask == WRITEMASK_XYZW)
1404 return "";
1405
1406 s[i++] = '.';
1407 if (writeMask & WRITEMASK_X)
1408 s[i++] = 'x';
1409 if (writeMask & WRITEMASK_Y)
1410 s[i++] = 'y';
1411 if (writeMask & WRITEMASK_Z)
1412 s[i++] = 'z';
1413 if (writeMask & WRITEMASK_W)
1414 s[i++] = 'w';
1415
1416 s[i] = 0;
1417 return s;
1418}
1419
Brian Paul7b98b402005-11-12 23:25:49 +00001420static void
1421print_dst_reg(const struct prog_dst_register *dstReg)
1422{
1423 _mesa_printf(" %s[%d]%s",
1424 program_file_string(dstReg->File),
1425 dstReg->Index,
1426 writemask_string(dstReg->WriteMask));
1427}
1428
1429static void
1430print_src_reg(const struct prog_src_register *srcReg)
1431{
1432 _mesa_printf("%s[%d]%s",
1433 program_file_string(srcReg->File),
1434 srcReg->Index,
1435 swizzle_string(srcReg->Swizzle,
1436 srcReg->NegateBase, GL_FALSE));
1437}
1438
Brian Paul30d6a4b2005-11-05 20:18:18 +00001439
1440/**
Brian Paulde997602005-11-12 17:53:14 +00001441 * Print a single vertex/fragment program instruction.
Brian Paul30d6a4b2005-11-05 20:18:18 +00001442 */
1443void
Brian Paulde997602005-11-12 17:53:14 +00001444_mesa_print_instruction(const struct prog_instruction *inst)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001445{
Brian Paulde997602005-11-12 17:53:14 +00001446 switch (inst->Opcode) {
1447 case OPCODE_PRINT:
1448 _mesa_printf("PRINT '%s'", inst->Data);
1449 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
1450 _mesa_printf(", ");
1451 _mesa_printf("%s[%d]%s",
1452 program_file_string(inst->SrcReg[0].File),
1453 inst->SrcReg[0].Index,
1454 swizzle_string(inst->SrcReg[0].Swizzle,
Brian Paul7b98b402005-11-12 23:25:49 +00001455 inst->SrcReg[0].NegateBase, GL_FALSE));
Brian Paulde997602005-11-12 17:53:14 +00001456 }
1457 _mesa_printf(";\n");
1458 break;
Brian Paul7b98b402005-11-12 23:25:49 +00001459 case OPCODE_SWZ:
1460 _mesa_printf("SWZ");
Brian Paule31ac052005-11-20 17:52:40 +00001461 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Paul7b98b402005-11-12 23:25:49 +00001462 _mesa_printf("_SAT");
1463 print_dst_reg(&inst->DstReg);
1464 _mesa_printf("%s[%d], %s;\n",
1465 program_file_string(inst->SrcReg[0].File),
1466 inst->SrcReg[0].Index,
1467 swizzle_string(inst->SrcReg[0].Swizzle,
1468 inst->SrcReg[0].NegateBase, GL_TRUE));
1469 break;
1470 case OPCODE_TEX:
1471 case OPCODE_TXP:
1472 case OPCODE_TXB:
1473 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
Brian Paule31ac052005-11-20 17:52:40 +00001474 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Paul7b98b402005-11-12 23:25:49 +00001475 _mesa_printf("_SAT");
1476 _mesa_printf(" ");
1477 print_dst_reg(&inst->DstReg);
1478 _mesa_printf(", ");
1479 print_src_reg(&inst->SrcReg[0]);
1480 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
1481 switch (inst->TexSrcTarget) {
1482 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
1483 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
1484 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
1485 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
1486 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
1487 default:
1488 ;
1489 }
1490 _mesa_printf("\n");
1491 break;
1492 case OPCODE_ARL:
1493 _mesa_printf("ARL addr.x, ");
1494 print_src_reg(&inst->SrcReg[0]);
1495 _mesa_printf(";\n");
1496 break;
1497 /* XXX may need for other special-case instructions */
Brian Paulde997602005-11-12 17:53:14 +00001498 default:
1499 /* typical alu instruction */
1500 {
1501 const GLuint numRegs = _mesa_num_inst_src_regs(inst->Opcode);
1502 GLuint j;
Brian Paul30d6a4b2005-11-05 20:18:18 +00001503
Brian Paulde997602005-11-12 17:53:14 +00001504 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
Brian Paul30d6a4b2005-11-05 20:18:18 +00001505
Brian Paulde997602005-11-12 17:53:14 +00001506 /* frag prog only */
Brian Paule31ac052005-11-20 17:52:40 +00001507 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Paulde997602005-11-12 17:53:14 +00001508 _mesa_printf("_SAT");
1509
1510 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
1511 _mesa_printf(" %s[%d]%s",
1512 program_file_string(inst->DstReg.File),
1513 inst->DstReg.Index,
1514 writemask_string(inst->DstReg.WriteMask));
1515 }
1516
1517 if (numRegs > 0)
Brian Paul02df9e12005-11-08 14:42:52 +00001518 _mesa_printf(", ");
Brian Paulde997602005-11-12 17:53:14 +00001519
1520 for (j = 0; j < numRegs; j++) {
Brian Paul7b98b402005-11-12 23:25:49 +00001521 print_src_reg(inst->SrcReg + j);
Brian Paulde997602005-11-12 17:53:14 +00001522 if (j + 1 < numRegs)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001523 _mesa_printf(", ");
Brian Paul30d6a4b2005-11-05 20:18:18 +00001524 }
Brian Paulde997602005-11-12 17:53:14 +00001525
1526 _mesa_printf(";\n");
Brian Paul30d6a4b2005-11-05 20:18:18 +00001527 }
1528 }
1529}
1530
1531
Brian Paulde997602005-11-12 17:53:14 +00001532/**
1533 * Print a vertx/fragment program to stdout.
1534 * XXX this function could be greatly improved.
1535 */
1536void
1537_mesa_print_program(const struct program *prog)
1538{
1539 GLuint i;
1540 for (i = 0; i < prog->NumInstructions; i++) {
1541 _mesa_printf("%3d: ", i);
1542 _mesa_print_instruction(prog->Instructions + i);
1543 }
1544}
1545
1546
1547/**
1548 * Print all of a program's parameters.
1549 */
1550void
1551_mesa_print_program_parameters(GLcontext *ctx, const struct program *prog)
1552{
1553 GLint i;
1554
1555 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
1556 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
1557 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
1558 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
1559 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
1560
1561 _mesa_load_state_parameters(ctx, prog->Parameters);
1562
1563#if 0
1564 _mesa_printf("Local Params:\n");
1565 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
1566 const GLfloat *p = prog->LocalParams[i];
1567 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
1568 }
1569#endif
1570
1571 for (i = 0; i < prog->Parameters->NumParameters; i++){
Brian Paul7b98b402005-11-12 23:25:49 +00001572 struct program_parameter *param = prog->Parameters->Parameters + i;
1573 const GLfloat *v = prog->Parameters->ParameterValues[i];
1574 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1575 i, param->Name, v[0], v[1], v[2], v[3]);
Brian Paulde997602005-11-12 17:53:14 +00001576 }
1577}
1578
Brian Paul30d6a4b2005-11-05 20:18:18 +00001579
1580
Michal Krol2861e732004-03-29 11:09:34 +00001581/**********************************************************************/
1582/* API functions */
1583/**********************************************************************/
1584
1585
1586/**
1587 * Bind a program (make it current)
1588 * \note Called from the GL API dispatcher by both glBindProgramNV
1589 * and glBindProgramARB.
1590 */
1591void GLAPIENTRY
1592_mesa_BindProgram(GLenum target, GLuint id)
1593{
1594 struct program *prog;
1595 GET_CURRENT_CONTEXT(ctx);
1596 ASSERT_OUTSIDE_BEGIN_END(ctx);
1597
1598 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1599
1600 if ((target == GL_VERTEX_PROGRAM_NV
1601 && ctx->Extensions.NV_vertex_program) ||
1602 (target == GL_VERTEX_PROGRAM_ARB
1603 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001604 /*** Vertex program binding ***/
1605 struct vertex_program *curProg = ctx->VertexProgram.Current;
1606 if (curProg->Base.Id == id) {
1607 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001608 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001609 }
1610 if (curProg->Base.Id != 0) {
1611 /* decrement refcount on previously bound vertex program */
1612 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001613 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001614 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001615 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001616 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001617 }
1618 }
1619 }
1620 else if ((target == GL_FRAGMENT_PROGRAM_NV
1621 && ctx->Extensions.NV_fragment_program) ||
1622 (target == GL_FRAGMENT_PROGRAM_ARB
1623 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001624 /*** Fragment program binding ***/
1625 struct fragment_program *curProg = ctx->FragmentProgram.Current;
1626 if (curProg->Base.Id == id) {
1627 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001628 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001629 }
1630 if (curProg->Base.Id != 0) {
1631 /* decrement refcount on previously bound fragment program */
1632 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001633 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001634 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001635 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001636 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001637 }
1638 }
1639 }
1640 else {
1641 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1642 return;
1643 }
1644
1645 /* NOTE: binding to a non-existant program is not an error.
1646 * That's supposed to be caught in glBegin.
1647 */
1648 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001649 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001650 prog = NULL;
1651 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1652 prog = ctx->Shared->DefaultVertexProgram;
1653 else
1654 prog = ctx->Shared->DefaultFragmentProgram;
1655 }
1656 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001657 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001658 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001659 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001660 /* allocate a new program now */
1661 prog = ctx->Driver.NewProgram(ctx, target, id);
1662 if (!prog) {
1663 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1664 return;
1665 }
Michal Krol2861e732004-03-29 11:09:34 +00001666 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1667 }
Brian Paul765f1a12004-09-14 22:28:27 +00001668 else if (prog->Target != target) {
1669 _mesa_error(ctx, GL_INVALID_OPERATION,
1670 "glBindProgramNV/ARB(target mismatch)");
1671 return;
1672 }
Michal Krol2861e732004-03-29 11:09:34 +00001673 }
1674
1675 /* bind now */
1676 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1677 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1678 }
1679 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1680 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1681 }
1682
Brian Paul765f1a12004-09-14 22:28:27 +00001683 /* Never null pointers */
1684 ASSERT(ctx->VertexProgram.Current);
1685 ASSERT(ctx->FragmentProgram.Current);
1686
Michal Krol2861e732004-03-29 11:09:34 +00001687 if (prog)
1688 prog->RefCount++;
1689
1690 if (ctx->Driver.BindProgram)
1691 ctx->Driver.BindProgram(ctx, target, prog);
1692}
1693
1694
1695/**
1696 * Delete a list of programs.
1697 * \note Not compiled into display lists.
1698 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1699 */
1700void GLAPIENTRY
1701_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1702{
1703 GLint i;
1704 GET_CURRENT_CONTEXT(ctx);
1705 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1706
1707 if (n < 0) {
1708 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1709 return;
1710 }
1711
1712 for (i = 0; i < n; i++) {
1713 if (ids[i] != 0) {
1714 struct program *prog = (struct program *)
1715 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001716 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001717 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1718 }
1719 else if (prog) {
1720 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001721 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1722 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1723 if (ctx->VertexProgram.Current &&
1724 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1725 /* unbind this currently bound program */
1726 _mesa_BindProgram(prog->Target, 0);
1727 }
1728 }
1729 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1730 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1731 if (ctx->FragmentProgram.Current &&
1732 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1733 /* unbind this currently bound program */
1734 _mesa_BindProgram(prog->Target, 0);
1735 }
1736 }
1737 else {
1738 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1739 return;
1740 }
Brian Paulea2943e2005-01-20 04:02:02 +00001741 /* The ID is immediately available for re-use now */
1742 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1743 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001744 if (prog->RefCount <= 0) {
1745 ctx->Driver.DeleteProgram(ctx, prog);
1746 }
1747 }
Michal Krol2861e732004-03-29 11:09:34 +00001748 }
1749 }
1750}
1751
1752
1753/**
1754 * Generate a list of new program identifiers.
1755 * \note Not compiled into display lists.
1756 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1757 */
1758void GLAPIENTRY
1759_mesa_GenPrograms(GLsizei n, GLuint *ids)
1760{
1761 GLuint first;
1762 GLuint i;
1763 GET_CURRENT_CONTEXT(ctx);
1764 ASSERT_OUTSIDE_BEGIN_END(ctx);
1765
1766 if (n < 0) {
1767 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1768 return;
1769 }
1770
1771 if (!ids)
1772 return;
1773
1774 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1775
Brian Paul765f1a12004-09-14 22:28:27 +00001776 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001777 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001778 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001779 }
1780
1781 /* Return the program names */
1782 for (i = 0; i < (GLuint) n; i++) {
1783 ids[i] = first + i;
1784 }
1785}
1786
1787
1788/**
Brian Paul765f1a12004-09-14 22:28:27 +00001789 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001790 * \note Not compiled into display lists.
1791 * \note Called from both glIsProgramNV and glIsProgramARB.
1792 * \param id is the program identifier
1793 * \return GL_TRUE if id is a program, else GL_FALSE.
1794 */
1795GLboolean GLAPIENTRY
1796_mesa_IsProgram(GLuint id)
1797{
1798 GET_CURRENT_CONTEXT(ctx);
1799 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1800
1801 if (id == 0)
1802 return GL_FALSE;
1803
1804 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1805 return GL_TRUE;
1806 else
1807 return GL_FALSE;
1808}
1809
1810
1811
1812/**********************************************************************/
1813/* GL_MESA_program_debug extension */
1814/**********************************************************************/
1815
1816
1817/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001818GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001819glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1820 GLvoid *data)
1821{
1822 _mesa_ProgramCallbackMESA(target, callback, data);
1823}
1824
1825
1826void
1827_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1828 GLvoid *data)
1829{
1830 GET_CURRENT_CONTEXT(ctx);
1831
1832 switch (target) {
1833 case GL_FRAGMENT_PROGRAM_ARB:
1834 if (!ctx->Extensions.ARB_fragment_program) {
1835 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1836 return;
1837 }
1838 ctx->FragmentProgram.Callback = callback;
1839 ctx->FragmentProgram.CallbackData = data;
1840 break;
1841 case GL_FRAGMENT_PROGRAM_NV:
1842 if (!ctx->Extensions.NV_fragment_program) {
1843 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1844 return;
1845 }
1846 ctx->FragmentProgram.Callback = callback;
1847 ctx->FragmentProgram.CallbackData = data;
1848 break;
1849 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1850 if (!ctx->Extensions.ARB_vertex_program &&
1851 !ctx->Extensions.NV_vertex_program) {
1852 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1853 return;
1854 }
1855 ctx->VertexProgram.Callback = callback;
1856 ctx->VertexProgram.CallbackData = data;
1857 break;
1858 default:
1859 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1860 return;
1861 }
1862}
1863
1864
1865/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001866GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001867glGetProgramRegisterfvMESA(GLenum target,
1868 GLsizei len, const GLubyte *registerName,
1869 GLfloat *v)
1870{
1871 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1872}
1873
1874
1875void
1876_mesa_GetProgramRegisterfvMESA(GLenum target,
1877 GLsizei len, const GLubyte *registerName,
1878 GLfloat *v)
1879{
1880 char reg[1000];
1881 GET_CURRENT_CONTEXT(ctx);
1882
1883 /* We _should_ be inside glBegin/glEnd */
1884#if 0
1885 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1886 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1887 return;
1888 }
1889#endif
1890
1891 /* make null-terminated copy of registerName */
1892 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1893 _mesa_memcpy(reg, registerName, len);
1894 reg[len] = 0;
1895
1896 switch (target) {
1897 case GL_VERTEX_PROGRAM_NV:
1898 if (!ctx->Extensions.ARB_vertex_program &&
1899 !ctx->Extensions.NV_vertex_program) {
1900 _mesa_error(ctx, GL_INVALID_ENUM,
1901 "glGetProgramRegisterfvMESA(target)");
1902 return;
1903 }
Brian Paul6d460af2004-04-23 14:16:46 +00001904 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001905 _mesa_error(ctx, GL_INVALID_OPERATION,
1906 "glGetProgramRegisterfvMESA");
1907 return;
1908 }
1909 /* GL_NV_vertex_program */
1910 if (reg[0] == 'R') {
1911 /* Temp register */
1912 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001913 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001914 _mesa_error(ctx, GL_INVALID_VALUE,
1915 "glGetProgramRegisterfvMESA(registerName)");
1916 return;
1917 }
1918 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1919 }
1920 else if (reg[0] == 'v' && reg[1] == '[') {
1921 /* Vertex Input attribute */
1922 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001923 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001924 const char *name = _mesa_nv_vertex_input_register_name(i);
1925 char number[10];
Brian Paulaa206952005-09-16 18:14:24 +00001926 _mesa_sprintf(number, "%d", i);
Michal Krol2861e732004-03-29 11:09:34 +00001927 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1928 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1929 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1930 return;
1931 }
1932 }
1933 _mesa_error(ctx, GL_INVALID_VALUE,
1934 "glGetProgramRegisterfvMESA(registerName)");
1935 return;
1936 }
1937 else if (reg[0] == 'o' && reg[1] == '[') {
1938 /* Vertex output attribute */
1939 }
1940 /* GL_ARB_vertex_program */
1941 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1942
1943 }
1944 else {
1945 _mesa_error(ctx, GL_INVALID_VALUE,
1946 "glGetProgramRegisterfvMESA(registerName)");
1947 return;
1948 }
1949 break;
1950 case GL_FRAGMENT_PROGRAM_ARB:
1951 if (!ctx->Extensions.ARB_fragment_program) {
1952 _mesa_error(ctx, GL_INVALID_ENUM,
1953 "glGetProgramRegisterfvMESA(target)");
1954 return;
1955 }
Brian Paul6d460af2004-04-23 14:16:46 +00001956 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001957 _mesa_error(ctx, GL_INVALID_OPERATION,
1958 "glGetProgramRegisterfvMESA");
1959 return;
1960 }
1961 /* XXX to do */
1962 break;
1963 case GL_FRAGMENT_PROGRAM_NV:
1964 if (!ctx->Extensions.NV_fragment_program) {
1965 _mesa_error(ctx, GL_INVALID_ENUM,
1966 "glGetProgramRegisterfvMESA(target)");
1967 return;
1968 }
Brian Paul6d460af2004-04-23 14:16:46 +00001969 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001970 _mesa_error(ctx, GL_INVALID_OPERATION,
1971 "glGetProgramRegisterfvMESA");
1972 return;
1973 }
1974 if (reg[0] == 'R') {
1975 /* Temp register */
1976 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001977 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001978 _mesa_error(ctx, GL_INVALID_VALUE,
1979 "glGetProgramRegisterfvMESA(registerName)");
1980 return;
1981 }
1982 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
1983 }
1984 else if (reg[0] == 'f' && reg[1] == '[') {
1985 /* Fragment input attribute */
1986 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001987 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001988 const char *name = _mesa_nv_fragment_input_register_name(i);
1989 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
1990 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
1991 return;
1992 }
1993 }
1994 _mesa_error(ctx, GL_INVALID_VALUE,
1995 "glGetProgramRegisterfvMESA(registerName)");
1996 return;
1997 }
1998 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
1999 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00002000 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLR]);
Michal Krol2861e732004-03-29 11:09:34 +00002001 }
2002 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
2003 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00002004 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLH]);
Michal Krol2861e732004-03-29 11:09:34 +00002005 }
2006 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
2007 /* Fragment output depth */
Brian Paul90ebb582005-11-02 18:06:12 +00002008 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_DEPR]);
Michal Krol2861e732004-03-29 11:09:34 +00002009 }
2010 else {
2011 /* try user-defined identifiers */
2012 const GLfloat *value = _mesa_lookup_parameter_value(
Brian Paulde997602005-11-12 17:53:14 +00002013 ctx->FragmentProgram.Current->Base.Parameters, -1, reg);
Michal Krol2861e732004-03-29 11:09:34 +00002014 if (value) {
2015 COPY_4V(v, value);
2016 }
2017 else {
2018 _mesa_error(ctx, GL_INVALID_VALUE,
2019 "glGetProgramRegisterfvMESA(registerName)");
2020 return;
2021 }
2022 }
2023 break;
2024 default:
2025 _mesa_error(ctx, GL_INVALID_ENUM,
2026 "glGetProgramRegisterfvMESA(target)");
2027 return;
2028 }
2029
2030}