blob: c539b5272041c76d2aca368b98adb1303f6f0f53 [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
Brian4b654d42007-08-23 08:53:43 +0100121/**
122 * Update the default program objects in the given context to reference those
123 * specified in the shared state and release those referencing the old
124 * shared state.
125 */
126void
127_mesa_update_default_objects_program(GLcontext *ctx)
128{
129#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
130 if (ctx->VertexProgram.Current) {
131 ctx->VertexProgram.Current->Base.RefCount--;
132 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
133 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
134 }
135 ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram;
136 assert(ctx->VertexProgram.Current);
137 ctx->VertexProgram.Current->Base.RefCount++;
138#endif
139
140#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
141 if (ctx->FragmentProgram.Current) {
142 ctx->FragmentProgram.Current->Base.RefCount--;
143 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
144 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
145 }
146 ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram;
147 assert(ctx->FragmentProgram.Current);
148 ctx->FragmentProgram.Current->Base.RefCount++;
149#endif
150
151 /* XXX probably move this stuff */
152#if FEATURE_ATI_fragment_shader
153 if (ctx->ATIFragmentShader.Current) {
154 ctx->ATIFragmentShader.Current->RefCount--;
155 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
156 _mesa_free(ctx->ATIFragmentShader.Current);
157 }
158 }
159 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
160 assert(ctx->ATIFragmentShader.Current);
161 ctx->ATIFragmentShader.Current->RefCount++;
162#endif
163}
Brian Paul21841f02004-08-14 14:28:11 +0000164
165
166/**
Michal Krol2861e732004-03-29 11:09:34 +0000167 * Set the vertex/fragment program error state (position and error string).
168 * This is generally called from within the parsers.
169 */
170void
171_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
172{
173 ctx->Program.ErrorPos = pos;
174 _mesa_free((void *) ctx->Program.ErrorString);
175 if (!string)
176 string = "";
177 ctx->Program.ErrorString = _mesa_strdup(string);
178}
179
180
181/**
182 * Find the line number and column for 'pos' within 'string'.
183 * Return a copy of the line which contains 'pos'. Free the line with
184 * _mesa_free().
185 * \param string the program string
186 * \param pos the position within the string
187 * \param line returns the line number corresponding to 'pos'.
188 * \param col returns the column number corresponding to 'pos'.
189 * \return copy of the line containing 'pos'.
190 */
191const GLubyte *
192_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
193 GLint *line, GLint *col)
194{
195 const GLubyte *lineStart = string;
196 const GLubyte *p = string;
197 GLubyte *s;
198 int len;
199
200 *line = 1;
201
202 while (p != pos) {
203 if (*p == (GLubyte) '\n') {
204 (*line)++;
205 lineStart = p + 1;
206 }
207 p++;
208 }
209
210 *col = (pos - lineStart) + 1;
211
212 /* return copy of this line */
213 while (*p != 0 && *p != '\n')
214 p++;
215 len = p - lineStart;
216 s = (GLubyte *) _mesa_malloc(len + 1);
217 _mesa_memcpy(s, lineStart, len);
218 s[len] = 0;
219
220 return s;
221}
222
223
Brian Paul765f1a12004-09-14 22:28:27 +0000224/**
225 * Initialize a new vertex/fragment program object.
226 */
Brian Paul122629f2006-07-20 16:49:57 +0000227static struct gl_program *
228_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000229 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000230{
Brian Paula6c423d2004-08-25 15:59:48 +0000231 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000232 if (prog) {
233 prog->Id = id;
234 prog->Target = target;
235 prog->Resident = GL_TRUE;
236 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000237 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Michal Krol2861e732004-03-29 11:09:34 +0000238 }
239
240 return prog;
241}
242
Brian Paul765f1a12004-09-14 22:28:27 +0000243
244/**
245 * Initialize a new fragment program object.
246 */
Brian Paul122629f2006-07-20 16:49:57 +0000247struct gl_program *
248_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000249 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000250{
251 if (prog)
252 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
253 else
254 return NULL;
255}
256
Brian Paul765f1a12004-09-14 22:28:27 +0000257
258/**
259 * Initialize a new vertex program object.
260 */
Brian Paul122629f2006-07-20 16:49:57 +0000261struct gl_program *
262_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000263 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000264{
265 if (prog)
266 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
267 else
268 return NULL;
269}
270
271
272/**
273 * Allocate and initialize a new fragment/vertex program object but
274 * don't put it into the program hash table. Called via
275 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
276 * device driver function to implement OO deriviation with additional
277 * types not understood by this function.
278 *
279 * \param ctx context
280 * \param id program id/number
281 * \param target program target/type
282 * \return pointer to new program object
283 */
Brian Paul122629f2006-07-20 16:49:57 +0000284struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000285_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
286{
287 switch (target) {
288 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000289 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
290 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000291 case GL_FRAGMENT_PROGRAM_NV:
292 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000293 return _mesa_init_fragment_program(ctx,
294 CALLOC_STRUCT(gl_fragment_program),
295 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000296 default:
297 _mesa_problem(ctx, "bad target in _mesa_new_program");
298 return NULL;
299 }
300}
301
302
303/**
304 * Delete a program and remove it from the hash table, ignoring the
305 * reference count.
306 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
307 * by a device driver function.
308 */
309void
Brian Paul122629f2006-07-20 16:49:57 +0000310_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000311{
Brian Paula6c423d2004-08-25 15:59:48 +0000312 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000313 ASSERT(prog);
314
Brian Paul308b85f2006-11-23 15:58:30 +0000315 if (prog == &_mesa_DummyProgram)
316 return;
317
Michal Krol2861e732004-03-29 11:09:34 +0000318 if (prog->String)
319 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000320
321 if (prog->Instructions) {
322 GLuint i;
323 for (i = 0; i < prog->NumInstructions; i++) {
324 if (prog->Instructions[i].Data)
325 _mesa_free(prog->Instructions[i].Data);
Brian4cc26742007-04-20 08:12:17 -0600326 if (prog->Instructions[i].Comment)
327 _mesa_free((char *) prog->Instructions[i].Comment);
Brian Paul575700f2004-12-16 03:07:18 +0000328 }
Brian Paulde997602005-11-12 17:53:14 +0000329 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000330 }
Brian Paulde997602005-11-12 17:53:14 +0000331
Brian Paul65a51c02006-05-24 03:30:31 +0000332 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000333 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000334 }
Brianfe1d01c2006-12-13 14:54:47 -0700335 if (prog->Varying) {
336 _mesa_free_parameter_list(prog->Varying);
337 }
Brian3493e862007-03-24 16:18:13 -0600338 if (prog->Attributes) {
339 _mesa_free_parameter_list(prog->Attributes);
340 }
Brianfe1d01c2006-12-13 14:54:47 -0700341
Brian Paul58d080b2006-08-25 19:46:31 +0000342 /* XXX this is a little ugly */
343 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
344 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
345 if (vprog->TnlData)
346 _mesa_free(vprog->TnlData);
347 }
348
Michal Krol2861e732004-03-29 11:09:34 +0000349 _mesa_free(prog);
350}
351
352
Brian Paul4d12a052006-08-23 23:10:14 +0000353/**
354 * Return the gl_program object for a given ID.
355 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
356 * casts elsewhere.
357 */
358struct gl_program *
359_mesa_lookup_program(GLcontext *ctx, GLuint id)
360{
361 if (id)
362 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
363 else
364 return NULL;
365}
366
Michal Krol2861e732004-03-29 11:09:34 +0000367
Brian Paul3b9b8de2006-08-24 21:57:36 +0000368/**
Brianb2a3a852006-12-14 13:56:58 -0700369 * Return a copy of a program.
370 * XXX Problem here if the program object is actually OO-derivation
371 * made by a device driver.
372 */
373struct gl_program *
374_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
375{
376 struct gl_program *clone;
377
Brian5b6858c2007-07-24 09:56:44 -0600378 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700379 if (!clone)
380 return NULL;
381
382 assert(clone->Target == prog->Target);
383 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
384 clone->RefCount = 1;
385 clone->Format = prog->Format;
386 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
387 if (!clone->Instructions) {
Briandc6fab92008-03-22 10:27:03 -0600388 ctx->Driver.DeleteProgram(ctx, clone);
Brianb2a3a852006-12-14 13:56:58 -0700389 return NULL;
390 }
Brian12229f12007-03-22 09:11:26 -0600391 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
392 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700393 clone->InputsRead = prog->InputsRead;
394 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700395 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
396
Brian2e76f0a2006-12-19 09:52:07 -0700397 if (prog->Parameters)
398 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700399 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700400 if (prog->Varying)
401 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700402 if (prog->Attributes)
403 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700404 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
405 clone->NumInstructions = prog->NumInstructions;
406 clone->NumTemporaries = prog->NumTemporaries;
407 clone->NumParameters = prog->NumParameters;
408 clone->NumAttributes = prog->NumAttributes;
409 clone->NumAddressRegs = prog->NumAddressRegs;
410 clone->NumNativeInstructions = prog->NumNativeInstructions;
411 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
412 clone->NumNativeParameters = prog->NumNativeParameters;
413 clone->NumNativeAttributes = prog->NumNativeAttributes;
414 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700415 clone->NumAluInstructions = prog->NumAluInstructions;
416 clone->NumTexInstructions = prog->NumTexInstructions;
417 clone->NumTexIndirections = prog->NumTexIndirections;
418 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
419 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
420 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700421
422 switch (prog->Target) {
423 case GL_VERTEX_PROGRAM_ARB:
424 {
425 const struct gl_vertex_program *vp
426 = (const struct gl_vertex_program *) prog;
427 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
428 vpc->IsPositionInvariant = vp->IsPositionInvariant;
429 }
430 break;
431 case GL_FRAGMENT_PROGRAM_ARB:
432 {
433 const struct gl_fragment_program *fp
434 = (const struct gl_fragment_program *) prog;
435 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700436 fpc->FogOption = fp->FogOption;
437 fpc->UsesKill = fp->UsesKill;
438 }
439 break;
440 default:
441 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
442 }
443
444 return clone;
445}
446
447
448
449/**
Brian Paul77427a12006-08-24 23:11:39 +0000450 * Mixing ARB and NV vertex/fragment programs can be tricky.
451 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
452 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
453 * The two different fragment program targets are supposed to be compatible
454 * to some extent (see GL_ARB_fragment_program spec).
455 * This function does the compatibility check.
456 */
457static GLboolean
458compatible_program_targets(GLenum t1, GLenum t2)
459{
460 if (t1 == t2)
461 return GL_TRUE;
462 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
463 return GL_TRUE;
464 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
465 return GL_TRUE;
466 return GL_FALSE;
467}
468
469
Brian Paul30d6a4b2005-11-05 20:18:18 +0000470
Michal Krol2861e732004-03-29 11:09:34 +0000471/**********************************************************************/
472/* API functions */
473/**********************************************************************/
474
475
476/**
477 * Bind a program (make it current)
478 * \note Called from the GL API dispatcher by both glBindProgramNV
479 * and glBindProgramARB.
480 */
481void GLAPIENTRY
482_mesa_BindProgram(GLenum target, GLuint id)
483{
Brian Paula360bc32006-08-25 17:18:56 +0000484 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000485 GET_CURRENT_CONTEXT(ctx);
486 ASSERT_OUTSIDE_BEGIN_END(ctx);
487
488 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
489
Brian Paula360bc32006-08-25 17:18:56 +0000490 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000491 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
492 (ctx->Extensions.NV_vertex_program ||
493 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000494 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000495 }
496 else if ((target == GL_FRAGMENT_PROGRAM_NV
497 && ctx->Extensions.NV_fragment_program) ||
498 (target == GL_FRAGMENT_PROGRAM_ARB
499 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000500 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000501 }
502 else {
503 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
504 return;
505 }
506
Brian Paula360bc32006-08-25 17:18:56 +0000507 /*
508 * Get pointer to new program to bind.
509 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000510 * That's supposed to be caught in glBegin.
511 */
512 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000513 /* Bind a default program */
514 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000515 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000516 newProg = ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000517 else
Brian Paula360bc32006-08-25 17:18:56 +0000518 newProg = ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000519 }
520 else {
Brian Paula360bc32006-08-25 17:18:56 +0000521 /* Bind a user program */
522 newProg = _mesa_lookup_program(ctx, id);
523 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000524 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000525 newProg = ctx->Driver.NewProgram(ctx, target, id);
526 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000527 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
528 return;
529 }
Brian Paula360bc32006-08-25 17:18:56 +0000530 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000531 }
Brian Paula360bc32006-08-25 17:18:56 +0000532 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000533 _mesa_error(ctx, GL_INVALID_OPERATION,
534 "glBindProgramNV/ARB(target mismatch)");
535 return;
536 }
Michal Krol2861e732004-03-29 11:09:34 +0000537 }
538
Brian Paula360bc32006-08-25 17:18:56 +0000539 /** All error checking is complete now **/
540
541 if (curProg->Id == id) {
542 /* binding same program - no change */
543 return;
544 }
545
546 /* unbind/delete oldProg */
547 if (curProg->Id != 0) {
548 /* decrement refcount on previously bound fragment program */
549 curProg->RefCount--;
550 /* and delete if refcount goes below one */
551 if (curProg->RefCount <= 0) {
552 /* the program ID was already removed from the hash table */
553 ctx->Driver.DeleteProgram(ctx, curProg);
554 }
555 }
556
557 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000558 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000559 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000560 }
Brian Paula360bc32006-08-25 17:18:56 +0000561 else if (target == GL_FRAGMENT_PROGRAM_NV ||
562 target == GL_FRAGMENT_PROGRAM_ARB) {
563 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000564 }
Brian Paula360bc32006-08-25 17:18:56 +0000565 newProg->RefCount++;
Michal Krol2861e732004-03-29 11:09:34 +0000566
Brian Paul765f1a12004-09-14 22:28:27 +0000567 /* Never null pointers */
568 ASSERT(ctx->VertexProgram.Current);
569 ASSERT(ctx->FragmentProgram.Current);
570
Michal Krol2861e732004-03-29 11:09:34 +0000571 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000572 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000573}
574
575
576/**
577 * Delete a list of programs.
578 * \note Not compiled into display lists.
579 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
580 */
581void GLAPIENTRY
582_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
583{
584 GLint i;
585 GET_CURRENT_CONTEXT(ctx);
586 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
587
588 if (n < 0) {
589 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
590 return;
591 }
592
593 for (i = 0; i < n; i++) {
594 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000595 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000596 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000597 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
598 }
599 else if (prog) {
600 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000601 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000602 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
603 if (ctx->VertexProgram.Current &&
604 ctx->VertexProgram.Current->Base.Id == ids[i]) {
605 /* unbind this currently bound program */
606 _mesa_BindProgram(prog->Target, 0);
607 }
608 }
609 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
610 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
611 if (ctx->FragmentProgram.Current &&
612 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
613 /* unbind this currently bound program */
614 _mesa_BindProgram(prog->Target, 0);
615 }
616 }
617 else {
618 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
619 return;
620 }
Brian Paulea2943e2005-01-20 04:02:02 +0000621 /* The ID is immediately available for re-use now */
622 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
623 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000624 if (prog->RefCount <= 0) {
625 ctx->Driver.DeleteProgram(ctx, prog);
626 }
627 }
Michal Krol2861e732004-03-29 11:09:34 +0000628 }
629 }
630}
631
632
633/**
634 * Generate a list of new program identifiers.
635 * \note Not compiled into display lists.
636 * \note Called by both glGenProgramsNV and glGenProgramsARB.
637 */
638void GLAPIENTRY
639_mesa_GenPrograms(GLsizei n, GLuint *ids)
640{
641 GLuint first;
642 GLuint i;
643 GET_CURRENT_CONTEXT(ctx);
644 ASSERT_OUTSIDE_BEGIN_END(ctx);
645
646 if (n < 0) {
647 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
648 return;
649 }
650
651 if (!ids)
652 return;
653
654 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
655
Brian Paul765f1a12004-09-14 22:28:27 +0000656 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000657 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000658 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000659 }
660
661 /* Return the program names */
662 for (i = 0; i < (GLuint) n; i++) {
663 ids[i] = first + i;
664 }
665}