blob: c504c7c5c54325ecfaf699899b5cf80f1789d19e [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian Paula23e6682006-10-30 00:12:05 +00003 * Version: 6.5.2
Michal Krol2861e732004-03-29 11:09:34 +00004 *
Brian Paul65a51c02006-05-24 03:30:31 +00005 * Copyright (C) 1999-2006 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"
Michal Krol2861e732004-03-29 11:09:34 +000038#include "nvfragparse.h"
Brian0560d812006-12-14 15:01:28 -070039#include "program.h"
40#include "prog_parameter.h"
41#include "prog_instruction.h"
42#include "prog_statevars.h"
Michal Krol2861e732004-03-29 11:09:34 +000043#include "nvvertparse.h"
Roland Scheideggerf519a772005-09-02 01:11:53 +000044#include "atifragshader.h"
Michal Krol2861e732004-03-29 11:09:34 +000045
46
Brian Paul7b98b402005-11-12 23:25:49 +000047
Michal Krol2861e732004-03-29 11:09:34 +000048/**********************************************************************/
49/* Utility functions */
50/**********************************************************************/
51
52
Brian Paul765f1a12004-09-14 22:28:27 +000053/* A pointer to this dummy program is put into the hash table when
54 * glGenPrograms is called.
55 */
Brian Paul122629f2006-07-20 16:49:57 +000056struct gl_program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000057
58
Michal Krol2861e732004-03-29 11:09:34 +000059/**
Brian Paul21841f02004-08-14 14:28:11 +000060 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000061 */
62void
63_mesa_init_program(GLcontext *ctx)
64{
65 GLuint i;
66
67 ctx->Program.ErrorPos = -1;
68 ctx->Program.ErrorString = _mesa_strdup("");
69
70#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
71 ctx->VertexProgram.Enabled = GL_FALSE;
72 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
73 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Brian Paul122629f2006-07-20 16:49:57 +000074 ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +000075 assert(ctx->VertexProgram.Current);
76 ctx->VertexProgram.Current->Base.RefCount++;
77 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
78 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
79 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
80 }
81#endif
82
83#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
84 ctx->FragmentProgram.Enabled = GL_FALSE;
Brian Paul122629f2006-07-20 16:49:57 +000085 ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +000086 assert(ctx->FragmentProgram.Current);
87 ctx->FragmentProgram.Current->Base.RefCount++;
88#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000089
Brian Paul63d68302005-11-19 16:43:04 +000090 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000091#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);
Brian Paul63d68302005-11-19 16:43:04 +000095 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +000096#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{
Roland Scheidegger93da6732006-03-01 23:11:14 +0000106#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul21841f02004-08-14 14:28:11 +0000107 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
Roland Scheidegger93da6732006-03-01 23:11:14 +0000113#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul21841f02004-08-14 14:28:11 +0000114 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
Brian Paul63d68302005-11-19 16:43:04 +0000120 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000121#if FEATURE_ATI_fragment_shader
122 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000123 ctx->ATIFragmentShader.Current->RefCount--;
124 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
125 _mesa_free(ctx->ATIFragmentShader.Current);
126 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000127 }
128#endif
Brian Paul21841f02004-08-14 14:28:11 +0000129 _mesa_free((void *) ctx->Program.ErrorString);
130}
131
132
133
134
135/**
Michal Krol2861e732004-03-29 11:09:34 +0000136 * Set the vertex/fragment program error state (position and error string).
137 * This is generally called from within the parsers.
138 */
139void
140_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
141{
142 ctx->Program.ErrorPos = pos;
143 _mesa_free((void *) ctx->Program.ErrorString);
144 if (!string)
145 string = "";
146 ctx->Program.ErrorString = _mesa_strdup(string);
147}
148
149
150/**
151 * Find the line number and column for 'pos' within 'string'.
152 * Return a copy of the line which contains 'pos'. Free the line with
153 * _mesa_free().
154 * \param string the program string
155 * \param pos the position within the string
156 * \param line returns the line number corresponding to 'pos'.
157 * \param col returns the column number corresponding to 'pos'.
158 * \return copy of the line containing 'pos'.
159 */
160const GLubyte *
161_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
162 GLint *line, GLint *col)
163{
164 const GLubyte *lineStart = string;
165 const GLubyte *p = string;
166 GLubyte *s;
167 int len;
168
169 *line = 1;
170
171 while (p != pos) {
172 if (*p == (GLubyte) '\n') {
173 (*line)++;
174 lineStart = p + 1;
175 }
176 p++;
177 }
178
179 *col = (pos - lineStart) + 1;
180
181 /* return copy of this line */
182 while (*p != 0 && *p != '\n')
183 p++;
184 len = p - lineStart;
185 s = (GLubyte *) _mesa_malloc(len + 1);
186 _mesa_memcpy(s, lineStart, len);
187 s[len] = 0;
188
189 return s;
190}
191
192
Brian Paul765f1a12004-09-14 22:28:27 +0000193/**
194 * Initialize a new vertex/fragment program object.
195 */
Brian Paul122629f2006-07-20 16:49:57 +0000196static struct gl_program *
197_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000198 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000199{
Brian Paula6c423d2004-08-25 15:59:48 +0000200 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000201 if (prog) {
Brianfe1d01c2006-12-13 14:54:47 -0700202 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000203 prog->Id = id;
204 prog->Target = target;
205 prog->Resident = GL_TRUE;
206 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000207 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Michal Krol2861e732004-03-29 11:09:34 +0000208 }
209
210 return prog;
211}
212
Brian Paul765f1a12004-09-14 22:28:27 +0000213
214/**
215 * Initialize a new fragment program object.
216 */
Brian Paul122629f2006-07-20 16:49:57 +0000217struct gl_program *
218_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000219 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000220{
221 if (prog)
222 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
223 else
224 return NULL;
225}
226
Brian Paul765f1a12004-09-14 22:28:27 +0000227
228/**
229 * Initialize a new vertex program object.
230 */
Brian Paul122629f2006-07-20 16:49:57 +0000231struct gl_program *
232_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000233 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000234{
235 if (prog)
236 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
237 else
238 return NULL;
239}
240
241
242/**
243 * Allocate and initialize a new fragment/vertex program object but
244 * don't put it into the program hash table. Called via
245 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
246 * device driver function to implement OO deriviation with additional
247 * types not understood by this function.
248 *
249 * \param ctx context
250 * \param id program id/number
251 * \param target program target/type
252 * \return pointer to new program object
253 */
Brian Paul122629f2006-07-20 16:49:57 +0000254struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000255_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
256{
257 switch (target) {
258 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000259 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
260 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000261 case GL_FRAGMENT_PROGRAM_NV:
262 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000263 return _mesa_init_fragment_program(ctx,
264 CALLOC_STRUCT(gl_fragment_program),
265 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000266 default:
267 _mesa_problem(ctx, "bad target in _mesa_new_program");
268 return NULL;
269 }
270}
271
272
273/**
274 * Delete a program and remove it from the hash table, ignoring the
275 * reference count.
276 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
277 * by a device driver function.
278 */
279void
Brian Paul122629f2006-07-20 16:49:57 +0000280_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000281{
Brian Paula6c423d2004-08-25 15:59:48 +0000282 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000283 ASSERT(prog);
284
Brian Paul308b85f2006-11-23 15:58:30 +0000285 if (prog == &_mesa_DummyProgram)
286 return;
287
Michal Krol2861e732004-03-29 11:09:34 +0000288 if (prog->String)
289 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000290
291 if (prog->Instructions) {
292 GLuint i;
293 for (i = 0; i < prog->NumInstructions; i++) {
294 if (prog->Instructions[i].Data)
295 _mesa_free(prog->Instructions[i].Data);
Brian Paul575700f2004-12-16 03:07:18 +0000296 }
Brian Paulde997602005-11-12 17:53:14 +0000297 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000298 }
Brian Paulde997602005-11-12 17:53:14 +0000299
Brian Paul65a51c02006-05-24 03:30:31 +0000300 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000301 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000302 }
Brian Paulde997602005-11-12 17:53:14 +0000303
Brianfe1d01c2006-12-13 14:54:47 -0700304 if (prog->Varying) {
305 _mesa_free_parameter_list(prog->Varying);
306 }
307
Brian Paul58d080b2006-08-25 19:46:31 +0000308 /* XXX this is a little ugly */
309 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
310 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
311 if (vprog->TnlData)
312 _mesa_free(vprog->TnlData);
313 }
314
Michal Krol2861e732004-03-29 11:09:34 +0000315 _mesa_free(prog);
316}
317
318
Brian Paul4d12a052006-08-23 23:10:14 +0000319/**
320 * Return the gl_program object for a given ID.
321 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
322 * casts elsewhere.
323 */
324struct gl_program *
325_mesa_lookup_program(GLcontext *ctx, GLuint id)
326{
327 if (id)
328 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
329 else
330 return NULL;
331}
332
Michal Krol2861e732004-03-29 11:09:34 +0000333
Brian Paul3b9b8de2006-08-24 21:57:36 +0000334/**
Brianb2a3a852006-12-14 13:56:58 -0700335 * Return a copy of a program.
336 * XXX Problem here if the program object is actually OO-derivation
337 * made by a device driver.
338 */
339struct gl_program *
340_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
341{
342 struct gl_program *clone;
343
344 clone = _mesa_new_program(ctx, prog->Target, prog->Id);
345 if (!clone)
346 return NULL;
347
348 assert(clone->Target == prog->Target);
349 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
350 clone->RefCount = 1;
351 clone->Format = prog->Format;
352 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
353 if (!clone->Instructions) {
354 _mesa_delete_program(ctx, clone);
355 return NULL;
356 }
357 memcpy(clone->Instructions, prog->Instructions,
358 prog->NumInstructions * sizeof(struct prog_instruction));
359 clone->InputsRead = prog->InputsRead;
360 clone->OutputsWritten = prog->OutputsWritten;
361 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
362 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
363 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
364 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
365 clone->NumInstructions = prog->NumInstructions;
366 clone->NumTemporaries = prog->NumTemporaries;
367 clone->NumParameters = prog->NumParameters;
368 clone->NumAttributes = prog->NumAttributes;
369 clone->NumAddressRegs = prog->NumAddressRegs;
370 clone->NumNativeInstructions = prog->NumNativeInstructions;
371 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
372 clone->NumNativeParameters = prog->NumNativeParameters;
373 clone->NumNativeAttributes = prog->NumNativeAttributes;
374 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
375
376 switch (prog->Target) {
377 case GL_VERTEX_PROGRAM_ARB:
378 {
379 const struct gl_vertex_program *vp
380 = (const struct gl_vertex_program *) prog;
381 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
382 vpc->IsPositionInvariant = vp->IsPositionInvariant;
383 }
384 break;
385 case GL_FRAGMENT_PROGRAM_ARB:
386 {
387 const struct gl_fragment_program *fp
388 = (const struct gl_fragment_program *) prog;
389 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
390 memcpy(fpc->TexturesUsed, fp->TexturesUsed, sizeof(fp->TexturesUsed));
391 fpc->NumAluInstructions = fp->NumAluInstructions;
392 fpc->NumTexInstructions = fp->NumTexInstructions;
393 fpc->NumTexIndirections = fp->NumTexIndirections;
394 fpc->NumNativeAluInstructions = fp->NumNativeAluInstructions;
395 fpc->NumNativeTexInstructions = fp->NumNativeTexInstructions;
396 fpc->NumNativeTexIndirections = fp->NumNativeTexIndirections;
397 fpc->FogOption = fp->FogOption;
398 fpc->UsesKill = fp->UsesKill;
399 }
400 break;
401 default:
402 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
403 }
404
405 return clone;
406}
407
408
409
410/**
Brian Paul77427a12006-08-24 23:11:39 +0000411 * Mixing ARB and NV vertex/fragment programs can be tricky.
412 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
413 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
414 * The two different fragment program targets are supposed to be compatible
415 * to some extent (see GL_ARB_fragment_program spec).
416 * This function does the compatibility check.
417 */
418static GLboolean
419compatible_program_targets(GLenum t1, GLenum t2)
420{
421 if (t1 == t2)
422 return GL_TRUE;
423 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
424 return GL_TRUE;
425 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
426 return GL_TRUE;
427 return GL_FALSE;
428}
429
430
Brian Paul30d6a4b2005-11-05 20:18:18 +0000431
Michal Krol2861e732004-03-29 11:09:34 +0000432/**********************************************************************/
433/* API functions */
434/**********************************************************************/
435
436
437/**
438 * Bind a program (make it current)
439 * \note Called from the GL API dispatcher by both glBindProgramNV
440 * and glBindProgramARB.
441 */
442void GLAPIENTRY
443_mesa_BindProgram(GLenum target, GLuint id)
444{
Brian Paula360bc32006-08-25 17:18:56 +0000445 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000446 GET_CURRENT_CONTEXT(ctx);
447 ASSERT_OUTSIDE_BEGIN_END(ctx);
448
449 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
450
Brian Paula360bc32006-08-25 17:18:56 +0000451 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000452 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
453 (ctx->Extensions.NV_vertex_program ||
454 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000455 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000456 }
457 else if ((target == GL_FRAGMENT_PROGRAM_NV
458 && ctx->Extensions.NV_fragment_program) ||
459 (target == GL_FRAGMENT_PROGRAM_ARB
460 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000461 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000462 }
463 else {
464 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
465 return;
466 }
467
Brian Paula360bc32006-08-25 17:18:56 +0000468 /*
469 * Get pointer to new program to bind.
470 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000471 * That's supposed to be caught in glBegin.
472 */
473 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000474 /* Bind a default program */
475 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000476 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000477 newProg = ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000478 else
Brian Paula360bc32006-08-25 17:18:56 +0000479 newProg = ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000480 }
481 else {
Brian Paula360bc32006-08-25 17:18:56 +0000482 /* Bind a user program */
483 newProg = _mesa_lookup_program(ctx, id);
484 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000485 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000486 newProg = ctx->Driver.NewProgram(ctx, target, id);
487 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000488 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
489 return;
490 }
Brian Paula360bc32006-08-25 17:18:56 +0000491 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000492 }
Brian Paula360bc32006-08-25 17:18:56 +0000493 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000494 _mesa_error(ctx, GL_INVALID_OPERATION,
495 "glBindProgramNV/ARB(target mismatch)");
496 return;
497 }
Michal Krol2861e732004-03-29 11:09:34 +0000498 }
499
Brian Paula360bc32006-08-25 17:18:56 +0000500 /** All error checking is complete now **/
501
502 if (curProg->Id == id) {
503 /* binding same program - no change */
504 return;
505 }
506
507 /* unbind/delete oldProg */
508 if (curProg->Id != 0) {
509 /* decrement refcount on previously bound fragment program */
510 curProg->RefCount--;
511 /* and delete if refcount goes below one */
512 if (curProg->RefCount <= 0) {
513 /* the program ID was already removed from the hash table */
514 ctx->Driver.DeleteProgram(ctx, curProg);
515 }
516 }
517
518 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000519 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000520 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000521 }
Brian Paula360bc32006-08-25 17:18:56 +0000522 else if (target == GL_FRAGMENT_PROGRAM_NV ||
523 target == GL_FRAGMENT_PROGRAM_ARB) {
524 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000525 }
Brian Paula360bc32006-08-25 17:18:56 +0000526 newProg->RefCount++;
Michal Krol2861e732004-03-29 11:09:34 +0000527
Brian Paul765f1a12004-09-14 22:28:27 +0000528 /* Never null pointers */
529 ASSERT(ctx->VertexProgram.Current);
530 ASSERT(ctx->FragmentProgram.Current);
531
Michal Krol2861e732004-03-29 11:09:34 +0000532 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000533 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000534}
535
536
537/**
538 * Delete a list of programs.
539 * \note Not compiled into display lists.
540 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
541 */
542void GLAPIENTRY
543_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
544{
545 GLint i;
546 GET_CURRENT_CONTEXT(ctx);
547 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
548
549 if (n < 0) {
550 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
551 return;
552 }
553
554 for (i = 0; i < n; i++) {
555 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000556 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000557 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000558 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
559 }
560 else if (prog) {
561 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000562 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000563 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
564 if (ctx->VertexProgram.Current &&
565 ctx->VertexProgram.Current->Base.Id == ids[i]) {
566 /* unbind this currently bound program */
567 _mesa_BindProgram(prog->Target, 0);
568 }
569 }
570 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
571 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
572 if (ctx->FragmentProgram.Current &&
573 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
574 /* unbind this currently bound program */
575 _mesa_BindProgram(prog->Target, 0);
576 }
577 }
578 else {
579 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
580 return;
581 }
Brian Paulea2943e2005-01-20 04:02:02 +0000582 /* The ID is immediately available for re-use now */
583 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
584 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000585 if (prog->RefCount <= 0) {
586 ctx->Driver.DeleteProgram(ctx, prog);
587 }
588 }
Michal Krol2861e732004-03-29 11:09:34 +0000589 }
590 }
591}
592
593
594/**
595 * Generate a list of new program identifiers.
596 * \note Not compiled into display lists.
597 * \note Called by both glGenProgramsNV and glGenProgramsARB.
598 */
599void GLAPIENTRY
600_mesa_GenPrograms(GLsizei n, GLuint *ids)
601{
602 GLuint first;
603 GLuint i;
604 GET_CURRENT_CONTEXT(ctx);
605 ASSERT_OUTSIDE_BEGIN_END(ctx);
606
607 if (n < 0) {
608 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
609 return;
610 }
611
612 if (!ids)
613 return;
614
615 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
616
Brian Paul765f1a12004-09-14 22:28:27 +0000617 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000618 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000619 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000620 }
621
622 /* Return the program names */
623 for (i = 0; i < (GLuint) n; i++) {
624 ids[i] = first + i;
625 }
626}
627
628
Michal Krol2861e732004-03-29 11:09:34 +0000629/**********************************************************************/
630/* GL_MESA_program_debug extension */
631/**********************************************************************/
632
633
634/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +0000635GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +0000636glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
637 GLvoid *data)
638{
639 _mesa_ProgramCallbackMESA(target, callback, data);
640}
641
642
643void
644_mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback,
645 GLvoid *data)
646{
647 GET_CURRENT_CONTEXT(ctx);
648
649 switch (target) {
650 case GL_FRAGMENT_PROGRAM_ARB:
651 if (!ctx->Extensions.ARB_fragment_program) {
652 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
653 return;
654 }
655 ctx->FragmentProgram.Callback = callback;
656 ctx->FragmentProgram.CallbackData = data;
657 break;
658 case GL_FRAGMENT_PROGRAM_NV:
659 if (!ctx->Extensions.NV_fragment_program) {
660 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
661 return;
662 }
663 ctx->FragmentProgram.Callback = callback;
664 ctx->FragmentProgram.CallbackData = data;
665 break;
666 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
667 if (!ctx->Extensions.ARB_vertex_program &&
668 !ctx->Extensions.NV_vertex_program) {
669 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
670 return;
671 }
672 ctx->VertexProgram.Callback = callback;
673 ctx->VertexProgram.CallbackData = data;
674 break;
675 default:
676 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)");
677 return;
678 }
679}
680
681
682/* XXX temporary */
Daniel Borca0a13ceb2005-02-14 08:01:59 +0000683GLAPI void GLAPIENTRY
Michal Krol2861e732004-03-29 11:09:34 +0000684glGetProgramRegisterfvMESA(GLenum target,
685 GLsizei len, const GLubyte *registerName,
686 GLfloat *v)
687{
688 _mesa_GetProgramRegisterfvMESA(target, len, registerName, v);
689}
690
691
692void
693_mesa_GetProgramRegisterfvMESA(GLenum target,
694 GLsizei len, const GLubyte *registerName,
695 GLfloat *v)
696{
697 char reg[1000];
698 GET_CURRENT_CONTEXT(ctx);
699
700 /* We _should_ be inside glBegin/glEnd */
701#if 0
702 if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) {
703 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA");
704 return;
705 }
706#endif
707
708 /* make null-terminated copy of registerName */
709 len = MIN2((unsigned int) len, sizeof(reg) - 1);
710 _mesa_memcpy(reg, registerName, len);
711 reg[len] = 0;
712
713 switch (target) {
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000714 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000715 if (!ctx->Extensions.ARB_vertex_program &&
716 !ctx->Extensions.NV_vertex_program) {
717 _mesa_error(ctx, GL_INVALID_ENUM,
718 "glGetProgramRegisterfvMESA(target)");
719 return;
720 }
Brian Paul6d460af2004-04-23 14:16:46 +0000721 if (!ctx->VertexProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +0000722 _mesa_error(ctx, GL_INVALID_OPERATION,
723 "glGetProgramRegisterfvMESA");
724 return;
725 }
726 /* GL_NV_vertex_program */
727 if (reg[0] == 'R') {
728 /* Temp register */
729 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +0000730 if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +0000731 _mesa_error(ctx, GL_INVALID_VALUE,
732 "glGetProgramRegisterfvMESA(registerName)");
733 return;
734 }
Brian Paula23e6682006-10-30 00:12:05 +0000735#if 0 /* FIX ME */
736 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_TEMPORARY, i, v);
737#endif
Michal Krol2861e732004-03-29 11:09:34 +0000738 }
739 else if (reg[0] == 'v' && reg[1] == '[') {
740 /* Vertex Input attribute */
741 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +0000742 for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000743 const char *name = _mesa_nv_vertex_input_register_name(i);
744 char number[10];
Brian Paulaa206952005-09-16 18:14:24 +0000745 _mesa_sprintf(number, "%d", i);
Michal Krol2861e732004-03-29 11:09:34 +0000746 if (_mesa_strncmp(reg + 2, name, 4) == 0 ||
747 _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) {
Brian Paula23e6682006-10-30 00:12:05 +0000748#if 0 /* FIX ME */
749 ctx->Driver.GetVertexProgramRegister(ctx, PROGRAM_INPUT,
750 i, v);
751#endif
Michal Krol2861e732004-03-29 11:09:34 +0000752 return;
753 }
754 }
755 _mesa_error(ctx, GL_INVALID_VALUE,
756 "glGetProgramRegisterfvMESA(registerName)");
757 return;
758 }
759 else if (reg[0] == 'o' && reg[1] == '[') {
760 /* Vertex output attribute */
761 }
762 /* GL_ARB_vertex_program */
763 else if (_mesa_strncmp(reg, "vertex.", 7) == 0) {
764
765 }
766 else {
767 _mesa_error(ctx, GL_INVALID_VALUE,
768 "glGetProgramRegisterfvMESA(registerName)");
769 return;
770 }
771 break;
772 case GL_FRAGMENT_PROGRAM_ARB:
773 if (!ctx->Extensions.ARB_fragment_program) {
774 _mesa_error(ctx, GL_INVALID_ENUM,
775 "glGetProgramRegisterfvMESA(target)");
776 return;
777 }
Brian Paul6d460af2004-04-23 14:16:46 +0000778 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +0000779 _mesa_error(ctx, GL_INVALID_OPERATION,
780 "glGetProgramRegisterfvMESA");
781 return;
782 }
783 /* XXX to do */
784 break;
785 case GL_FRAGMENT_PROGRAM_NV:
786 if (!ctx->Extensions.NV_fragment_program) {
787 _mesa_error(ctx, GL_INVALID_ENUM,
788 "glGetProgramRegisterfvMESA(target)");
789 return;
790 }
Brian Paul6d460af2004-04-23 14:16:46 +0000791 if (!ctx->FragmentProgram._Enabled) {
Michal Krol2861e732004-03-29 11:09:34 +0000792 _mesa_error(ctx, GL_INVALID_OPERATION,
793 "glGetProgramRegisterfvMESA");
794 return;
795 }
796 if (reg[0] == 'R') {
797 /* Temp register */
798 GLint i = _mesa_atoi(reg + 1);
Brian Paul05051032005-11-01 04:36:33 +0000799 if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) {
Michal Krol2861e732004-03-29 11:09:34 +0000800 _mesa_error(ctx, GL_INVALID_VALUE,
801 "glGetProgramRegisterfvMESA(registerName)");
802 return;
803 }
Brian Paul39c4daa2006-10-10 21:43:31 +0000804 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_TEMPORARY,
805 i, v);
Michal Krol2861e732004-03-29 11:09:34 +0000806 }
807 else if (reg[0] == 'f' && reg[1] == '[') {
808 /* Fragment input attribute */
809 GLuint i;
Brian Paul05051032005-11-01 04:36:33 +0000810 for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) {
Michal Krol2861e732004-03-29 11:09:34 +0000811 const char *name = _mesa_nv_fragment_input_register_name(i);
812 if (_mesa_strncmp(reg + 2, name, 4) == 0) {
Brian Paul39c4daa2006-10-10 21:43:31 +0000813 ctx->Driver.GetFragmentProgramRegister(ctx,
814 PROGRAM_INPUT, i, v);
Michal Krol2861e732004-03-29 11:09:34 +0000815 return;
816 }
817 }
818 _mesa_error(ctx, GL_INVALID_VALUE,
819 "glGetProgramRegisterfvMESA(registerName)");
820 return;
821 }
822 else if (_mesa_strcmp(reg, "o[COLR]") == 0) {
823 /* Fragment output color */
Brian Paul39c4daa2006-10-10 21:43:31 +0000824 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
825 FRAG_RESULT_COLR, v);
Michal Krol2861e732004-03-29 11:09:34 +0000826 }
827 else if (_mesa_strcmp(reg, "o[COLH]") == 0) {
828 /* Fragment output color */
Brian Paul39c4daa2006-10-10 21:43:31 +0000829 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
830 FRAG_RESULT_COLH, v);
Michal Krol2861e732004-03-29 11:09:34 +0000831 }
832 else if (_mesa_strcmp(reg, "o[DEPR]") == 0) {
833 /* Fragment output depth */
Brian Paul39c4daa2006-10-10 21:43:31 +0000834 ctx->Driver.GetFragmentProgramRegister(ctx, PROGRAM_OUTPUT,
835 FRAG_RESULT_DEPR, v);
Michal Krol2861e732004-03-29 11:09:34 +0000836 }
837 else {
838 /* try user-defined identifiers */
839 const GLfloat *value = _mesa_lookup_parameter_value(
Brian Paulde997602005-11-12 17:53:14 +0000840 ctx->FragmentProgram.Current->Base.Parameters, -1, reg);
Michal Krol2861e732004-03-29 11:09:34 +0000841 if (value) {
842 COPY_4V(v, value);
843 }
844 else {
845 _mesa_error(ctx, GL_INVALID_VALUE,
846 "glGetProgramRegisterfvMESA(registerName)");
847 return;
848 }
849 }
850 break;
851 default:
852 _mesa_error(ctx, GL_INVALID_ENUM,
853 "glGetProgramRegisterfvMESA(target)");
854 return;
855 }
Michal Krol2861e732004-03-29 11:09:34 +0000856}