blob: 6e0805c3cb2d1ac161558b2c1e166a3c512b7d8b [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
91#if FEATURE_ATI_fragment_shader
92 ctx->ATIFragmentShader.Enabled = GL_FALSE;
93 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
94 assert(ctx->ATIFragmentShader.Current);
95 ctx->ATIFragmentShader.Current->Base.RefCount++;
96#endif
Michal Krol2861e732004-03-29 11:09:34 +000097}
98
99
100/**
Brian Paul21841f02004-08-14 14:28:11 +0000101 * Free a context's vertex/fragment program state
102 */
103void
104_mesa_free_program_data(GLcontext *ctx)
105{
106#if FEATURE_NV_vertex_program
107 if (ctx->VertexProgram.Current) {
108 ctx->VertexProgram.Current->Base.RefCount--;
109 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
110 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
111 }
112#endif
113#if FEATURE_NV_fragment_program
114 if (ctx->FragmentProgram.Current) {
115 ctx->FragmentProgram.Current->Base.RefCount--;
116 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
117 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
118 }
119#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000120#if FEATURE_ATI_fragment_shader
121 if (ctx->ATIFragmentShader.Current) {
122 ctx->ATIFragmentShader.Current->Base.RefCount--;
123 if (ctx->ATIFragmentShader.Current->Base.RefCount <= 0)
124 ctx->Driver.DeleteProgram(ctx, &(ctx->ATIFragmentShader.Current->Base));
125 }
126#endif
Brian Paul21841f02004-08-14 14:28:11 +0000127 _mesa_free((void *) ctx->Program.ErrorString);
128}
129
130
131
132
133/**
Michal Krol2861e732004-03-29 11:09:34 +0000134 * Set the vertex/fragment program error state (position and error string).
135 * This is generally called from within the parsers.
136 */
137void
138_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
139{
140 ctx->Program.ErrorPos = pos;
141 _mesa_free((void *) ctx->Program.ErrorString);
142 if (!string)
143 string = "";
144 ctx->Program.ErrorString = _mesa_strdup(string);
145}
146
147
148/**
149 * Find the line number and column for 'pos' within 'string'.
150 * Return a copy of the line which contains 'pos'. Free the line with
151 * _mesa_free().
152 * \param string the program string
153 * \param pos the position within the string
154 * \param line returns the line number corresponding to 'pos'.
155 * \param col returns the column number corresponding to 'pos'.
156 * \return copy of the line containing 'pos'.
157 */
158const GLubyte *
159_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
160 GLint *line, GLint *col)
161{
162 const GLubyte *lineStart = string;
163 const GLubyte *p = string;
164 GLubyte *s;
165 int len;
166
167 *line = 1;
168
169 while (p != pos) {
170 if (*p == (GLubyte) '\n') {
171 (*line)++;
172 lineStart = p + 1;
173 }
174 p++;
175 }
176
177 *col = (pos - lineStart) + 1;
178
179 /* return copy of this line */
180 while (*p != 0 && *p != '\n')
181 p++;
182 len = p - lineStart;
183 s = (GLubyte *) _mesa_malloc(len + 1);
184 _mesa_memcpy(s, lineStart, len);
185 s[len] = 0;
186
187 return s;
188}
189
190
Brian Paul765f1a12004-09-14 22:28:27 +0000191/**
192 * Initialize a new vertex/fragment program object.
193 */
194static struct program *
195_mesa_init_program_struct( GLcontext *ctx, struct program *prog,
196 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000197{
Brian Paula6c423d2004-08-25 15:59:48 +0000198 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000199 if (prog) {
200 prog->Id = id;
201 prog->Target = target;
202 prog->Resident = GL_TRUE;
203 prog->RefCount = 1;
204 }
205
206 return prog;
207}
208
Brian Paul765f1a12004-09-14 22:28:27 +0000209
210/**
211 * Initialize a new fragment program object.
212 */
213struct program *
214_mesa_init_fragment_program( GLcontext *ctx, struct fragment_program *prog,
215 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000216{
217 if (prog)
218 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
219 else
220 return NULL;
221}
222
Brian Paul765f1a12004-09-14 22:28:27 +0000223
224/**
225 * Initialize a new vertex program object.
226 */
227struct program *
228_mesa_init_vertex_program( GLcontext *ctx, struct vertex_program *prog,
229 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000230{
231 if (prog)
232 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
233 else
234 return NULL;
235}
236
Dave Airlie7f752fe2004-12-19 03:06:59 +0000237/**
238 * Initialize a new ATI fragment shader object.
239 */
240struct program *
Brian Paulcdb65412005-01-11 15:56:47 +0000241_mesa_init_ati_fragment_shader( GLcontext *ctx,
242 struct ati_fragment_shader *prog,
243 GLenum target, GLuint id )
Dave Airlie7f752fe2004-12-19 03:06:59 +0000244{
245 if (prog)
246 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
247 else
248 return NULL;
249}
250
251
Michal Krol2861e732004-03-29 11:09:34 +0000252
253/**
254 * Allocate and initialize a new fragment/vertex program object but
255 * don't put it into the program hash table. Called via
256 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
257 * device driver function to implement OO deriviation with additional
258 * types not understood by this function.
259 *
260 * \param ctx context
261 * \param id program id/number
262 * \param target program target/type
263 * \return pointer to new program object
264 */
265struct program *
266_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
267{
268 switch (target) {
269 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
270 return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program),
271 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000272 case GL_FRAGMENT_PROGRAM_NV:
273 case GL_FRAGMENT_PROGRAM_ARB:
274 return _mesa_init_fragment_program( ctx, CALLOC_STRUCT(fragment_program),
275 target, id );
Dave Airlie7f752fe2004-12-19 03:06:59 +0000276 case GL_FRAGMENT_SHADER_ATI:
277 return _mesa_init_ati_fragment_shader( ctx, CALLOC_STRUCT(ati_fragment_shader),
278 target, id );
279
Michal Krol2861e732004-03-29 11:09:34 +0000280 default:
281 _mesa_problem(ctx, "bad target in _mesa_new_program");
282 return NULL;
283 }
284}
285
286
287/**
288 * Delete a program and remove it from the hash table, ignoring the
289 * reference count.
290 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
291 * by a device driver function.
292 */
293void
294_mesa_delete_program(GLcontext *ctx, struct program *prog)
295{
Brian Paula6c423d2004-08-25 15:59:48 +0000296 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000297 ASSERT(prog);
298
299 if (prog->String)
300 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000301
302 if (prog->Instructions) {
303 GLuint i;
304 for (i = 0; i < prog->NumInstructions; i++) {
305 if (prog->Instructions[i].Data)
306 _mesa_free(prog->Instructions[i].Data);
Brian Paul575700f2004-12-16 03:07:18 +0000307 }
Brian Paulde997602005-11-12 17:53:14 +0000308 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000309 }
Brian Paulde997602005-11-12 17:53:14 +0000310
311 if (prog->Parameters)
312 _mesa_free_parameter_list(prog->Parameters);
313
314 if (prog->Target == GL_FRAGMENT_SHADER_ATI) {
Dave Airlie7f752fe2004-12-19 03:06:59 +0000315 struct ati_fragment_shader *atifs = (struct ati_fragment_shader *)prog;
Roland Scheideggerf519a772005-09-02 01:11:53 +0000316 GLuint i;
317 for (i = 0; i < MAX_NUM_PASSES_ATI; i++) {
318 if (atifs->Instructions[i])
319 _mesa_free(atifs->Instructions[i]);
320 if (atifs->SetupInst[i])
321 _mesa_free(atifs->SetupInst[i]);
322 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000323 }
324
Michal Krol2861e732004-03-29 11:09:34 +0000325 _mesa_free(prog);
326}
327
328
329
330/**********************************************************************/
331/* Program parameter functions */
332/**********************************************************************/
333
334struct program_parameter_list *
335_mesa_new_parameter_list(void)
336{
337 return (struct program_parameter_list *)
338 _mesa_calloc(sizeof(struct program_parameter_list));
339}
340
341
342/**
343 * Free a parameter list and all its parameters
344 */
345void
346_mesa_free_parameter_list(struct program_parameter_list *paramList)
347{
348 _mesa_free_parameters(paramList);
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000349 _mesa_free(paramList->Parameters);
Keith Whitwell9899f582005-06-08 21:57:45 +0000350 if (paramList->ParameterValues)
351 ALIGN_FREE(paramList->ParameterValues);
Michal Krol2861e732004-03-29 11:09:34 +0000352 _mesa_free(paramList);
353}
354
355
356/**
357 * Free all the parameters in the given list, but don't free the
358 * paramList structure itself.
359 */
360void
361_mesa_free_parameters(struct program_parameter_list *paramList)
362{
363 GLuint i;
364 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000365 if (paramList->Parameters[i].Name)
366 _mesa_free((void *) paramList->Parameters[i].Name);
Michal Krol2861e732004-03-29 11:09:34 +0000367 }
Michal Krol2861e732004-03-29 11:09:34 +0000368 paramList->NumParameters = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000369}
370
371
372/**
373 * Helper function used by the functions below.
Brian Paul16241622005-11-03 02:26:47 +0000374 * \return index of new parameter in the list, or -1 if error (out of mem)
Michal Krol2861e732004-03-29 11:09:34 +0000375 */
376static GLint
377add_parameter(struct program_parameter_list *paramList,
378 const char *name, const GLfloat values[4],
Brian Paul613e1ad2005-11-05 02:15:21 +0000379 enum register_file type)
Michal Krol2861e732004-03-29 11:09:34 +0000380{
381 const GLuint n = paramList->NumParameters;
382
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000383 if (n == paramList->Size) {
Keith Whitwell9899f582005-06-08 21:57:45 +0000384 GLfloat (*tmp)[4];
385
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000386 paramList->Size *= 2;
387 if (!paramList->Size)
Keith Whitwell9899f582005-06-08 21:57:45 +0000388 paramList->Size = 8;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000389
390 paramList->Parameters = (struct program_parameter *)
391 _mesa_realloc(paramList->Parameters,
392 n * sizeof(struct program_parameter),
393 paramList->Size * sizeof(struct program_parameter));
Keith Whitwell9899f582005-06-08 21:57:45 +0000394
395 tmp = paramList->ParameterValues;
396 paramList->ParameterValues = ALIGN_MALLOC(paramList->Size * 4 * sizeof(GLfloat), 16);
397 if (tmp) {
398 _mesa_memcpy(paramList->ParameterValues, tmp,
399 n * 4 * sizeof(GLfloat));
400 ALIGN_FREE(tmp);
401 }
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000402 }
Keith Whitwell7c26b612005-04-21 14:46:57 +0000403
404 if (!paramList->Parameters ||
405 !paramList->ParameterValues) {
Michal Krol2861e732004-03-29 11:09:34 +0000406 /* out of memory */
407 paramList->NumParameters = 0;
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000408 paramList->Size = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000409 return -1;
410 }
411 else {
412 paramList->NumParameters = n + 1;
Keith Whitwella42fe192005-05-10 18:22:19 +0000413
414 _mesa_memset(&paramList->Parameters[n], 0,
415 sizeof(struct program_parameter));
416
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000417 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000418 paramList->Parameters[n].Type = type;
419 if (values)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000420 COPY_4V(paramList->ParameterValues[n], values);
Michal Krol2861e732004-03-29 11:09:34 +0000421 return (GLint) n;
422 }
423}
424
425
426/**
427 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
428 * \return index of the new entry in the parameter list
429 */
430GLint
431_mesa_add_named_parameter(struct program_parameter_list *paramList,
432 const char *name, const GLfloat values[4])
433{
Brian Paul613e1ad2005-11-05 02:15:21 +0000434 return add_parameter(paramList, name, values, PROGRAM_NAMED_PARAM);
Michal Krol2861e732004-03-29 11:09:34 +0000435}
436
437
438/**
439 * Add a new unnamed constant to the parameter list.
440 * \param paramList - the parameter list
441 * \param values - four float values
442 * \return index of the new parameter.
443 */
444GLint
445_mesa_add_named_constant(struct program_parameter_list *paramList,
446 const char *name, const GLfloat values[4])
447{
Brian Paul613e1ad2005-11-05 02:15:21 +0000448 return add_parameter(paramList, name, values, PROGRAM_CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000449}
450
451
452/**
453 * Add a new unnamed constant to the parameter list.
454 * \param paramList - the parameter list
455 * \param values - four float values
456 * \return index of the new parameter.
457 */
458GLint
459_mesa_add_unnamed_constant(struct program_parameter_list *paramList,
460 const GLfloat values[4])
461{
Brian Paul613e1ad2005-11-05 02:15:21 +0000462 return add_parameter(paramList, NULL, values, PROGRAM_CONSTANT);
Michal Krol2861e732004-03-29 11:09:34 +0000463}
464
465
466/**
467 * Add a new state reference to the parameter list.
468 * \param paramList - the parameter list
469 * \param state - an array of 6 state tokens
470 *
471 * \return index of the new parameter.
472 */
473GLint
474_mesa_add_state_reference(struct program_parameter_list *paramList,
Brian Paul16241622005-11-03 02:26:47 +0000475 const GLint *stateTokens)
Michal Krol2861e732004-03-29 11:09:34 +0000476{
Brian Paul16241622005-11-03 02:26:47 +0000477 /* XXX we should probably search the current parameter list to see if
478 * the new state reference is already present.
Michal Krol2861e732004-03-29 11:09:34 +0000479 */
Brian Paul16241622005-11-03 02:26:47 +0000480 GLint index;
Brian Paul7b98b402005-11-12 23:25:49 +0000481 const char *name = make_state_string(stateTokens);
Michal Krol2861e732004-03-29 11:09:34 +0000482
Brian Paul7b98b402005-11-12 23:25:49 +0000483 index = add_parameter(paramList, name, NULL, PROGRAM_STATE_VAR);
Brian Paul16241622005-11-03 02:26:47 +0000484 if (index >= 0) {
485 GLuint i;
486 for (i = 0; i < 6; i++)
487 paramList->Parameters[index].StateIndexes[i]
488 = (enum state_index) stateTokens[i];
489 }
Michal Krol2861e732004-03-29 11:09:34 +0000490
Brian Paul16241622005-11-03 02:26:47 +0000491 return index;
Michal Krol2861e732004-03-29 11:09:34 +0000492}
493
494
495/**
496 * Lookup a parameter value by name in the given parameter list.
497 * \return pointer to the float[4] values.
498 */
499GLfloat *
500_mesa_lookup_parameter_value(struct program_parameter_list *paramList,
501 GLsizei nameLen, const char *name)
502{
503 GLuint i;
504
505 if (!paramList)
506 return NULL;
507
508 if (nameLen == -1) {
509 /* name is null-terminated */
510 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000511 if (paramList->Parameters[i].Name &&
512 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000513 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000514 }
515 }
516 else {
517 /* name is not null-terminated, use nameLen */
518 for (i = 0; i < paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000519 if (paramList->Parameters[i].Name &&
520 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000521 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
Keith Whitwell7c26b612005-04-21 14:46:57 +0000522 return paramList->ParameterValues[i];
Michal Krol2861e732004-03-29 11:09:34 +0000523 }
524 }
525 return NULL;
526}
527
528
529/**
530 * Lookup a parameter index by name in the given parameter list.
531 * \return index of parameter in the list.
532 */
533GLint
534_mesa_lookup_parameter_index(struct program_parameter_list *paramList,
535 GLsizei nameLen, const char *name)
536{
537 GLint i;
538
539 if (!paramList)
540 return -1;
541
542 if (nameLen == -1) {
543 /* name is null-terminated */
544 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000545 if (paramList->Parameters[i].Name &&
546 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
Michal Krol2861e732004-03-29 11:09:34 +0000547 return i;
548 }
549 }
550 else {
551 /* name is not null-terminated, use nameLen */
552 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
Keith Whitwellf29f2fc2005-05-10 13:56:23 +0000553 if (paramList->Parameters[i].Name &&
554 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
Michal Krol2861e732004-03-29 11:09:34 +0000555 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
556 return i;
557 }
558 }
559 return -1;
560}
561
562
563/**
564 * Use the list of tokens in the state[] array to find global GL state
565 * and return it in <value>. Usually, four values are returned in <value>
566 * but matrix queries may return as many as 16 values.
567 * This function is used for ARB vertex/fragment programs.
568 * The program parser will produce the state[] values.
569 */
570static void
571_mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
572 GLfloat *value)
573{
574 switch (state[0]) {
575 case STATE_MATERIAL:
576 {
577 /* state[1] is either 0=front or 1=back side */
578 const GLuint face = (GLuint) state[1];
579 /* state[2] is the material attribute */
580 switch (state[2]) {
581 case STATE_AMBIENT:
582 if (face == 0)
583 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]);
584 else
585 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]);
586 return;
587 case STATE_DIFFUSE:
588 if (face == 0)
589 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]);
590 else
591 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]);
592 return;
593 case STATE_SPECULAR:
594 if (face == 0)
595 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]);
596 else
597 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]);
598 return;
599 case STATE_EMISSION:
600 if (face == 0)
601 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]);
602 else
603 COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]);
604 return;
605 case STATE_SHININESS:
606 if (face == 0)
607 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
608 else
609 value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
610 value[1] = 0.0F;
611 value[2] = 0.0F;
612 value[3] = 1.0F;
613 return;
614 default:
615 _mesa_problem(ctx, "Invalid material state in fetch_state");
616 return;
617 }
Alan Hourihane22ae6332004-12-02 13:29:40 +0000618 }
Michal Krol2861e732004-03-29 11:09:34 +0000619 case STATE_LIGHT:
620 {
621 /* state[1] is the light number */
622 const GLuint ln = (GLuint) state[1];
623 /* state[2] is the light attribute */
624 switch (state[2]) {
625 case STATE_AMBIENT:
626 COPY_4V(value, ctx->Light.Light[ln].Ambient);
627 return;
628 case STATE_DIFFUSE:
629 COPY_4V(value, ctx->Light.Light[ln].Diffuse);
630 return;
631 case STATE_SPECULAR:
632 COPY_4V(value, ctx->Light.Light[ln].Specular);
633 return;
634 case STATE_POSITION:
635 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
636 return;
637 case STATE_ATTENUATION:
638 value[0] = ctx->Light.Light[ln].ConstantAttenuation;
639 value[1] = ctx->Light.Light[ln].LinearAttenuation;
640 value[2] = ctx->Light.Light[ln].QuadraticAttenuation;
641 value[3] = ctx->Light.Light[ln].SpotExponent;
642 return;
643 case STATE_SPOT_DIRECTION:
Brian Paul52bf0052005-04-20 23:47:03 +0000644 COPY_3V(value, ctx->Light.Light[ln].EyeDirection);
645 value[3] = ctx->Light.Light[ln]._CosCutoff;
Michal Krol2861e732004-03-29 11:09:34 +0000646 return;
647 case STATE_HALF:
648 {
649 GLfloat eye_z[] = {0, 0, 1};
650
651 /* Compute infinite half angle vector:
652 * half-vector = light_position + (0, 0, 1)
653 * and then normalize. w = 0
Keith Whitwell7c26b612005-04-21 14:46:57 +0000654 *
655 * light.EyePosition.w should be 0 for infinite lights.
Michal Krol2861e732004-03-29 11:09:34 +0000656 */
Keith Whitwell7c26b612005-04-21 14:46:57 +0000657 ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition);
658 NORMALIZE_3FV(value);
659 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000660 }
661 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000662 case STATE_POSITION_NORMALIZED:
663 COPY_4V(value, ctx->Light.Light[ln].EyePosition);
664 NORMALIZE_3FV( value );
665 return;
Michal Krol2861e732004-03-29 11:09:34 +0000666 default:
667 _mesa_problem(ctx, "Invalid light state in fetch_state");
668 return;
669 }
670 }
Michal Krol2861e732004-03-29 11:09:34 +0000671 case STATE_LIGHTMODEL_AMBIENT:
672 COPY_4V(value, ctx->Light.Model.Ambient);
673 return;
674 case STATE_LIGHTMODEL_SCENECOLOR:
675 if (state[1] == 0) {
676 /* front */
677 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000678 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000679 value[i] = ctx->Light.Model.Ambient[i]
680 * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i]
681 + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i];
682 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000683 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000684 }
685 else {
686 /* back */
687 GLint i;
Keith Whitwell78803b22005-04-15 12:57:23 +0000688 for (i = 0; i < 3; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000689 value[i] = ctx->Light.Model.Ambient[i]
690 * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i]
691 + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i];
692 }
Keith Whitwell78803b22005-04-15 12:57:23 +0000693 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
Michal Krol2861e732004-03-29 11:09:34 +0000694 }
695 return;
696 case STATE_LIGHTPROD:
697 {
698 const GLuint ln = (GLuint) state[1];
699 const GLuint face = (GLuint) state[2];
700 GLint i;
701 ASSERT(face == 0 || face == 1);
702 switch (state[3]) {
703 case STATE_AMBIENT:
704 for (i = 0; i < 3; i++) {
705 value[i] = ctx->Light.Light[ln].Ambient[i] *
706 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i];
707 }
708 /* [3] = material alpha */
709 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
710 return;
711 case STATE_DIFFUSE:
712 for (i = 0; i < 3; i++) {
713 value[i] = ctx->Light.Light[ln].Diffuse[i] *
714 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i];
715 }
716 /* [3] = material alpha */
717 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
718 return;
719 case STATE_SPECULAR:
720 for (i = 0; i < 3; i++) {
721 value[i] = ctx->Light.Light[ln].Specular[i] *
722 ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i];
723 }
724 /* [3] = material alpha */
725 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
726 return;
727 default:
728 _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
729 return;
730 }
731 }
Michal Krol2861e732004-03-29 11:09:34 +0000732 case STATE_TEXGEN:
733 {
734 /* state[1] is the texture unit */
735 const GLuint unit = (GLuint) state[1];
736 /* state[2] is the texgen attribute */
737 switch (state[2]) {
738 case STATE_TEXGEN_EYE_S:
739 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS);
740 return;
741 case STATE_TEXGEN_EYE_T:
742 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT);
743 return;
744 case STATE_TEXGEN_EYE_R:
745 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR);
746 return;
747 case STATE_TEXGEN_EYE_Q:
748 COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ);
749 return;
750 case STATE_TEXGEN_OBJECT_S:
751 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS);
752 return;
753 case STATE_TEXGEN_OBJECT_T:
754 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT);
755 return;
756 case STATE_TEXGEN_OBJECT_R:
757 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR);
758 return;
759 case STATE_TEXGEN_OBJECT_Q:
760 COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ);
761 return;
762 default:
763 _mesa_problem(ctx, "Invalid texgen state in fetch_state");
764 return;
765 }
766 }
Michal Krol2861e732004-03-29 11:09:34 +0000767 case STATE_TEXENV_COLOR:
768 {
769 /* state[1] is the texture unit */
770 const GLuint unit = (GLuint) state[1];
771 COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
772 }
773 return;
774 case STATE_FOG_COLOR:
775 COPY_4V(value, ctx->Fog.Color);
776 return;
777 case STATE_FOG_PARAMS:
778 value[0] = ctx->Fog.Density;
779 value[1] = ctx->Fog.Start;
780 value[2] = ctx->Fog.End;
781 value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
782 return;
783 case STATE_CLIPPLANE:
784 {
785 const GLuint plane = (GLuint) state[1];
786 COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
787 }
788 return;
789 case STATE_POINT_SIZE:
790 value[0] = ctx->Point.Size;
791 value[1] = ctx->Point.MinSize;
792 value[2] = ctx->Point.MaxSize;
793 value[3] = ctx->Point.Threshold;
794 return;
795 case STATE_POINT_ATTENUATION:
796 value[0] = ctx->Point.Params[0];
797 value[1] = ctx->Point.Params[1];
798 value[2] = ctx->Point.Params[2];
799 value[3] = 1.0F;
800 return;
801 case STATE_MATRIX:
802 {
803 /* state[1] = modelview, projection, texture, etc. */
804 /* state[2] = which texture matrix or program matrix */
805 /* state[3] = first column to fetch */
806 /* state[4] = last column to fetch */
807 /* state[5] = transpose, inverse or invtrans */
808
809 const GLmatrix *matrix;
810 const enum state_index mat = state[1];
811 const GLuint index = (GLuint) state[2];
812 const GLuint first = (GLuint) state[3];
813 const GLuint last = (GLuint) state[4];
814 const enum state_index modifier = state[5];
815 const GLfloat *m;
816 GLuint row, i;
817 if (mat == STATE_MODELVIEW) {
818 matrix = ctx->ModelviewMatrixStack.Top;
819 }
820 else if (mat == STATE_PROJECTION) {
821 matrix = ctx->ProjectionMatrixStack.Top;
822 }
823 else if (mat == STATE_MVP) {
824 matrix = &ctx->_ModelProjectMatrix;
825 }
826 else if (mat == STATE_TEXTURE) {
827 matrix = ctx->TextureMatrixStack[index].Top;
828 }
829 else if (mat == STATE_PROGRAM) {
830 matrix = ctx->ProgramMatrixStack[index].Top;
831 }
832 else {
833 _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
834 return;
835 }
836 if (modifier == STATE_MATRIX_INVERSE ||
837 modifier == STATE_MATRIX_INVTRANS) {
Keith Whitwell78803b22005-04-15 12:57:23 +0000838 /* Be sure inverse is up to date:
839 */
Jouk Jansen49b1d952005-04-18 13:05:24 +0000840 _math_matrix_analyse( (GLmatrix*) matrix );
Michal Krol2861e732004-03-29 11:09:34 +0000841 m = matrix->inv;
842 }
843 else {
844 m = matrix->m;
845 }
846 if (modifier == STATE_MATRIX_TRANSPOSE ||
847 modifier == STATE_MATRIX_INVTRANS) {
848 for (i = 0, row = first; row <= last; row++) {
849 value[i++] = m[row * 4 + 0];
850 value[i++] = m[row * 4 + 1];
851 value[i++] = m[row * 4 + 2];
852 value[i++] = m[row * 4 + 3];
853 }
854 }
855 else {
856 for (i = 0, row = first; row <= last; row++) {
857 value[i++] = m[row + 0];
858 value[i++] = m[row + 4];
859 value[i++] = m[row + 8];
860 value[i++] = m[row + 12];
861 }
862 }
863 }
864 return;
865 case STATE_DEPTH_RANGE:
Brian Paul824fdf02004-06-29 00:00:06 +0000866 value[0] = ctx->Viewport.Near; /* near */
867 value[1] = ctx->Viewport.Far; /* far */
868 value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */
869 value[3] = 0;
Michal Krol2861e732004-03-29 11:09:34 +0000870 return;
871 case STATE_FRAGMENT_PROGRAM:
872 {
873 /* state[1] = {STATE_ENV, STATE_LOCAL} */
874 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000875 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000876 switch (state[1]) {
877 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000878 COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000879 break;
Michal Krol2861e732004-03-29 11:09:34 +0000880 case STATE_LOCAL:
881 COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000882 break;
Michal Krol2861e732004-03-29 11:09:34 +0000883 default:
884 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
885 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000886 }
887 }
Michal Krol2861e732004-03-29 11:09:34 +0000888 return;
889
890 case STATE_VERTEX_PROGRAM:
Brian Paul824fdf02004-06-29 00:00:06 +0000891 {
Michal Krol2861e732004-03-29 11:09:34 +0000892 /* state[1] = {STATE_ENV, STATE_LOCAL} */
893 /* state[2] = parameter index */
Brian Paul824fdf02004-06-29 00:00:06 +0000894 const int idx = (int) state[2];
Michal Krol2861e732004-03-29 11:09:34 +0000895 switch (state[1]) {
896 case STATE_ENV:
Brian Paul824fdf02004-06-29 00:00:06 +0000897 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
Michal Krol2861e732004-03-29 11:09:34 +0000898 break;
Michal Krol2861e732004-03-29 11:09:34 +0000899 case STATE_LOCAL:
900 COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]);
Brian Paul824fdf02004-06-29 00:00:06 +0000901 break;
Michal Krol2861e732004-03-29 11:09:34 +0000902 default:
903 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
904 return;
Brian Paul824fdf02004-06-29 00:00:06 +0000905 }
906 }
Michal Krol2861e732004-03-29 11:09:34 +0000907 return;
Keith Whitwell7c26b612005-04-21 14:46:57 +0000908
909 case STATE_INTERNAL:
910 {
911 switch (state[1]) {
912 case STATE_NORMAL_SCALE:
913 ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
914 break;
915 default:
916 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
917 return;
918 }
919 }
920 return;
921
Michal Krol2861e732004-03-29 11:09:34 +0000922 default:
Brian Paul824fdf02004-06-29 00:00:06 +0000923 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
Michal Krol2861e732004-03-29 11:09:34 +0000924 return;
925 }
926}
927
928
Brian Paul7b98b402005-11-12 23:25:49 +0000929static void
930append(char *dst, const char *src)
931{
932 while (*dst)
933 dst++;
934 while (*src)
935 *dst++ = *src++;
936 *dst = 0;
937}
938
939static void
940append_token(char *dst, enum state_index k)
941{
942 switch (k) {
943 case STATE_MATERIAL:
944 append(dst, "material.");
945 break;
946 case STATE_LIGHT:
947 append(dst, "light");
948 break;
949 case STATE_LIGHTMODEL_AMBIENT:
950 append(dst, "lightmodel.ambient");
951 break;
952 case STATE_LIGHTMODEL_SCENECOLOR:
953 break;
954 case STATE_LIGHTPROD:
955 append(dst, "lightprod");
956 break;
957 case STATE_TEXGEN:
958 append(dst, "texgen");
959 break;
960 case STATE_FOG_COLOR:
961 append(dst, "fog.color");
962 break;
963 case STATE_FOG_PARAMS:
964 append(dst, "fog.params");
965 break;
966 case STATE_CLIPPLANE:
967 append(dst, "clip");
968 break;
969 case STATE_POINT_SIZE:
970 append(dst, "point.size");
971 break;
972 case STATE_POINT_ATTENUATION:
973 append(dst, "point.attenuation");
974 break;
975 case STATE_MATRIX:
976 append(dst, "matrix.");
977 break;
978 case STATE_MODELVIEW:
979 append(dst, "modelview");
980 break;
981 case STATE_PROJECTION:
982 append(dst, "projection");
983 break;
984 case STATE_MVP:
985 append(dst, "mvp");
986 break;
987 case STATE_TEXTURE:
988 append(dst, "texture");
989 break;
990 case STATE_PROGRAM:
991 append(dst, "program");
992 break;
993 case STATE_MATRIX_INVERSE:
994 append(dst, ".inverse");
995 break;
996 case STATE_MATRIX_TRANSPOSE:
997 append(dst, ".transpose");
998 break;
999 case STATE_MATRIX_INVTRANS:
1000 append(dst, ".invtrans");
1001 break;
1002 case STATE_AMBIENT:
1003 append(dst, "ambient");
1004 break;
1005 case STATE_DIFFUSE:
1006 append(dst, "diffuse");
1007 break;
1008 case STATE_SPECULAR:
1009 append(dst, "specular");
1010 break;
1011 case STATE_EMISSION:
1012 append(dst, "emission");
1013 break;
1014 case STATE_SHININESS:
1015 append(dst, "shininess");
1016 break;
1017 case STATE_HALF:
1018 append(dst, "half");
1019 break;
1020 case STATE_POSITION:
1021 append(dst, ".position");
1022 break;
1023 case STATE_ATTENUATION:
1024 append(dst, ".attenuation");
1025 break;
1026 case STATE_SPOT_DIRECTION:
1027 append(dst, ".spot.direction");
1028 break;
1029 case STATE_TEXGEN_EYE_S:
1030 append(dst, "eye.s");
1031 break;
1032 case STATE_TEXGEN_EYE_T:
1033 append(dst, "eye.t");
1034 break;
1035 case STATE_TEXGEN_EYE_R:
1036 append(dst, "eye.r");
1037 break;
1038 case STATE_TEXGEN_EYE_Q:
1039 append(dst, "eye.q");
1040 break;
1041 case STATE_TEXGEN_OBJECT_S:
1042 append(dst, "object.s");
1043 break;
1044 case STATE_TEXGEN_OBJECT_T:
1045 append(dst, "object.t");
1046 break;
1047 case STATE_TEXGEN_OBJECT_R:
1048 append(dst, "object.r");
1049 break;
1050 case STATE_TEXGEN_OBJECT_Q:
1051 append(dst, "object.q");
1052 break;
1053 case STATE_TEXENV_COLOR:
1054 append(dst, "texenv");
1055 break;
1056 case STATE_DEPTH_RANGE:
1057 append(dst, "depth.range");
1058 break;
1059 case STATE_VERTEX_PROGRAM:
1060 case STATE_FRAGMENT_PROGRAM:
1061 break;
1062 case STATE_ENV:
1063 append(dst, "env");
1064 break;
1065 case STATE_LOCAL:
1066 append(dst, "local");
1067 break;
1068 case STATE_INTERNAL:
1069 case STATE_NORMAL_SCALE:
1070 case STATE_POSITION_NORMALIZED:
1071 append(dst, "(internal)");
1072 break;
1073 default:
1074 ;
1075 }
1076}
1077
1078static void
1079append_face(char *dst, GLint face)
1080{
1081 if (face == 0)
1082 append(dst, "front.");
1083 else
1084 append(dst, "back.");
1085}
1086
1087static void
1088append_index(char *dst, GLint index)
1089{
1090 char s[20];
1091 _mesa_sprintf(s, "[%d].", index);
1092 append(dst, s);
1093}
1094
1095/**
1096 * Make a string from the given state vector.
1097 * For example, return "state.matrix.texture[2].inverse".
1098 */
1099static const char *
1100make_state_string(const GLint state[6])
1101{
1102 char str[1000] = "";
1103 char tmp[30];
1104
1105 append(str, "state.");
1106 append_token(str, state[0]);
1107
1108 switch (state[0]) {
1109 case STATE_MATERIAL:
1110 append_face(str, state[1]);
1111 append_token(str, state[2]);
1112 break;
1113 case STATE_LIGHT:
1114 append(str, "light");
1115 append_index(str, state[1]); /* light number [i]. */
1116 append_token(str, state[2]); /* coefficients */
1117 break;
1118 case STATE_LIGHTMODEL_AMBIENT:
1119 append(str, "lightmodel.ambient");
1120 break;
1121 case STATE_LIGHTMODEL_SCENECOLOR:
1122 if (state[1] == 0) {
1123 append(str, "lightmodel.front.scenecolor");
1124 }
1125 else {
1126 append(str, "lightmodel.back.scenecolor");
1127 }
1128 break;
1129 case STATE_LIGHTPROD:
1130 append_index(str, state[1]); /* light number [i]. */
1131 append_face(str, state[2]);
1132 append_token(str, state[3]);
1133 break;
1134 case STATE_TEXGEN:
1135 append_index(str, state[1]); /* tex unit [i] */
1136 append_token(str, state[2]); /* plane coef */
1137 break;
1138 case STATE_TEXENV_COLOR:
1139 append_index(str, state[1]); /* tex unit [i] */
1140 append(str, "color");
1141 break;
1142 case STATE_FOG_COLOR:
1143 case STATE_FOG_PARAMS:
1144 break;
1145 case STATE_CLIPPLANE:
1146 append_index(str, state[1]); /* plane [i] */
1147 append(str, "plane");
1148 break;
1149 case STATE_POINT_SIZE:
1150 case STATE_POINT_ATTENUATION:
1151 break;
1152 case STATE_MATRIX:
1153 {
1154 /* state[1] = modelview, projection, texture, etc. */
1155 /* state[2] = which texture matrix or program matrix */
1156 /* state[3] = first column to fetch */
1157 /* state[4] = last column to fetch */
1158 /* state[5] = transpose, inverse or invtrans */
1159 const enum state_index mat = state[1];
1160 const GLuint index = (GLuint) state[2];
1161 const GLuint first = (GLuint) state[3];
1162 const GLuint last = (GLuint) state[4];
1163 const enum state_index modifier = state[5];
1164 append_token(str, mat);
1165 if (index)
1166 append_index(str, index);
1167 if (modifier)
1168 append_token(str, modifier);
1169 if (first == last)
1170 _mesa_sprintf(tmp, ".row[%d]", first);
1171 else
1172 _mesa_sprintf(tmp, ".row[%d..%d]", first, last);
1173 append(str, tmp);
1174 }
1175 break;
1176 case STATE_DEPTH_RANGE:
1177 break;
1178 case STATE_FRAGMENT_PROGRAM:
1179 case STATE_VERTEX_PROGRAM:
1180 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1181 /* state[2] = parameter index */
1182 append_token(str, state[1]);
1183 append_index(str, state[2]);
1184 break;
1185 case STATE_INTERNAL:
1186 break;
1187 default:
1188 _mesa_problem(NULL, "Invalid state in maka_state_string");
1189 break;
1190 }
1191
1192 return _mesa_strdup(str);
1193}
1194
1195
Michal Krol2861e732004-03-29 11:09:34 +00001196/**
1197 * Loop over all the parameters in a parameter list. If the parameter
1198 * is a GL state reference, look up the current value of that state
1199 * variable and put it into the parameter's Value[4] array.
1200 * This would be called at glBegin time when using a fragment program.
1201 */
1202void
1203_mesa_load_state_parameters(GLcontext *ctx,
1204 struct program_parameter_list *paramList)
1205{
1206 GLuint i;
1207
1208 if (!paramList)
1209 return;
1210
1211 for (i = 0; i < paramList->NumParameters; i++) {
Brian Paul613e1ad2005-11-05 02:15:21 +00001212 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00001213 _mesa_fetch_state(ctx,
1214 paramList->Parameters[i].StateIndexes,
1215 paramList->ParameterValues[i]);
Michal Krol2861e732004-03-29 11:09:34 +00001216 }
1217 }
1218}
1219
1220
Brian Paul1fcdaf12005-11-05 19:12:36 +00001221/**
1222 * Basic info about each instruction
1223 */
1224struct instruction_info
1225{
1226 enum prog_opcode Opcode;
1227 const char *Name;
1228 GLuint NumSrcRegs;
1229};
1230
1231/**
1232 * Instruction info
1233 * \note Opcode should equal array index!
1234 */
1235static const struct instruction_info InstInfo[MAX_OPCODE] = {
1236 { OPCODE_ABS, "ABS", 1 },
1237 { OPCODE_ADD, "ADD", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001238 { OPCODE_ARA, "ARA", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001239 { OPCODE_ARL, "ARL", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001240 { OPCODE_ARL_NV, "ARL", 1 },
1241 { OPCODE_ARR, "ARL", 1 },
1242 { OPCODE_BRA, "BRA", 1 },
1243 { OPCODE_CAL, "CAL", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001244 { OPCODE_CMP, "CMP", 3 },
1245 { OPCODE_COS, "COS", 1 },
1246 { OPCODE_DDX, "DDX", 1 },
1247 { OPCODE_DDY, "DDY", 1 },
1248 { OPCODE_DP3, "DP3", 2 },
1249 { OPCODE_DP4, "DP4", 2 },
1250 { OPCODE_DPH, "DPH", 2 },
1251 { OPCODE_DST, "DST", 2 },
1252 { OPCODE_END, "END", 0 },
1253 { OPCODE_EX2, "EX2", 1 },
1254 { OPCODE_EXP, "EXP", 1 },
1255 { OPCODE_FLR, "FLR", 1 },
1256 { OPCODE_FRC, "FRC", 1 },
1257 { OPCODE_KIL, "KIL", 1 },
1258 { OPCODE_KIL_NV, "KIL", 0 },
1259 { OPCODE_LG2, "LG2", 1 },
1260 { OPCODE_LIT, "LIT", 1 },
1261 { OPCODE_LOG, "LOG", 1 },
1262 { OPCODE_LRP, "LRP", 3 },
1263 { OPCODE_MAD, "MAD", 3 },
1264 { OPCODE_MAX, "MAX", 2 },
1265 { OPCODE_MIN, "MIN", 2 },
1266 { OPCODE_MOV, "MOV", 1 },
1267 { OPCODE_MUL, "MUL", 2 },
1268 { OPCODE_PK2H, "PK2H", 1 },
1269 { OPCODE_PK2US, "PK2US", 1 },
1270 { OPCODE_PK4B, "PK4B", 1 },
1271 { OPCODE_PK4UB, "PK4UB", 1 },
1272 { OPCODE_POW, "POW", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001273 { OPCODE_POPA, "POPA", 0 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001274 { OPCODE_PRINT, "PRINT", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001275 { OPCODE_PUSHA, "PUSHA", 0 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001276 { OPCODE_RCC, "RCC", 1 },
1277 { OPCODE_RCP, "RCP", 1 },
Ian Romanick4884db62005-11-08 22:40:26 +00001278 { OPCODE_RET, "RET", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001279 { OPCODE_RFL, "RFL", 1 },
1280 { OPCODE_RSQ, "RSQ", 1 },
1281 { OPCODE_SCS, "SCS", 1 },
1282 { OPCODE_SEQ, "SEQ", 2 },
1283 { OPCODE_SFL, "SFL", 0 },
1284 { OPCODE_SGE, "SGE", 2 },
1285 { OPCODE_SGT, "SGT", 2 },
1286 { OPCODE_SIN, "SIN", 1 },
1287 { OPCODE_SLE, "SLE", 2 },
1288 { OPCODE_SLT, "SLT", 2 },
1289 { OPCODE_SNE, "SNE", 2 },
Ian Romanick4884db62005-11-08 22:40:26 +00001290 { OPCODE_SSG, "SSG", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001291 { OPCODE_STR, "STR", 0 },
1292 { OPCODE_SUB, "SUB", 2 },
1293 { OPCODE_SWZ, "SWZ", 1 },
1294 { OPCODE_TEX, "TEX", 1 },
1295 { OPCODE_TXB, "TXB", 1 },
1296 { OPCODE_TXD, "TXD", 3 },
Ian Romanick4884db62005-11-08 22:40:26 +00001297 { OPCODE_TXL, "TXL", 1 },
Brian Paul1fcdaf12005-11-05 19:12:36 +00001298 { OPCODE_TXP, "TXP", 1 },
1299 { OPCODE_TXP_NV, "TXP", 1 },
1300 { OPCODE_UP2H, "UP2H", 1 },
1301 { OPCODE_UP2US, "UP2US", 1 },
1302 { OPCODE_UP4B, "UP4B", 1 },
1303 { OPCODE_UP4UB, "UP4UB", 1 },
1304 { OPCODE_X2D, "X2D", 3 },
1305 { OPCODE_XPD, "XPD", 2 }
1306};
1307
1308
1309/**
1310 * Return the number of src registers for the given instruction/opcode.
1311 */
1312GLuint
1313_mesa_num_inst_src_regs(enum prog_opcode opcode)
1314{
1315 GLuint i;
1316#ifdef DEBUG
1317 for (i = 0; i < MAX_OPCODE; i++) {
1318 ASSERT(i == InstInfo[i].Opcode);
1319 }
1320#endif
1321 for (i = 0; i < MAX_OPCODE; i++) {
1322 if (InstInfo[i].Opcode == opcode) {
1323 return InstInfo[i].NumSrcRegs;
1324 }
1325 }
1326 _mesa_problem(NULL, "invalid opcode in _mesa_num_inst_src_regs");
1327 return 0;
1328}
1329
1330
1331/**
1332 * Return string name for given program opcode.
1333 */
1334const char *
1335_mesa_opcode_string(enum prog_opcode opcode)
1336{
1337 ASSERT(opcode < MAX_OPCODE);
1338 return InstInfo[opcode].Name;
1339}
1340
Brian Paulbf41bc02005-11-05 19:32:36 +00001341/**
1342 * Return string name for given program/register file.
1343 */
Brian Paul30d6a4b2005-11-05 20:18:18 +00001344static const char *
1345program_file_string(enum register_file f)
Brian Paulbf41bc02005-11-05 19:32:36 +00001346{
1347 switch (f) {
1348 case PROGRAM_TEMPORARY:
1349 return "TEMP";
1350 case PROGRAM_LOCAL_PARAM:
1351 return "LOCAL";
1352 case PROGRAM_ENV_PARAM:
1353 return "ENV";
1354 case PROGRAM_STATE_VAR:
1355 return "STATE";
1356 case PROGRAM_INPUT:
1357 return "INPUT";
1358 case PROGRAM_OUTPUT:
1359 return "OUTPUT";
1360 case PROGRAM_NAMED_PARAM:
1361 return "NAMED";
1362 case PROGRAM_CONSTANT:
1363 return "CONST";
1364 case PROGRAM_WRITE_ONLY:
1365 return "WRITE_ONLY";
1366 case PROGRAM_ADDRESS:
1367 return "ADDR";
1368 default:
1369 return "!unkown!";
1370 }
1371}
1372
Michal Krol2861e732004-03-29 11:09:34 +00001373
Brian Paul30d6a4b2005-11-05 20:18:18 +00001374/**
1375 * Return a string representation of the given swizzle word.
Brian Paul7b98b402005-11-12 23:25:49 +00001376 * If extended is true, use extended (comma-separated) format.
Brian Paul30d6a4b2005-11-05 20:18:18 +00001377 */
1378static const char *
Brian Paul7b98b402005-11-12 23:25:49 +00001379swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001380{
1381 static const char swz[] = "xyzw01";
1382 static char s[20];
1383 GLuint i = 0;
1384
Brian Paul7b98b402005-11-12 23:25:49 +00001385 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001386 return ""; /* no swizzle/negation */
1387
Brian Paul7b98b402005-11-12 23:25:49 +00001388 if (!extended)
1389 s[i++] = '.';
Brian Paul30d6a4b2005-11-05 20:18:18 +00001390
1391 if (negateBase & 0x1)
1392 s[i++] = '-';
1393 s[i++] = swz[GET_SWZ(swizzle, 0)];
1394
Brian Paul7b98b402005-11-12 23:25:49 +00001395 if (extended) {
1396 s[i++] = ',';
1397 }
1398
Brian Paul30d6a4b2005-11-05 20:18:18 +00001399 if (negateBase & 0x2)
1400 s[i++] = '-';
1401 s[i++] = swz[GET_SWZ(swizzle, 1)];
1402
Brian Paul7b98b402005-11-12 23:25:49 +00001403 if (extended) {
1404 s[i++] = ',';
1405 }
1406
Brian Paul30d6a4b2005-11-05 20:18:18 +00001407 if (negateBase & 0x4)
1408 s[i++] = '-';
1409 s[i++] = swz[GET_SWZ(swizzle, 2)];
1410
Brian Paul7b98b402005-11-12 23:25:49 +00001411 if (extended) {
1412 s[i++] = ',';
1413 }
1414
Brian Paul30d6a4b2005-11-05 20:18:18 +00001415 if (negateBase & 0x8)
1416 s[i++] = '-';
1417 s[i++] = swz[GET_SWZ(swizzle, 3)];
1418
1419 s[i] = 0;
1420 return s;
1421}
1422
1423
1424static const char *
1425writemask_string(GLuint writeMask)
1426{
1427 static char s[10];
1428 GLuint i = 0;
1429
1430 if (writeMask == WRITEMASK_XYZW)
1431 return "";
1432
1433 s[i++] = '.';
1434 if (writeMask & WRITEMASK_X)
1435 s[i++] = 'x';
1436 if (writeMask & WRITEMASK_Y)
1437 s[i++] = 'y';
1438 if (writeMask & WRITEMASK_Z)
1439 s[i++] = 'z';
1440 if (writeMask & WRITEMASK_W)
1441 s[i++] = 'w';
1442
1443 s[i] = 0;
1444 return s;
1445}
1446
Brian Paul7b98b402005-11-12 23:25:49 +00001447static void
1448print_dst_reg(const struct prog_dst_register *dstReg)
1449{
1450 _mesa_printf(" %s[%d]%s",
1451 program_file_string(dstReg->File),
1452 dstReg->Index,
1453 writemask_string(dstReg->WriteMask));
1454}
1455
1456static void
1457print_src_reg(const struct prog_src_register *srcReg)
1458{
1459 _mesa_printf("%s[%d]%s",
1460 program_file_string(srcReg->File),
1461 srcReg->Index,
1462 swizzle_string(srcReg->Swizzle,
1463 srcReg->NegateBase, GL_FALSE));
1464}
1465
Brian Paul30d6a4b2005-11-05 20:18:18 +00001466
1467/**
Brian Paulde997602005-11-12 17:53:14 +00001468 * Print a single vertex/fragment program instruction.
Brian Paul30d6a4b2005-11-05 20:18:18 +00001469 */
1470void
Brian Paulde997602005-11-12 17:53:14 +00001471_mesa_print_instruction(const struct prog_instruction *inst)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001472{
Brian Paulde997602005-11-12 17:53:14 +00001473 switch (inst->Opcode) {
1474 case OPCODE_PRINT:
1475 _mesa_printf("PRINT '%s'", inst->Data);
1476 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
1477 _mesa_printf(", ");
1478 _mesa_printf("%s[%d]%s",
1479 program_file_string(inst->SrcReg[0].File),
1480 inst->SrcReg[0].Index,
1481 swizzle_string(inst->SrcReg[0].Swizzle,
Brian Paul7b98b402005-11-12 23:25:49 +00001482 inst->SrcReg[0].NegateBase, GL_FALSE));
Brian Paulde997602005-11-12 17:53:14 +00001483 }
1484 _mesa_printf(";\n");
1485 break;
Brian Paul7b98b402005-11-12 23:25:49 +00001486 case OPCODE_SWZ:
1487 _mesa_printf("SWZ");
1488 if (inst->Saturate)
1489 _mesa_printf("_SAT");
1490 print_dst_reg(&inst->DstReg);
1491 _mesa_printf("%s[%d], %s;\n",
1492 program_file_string(inst->SrcReg[0].File),
1493 inst->SrcReg[0].Index,
1494 swizzle_string(inst->SrcReg[0].Swizzle,
1495 inst->SrcReg[0].NegateBase, GL_TRUE));
1496 break;
1497 case OPCODE_TEX:
1498 case OPCODE_TXP:
1499 case OPCODE_TXB:
1500 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
1501 if (inst->Saturate)
1502 _mesa_printf("_SAT");
1503 _mesa_printf(" ");
1504 print_dst_reg(&inst->DstReg);
1505 _mesa_printf(", ");
1506 print_src_reg(&inst->SrcReg[0]);
1507 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
1508 switch (inst->TexSrcTarget) {
1509 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
1510 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
1511 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
1512 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
1513 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
1514 default:
1515 ;
1516 }
1517 _mesa_printf("\n");
1518 break;
1519 case OPCODE_ARL:
1520 _mesa_printf("ARL addr.x, ");
1521 print_src_reg(&inst->SrcReg[0]);
1522 _mesa_printf(";\n");
1523 break;
1524 /* XXX may need for other special-case instructions */
Brian Paulde997602005-11-12 17:53:14 +00001525 default:
1526 /* typical alu instruction */
1527 {
1528 const GLuint numRegs = _mesa_num_inst_src_regs(inst->Opcode);
1529 GLuint j;
Brian Paul30d6a4b2005-11-05 20:18:18 +00001530
Brian Paulde997602005-11-12 17:53:14 +00001531 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
Brian Paul30d6a4b2005-11-05 20:18:18 +00001532
Brian Paulde997602005-11-12 17:53:14 +00001533 /* frag prog only */
1534 if (inst->Saturate)
1535 _mesa_printf("_SAT");
1536
1537 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
1538 _mesa_printf(" %s[%d]%s",
1539 program_file_string(inst->DstReg.File),
1540 inst->DstReg.Index,
1541 writemask_string(inst->DstReg.WriteMask));
1542 }
1543
1544 if (numRegs > 0)
Brian Paul02df9e12005-11-08 14:42:52 +00001545 _mesa_printf(", ");
Brian Paulde997602005-11-12 17:53:14 +00001546
1547 for (j = 0; j < numRegs; j++) {
Brian Paul7b98b402005-11-12 23:25:49 +00001548 print_src_reg(inst->SrcReg + j);
Brian Paulde997602005-11-12 17:53:14 +00001549 if (j + 1 < numRegs)
Brian Paul30d6a4b2005-11-05 20:18:18 +00001550 _mesa_printf(", ");
Brian Paul30d6a4b2005-11-05 20:18:18 +00001551 }
Brian Paulde997602005-11-12 17:53:14 +00001552
1553 _mesa_printf(";\n");
Brian Paul30d6a4b2005-11-05 20:18:18 +00001554 }
1555 }
1556}
1557
1558
Brian Paulde997602005-11-12 17:53:14 +00001559/**
1560 * Print a vertx/fragment program to stdout.
1561 * XXX this function could be greatly improved.
1562 */
1563void
1564_mesa_print_program(const struct program *prog)
1565{
1566 GLuint i;
1567 for (i = 0; i < prog->NumInstructions; i++) {
1568 _mesa_printf("%3d: ", i);
1569 _mesa_print_instruction(prog->Instructions + i);
1570 }
1571}
1572
1573
1574/**
1575 * Print all of a program's parameters.
1576 */
1577void
1578_mesa_print_program_parameters(GLcontext *ctx, const struct program *prog)
1579{
1580 GLint i;
1581
1582 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
1583 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
1584 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
1585 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
1586 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
1587
1588 _mesa_load_state_parameters(ctx, prog->Parameters);
1589
1590#if 0
1591 _mesa_printf("Local Params:\n");
1592 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
1593 const GLfloat *p = prog->LocalParams[i];
1594 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
1595 }
1596#endif
1597
1598 for (i = 0; i < prog->Parameters->NumParameters; i++){
Brian Paul7b98b402005-11-12 23:25:49 +00001599 struct program_parameter *param = prog->Parameters->Parameters + i;
1600 const GLfloat *v = prog->Parameters->ParameterValues[i];
1601 _mesa_printf("param[%d] %s = {%.3f, %.3f, %.3f, %.3f};\n",
1602 i, param->Name, v[0], v[1], v[2], v[3]);
Brian Paulde997602005-11-12 17:53:14 +00001603 }
1604}
1605
Brian Paul30d6a4b2005-11-05 20:18:18 +00001606
1607
Michal Krol2861e732004-03-29 11:09:34 +00001608/**********************************************************************/
1609/* API functions */
1610/**********************************************************************/
1611
1612
1613/**
1614 * Bind a program (make it current)
1615 * \note Called from the GL API dispatcher by both glBindProgramNV
1616 * and glBindProgramARB.
1617 */
1618void GLAPIENTRY
1619_mesa_BindProgram(GLenum target, GLuint id)
1620{
1621 struct program *prog;
1622 GET_CURRENT_CONTEXT(ctx);
1623 ASSERT_OUTSIDE_BEGIN_END(ctx);
1624
1625 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1626
1627 if ((target == GL_VERTEX_PROGRAM_NV
1628 && ctx->Extensions.NV_vertex_program) ||
1629 (target == GL_VERTEX_PROGRAM_ARB
1630 && ctx->Extensions.ARB_vertex_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001631 /*** Vertex program binding ***/
1632 struct vertex_program *curProg = ctx->VertexProgram.Current;
1633 if (curProg->Base.Id == id) {
1634 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001635 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001636 }
1637 if (curProg->Base.Id != 0) {
1638 /* decrement refcount on previously bound vertex program */
1639 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001640 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001641 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001642 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001643 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001644 }
1645 }
1646 }
1647 else if ((target == GL_FRAGMENT_PROGRAM_NV
1648 && ctx->Extensions.NV_fragment_program) ||
1649 (target == GL_FRAGMENT_PROGRAM_ARB
1650 && ctx->Extensions.ARB_fragment_program)) {
Brian Paul765f1a12004-09-14 22:28:27 +00001651 /*** Fragment program binding ***/
1652 struct fragment_program *curProg = ctx->FragmentProgram.Current;
1653 if (curProg->Base.Id == id) {
1654 /* binding same program - no change */
Michal Krol2861e732004-03-29 11:09:34 +00001655 return;
Brian Paul765f1a12004-09-14 22:28:27 +00001656 }
1657 if (curProg->Base.Id != 0) {
1658 /* decrement refcount on previously bound fragment program */
1659 curProg->Base.RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001660 /* and delete if refcount goes below one */
Brian Paul765f1a12004-09-14 22:28:27 +00001661 if (curProg->Base.RefCount <= 0) {
Brian Paulea2943e2005-01-20 04:02:02 +00001662 /* the program ID was already removed from the hash table */
Brian Paul765f1a12004-09-14 22:28:27 +00001663 ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
Michal Krol2861e732004-03-29 11:09:34 +00001664 }
1665 }
1666 }
1667 else {
1668 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
1669 return;
1670 }
1671
1672 /* NOTE: binding to a non-existant program is not an error.
1673 * That's supposed to be caught in glBegin.
1674 */
1675 if (id == 0) {
Brian Paul765f1a12004-09-14 22:28:27 +00001676 /* Bind default program */
Michal Krol2861e732004-03-29 11:09:34 +00001677 prog = NULL;
1678 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
1679 prog = ctx->Shared->DefaultVertexProgram;
1680 else
1681 prog = ctx->Shared->DefaultFragmentProgram;
1682 }
1683 else {
Brian Paul765f1a12004-09-14 22:28:27 +00001684 /* Bind user program */
Michal Krol2861e732004-03-29 11:09:34 +00001685 prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
Brian Paul9ca83922004-10-02 15:16:59 +00001686 if (!prog || prog == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +00001687 /* allocate a new program now */
1688 prog = ctx->Driver.NewProgram(ctx, target, id);
1689 if (!prog) {
1690 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
1691 return;
1692 }
Michal Krol2861e732004-03-29 11:09:34 +00001693 _mesa_HashInsert(ctx->Shared->Programs, id, prog);
1694 }
Brian Paul765f1a12004-09-14 22:28:27 +00001695 else if (prog->Target != target) {
1696 _mesa_error(ctx, GL_INVALID_OPERATION,
1697 "glBindProgramNV/ARB(target mismatch)");
1698 return;
1699 }
Michal Krol2861e732004-03-29 11:09:34 +00001700 }
1701
1702 /* bind now */
1703 if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
1704 ctx->VertexProgram.Current = (struct vertex_program *) prog;
1705 }
1706 else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
1707 ctx->FragmentProgram.Current = (struct fragment_program *) prog;
1708 }
1709
Brian Paul765f1a12004-09-14 22:28:27 +00001710 /* Never null pointers */
1711 ASSERT(ctx->VertexProgram.Current);
1712 ASSERT(ctx->FragmentProgram.Current);
1713
Michal Krol2861e732004-03-29 11:09:34 +00001714 if (prog)
1715 prog->RefCount++;
1716
1717 if (ctx->Driver.BindProgram)
1718 ctx->Driver.BindProgram(ctx, target, prog);
1719}
1720
1721
1722/**
1723 * Delete a list of programs.
1724 * \note Not compiled into display lists.
1725 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
1726 */
1727void GLAPIENTRY
1728_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
1729{
1730 GLint i;
1731 GET_CURRENT_CONTEXT(ctx);
1732 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1733
1734 if (n < 0) {
1735 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
1736 return;
1737 }
1738
1739 for (i = 0; i < n; i++) {
1740 if (ids[i] != 0) {
1741 struct program *prog = (struct program *)
1742 _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +00001743 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +00001744 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1745 }
1746 else if (prog) {
1747 /* Unbind program if necessary */
Michal Krol2861e732004-03-29 11:09:34 +00001748 if (prog->Target == GL_VERTEX_PROGRAM_NV ||
1749 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
1750 if (ctx->VertexProgram.Current &&
1751 ctx->VertexProgram.Current->Base.Id == ids[i]) {
1752 /* unbind this currently bound program */
1753 _mesa_BindProgram(prog->Target, 0);
1754 }
1755 }
1756 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
1757 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1758 if (ctx->FragmentProgram.Current &&
1759 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
1760 /* unbind this currently bound program */
1761 _mesa_BindProgram(prog->Target, 0);
1762 }
1763 }
1764 else {
1765 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
1766 return;
1767 }
Brian Paulea2943e2005-01-20 04:02:02 +00001768 /* The ID is immediately available for re-use now */
1769 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
1770 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +00001771 if (prog->RefCount <= 0) {
1772 ctx->Driver.DeleteProgram(ctx, prog);
1773 }
1774 }
Michal Krol2861e732004-03-29 11:09:34 +00001775 }
1776 }
1777}
1778
1779
1780/**
1781 * Generate a list of new program identifiers.
1782 * \note Not compiled into display lists.
1783 * \note Called by both glGenProgramsNV and glGenProgramsARB.
1784 */
1785void GLAPIENTRY
1786_mesa_GenPrograms(GLsizei n, GLuint *ids)
1787{
1788 GLuint first;
1789 GLuint i;
1790 GET_CURRENT_CONTEXT(ctx);
1791 ASSERT_OUTSIDE_BEGIN_END(ctx);
1792
1793 if (n < 0) {
1794 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
1795 return;
1796 }
1797
1798 if (!ids)
1799 return;
1800
1801 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
1802
Brian Paul765f1a12004-09-14 22:28:27 +00001803 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +00001804 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +00001805 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +00001806 }
1807
1808 /* Return the program names */
1809 for (i = 0; i < (GLuint) n; i++) {
1810 ids[i] = first + i;
1811 }
1812}
1813
1814
1815/**
Brian Paul765f1a12004-09-14 22:28:27 +00001816 * Determine if id names a vertex or fragment program.
Michal Krol2861e732004-03-29 11:09:34 +00001817 * \note Not compiled into display lists.
1818 * \note Called from both glIsProgramNV and glIsProgramARB.
1819 * \param id is the program identifier
1820 * \return GL_TRUE if id is a program, else GL_FALSE.
1821 */
1822GLboolean GLAPIENTRY
1823_mesa_IsProgram(GLuint id)
1824{
1825 GET_CURRENT_CONTEXT(ctx);
1826 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1827
1828 if (id == 0)
1829 return GL_FALSE;
1830
1831 if (_mesa_HashLookup(ctx->Shared->Programs, id))
1832 return GL_TRUE;
1833 else
1834 return GL_FALSE;
1835}
1836
1837
1838
1839/**********************************************************************/
1840/* GL_MESA_program_debug extension */
1841/**********************************************************************/
1842
1843
1844/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001845GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001846glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1847 GLvoid *data)
1848{
1849 _mesa_ProgramCallbackMESA(target, callback, data);
1850}
1851
1852
1853void
1854_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
1855 GLvoid *data)
1856{
1857 GET_CURRENT_CONTEXT(ctx);
1858
1859 switch (target) {
1860 case GL_FRAGMENT_PROGRAM_ARB:
1861 if (!ctx->Extensions.ARB_fragment_program) {
1862 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1863 return;
1864 }
1865 ctx->FragmentProgram.Callback = callback;
1866 ctx->FragmentProgram.CallbackData = data;
1867 break;
1868 case GL_FRAGMENT_PROGRAM_NV:
1869 if (!ctx->Extensions.NV_fragment_program) {
1870 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1871 return;
1872 }
1873 ctx->FragmentProgram.Callback = callback;
1874 ctx->FragmentProgram.CallbackData = data;
1875 break;
1876 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
1877 if (!ctx->Extensions.ARB_vertex_program &&
1878 !ctx->Extensions.NV_vertex_program) {
1879 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1880 return;
1881 }
1882 ctx->VertexProgram.Callback = callback;
1883 ctx->VertexProgram.CallbackData = data;
1884 break;
1885 default:
1886 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
1887 return;
1888 }
1889}
1890
1891
1892/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +00001893GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +00001894glGetProgramRegisterfvMESA(GLenum target,
1895 GLsizei len, const GLubyte *registerName,
1896 GLfloat *v)
1897{
1898 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
1899}
1900
1901
1902void
1903_mesa_GetProgramRegisterfvMESA(GLenum target,
1904 GLsizei len, const GLubyte *registerName,
1905 GLfloat *v)
1906{
1907 char reg[1000];
1908 GET_CURRENT_CONTEXT(ctx);
1909
1910 /* We _should_ be inside glBegin/glEnd */
1911#if 0
1912 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
1913 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
1914 return;
1915 }
1916#endif
1917
1918 /* make null-terminated copy of registerName */
1919 len = MIN2((unsigned int) len, sizeof(reg) - 1);
1920 _mesa_memcpy(reg, registerName, len);
1921 reg[len] = 0;
1922
1923 switch (target) {
1924 case GL_VERTEX_PROGRAM_NV:
1925 if (!ctx->Extensions.ARB_vertex_program &&
1926 !ctx->Extensions.NV_vertex_program) {
1927 _mesa_error(ctx, GL_INVALID_ENUM,
1928 "glGetProgramRegisterfvMESA(target)");
1929 return;
1930 }
Brian Paul6d460af2004-04-23 14:16:46 +00001931 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001932 _mesa_error(ctx, GL_INVALID_OPERATION,
1933 "glGetProgramRegisterfvMESA");
1934 return;
1935 }
1936 /* GL_NV_vertex_program */
1937 if (reg[0] == 'R') {
1938 /* Temp register */
1939 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00001940 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00001941 _mesa_error(ctx, GL_INVALID_VALUE,
1942 "glGetProgramRegisterfvMESA(registerName)");
1943 return;
1944 }
1945 COPY_4V(v, ctx->VertexProgram.Temporaries[i]);
1946 }
1947 else if (reg[0] == 'v' && reg[1] == '[') {
1948 /* Vertex Input attribute */
1949 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00001950 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00001951 const char *name = _mesa_nv_vertex_input_register_name(i);
1952 char number[10];
Brian Paulaa206952005-09-16 18:14:24 +00001953 _mesa_sprintf(number, "%d", i);
Michal Krol2861e732004-03-29 11:09:34 +00001954 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
1955 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
1956 COPY_4V(v, ctx->VertexProgram.Inputs[i]);
1957 return;
1958 }
1959 }
1960 _mesa_error(ctx, GL_INVALID_VALUE,
1961 "glGetProgramRegisterfvMESA(registerName)");
1962 return;
1963 }
1964 else if (reg[0] == 'o' && reg[1] == '[') {
1965 /* Vertex output attribute */
1966 }
1967 /* GL_ARB_vertex_program */
1968 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
1969
1970 }
1971 else {
1972 _mesa_error(ctx, GL_INVALID_VALUE,
1973 "glGetProgramRegisterfvMESA(registerName)");
1974 return;
1975 }
1976 break;
1977 case GL_FRAGMENT_PROGRAM_ARB:
1978 if (!ctx->Extensions.ARB_fragment_program) {
1979 _mesa_error(ctx, GL_INVALID_ENUM,
1980 "glGetProgramRegisterfvMESA(target)");
1981 return;
1982 }
Brian Paul6d460af2004-04-23 14:16:46 +00001983 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001984 _mesa_error(ctx, GL_INVALID_OPERATION,
1985 "glGetProgramRegisterfvMESA");
1986 return;
1987 }
1988 /* XXX to do */
1989 break;
1990 case GL_FRAGMENT_PROGRAM_NV:
1991 if (!ctx->Extensions.NV_fragment_program) {
1992 _mesa_error(ctx, GL_INVALID_ENUM,
1993 "glGetProgramRegisterfvMESA(target)");
1994 return;
1995 }
Brian Paul6d460af2004-04-23 14:16:46 +00001996 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +00001997 _mesa_error(ctx, GL_INVALID_OPERATION,
1998 "glGetProgramRegisterfvMESA");
1999 return;
2000 }
2001 if (reg[0] == 'R') {
2002 /* Temp register */
2003 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +00002004 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +00002005 _mesa_error(ctx, GL_INVALID_VALUE,
2006 "glGetProgramRegisterfvMESA(registerName)");
2007 return;
2008 }
2009 COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]);
2010 }
2011 else if (reg[0] == 'f' && reg[1] == '[') {
2012 /* Fragment input attribute */
2013 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +00002014 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +00002015 const char *name = _mesa_nv_fragment_input_register_name(i);
2016 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
2017 COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]);
2018 return;
2019 }
2020 }
2021 _mesa_error(ctx, GL_INVALID_VALUE,
2022 "glGetProgramRegisterfvMESA(registerName)");
2023 return;
2024 }
2025 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
2026 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00002027 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLR]);
Michal Krol2861e732004-03-29 11:09:34 +00002028 }
2029 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
2030 /* Fragment output color */
Brian Paul90ebb582005-11-02 18:06:12 +00002031 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLH]);
Michal Krol2861e732004-03-29 11:09:34 +00002032 }
2033 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
2034 /* Fragment output depth */
Brian Paul90ebb582005-11-02 18:06:12 +00002035 COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_DEPR]);
Michal Krol2861e732004-03-29 11:09:34 +00002036 }
2037 else {
2038 /* try user-defined identifiers */
2039 const GLfloat *value = _mesa_lookup_parameter_value(
Brian Paulde997602005-11-12 17:53:14 +00002040 ctx->FragmentProgram.Current->Base.Parameters, -1, reg);
Michal Krol2861e732004-03-29 11:09:34 +00002041 if (value) {
2042 COPY_4V(v, value);
2043 }
2044 else {
2045 _mesa_error(ctx, GL_INVALID_VALUE,
2046 "glGetProgramRegisterfvMESA(registerName)");
2047 return;
2048 }
2049 }
2050 break;
2051 default:
2052 _mesa_error(ctx, GL_INVALID_ENUM,
2053 "glGetProgramRegisterfvMESA(target)");
2054 return;
2055 }
2056
2057}