blob: e872d78611f54c184d55cfad1b79d947440d2275 [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian942ee022007-02-09 15:40:00 -07003 * Version: 6.5.3
Michal Krol2861e732004-03-29 11:09:34 +00004 *
Brian942ee022007-02-09 15:40:00 -07005 * Copyright (C) 1999-2007 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"
Brian0560d812006-12-14 15:01:28 -070035#include "program.h"
36#include "prog_parameter.h"
37#include "prog_instruction.h"
Michal Krol2861e732004-03-29 11:09:34 +000038
39
Brian942ee022007-02-09 15:40:00 -070040/**
41 * A pointer to this dummy program is put into the hash table when
Brian Paul765f1a12004-09-14 22:28:27 +000042 * glGenPrograms is called.
43 */
Brian Paul122629f2006-07-20 16:49:57 +000044struct gl_program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000045
46
Michal Krol2861e732004-03-29 11:09:34 +000047/**
Brian Paul21841f02004-08-14 14:28:11 +000048 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000049 */
50void
51_mesa_init_program(GLcontext *ctx)
52{
53 GLuint i;
54
55 ctx->Program.ErrorPos = -1;
56 ctx->Program.ErrorString = _mesa_strdup("");
57
58#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
59 ctx->VertexProgram.Enabled = GL_FALSE;
60 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
61 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Brian Paul122629f2006-07-20 16:49:57 +000062 ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +000063 assert(ctx->VertexProgram.Current);
64 ctx->VertexProgram.Current->Base.RefCount++;
65 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
66 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
67 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
68 }
69#endif
70
71#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
72 ctx->FragmentProgram.Enabled = GL_FALSE;
Brian Paul122629f2006-07-20 16:49:57 +000073 ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +000074 assert(ctx->FragmentProgram.Current);
75 ctx->FragmentProgram.Current->Base.RefCount++;
76#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000077
Brian Paul63d68302005-11-19 16:43:04 +000078 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000079#if FEATURE_ATI_fragment_shader
80 ctx->ATIFragmentShader.Enabled = GL_FALSE;
81 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
82 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +000083 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +000084#endif
Michal Krol2861e732004-03-29 11:09:34 +000085}
86
87
88/**
Brian Paul21841f02004-08-14 14:28:11 +000089 * Free a context's vertex/fragment program state
90 */
91void
92_mesa_free_program_data(GLcontext *ctx)
93{
Roland Scheidegger93da6732006-03-01 23:11:14 +000094#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul21841f02004-08-14 14:28:11 +000095 if (ctx->VertexProgram.Current) {
96 ctx->VertexProgram.Current->Base.RefCount--;
97 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
98 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
99 }
100#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000101#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul21841f02004-08-14 14:28:11 +0000102 if (ctx->FragmentProgram.Current) {
103 ctx->FragmentProgram.Current->Base.RefCount--;
104 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
105 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
106 }
107#endif
Brian Paul63d68302005-11-19 16:43:04 +0000108 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000109#if FEATURE_ATI_fragment_shader
110 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000111 ctx->ATIFragmentShader.Current->RefCount--;
112 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
113 _mesa_free(ctx->ATIFragmentShader.Current);
114 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000115 }
116#endif
Brian Paul21841f02004-08-14 14:28:11 +0000117 _mesa_free((void *) ctx->Program.ErrorString);
118}
119
120
121
122
123/**
Michal Krol2861e732004-03-29 11:09:34 +0000124 * Set the vertex/fragment program error state (position and error string).
125 * This is generally called from within the parsers.
126 */
127void
128_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
129{
130 ctx->Program.ErrorPos = pos;
131 _mesa_free((void *) ctx->Program.ErrorString);
132 if (!string)
133 string = "";
134 ctx->Program.ErrorString = _mesa_strdup(string);
135}
136
137
138/**
139 * Find the line number and column for 'pos' within 'string'.
140 * Return a copy of the line which contains 'pos'. Free the line with
141 * _mesa_free().
142 * \param string the program string
143 * \param pos the position within the string
144 * \param line returns the line number corresponding to 'pos'.
145 * \param col returns the column number corresponding to 'pos'.
146 * \return copy of the line containing 'pos'.
147 */
148const GLubyte *
149_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
150 GLint *line, GLint *col)
151{
152 const GLubyte *lineStart = string;
153 const GLubyte *p = string;
154 GLubyte *s;
155 int len;
156
157 *line = 1;
158
159 while (p != pos) {
160 if (*p == (GLubyte) '\n') {
161 (*line)++;
162 lineStart = p + 1;
163 }
164 p++;
165 }
166
167 *col = (pos - lineStart) + 1;
168
169 /* return copy of this line */
170 while (*p != 0 && *p != '\n')
171 p++;
172 len = p - lineStart;
173 s = (GLubyte *) _mesa_malloc(len + 1);
174 _mesa_memcpy(s, lineStart, len);
175 s[len] = 0;
176
177 return s;
178}
179
180
Brian Paul765f1a12004-09-14 22:28:27 +0000181/**
182 * Initialize a new vertex/fragment program object.
183 */
Brian Paul122629f2006-07-20 16:49:57 +0000184static struct gl_program *
185_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000186 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000187{
Brian Paula6c423d2004-08-25 15:59:48 +0000188 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000189 if (prog) {
Brianfe1d01c2006-12-13 14:54:47 -0700190 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000191 prog->Id = id;
192 prog->Target = target;
193 prog->Resident = GL_TRUE;
194 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000195 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Michal Krol2861e732004-03-29 11:09:34 +0000196 }
197
198 return prog;
199}
200
Brian Paul765f1a12004-09-14 22:28:27 +0000201
202/**
203 * Initialize a new fragment program object.
204 */
Brian Paul122629f2006-07-20 16:49:57 +0000205struct gl_program *
206_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000207 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000208{
209 if (prog)
210 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
211 else
212 return NULL;
213}
214
Brian Paul765f1a12004-09-14 22:28:27 +0000215
216/**
217 * Initialize a new vertex program object.
218 */
Brian Paul122629f2006-07-20 16:49:57 +0000219struct gl_program *
220_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000221 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000222{
223 if (prog)
224 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
225 else
226 return NULL;
227}
228
229
230/**
231 * Allocate and initialize a new fragment/vertex program object but
232 * don't put it into the program hash table. Called via
233 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
234 * device driver function to implement OO deriviation with additional
235 * types not understood by this function.
236 *
237 * \param ctx context
238 * \param id program id/number
239 * \param target program target/type
240 * \return pointer to new program object
241 */
Brian Paul122629f2006-07-20 16:49:57 +0000242struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000243_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
244{
245 switch (target) {
246 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000247 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
248 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000249 case GL_FRAGMENT_PROGRAM_NV:
250 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000251 return _mesa_init_fragment_program(ctx,
252 CALLOC_STRUCT(gl_fragment_program),
253 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000254 default:
255 _mesa_problem(ctx, "bad target in _mesa_new_program");
256 return NULL;
257 }
258}
259
260
261/**
262 * Delete a program and remove it from the hash table, ignoring the
263 * reference count.
264 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
265 * by a device driver function.
266 */
267void
Brian Paul122629f2006-07-20 16:49:57 +0000268_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000269{
Brian Paula6c423d2004-08-25 15:59:48 +0000270 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000271 ASSERT(prog);
272
Brian Paul308b85f2006-11-23 15:58:30 +0000273 if (prog == &_mesa_DummyProgram)
274 return;
275
Michal Krol2861e732004-03-29 11:09:34 +0000276 if (prog->String)
277 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000278
279 if (prog->Instructions) {
280 GLuint i;
281 for (i = 0; i < prog->NumInstructions; i++) {
282 if (prog->Instructions[i].Data)
283 _mesa_free(prog->Instructions[i].Data);
Brian Paul575700f2004-12-16 03:07:18 +0000284 }
Brian Paulde997602005-11-12 17:53:14 +0000285 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000286 }
Brian Paulde997602005-11-12 17:53:14 +0000287
Brian Paul65a51c02006-05-24 03:30:31 +0000288 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000289 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000290 }
Brianfe1d01c2006-12-13 14:54:47 -0700291 if (prog->Varying) {
292 _mesa_free_parameter_list(prog->Varying);
293 }
Brian3493e862007-03-24 16:18:13 -0600294 if (prog->Attributes) {
295 _mesa_free_parameter_list(prog->Attributes);
296 }
Brianfe1d01c2006-12-13 14:54:47 -0700297
Brian Paul58d080b2006-08-25 19:46:31 +0000298 /* XXX this is a little ugly */
299 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
300 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
301 if (vprog->TnlData)
302 _mesa_free(vprog->TnlData);
303 }
304
Michal Krol2861e732004-03-29 11:09:34 +0000305 _mesa_free(prog);
306}
307
308
Brian Paul4d12a052006-08-23 23:10:14 +0000309/**
310 * Return the gl_program object for a given ID.
311 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
312 * casts elsewhere.
313 */
314struct gl_program *
315_mesa_lookup_program(GLcontext *ctx, GLuint id)
316{
317 if (id)
318 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
319 else
320 return NULL;
321}
322
Michal Krol2861e732004-03-29 11:09:34 +0000323
Brian Paul3b9b8de2006-08-24 21:57:36 +0000324/**
Brianb2a3a852006-12-14 13:56:58 -0700325 * Return a copy of a program.
326 * XXX Problem here if the program object is actually OO-derivation
327 * made by a device driver.
328 */
329struct gl_program *
330_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
331{
332 struct gl_program *clone;
333
334 clone = _mesa_new_program(ctx, prog->Target, prog->Id);
335 if (!clone)
336 return NULL;
337
338 assert(clone->Target == prog->Target);
339 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
340 clone->RefCount = 1;
341 clone->Format = prog->Format;
342 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
343 if (!clone->Instructions) {
344 _mesa_delete_program(ctx, clone);
345 return NULL;
346 }
Brian12229f12007-03-22 09:11:26 -0600347 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
348 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700349 clone->InputsRead = prog->InputsRead;
350 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700351 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
352
Brian2e76f0a2006-12-19 09:52:07 -0700353 if (prog->Parameters)
354 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700355 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700356 if (prog->Varying)
357 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700358 if (prog->Attributes)
359 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700360 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
361 clone->NumInstructions = prog->NumInstructions;
362 clone->NumTemporaries = prog->NumTemporaries;
363 clone->NumParameters = prog->NumParameters;
364 clone->NumAttributes = prog->NumAttributes;
365 clone->NumAddressRegs = prog->NumAddressRegs;
366 clone->NumNativeInstructions = prog->NumNativeInstructions;
367 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
368 clone->NumNativeParameters = prog->NumNativeParameters;
369 clone->NumNativeAttributes = prog->NumNativeAttributes;
370 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700371 clone->NumAluInstructions = prog->NumAluInstructions;
372 clone->NumTexInstructions = prog->NumTexInstructions;
373 clone->NumTexIndirections = prog->NumTexIndirections;
374 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
375 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
376 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700377
378 switch (prog->Target) {
379 case GL_VERTEX_PROGRAM_ARB:
380 {
381 const struct gl_vertex_program *vp
382 = (const struct gl_vertex_program *) prog;
383 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
384 vpc->IsPositionInvariant = vp->IsPositionInvariant;
385 }
386 break;
387 case GL_FRAGMENT_PROGRAM_ARB:
388 {
389 const struct gl_fragment_program *fp
390 = (const struct gl_fragment_program *) prog;
391 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700392 fpc->FogOption = fp->FogOption;
393 fpc->UsesKill = fp->UsesKill;
394 }
395 break;
396 default:
397 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
398 }
399
400 return clone;
401}
402
403
404
405/**
Brian Paul77427a12006-08-24 23:11:39 +0000406 * Mixing ARB and NV vertex/fragment programs can be tricky.
407 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
408 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
409 * The two different fragment program targets are supposed to be compatible
410 * to some extent (see GL_ARB_fragment_program spec).
411 * This function does the compatibility check.
412 */
413static GLboolean
414compatible_program_targets(GLenum t1, GLenum t2)
415{
416 if (t1 == t2)
417 return GL_TRUE;
418 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
419 return GL_TRUE;
420 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
421 return GL_TRUE;
422 return GL_FALSE;
423}
424
425
Brian Paul30d6a4b2005-11-05 20:18:18 +0000426
Michal Krol2861e732004-03-29 11:09:34 +0000427/**********************************************************************/
428/* API functions */
429/**********************************************************************/
430
431
432/**
433 * Bind a program (make it current)
434 * \note Called from the GL API dispatcher by both glBindProgramNV
435 * and glBindProgramARB.
436 */
437void GLAPIENTRY
438_mesa_BindProgram(GLenum target, GLuint id)
439{
Brian Paula360bc32006-08-25 17:18:56 +0000440 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000441 GET_CURRENT_CONTEXT(ctx);
442 ASSERT_OUTSIDE_BEGIN_END(ctx);
443
444 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
445
Brian Paula360bc32006-08-25 17:18:56 +0000446 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000447 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
448 (ctx->Extensions.NV_vertex_program ||
449 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000450 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000451 }
452 else if ((target == GL_FRAGMENT_PROGRAM_NV
453 && ctx->Extensions.NV_fragment_program) ||
454 (target == GL_FRAGMENT_PROGRAM_ARB
455 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000456 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000457 }
458 else {
459 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
460 return;
461 }
462
Brian Paula360bc32006-08-25 17:18:56 +0000463 /*
464 * Get pointer to new program to bind.
465 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000466 * That's supposed to be caught in glBegin.
467 */
468 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000469 /* Bind a default program */
470 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000471 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000472 newProg = ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000473 else
Brian Paula360bc32006-08-25 17:18:56 +0000474 newProg = ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000475 }
476 else {
Brian Paula360bc32006-08-25 17:18:56 +0000477 /* Bind a user program */
478 newProg = _mesa_lookup_program(ctx, id);
479 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000480 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000481 newProg = ctx->Driver.NewProgram(ctx, target, id);
482 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000483 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
484 return;
485 }
Brian Paula360bc32006-08-25 17:18:56 +0000486 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000487 }
Brian Paula360bc32006-08-25 17:18:56 +0000488 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000489 _mesa_error(ctx, GL_INVALID_OPERATION,
490 "glBindProgramNV/ARB(target mismatch)");
491 return;
492 }
Michal Krol2861e732004-03-29 11:09:34 +0000493 }
494
Brian Paula360bc32006-08-25 17:18:56 +0000495 /** All error checking is complete now **/
496
497 if (curProg->Id == id) {
498 /* binding same program - no change */
499 return;
500 }
501
502 /* unbind/delete oldProg */
503 if (curProg->Id != 0) {
504 /* decrement refcount on previously bound fragment program */
505 curProg->RefCount--;
506 /* and delete if refcount goes below one */
507 if (curProg->RefCount <= 0) {
508 /* the program ID was already removed from the hash table */
509 ctx->Driver.DeleteProgram(ctx, curProg);
510 }
511 }
512
513 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000514 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000515 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000516 }
Brian Paula360bc32006-08-25 17:18:56 +0000517 else if (target == GL_FRAGMENT_PROGRAM_NV ||
518 target == GL_FRAGMENT_PROGRAM_ARB) {
519 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000520 }
Brian Paula360bc32006-08-25 17:18:56 +0000521 newProg->RefCount++;
Michal Krol2861e732004-03-29 11:09:34 +0000522
Brian Paul765f1a12004-09-14 22:28:27 +0000523 /* Never null pointers */
524 ASSERT(ctx->VertexProgram.Current);
525 ASSERT(ctx->FragmentProgram.Current);
526
Michal Krol2861e732004-03-29 11:09:34 +0000527 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000528 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000529}
530
531
532/**
533 * Delete a list of programs.
534 * \note Not compiled into display lists.
535 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
536 */
537void GLAPIENTRY
538_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
539{
540 GLint i;
541 GET_CURRENT_CONTEXT(ctx);
542 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
543
544 if (n < 0) {
545 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
546 return;
547 }
548
549 for (i = 0; i < n; i++) {
550 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000551 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000552 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000553 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
554 }
555 else if (prog) {
556 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000557 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000558 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
559 if (ctx->VertexProgram.Current &&
560 ctx->VertexProgram.Current->Base.Id == ids[i]) {
561 /* unbind this currently bound program */
562 _mesa_BindProgram(prog->Target, 0);
563 }
564 }
565 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
566 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
567 if (ctx->FragmentProgram.Current &&
568 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
569 /* unbind this currently bound program */
570 _mesa_BindProgram(prog->Target, 0);
571 }
572 }
573 else {
574 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
575 return;
576 }
Brian Paulea2943e2005-01-20 04:02:02 +0000577 /* The ID is immediately available for re-use now */
578 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
579 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000580 if (prog->RefCount <= 0) {
581 ctx->Driver.DeleteProgram(ctx, prog);
582 }
583 }
Michal Krol2861e732004-03-29 11:09:34 +0000584 }
585 }
586}
587
588
589/**
590 * Generate a list of new program identifiers.
591 * \note Not compiled into display lists.
592 * \note Called by both glGenProgramsNV and glGenProgramsARB.
593 */
594void GLAPIENTRY
595_mesa_GenPrograms(GLsizei n, GLuint *ids)
596{
597 GLuint first;
598 GLuint i;
599 GET_CURRENT_CONTEXT(ctx);
600 ASSERT_OUTSIDE_BEGIN_END(ctx);
601
602 if (n < 0) {
603 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
604 return;
605 }
606
607 if (!ids)
608 return;
609
610 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
611
Brian Paul765f1a12004-09-14 22:28:27 +0000612 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000613 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000614 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000615 }
616
617 /* Return the program names */
618 for (i = 0; i < (GLuint) n; i++) {
619 ids[i] = first + i;
620 }
621}