blob: b6371329a5cbb77932f546ad127ceda2c8973a82 [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/**
Brian Paulec770b82005-11-20 17:57:22 +00001195 * Initialize program instruction fields to defaults.
1196 */
1197void
1198_mesa_init_instruction(struct prog_instruction *inst)
1199{
1200 _mesa_bzero(inst, sizeof(struct prog_instruction));
1201
1202 inst->SrcReg[0].File = PROGRAM_UNDEFINED;
1203 inst->SrcReg[0].Swizzle = SWIZZLE_NOOP;
1204 inst->SrcReg[1].File = PROGRAM_UNDEFINED;
1205 inst->SrcReg[1].Swizzle = SWIZZLE_NOOP;
1206 inst->SrcReg[2].File = PROGRAM_UNDEFINED;
1207 inst->SrcReg[2].Swizzle = SWIZZLE_NOOP;
1208
1209 inst->DstReg.File = PROGRAM_UNDEFINED;
1210 inst->DstReg.WriteMask = WRITEMASK_XYZW;
1211 inst->DstReg.CondMask = COND_TR;
1212 inst->DstReg.CondSwizzle = SWIZZLE_NOOP;
1213
1214 inst->SaturateMode = SATURATE_OFF;
1215 inst->Precision = FLOAT32;
1216}
1217
1218
1219/**
Brian Paul1fcdaf12005-11-05 19:12:36 +00001220 * Basic info about each instruction
1221 */
1222struct instruction_info
1223{
1224 enum prog_opcode Opcode;
1225 const char *Name;
1226 GLuint NumSrcRegs;
1227};
1228
1229/**
1230 * Instruction info
1231 * \note Opcode should equal array index!
1232 */
1233static const struct instruction_info InstInfo[MAX_OPCODE] = {
1234 { OPCODE_ABS, "ABS", 1 },
1235 { OPCODE_ADD, "ADD", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001236 { OPCODE_ARA, "ARA", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001237 { OPCODE_ARL, "ARL", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001238 { OPCODE_ARL_NV, "ARL", 1 },
1239 { OPCODE_ARR, "ARL", 1 },
1240 { OPCODE_BRA, "BRA", 1 },
1241 { OPCODE_CAL, "CAL", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001242 { OPCODE_CMP, "CMP", 3 },
1243 { OPCODE_COS, "COS", 1 },
1244 { OPCODE_DDX, "DDX", 1 },
1245 { OPCODE_DDY, "DDY", 1 },
1246 { OPCODE_DP3, "DP3", 2 },
1247 { OPCODE_DP4, "DP4", 2 },
1248 { OPCODE_DPH, "DPH", 2 },
1249 { OPCODE_DST, "DST", 2 },
1250 { OPCODE_END, "END", 0 },
1251 { OPCODE_EX2, "EX2", 1 },
1252 { OPCODE_EXP, "EXP", 1 },
1253 { OPCODE_FLR, "FLR", 1 },
1254 { OPCODE_FRC, "FRC", 1 },
1255 { OPCODE_KIL, "KIL", 1 },
1256 { OPCODE_KIL_NV, "KIL", 0 },
1257 { OPCODE_LG2, "LG2", 1 },
1258 { OPCODE_LIT, "LIT", 1 },
1259 { OPCODE_LOG, "LOG", 1 },
1260 { OPCODE_LRP, "LRP", 3 },
1261 { OPCODE_MAD, "MAD", 3 },
1262 { OPCODE_MAX, "MAX", 2 },
1263 { OPCODE_MIN, "MIN", 2 },
1264 { OPCODE_MOV, "MOV", 1 },
1265 { OPCODE_MUL, "MUL", 2 },
1266 { OPCODE_PK2H, "PK2H", 1 },
1267 { OPCODE_PK2US, "PK2US", 1 },
1268 { OPCODE_PK4B, "PK4B", 1 },
1269 { OPCODE_PK4UB, "PK4UB", 1 },
1270 { OPCODE_POW, "POW", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001271 { OPCODE_POPA, "POPA", 0 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001272 { OPCODE_PRINT, "PRINT", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001273 { OPCODE_PUSHA, "PUSHA", 0 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001274 { OPCODE_RCC, "RCC", 1 },
1275 { OPCODE_RCP, "RCP", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001276 { OPCODE_RET, "RET", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001277 { OPCODE_RFL, "RFL", 1 },
1278 { OPCODE_RSQ, "RSQ", 1 },
1279 { OPCODE_SCS, "SCS", 1 },
1280 { OPCODE_SEQ, "SEQ", 2 },
1281 { OPCODE_SFL, "SFL", 0 },
1282 { OPCODE_SGE, "SGE", 2 },
1283 { OPCODE_SGT, "SGT", 2 },
1284 { OPCODE_SIN, "SIN", 1 },
1285 { OPCODE_SLE, "SLE", 2 },
1286 { OPCODE_SLT, "SLT", 2 },
1287 { OPCODE_SNE, "SNE", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001288 { OPCODE_SSG, "SSG", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001289 { OPCODE_STR, "STR", 0 },
1290 { OPCODE_SUB, "SUB", 2 },
1291 { OPCODE_SWZ, "SWZ", 1 },
1292 { OPCODE_TEX, "TEX", 1 },
1293 { OPCODE_TXB, "TXB", 1 },
1294 { OPCODE_TXD, "TXD", 3 },
Ian Romanick4884db62005-11-08 22:40:26 +00001295 { OPCODE_TXL, "TXL", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001296 { OPCODE_TXP, "TXP", 1 },
1297 { OPCODE_TXP_NV, "TXP", 1 },
1298 { OPCODE_UP2H, "UP2H", 1 },
1299 { OPCODE_UP2US, "UP2US", 1 },
1300 { OPCODE_UP4B, "UP4B", 1 },
1301 { OPCODE_UP4UB, "UP4UB", 1 },
1302 { OPCODE_X2D, "X2D", 3 },
1303 { OPCODE_XPD, "XPD", 2 }
1304};
1305
1306
1307/**
1308 * Return the number of src registers for the given instruction/opcode.
1309 */
1310GLuint
1311_mesa_num_inst_src_regs(enum prog_opcode opcode)
1312{
1313 GLuint i;
1314#ifdef DEBUG
1315 for (i = 0; i < MAX_OPCODE; i++) {
1316 ASSERT(i == InstInfo[i].Opcode);
1317 }
1318#endif
1319 for (i = 0; i < MAX_OPCODE; i++) {
1320 if (InstInfo[i].Opcode == opcode) {
1321 return InstInfo[i].NumSrcRegs;
1322 }
1323 }
1324 _mesa_problem(NULL, "invalid opcode in _mesa_num_inst_src_regs");
1325 return 0;
1326}
1327
1328
1329/**
1330 * Return string name for given program opcode.
1331 */
1332const char *
1333_mesa_opcode_string(enum prog_opcode opcode)
1334{
1335 ASSERT(opcode < MAX_OPCODE);
1336 return InstInfo[opcode].Name;
1337}
1338
Brian Paulbf41bc02005-11-05 19:32:36 +00001339/**
1340 * Return string name for given program/register file.
1341 */
Brian Paul30d6a4b2005-11-05 20:18:18 +00001342static const char *
1343program_file_string(enum register_file f)
Brian Paulbf41bc02005-11-05 19:32:36 +00001344{
1345 switch (f) {
1346 case PROGRAM_TEMPORARY:
1347 return "TEMP";
1348 case PROGRAM_LOCAL_PARAM:
1349 return "LOCAL";
1350 case PROGRAM_ENV_PARAM:
1351 return "ENV";
1352 case PROGRAM_STATE_VAR:
1353 return "STATE";
1354 case PROGRAM_INPUT:
1355 return "INPUT";
1356 case PROGRAM_OUTPUT:
1357 return "OUTPUT";
1358 case PROGRAM_NAMED_PARAM:
1359 return "NAMED";
1360 case PROGRAM_CONSTANT:
1361 return "CONST";
1362 case PROGRAM_WRITE_ONLY:
1363 return "WRITE_ONLY";
1364 case PROGRAM_ADDRESS:
1365 return "ADDR";
1366 default:
1367 return "!unkown!";
1368 }
1369}
1370
Michal Krol2861e732004-03-29 11:09:34 +00001371
Brian Paul30d6a4b2005-11-05 20:18:18 +00001372/**
1373 * Return a string representation of the given swizzle word.
Brian Paul7b98b402005-11-12 23:25:49 +00001374 * If extended is true, use extended (comma-separated) format.
Brian Paul30d6a4b2005-11-05 20:18:18 +00001375 */
1376static const char *
Brian Paul7b98b402005-11-12 23:25:49 +00001377swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001378{
1379 static const char swz[] = "xyzw01";
1380 static char s[20];
1381 GLuint i = 0;
1382
Brian Paul7b98b402005-11-12 23:25:49 +00001383 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001384 return ""; /* no swizzle/negation */
1385
Brian Paul7b98b402005-11-12 23:25:49 +00001386 if (!extended)
1387 s[i++] = '.';
Brian Paul30d6a4b2005-11-05 20:18:18 +00001388
1389 if (negateBase & 0x1)
1390 s[i++] = '-';
1391 s[i++] = swz[GET_SWZ(swizzle, 0)];
1392
Brian Paul7b98b402005-11-12 23:25:49 +00001393 if (extended) {
1394 s[i++] = ',';
1395 }
1396
Brian Paul30d6a4b2005-11-05 20:18:18 +00001397 if (negateBase & 0x2)
1398 s[i++] = '-';
1399 s[i++] = swz[GET_SWZ(swizzle, 1)];
1400
Brian Paul7b98b402005-11-12 23:25:49 +00001401 if (extended) {
1402 s[i++] = ',';
1403 }
1404
Brian Paul30d6a4b2005-11-05 20:18:18 +00001405 if (negateBase & 0x4)
1406 s[i++] = '-';
1407 s[i++] = swz[GET_SWZ(swizzle, 2)];
1408
Brian Paul7b98b402005-11-12 23:25:49 +00001409 if (extended) {
1410 s[i++] = ',';
1411 }
1412
Brian Paul30d6a4b2005-11-05 20:18:18 +00001413 if (negateBase & 0x8)
1414 s[i++] = '-';
1415 s[i++] = swz[GET_SWZ(swizzle, 3)];
1416
1417 s[i] = 0;
1418 return s;
1419}
1420
1421
1422static const char *
1423writemask_string(GLuint writeMask)
1424{
1425 static char s[10];
1426 GLuint i = 0;
1427
1428 if (writeMask == WRITEMASK_XYZW)
1429 return "";
1430
1431 s[i++] = '.';
1432 if (writeMask & WRITEMASK_X)
1433 s[i++] = 'x';
1434 if (writeMask & WRITEMASK_Y)
1435 s[i++] = 'y';
1436 if (writeMask & WRITEMASK_Z)
1437 s[i++] = 'z';
1438 if (writeMask & WRITEMASK_W)
1439 s[i++] = 'w';
1440
1441 s[i] = 0;
1442 return s;
1443}
1444
Brian Paul7b98b402005-11-12 23:25:49 +00001445static void
1446print_dst_reg(const struct prog_dst_register *dstReg)
1447{
1448 _mesa_printf(" %s[%d]%s",
1449 program_file_string(dstReg->File),
1450 dstReg->Index,
1451 writemask_string(dstReg->WriteMask));
1452}
1453
1454static void
1455print_src_reg(const struct prog_src_register *srcReg)
1456{
1457 _mesa_printf("%s[%d]%s",
1458 program_file_string(srcReg->File),
1459 srcReg->Index,
1460 swizzle_string(srcReg->Swizzle,
1461 srcReg->NegateBase, GL_FALSE));
1462}
1463
Brian Paul30d6a4b2005-11-05 20:18:18 +00001464
1465/**
Brian Paulde997602005-11-12 17:53:14 +00001466 * Print a single vertex/fragment program instruction.
Brian Paul30d6a4b2005-11-05 20:18:18 +00001467 */
1468void
Brian Paulde997602005-11-12 17:53:14 +00001469_mesa_print_instruction(const struct prog_instruction *inst)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001470{
Brian Paulde997602005-11-12 17:53:14 +00001471 switch (inst->Opcode) {
1472 case OPCODE_PRINT:
1473 _mesa_printf("PRINT '%s'", inst->Data);
1474 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
1475 _mesa_printf(", ");
1476 _mesa_printf("%s[%d]%s",
1477 program_file_string(inst->SrcReg[0].File),
1478 inst->SrcReg[0].Index,
1479 swizzle_string(inst->SrcReg[0].Swizzle,
Brian Paul7b98b402005-11-12 23:25:49 +00001480 inst->SrcReg[0].NegateBase, GL_FALSE));
Brian Paulde997602005-11-12 17:53:14 +00001481 }
1482 _mesa_printf(";\n");
1483 break;
Brian Paul7b98b402005-11-12 23:25:49 +00001484 case OPCODE_SWZ:
1485 _mesa_printf("SWZ");
Brian Paule31ac052005-11-20 17:52:40 +00001486 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Paul7b98b402005-11-12 23:25:49 +00001487 _mesa_printf("_SAT");
1488 print_dst_reg(&inst->DstReg);
1489 _mesa_printf("%s[%d], %s;\n",
1490 program_file_string(inst->SrcReg[0].File),
1491 inst->SrcReg[0].Index,
1492 swizzle_string(inst->SrcReg[0].Swizzle,
1493 inst->SrcReg[0].NegateBase, GL_TRUE));
1494 break;
1495 case OPCODE_TEX:
1496 case OPCODE_TXP:
1497 case OPCODE_TXB:
1498 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
Brian Paule31ac052005-11-20 17:52:40 +00001499 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Paul7b98b402005-11-12 23:25:49 +00001500 _mesa_printf("_SAT");
1501 _mesa_printf(" ");
1502 print_dst_reg(&inst->DstReg);
1503 _mesa_printf(", ");
1504 print_src_reg(&inst->SrcReg[0]);
1505 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
1506 switch (inst->TexSrcTarget) {
1507 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
1508 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
1509 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
1510 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
1511 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
1512 default:
1513 ;
1514 }
1515 _mesa_printf("\n");
1516 break;
1517 case OPCODE_ARL:
1518 _mesa_printf("ARL addr.x, ");
1519 print_src_reg(&inst->SrcReg[0]);
1520 _mesa_printf(";\n");
1521 break;
1522 /* XXX may need for other special-case instructions */
Brian Paulde997602005-11-12 17:53:14 +00001523 default:
1524 /* typical alu instruction */
1525 {
1526 const GLuint numRegs = _mesa_num_inst_src_regs(inst->Opcode);
1527 GLuint j;
Brian Paul30d6a4b2005-11-05 20:18:18 +00001528
Brian Paulde997602005-11-12 17:53:14 +00001529 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
Brian Paul30d6a4b2005-11-05 20:18:18 +00001530
Brian Paulde997602005-11-12 17:53:14 +00001531 /* frag prog only */
Brian Paule31ac052005-11-20 17:52:40 +00001532 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Paulde997602005-11-12 17:53:14 +00001533 _mesa_printf("_SAT");
1534
1535 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
1536 _mesa_printf(" %s[%d]%s",
1537 program_file_string(inst->DstReg.File),
1538 inst->DstReg.Index,
1539 writemask_string(inst->DstReg.WriteMask));
1540 }
1541
1542 if (numRegs > 0)
Brian Paul02df9e12005-11-08 14:42:52 +00001543 _mesa_printf(", ");
Brian Paulde997602005-11-12 17:53:14 +00001544
1545 for (j = 0; j < numRegs; j++) {
Brian Paul7b98b402005-11-12 23:25:49 +00001546 print_src_reg(inst->SrcReg + j);
Brian Paulde997602005-11-12 17:53:14 +00001547 if (j + 1 < numRegs)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001548 _mesa_printf(", ");
Brian Paul30d6a4b2005-11-05 20:18:18 +00001549 }
Brian Paulde997602005-11-12 17:53:14 +00001550
1551 _mesa_printf(";\n");
Brian Paul30d6a4b2005-11-05 20:18:18 +00001552 }
1553 }
1554}
1555
1556
Brian Paulde997602005-11-12 17:53:14 +00001557/**
1558 * Print a vertx/fragment program to stdout.
1559 * XXX this function could be greatly improved.
1560 */
1561void
1562_mesa_print_program(const struct program *prog)
1563{
1564 GLuint i;
1565 for (i = 0; i < prog->NumInstructions; i++) {
1566 _mesa_printf("%3d: ", i);
1567 _mesa_print_instruction(prog->Instructions + i);
1568 }
1569}
1570
1571
1572/**
1573 * Print all of a program's parameters.
1574 */
1575void
1576_mesa_print_program_parameters(GLcontext *ctx, const struct program *prog)
1577{
1578 GLint i;
1579
1580 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
1581 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
1582 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
1583 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
1584 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
1585
1586 _mesa_load_state_parameters(ctx, prog->Parameters);
1587
1588#if 0
1589 _mesa_printf("Local Params:\n");
1590 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
1591 const GLfloat *p = prog->LocalParams[i];
1592 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
1593 }
1594#endif
1595
1596 for (i = 0; i < prog->Parameters->NumParameters; i++){
Brian Paul7b98b402005-11-12 23:25:49 +00001597 struct program_parameter *param = prog->Parameters->Parameters + i;
1598 const GLfloat *v = prog->Parameters->ParameterValues[i];
1599 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1600 i, param->Name, v[0], v[1], v[2], v[3]);
Brian Paulde997602005-11-12 17:53:14 +00001601 }
1602}
1603
Brian Paul30d6a4b2005-11-05 20:18:18 +00001604
1605
Michal Krol2861e732004-03-29 11:09:34 +00001606/**********************************************************************/
1607/* API functions */
1608/**********************************************************************/
1609
1610
1611/**
1612 * Bind a program (make it current)
1613 * \note Called from the GL API dispatcher by both glBindProgramNV
1614 * and glBindProgramARB.
1615 */
1616void GLAPIENTRY
1617_mesa_BindProgram(GLenum target, GLuint id)
1618{
1619 struct program *prog;
1620 GET_CURRENT_CONTEXT(ctx);
1621 ASSERT_OUTSIDE_BEGIN_END(ctx);
1622
1623 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1624
1625 if ((target == GL_VERTEX_PROGRAM_NV
1626 && ctx->Extensions.NV_vertex_program) ||
1627 (target == GL_VERTEX_PROGRAM_ARB
1628 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001629 /*** Vertex program binding ***/
1630 struct vertex_program *curProg = ctx->VertexProgram.Current;
1631 if (curProg->Base.Id == id) {
1632 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001633 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001634 }
1635 if (curProg->Base.Id != 0) {
1636 /* decrement refcount on previously bound vertex program */
1637 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001638 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001639 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001640 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001641 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001642 }
1643 }
1644 }
1645 else if ((target == GL_FRAGMENT_PROGRAM_NV
1646 && ctx->Extensions.NV_fragment_program) ||
1647 (target == GL_FRAGMENT_PROGRAM_ARB
1648 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001649 /*** Fragment program binding ***/
1650 struct fragment_program *curProg = ctx->FragmentProgram.Current;
1651 if (curProg->Base.Id == id) {
1652 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001653 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001654 }
1655 if (curProg->Base.Id != 0) {
1656 /* decrement refcount on previously bound fragment program */
1657 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001658 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001659 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001660 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001661 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001662 }
1663 }
1664 }
1665 else {
1666 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1667 return;
1668 }
1669
1670 /* NOTE: binding to a non-existant program is not an error.
1671 * That's supposed to be caught in glBegin.
1672 */
1673 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001674 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001675 prog = NULL;
1676 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1677 prog = ctx->Shared->DefaultVertexProgram;
1678 else
1679 prog = ctx->Shared->DefaultFragmentProgram;
1680 }
1681 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001682 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001683 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001684 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001685 /* allocate a new program now */
1686 prog = ctx->Driver.NewProgram(ctx, target, id);
1687 if (!prog) {
1688 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1689 return;
1690 }
Michal Krol2861e732004-03-29 11:09:34 +00001691 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1692 }
Brian Paul765f1a12004-09-14 22:28:27 +00001693 else if (prog->Target != target) {
1694 _mesa_error(ctx, GL_INVALID_OPERATION,
1695 "glBindProgramNV/ARB(target mismatch)");
1696 return;
1697 }
Michal Krol2861e732004-03-29 11:09:34 +00001698 }
1699
1700 /* bind now */
1701 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1702 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1703 }
1704 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1705 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1706 }
1707
Brian Paul765f1a12004-09-14 22:28:27 +00001708 /* Never null pointers */
1709 ASSERT(ctx->VertexProgram.Current);
1710 ASSERT(ctx->FragmentProgram.Current);
1711
Michal Krol2861e732004-03-29 11:09:34 +00001712 if (prog)
1713 prog->RefCount++;
1714
1715 if (ctx->Driver.BindProgram)
1716 ctx->Driver.BindProgram(ctx, target, prog);
1717}
1718
1719
1720/**
1721 * Delete a list of programs.
1722 * \note Not compiled into display lists.
1723 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1724 */
1725void GLAPIENTRY
1726_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1727{
1728 GLint i;
1729 GET_CURRENT_CONTEXT(ctx);
1730 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1731
1732 if (n < 0) {
1733 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1734 return;
1735 }
1736
1737 for (i = 0; i < n; i++) {
1738 if (ids[i] != 0) {
1739 struct program *prog = (struct program *)
1740 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001741 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001742 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1743 }
1744 else if (prog) {
1745 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001746 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1747 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1748 if (ctx->VertexProgram.Current &&
1749 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1750 /* unbind this currently bound program */
1751 _mesa_BindProgram(prog->Target, 0);
1752 }
1753 }
1754 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1755 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1756 if (ctx->FragmentProgram.Current &&
1757 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1758 /* unbind this currently bound program */
1759 _mesa_BindProgram(prog->Target, 0);
1760 }
1761 }
1762 else {
1763 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1764 return;
1765 }
Brian Paulea2943e2005-01-20 04:02:02 +00001766 /* The ID is immediately available for re-use now */
1767 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1768 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001769 if (prog->RefCount <= 0) {
1770 ctx->Driver.DeleteProgram(ctx, prog);
1771 }
1772 }
Michal Krol2861e732004-03-29 11:09:34 +00001773 }
1774 }
1775}
1776
1777
1778/**
1779 * Generate a list of new program identifiers.
1780 * \note Not compiled into display lists.
1781 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1782 */
1783void GLAPIENTRY
1784_mesa_GenPrograms(GLsizei n, GLuint *ids)
1785{
1786 GLuint first;
1787 GLuint i;
1788 GET_CURRENT_CONTEXT(ctx);
1789 ASSERT_OUTSIDE_BEGIN_END(ctx);
1790
1791 if (n < 0) {
1792 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1793 return;
1794 }
1795
1796 if (!ids)
1797 return;
1798
1799 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1800
Brian Paul765f1a12004-09-14 22:28:27 +00001801 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001802 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001803 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001804 }
1805
1806 /* Return the program names */
1807 for (i = 0; i < (GLuint) n; i++) {
1808 ids[i] = first + i;
1809 }
1810}
1811
1812
1813/**
Brian Paul765f1a12004-09-14 22:28:27 +00001814 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001815 * \note Not compiled into display lists.
1816 * \note Called from both glIsProgramNV and glIsProgramARB.
1817 * \param id is the program identifier
1818 * \return GL_TRUE if id is a program, else GL_FALSE.
1819 */
1820GLboolean GLAPIENTRY
1821_mesa_IsProgram(GLuint id)
1822{
1823 GET_CURRENT_CONTEXT(ctx);
1824 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1825
1826 if (id == 0)
1827 return GL_FALSE;
1828
1829 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1830 return GL_TRUE;
1831 else
1832 return GL_FALSE;
1833}
1834
1835
1836
1837/**********************************************************************/
1838/* GL_MESA_program_debug extension */
1839/**********************************************************************/
1840
1841
1842/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001843GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001844glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1845 GLvoid *data)
1846{
1847 _mesa_ProgramCallbackMESA(target, callback, data);
1848}
1849
1850
1851void
1852_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1853 GLvoid *data)
1854{
1855 GET_CURRENT_CONTEXT(ctx);
1856
1857 switch (target) {
1858 case GL_FRAGMENT_PROGRAM_ARB:
1859 if (!ctx->Extensions.ARB_fragment_program) {
1860 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1861 return;
1862 }
1863 ctx->FragmentProgram.Callback = callback;
1864 ctx->FragmentProgram.CallbackData = data;
1865 break;
1866 case GL_FRAGMENT_PROGRAM_NV:
1867 if (!ctx->Extensions.NV_fragment_program) {
1868 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1869 return;
1870 }
1871 ctx->FragmentProgram.Callback = callback;
1872 ctx->FragmentProgram.CallbackData = data;
1873 break;
1874 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1875 if (!ctx->Extensions.ARB_vertex_program &&
1876 !ctx->Extensions.NV_vertex_program) {
1877 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1878 return;
1879 }
1880 ctx->VertexProgram.Callback = callback;
1881 ctx->VertexProgram.CallbackData = data;
1882 break;
1883 default:
1884 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1885 return;
1886 }
1887}
1888
1889
1890/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001891GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001892glGetProgramRegisterfvMESA(GLenum target,
1893 GLsizei len, const GLubyte *registerName,
1894 GLfloat *v)
1895{
1896 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1897}
1898
1899
1900void
1901_mesa_GetProgramRegisterfvMESA(GLenum target,
1902 GLsizei len, const GLubyte *registerName,
1903 GLfloat *v)
1904{
1905 char reg[1000];
1906 GET_CURRENT_CONTEXT(ctx);
1907
1908 /* We _should_ be inside glBegin/glEnd */
1909#if 0
1910 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1911 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1912 return;
1913 }
1914#endif
1915
1916 /* make null-terminated copy of registerName */
1917 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1918 _mesa_memcpy(reg, registerName, len);
1919 reg[len] = 0;
1920
1921 switch (target) {
1922 case GL_VERTEX_PROGRAM_NV:
1923 if (!ctx->Extensions.ARB_vertex_program &&
1924 !ctx->Extensions.NV_vertex_program) {
1925 _mesa_error(ctx, GL_INVALID_ENUM,
1926 "glGetProgramRegisterfvMESA(target)");
1927 return;
1928 }
Brian Paul6d460af2004-04-23 14:16:46 +00001929 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001930 _mesa_error(ctx, GL_INVALID_OPERATION,
1931 "glGetProgramRegisterfvMESA");
1932 return;
1933 }
1934 /* GL_NV_vertex_program */
1935 if (reg[0] == 'R') {
1936 /* Temp register */
1937 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001938 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001939 _mesa_error(ctx, GL_INVALID_VALUE,
1940 "glGetProgramRegisterfvMESA(registerName)");
1941 return;
1942 }
1943 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1944 }
1945 else if (reg[0] == 'v' && reg[1] == '[') {
1946 /* Vertex Input attribute */
1947 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001948 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001949 const char *name = _mesa_nv_vertex_input_register_name(i);
1950 char number[10];
Brian Paulaa206952005-09-16 18:14:24 +00001951 _mesa_sprintf(number, "%d", i);
Michal Krol2861e732004-03-29 11:09:34 +00001952 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1953 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1954 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1955 return;
1956 }
1957 }
1958 _mesa_error(ctx, GL_INVALID_VALUE,
1959 "glGetProgramRegisterfvMESA(registerName)");
1960 return;
1961 }
1962 else if (reg[0] == 'o' && reg[1] == '[') {
1963 /* Vertex output attribute */
1964 }
1965 /* GL_ARB_vertex_program */
1966 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1967
1968 }
1969 else {
1970 _mesa_error(ctx, GL_INVALID_VALUE,
1971 "glGetProgramRegisterfvMESA(registerName)");
1972 return;
1973 }
1974 break;
1975 case GL_FRAGMENT_PROGRAM_ARB:
1976 if (!ctx->Extensions.ARB_fragment_program) {
1977 _mesa_error(ctx, GL_INVALID_ENUM,
1978 "glGetProgramRegisterfvMESA(target)");
1979 return;
1980 }
Brian Paul6d460af2004-04-23 14:16:46 +00001981 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001982 _mesa_error(ctx, GL_INVALID_OPERATION,
1983 "glGetProgramRegisterfvMESA");
1984 return;
1985 }
1986 /* XXX to do */
1987 break;
1988 case GL_FRAGMENT_PROGRAM_NV:
1989 if (!ctx->Extensions.NV_fragment_program) {
1990 _mesa_error(ctx, GL_INVALID_ENUM,
1991 "glGetProgramRegisterfvMESA(target)");
1992 return;
1993 }
Brian Paul6d460af2004-04-23 14:16:46 +00001994 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001995 _mesa_error(ctx, GL_INVALID_OPERATION,
1996 "glGetProgramRegisterfvMESA");
1997 return;
1998 }
1999 if (reg[0] == 'R') {
2000 /* Temp register */
2001 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00002002 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00002003 _mesa_error(ctx, GL_INVALID_VALUE,
2004 "glGetProgramRegisterfvMESA(registerName)");
2005 return;
2006 }
2007 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
2008 }
2009 else if (reg[0] == 'f' && reg[1] == '[') {
2010 /* Fragment input attribute */
2011 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00002012 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00002013 const char *name = _mesa_nv_fragment_input_register_name(i);
2014 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
2015 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
2016 return;
2017 }
2018 }
2019 _mesa_error(ctx, GL_INVALID_VALUE,
2020 "glGetProgramRegisterfvMESA(registerName)");
2021 return;
2022 }
2023 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
2024 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00002025 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLR]);
Michal Krol2861e732004-03-29 11:09:34 +00002026 }
2027 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
2028 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00002029 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLH]);
Michal Krol2861e732004-03-29 11:09:34 +00002030 }
2031 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
2032 /* Fragment output depth */
Brian Paul90ebb582005-11-02 18:06:12 +00002033 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_DEPR]);
Michal Krol2861e732004-03-29 11:09:34 +00002034 }
2035 else {
2036 /* try user-defined identifiers */
2037 const GLfloat *value = _mesa_lookup_parameter_value(
Brian Paulde997602005-11-12 17:53:14 +00002038 ctx->FragmentProgram.Current->Base.Parameters, -1, reg);
Michal Krol2861e732004-03-29 11:09:34 +00002039 if (value) {
2040 COPY_4V(v, value);
2041 }
2042 else {
2043 _mesa_error(ctx, GL_INVALID_VALUE,
2044 "glGetProgramRegisterfvMESA(registerName)");
2045 return;
2046 }
2047 }
2048 break;
2049 default:
2050 _mesa_error(ctx, GL_INVALID_ENUM,
2051 "glGetProgramRegisterfvMESA(target)");
2052 return;
2053 }
2054
2055}