blob: ce64519448e83aa6a39c6c21bdfd7bf9eb6b3aea [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;
Briandf43fb62008-05-06 23:08:51 -060062 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
63 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000064 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000065 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;
Briandf43fb62008-05-06 23:08:51 -060073 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
74 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +000075 assert(ctx->FragmentProgram.Current);
76 ctx->FragmentProgram.Current->Base.RefCount++;
77#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000078
Brian Paul63d68302005-11-19 16:43:04 +000079 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000080#if FEATURE_ATI_fragment_shader
81 ctx->ATIFragmentShader.Enabled = GL_FALSE;
82 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
83 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +000084 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +000085#endif
Michal Krol2861e732004-03-29 11:09:34 +000086}
87
88
89/**
Brian Paul21841f02004-08-14 14:28:11 +000090 * Free a context's vertex/fragment program state
91 */
92void
93_mesa_free_program_data(GLcontext *ctx)
94{
Roland Scheidegger93da6732006-03-01 23:11:14 +000095#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -060096 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian Paul21841f02004-08-14 14:28:11 +000097#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +000098#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -060099 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Brian Paul21841f02004-08-14 14:28:11 +0000100#endif
Brian Paul63d68302005-11-19 16:43:04 +0000101 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000102#if FEATURE_ATI_fragment_shader
103 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000104 ctx->ATIFragmentShader.Current->RefCount--;
105 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
106 _mesa_free(ctx->ATIFragmentShader.Current);
107 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000108 }
109#endif
Brian Paul21841f02004-08-14 14:28:11 +0000110 _mesa_free((void *) ctx->Program.ErrorString);
111}
112
113
Brian4b654d42007-08-23 08:53:43 +0100114/**
115 * Update the default program objects in the given context to reference those
116 * specified in the shared state and release those referencing the old
117 * shared state.
118 */
119void
120_mesa_update_default_objects_program(GLcontext *ctx)
121{
122#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
123 if (ctx->VertexProgram.Current) {
124 ctx->VertexProgram.Current->Base.RefCount--;
125 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
126 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
127 }
128 ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram;
129 assert(ctx->VertexProgram.Current);
130 ctx->VertexProgram.Current->Base.RefCount++;
131#endif
132
133#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
134 if (ctx->FragmentProgram.Current) {
135 ctx->FragmentProgram.Current->Base.RefCount--;
136 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
137 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
138 }
139 ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram;
140 assert(ctx->FragmentProgram.Current);
141 ctx->FragmentProgram.Current->Base.RefCount++;
142#endif
143
144 /* XXX probably move this stuff */
145#if FEATURE_ATI_fragment_shader
146 if (ctx->ATIFragmentShader.Current) {
147 ctx->ATIFragmentShader.Current->RefCount--;
148 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
149 _mesa_free(ctx->ATIFragmentShader.Current);
150 }
151 }
152 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
153 assert(ctx->ATIFragmentShader.Current);
154 ctx->ATIFragmentShader.Current->RefCount++;
155#endif
156}
Brian Paul21841f02004-08-14 14:28:11 +0000157
158
159/**
Michal Krol2861e732004-03-29 11:09:34 +0000160 * Set the vertex/fragment program error state (position and error string).
161 * This is generally called from within the parsers.
162 */
163void
164_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
165{
166 ctx->Program.ErrorPos = pos;
167 _mesa_free((void *) ctx->Program.ErrorString);
168 if (!string)
169 string = "";
170 ctx->Program.ErrorString = _mesa_strdup(string);
171}
172
173
174/**
175 * Find the line number and column for 'pos' within 'string'.
176 * Return a copy of the line which contains 'pos'. Free the line with
177 * _mesa_free().
178 * \param string the program string
179 * \param pos the position within the string
180 * \param line returns the line number corresponding to 'pos'.
181 * \param col returns the column number corresponding to 'pos'.
182 * \return copy of the line containing 'pos'.
183 */
184const GLubyte *
185_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
186 GLint *line, GLint *col)
187{
188 const GLubyte *lineStart = string;
189 const GLubyte *p = string;
190 GLubyte *s;
191 int len;
192
193 *line = 1;
194
195 while (p != pos) {
196 if (*p == (GLubyte) '\n') {
197 (*line)++;
198 lineStart = p + 1;
199 }
200 p++;
201 }
202
203 *col = (pos - lineStart) + 1;
204
205 /* return copy of this line */
206 while (*p != 0 && *p != '\n')
207 p++;
208 len = p - lineStart;
209 s = (GLubyte *) _mesa_malloc(len + 1);
210 _mesa_memcpy(s, lineStart, len);
211 s[len] = 0;
212
213 return s;
214}
215
216
Brian Paul765f1a12004-09-14 22:28:27 +0000217/**
218 * Initialize a new vertex/fragment program object.
219 */
Brian Paul122629f2006-07-20 16:49:57 +0000220static struct gl_program *
221_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000222 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000223{
Brian Paula6c423d2004-08-25 15:59:48 +0000224 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000225 if (prog) {
226 prog->Id = id;
227 prog->Target = target;
228 prog->Resident = GL_TRUE;
229 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000230 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Michal Krol2861e732004-03-29 11:09:34 +0000231 }
232
233 return prog;
234}
235
Brian Paul765f1a12004-09-14 22:28:27 +0000236
237/**
238 * Initialize a new fragment program object.
239 */
Brian Paul122629f2006-07-20 16:49:57 +0000240struct gl_program *
241_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000242 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000243{
244 if (prog)
245 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
246 else
247 return NULL;
248}
249
Brian Paul765f1a12004-09-14 22:28:27 +0000250
251/**
252 * Initialize a new vertex program object.
253 */
Brian Paul122629f2006-07-20 16:49:57 +0000254struct gl_program *
255_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000256 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000257{
258 if (prog)
259 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
260 else
261 return NULL;
262}
263
264
265/**
266 * Allocate and initialize a new fragment/vertex program object but
267 * don't put it into the program hash table. Called via
268 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
269 * device driver function to implement OO deriviation with additional
270 * types not understood by this function.
271 *
272 * \param ctx context
273 * \param id program id/number
274 * \param target program target/type
275 * \return pointer to new program object
276 */
Brian Paul122629f2006-07-20 16:49:57 +0000277struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000278_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
279{
280 switch (target) {
281 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000282 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
283 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000284 case GL_FRAGMENT_PROGRAM_NV:
285 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000286 return _mesa_init_fragment_program(ctx,
287 CALLOC_STRUCT(gl_fragment_program),
288 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000289 default:
290 _mesa_problem(ctx, "bad target in _mesa_new_program");
291 return NULL;
292 }
293}
294
295
296/**
297 * Delete a program and remove it from the hash table, ignoring the
298 * reference count.
299 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
300 * by a device driver function.
301 */
302void
Brian Paul122629f2006-07-20 16:49:57 +0000303_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000304{
Brian Paula6c423d2004-08-25 15:59:48 +0000305 (void) ctx;
Brian Paul5b5c9312008-05-07 18:51:44 -0600306 ASSERT(prog); assert(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000307
Brian Paul308b85f2006-11-23 15:58:30 +0000308 if (prog == &_mesa_DummyProgram)
309 return;
310
Michal Krol2861e732004-03-29 11:09:34 +0000311 if (prog->String)
312 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000313
314 if (prog->Instructions) {
315 GLuint i;
316 for (i = 0; i < prog->NumInstructions; i++) {
317 if (prog->Instructions[i].Data)
318 _mesa_free(prog->Instructions[i].Data);
Brian4cc26742007-04-20 08:12:17 -0600319 if (prog->Instructions[i].Comment)
320 _mesa_free((char *) prog->Instructions[i].Comment);
Brian Paul575700f2004-12-16 03:07:18 +0000321 }
Brian Paulde997602005-11-12 17:53:14 +0000322 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000323 }
Brian Paulde997602005-11-12 17:53:14 +0000324
Brian Paul65a51c02006-05-24 03:30:31 +0000325 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000326 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000327 }
Brianfe1d01c2006-12-13 14:54:47 -0700328 if (prog->Varying) {
329 _mesa_free_parameter_list(prog->Varying);
330 }
Brian3493e862007-03-24 16:18:13 -0600331 if (prog->Attributes) {
332 _mesa_free_parameter_list(prog->Attributes);
333 }
Brianfe1d01c2006-12-13 14:54:47 -0700334
Brian Paul58d080b2006-08-25 19:46:31 +0000335 /* XXX this is a little ugly */
336 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
337 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
338 if (vprog->TnlData)
339 _mesa_free(vprog->TnlData);
340 }
341
Michal Krol2861e732004-03-29 11:09:34 +0000342 _mesa_free(prog);
343}
344
345
Brian Paul4d12a052006-08-23 23:10:14 +0000346/**
347 * Return the gl_program object for a given ID.
348 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
349 * casts elsewhere.
350 */
351struct gl_program *
352_mesa_lookup_program(GLcontext *ctx, GLuint id)
353{
354 if (id)
355 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
356 else
357 return NULL;
358}
359
Michal Krol2861e732004-03-29 11:09:34 +0000360
Brian Paul3b9b8de2006-08-24 21:57:36 +0000361/**
Briandf43fb62008-05-06 23:08:51 -0600362 * Reference counting for vertex/fragment programs
363 */
364void
365_mesa_reference_program(GLcontext *ctx,
366 struct gl_program **ptr,
367 struct gl_program *prog)
368{
369 assert(ptr);
370 if (*ptr && prog) {
371 /* sanity check */
372 ASSERT((*ptr)->Target == prog->Target);
373 }
374 if (*ptr == prog) {
375 return; /* no change */
376 }
377 if (*ptr) {
378 GLboolean deleteFlag;
379
380 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600381#if 0
Briandf43fb62008-05-06 23:08:51 -0600382 printf("Program %p %u 0x%x Refcount-- to %d\n",
383 *ptr, (*ptr)->Id, (*ptr)->Target, (*ptr)->RefCount - 1);
384#endif
385 ASSERT((*ptr)->RefCount > 0);
386 (*ptr)->RefCount--;
387
388 deleteFlag = ((*ptr)->RefCount == 0);
389 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
390
391 if (deleteFlag) {
392 ASSERT(ctx);
393 ctx->Driver.DeleteProgram(ctx, *ptr);
394 }
395
396 *ptr = NULL;
397 }
398
399 assert(!*ptr);
400 if (prog) {
401 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
402 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600403#if 0
Briandf43fb62008-05-06 23:08:51 -0600404 printf("Program %p %u 0x%x Refcount++ to %d\n",
405 prog, prog->Id, prog->Target, prog->RefCount);
406#endif
407 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
408 }
409
410 *ptr = prog;
411}
412
413
414/**
Brianb2a3a852006-12-14 13:56:58 -0700415 * Return a copy of a program.
416 * XXX Problem here if the program object is actually OO-derivation
417 * made by a device driver.
418 */
419struct gl_program *
420_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
421{
422 struct gl_program *clone;
423
Brian5b6858c2007-07-24 09:56:44 -0600424 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700425 if (!clone)
426 return NULL;
427
428 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600429 assert(clone->RefCount == 1);
430
Brianb2a3a852006-12-14 13:56:58 -0700431 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700432 clone->Format = prog->Format;
433 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
434 if (!clone->Instructions) {
Briandc6fab92008-03-22 10:27:03 -0600435 ctx->Driver.DeleteProgram(ctx, clone);
Brianb2a3a852006-12-14 13:56:58 -0700436 return NULL;
437 }
Brian12229f12007-03-22 09:11:26 -0600438 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
439 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700440 clone->InputsRead = prog->InputsRead;
441 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700442 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
443
Brian2e76f0a2006-12-19 09:52:07 -0700444 if (prog->Parameters)
445 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700446 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700447 if (prog->Varying)
448 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700449 if (prog->Attributes)
450 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700451 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
452 clone->NumInstructions = prog->NumInstructions;
453 clone->NumTemporaries = prog->NumTemporaries;
454 clone->NumParameters = prog->NumParameters;
455 clone->NumAttributes = prog->NumAttributes;
456 clone->NumAddressRegs = prog->NumAddressRegs;
457 clone->NumNativeInstructions = prog->NumNativeInstructions;
458 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
459 clone->NumNativeParameters = prog->NumNativeParameters;
460 clone->NumNativeAttributes = prog->NumNativeAttributes;
461 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700462 clone->NumAluInstructions = prog->NumAluInstructions;
463 clone->NumTexInstructions = prog->NumTexInstructions;
464 clone->NumTexIndirections = prog->NumTexIndirections;
465 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
466 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
467 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700468
469 switch (prog->Target) {
470 case GL_VERTEX_PROGRAM_ARB:
471 {
472 const struct gl_vertex_program *vp
473 = (const struct gl_vertex_program *) prog;
474 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
475 vpc->IsPositionInvariant = vp->IsPositionInvariant;
476 }
477 break;
478 case GL_FRAGMENT_PROGRAM_ARB:
479 {
480 const struct gl_fragment_program *fp
481 = (const struct gl_fragment_program *) prog;
482 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700483 fpc->FogOption = fp->FogOption;
484 fpc->UsesKill = fp->UsesKill;
485 }
486 break;
487 default:
488 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
489 }
490
491 return clone;
492}
493
494
495
496/**
Brian Paul77427a12006-08-24 23:11:39 +0000497 * Mixing ARB and NV vertex/fragment programs can be tricky.
498 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
499 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
500 * The two different fragment program targets are supposed to be compatible
501 * to some extent (see GL_ARB_fragment_program spec).
502 * This function does the compatibility check.
503 */
504static GLboolean
505compatible_program_targets(GLenum t1, GLenum t2)
506{
507 if (t1 == t2)
508 return GL_TRUE;
509 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
510 return GL_TRUE;
511 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
512 return GL_TRUE;
513 return GL_FALSE;
514}
515
516
Brian Paul30d6a4b2005-11-05 20:18:18 +0000517
Michal Krol2861e732004-03-29 11:09:34 +0000518/**********************************************************************/
519/* API functions */
520/**********************************************************************/
521
522
523/**
524 * Bind a program (make it current)
525 * \note Called from the GL API dispatcher by both glBindProgramNV
526 * and glBindProgramARB.
527 */
528void GLAPIENTRY
529_mesa_BindProgram(GLenum target, GLuint id)
530{
Brian Paula360bc32006-08-25 17:18:56 +0000531 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000532 GET_CURRENT_CONTEXT(ctx);
533 ASSERT_OUTSIDE_BEGIN_END(ctx);
534
535 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
536
Brian Paula360bc32006-08-25 17:18:56 +0000537 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000538 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
539 (ctx->Extensions.NV_vertex_program ||
540 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000541 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000542 }
543 else if ((target == GL_FRAGMENT_PROGRAM_NV
544 && ctx->Extensions.NV_fragment_program) ||
545 (target == GL_FRAGMENT_PROGRAM_ARB
546 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000547 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000548 }
549 else {
550 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
551 return;
552 }
553
Brian Paula360bc32006-08-25 17:18:56 +0000554 /*
555 * Get pointer to new program to bind.
556 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000557 * That's supposed to be caught in glBegin.
558 */
559 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000560 /* Bind a default program */
561 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000562 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600563 newProg = &ctx->Shared->DefaultVertexProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000564 else
Briandf43fb62008-05-06 23:08:51 -0600565 newProg = &ctx->Shared->DefaultFragmentProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000566 }
567 else {
Brian Paula360bc32006-08-25 17:18:56 +0000568 /* Bind a user program */
569 newProg = _mesa_lookup_program(ctx, id);
570 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000571 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000572 newProg = ctx->Driver.NewProgram(ctx, target, id);
573 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000574 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
575 return;
576 }
Brian Paula360bc32006-08-25 17:18:56 +0000577 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000578 }
Brian Paula360bc32006-08-25 17:18:56 +0000579 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000580 _mesa_error(ctx, GL_INVALID_OPERATION,
581 "glBindProgramNV/ARB(target mismatch)");
582 return;
583 }
Michal Krol2861e732004-03-29 11:09:34 +0000584 }
585
Brian Paula360bc32006-08-25 17:18:56 +0000586 /** All error checking is complete now **/
587
588 if (curProg->Id == id) {
589 /* binding same program - no change */
590 return;
591 }
592
Brian Paula360bc32006-08-25 17:18:56 +0000593 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000594 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600595 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
596 (struct gl_vertex_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000597 }
Brian Paula360bc32006-08-25 17:18:56 +0000598 else if (target == GL_FRAGMENT_PROGRAM_NV ||
599 target == GL_FRAGMENT_PROGRAM_ARB) {
Briandf43fb62008-05-06 23:08:51 -0600600 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
601 (struct gl_fragment_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000602 }
603
Brian Paul765f1a12004-09-14 22:28:27 +0000604 /* Never null pointers */
605 ASSERT(ctx->VertexProgram.Current);
606 ASSERT(ctx->FragmentProgram.Current);
607
Michal Krol2861e732004-03-29 11:09:34 +0000608 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000609 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000610}
611
612
613/**
614 * Delete a list of programs.
615 * \note Not compiled into display lists.
616 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
617 */
618void GLAPIENTRY
619_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
620{
621 GLint i;
622 GET_CURRENT_CONTEXT(ctx);
623 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
624
625 if (n < 0) {
626 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
627 return;
628 }
629
630 for (i = 0; i < n; i++) {
631 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000632 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000633 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000634 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
635 }
636 else if (prog) {
637 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000638 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000639 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
640 if (ctx->VertexProgram.Current &&
641 ctx->VertexProgram.Current->Base.Id == ids[i]) {
642 /* unbind this currently bound program */
643 _mesa_BindProgram(prog->Target, 0);
644 }
645 }
646 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
647 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
648 if (ctx->FragmentProgram.Current &&
649 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
650 /* unbind this currently bound program */
651 _mesa_BindProgram(prog->Target, 0);
652 }
653 }
654 else {
655 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
656 return;
657 }
Brian Paulea2943e2005-01-20 04:02:02 +0000658 /* The ID is immediately available for re-use now */
659 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
Briandf43fb62008-05-06 23:08:51 -0600660 _mesa_reference_program(ctx, &prog, NULL);
Michal Krol2861e732004-03-29 11:09:34 +0000661 }
Michal Krol2861e732004-03-29 11:09:34 +0000662 }
663 }
664}
665
666
667/**
668 * Generate a list of new program identifiers.
669 * \note Not compiled into display lists.
670 * \note Called by both glGenProgramsNV and glGenProgramsARB.
671 */
672void GLAPIENTRY
673_mesa_GenPrograms(GLsizei n, GLuint *ids)
674{
675 GLuint first;
676 GLuint i;
677 GET_CURRENT_CONTEXT(ctx);
678 ASSERT_OUTSIDE_BEGIN_END(ctx);
679
680 if (n < 0) {
681 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
682 return;
683 }
684
685 if (!ids)
686 return;
687
688 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
689
Brian Paul765f1a12004-09-14 22:28:27 +0000690 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000691 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000692 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000693 }
694
695 /* Return the program names */
696 for (i = 0; i < (GLuint) n; i++) {
697 ids[i] = first + i;
698 }
699}