blob: bde04b9c29eef95d9572e639120fa129f8b89838 [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
Brian Paul57e222d2008-05-14 12:10:45 -0600123 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
124 (struct gl_vertex_program *)
125 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100126 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100127#endif
128
129#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600130 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
131 (struct gl_fragment_program *)
132 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100133 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100134#endif
135
136 /* XXX probably move this stuff */
137#if FEATURE_ATI_fragment_shader
138 if (ctx->ATIFragmentShader.Current) {
139 ctx->ATIFragmentShader.Current->RefCount--;
140 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
141 _mesa_free(ctx->ATIFragmentShader.Current);
142 }
143 }
144 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
145 assert(ctx->ATIFragmentShader.Current);
146 ctx->ATIFragmentShader.Current->RefCount++;
147#endif
148}
Brian Paul21841f02004-08-14 14:28:11 +0000149
150
151/**
Michal Krol2861e732004-03-29 11:09:34 +0000152 * Set the vertex/fragment program error state (position and error string).
153 * This is generally called from within the parsers.
154 */
155void
156_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
157{
158 ctx->Program.ErrorPos = pos;
159 _mesa_free((void *) ctx->Program.ErrorString);
160 if (!string)
161 string = "";
162 ctx->Program.ErrorString = _mesa_strdup(string);
163}
164
165
166/**
167 * Find the line number and column for 'pos' within 'string'.
168 * Return a copy of the line which contains 'pos'. Free the line with
169 * _mesa_free().
170 * \param string the program string
171 * \param pos the position within the string
172 * \param line returns the line number corresponding to 'pos'.
173 * \param col returns the column number corresponding to 'pos'.
174 * \return copy of the line containing 'pos'.
175 */
176const GLubyte *
177_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
178 GLint *line, GLint *col)
179{
180 const GLubyte *lineStart = string;
181 const GLubyte *p = string;
182 GLubyte *s;
183 int len;
184
185 *line = 1;
186
187 while (p != pos) {
188 if (*p == (GLubyte) '\n') {
189 (*line)++;
190 lineStart = p + 1;
191 }
192 p++;
193 }
194
195 *col = (pos - lineStart) + 1;
196
197 /* return copy of this line */
198 while (*p != 0 && *p != '\n')
199 p++;
200 len = p - lineStart;
201 s = (GLubyte *) _mesa_malloc(len + 1);
202 _mesa_memcpy(s, lineStart, len);
203 s[len] = 0;
204
205 return s;
206}
207
208
Brian Paul765f1a12004-09-14 22:28:27 +0000209/**
210 * Initialize a new vertex/fragment program object.
211 */
Brian Paul122629f2006-07-20 16:49:57 +0000212static struct gl_program *
213_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000214 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000215{
Brian Paula6c423d2004-08-25 15:59:48 +0000216 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000217 if (prog) {
218 prog->Id = id;
219 prog->Target = target;
220 prog->Resident = GL_TRUE;
221 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000222 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Michal Krol2861e732004-03-29 11:09:34 +0000223 }
224
225 return prog;
226}
227
Brian Paul765f1a12004-09-14 22:28:27 +0000228
229/**
230 * Initialize a new fragment program object.
231 */
Brian Paul122629f2006-07-20 16:49:57 +0000232struct gl_program *
233_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000234 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000235{
236 if (prog)
237 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
238 else
239 return NULL;
240}
241
Brian Paul765f1a12004-09-14 22:28:27 +0000242
243/**
244 * Initialize a new vertex program object.
245 */
Brian Paul122629f2006-07-20 16:49:57 +0000246struct gl_program *
247_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000248 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000249{
250 if (prog)
251 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
252 else
253 return NULL;
254}
255
256
257/**
258 * Allocate and initialize a new fragment/vertex program object but
259 * don't put it into the program hash table. Called via
260 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
261 * device driver function to implement OO deriviation with additional
262 * types not understood by this function.
263 *
264 * \param ctx context
265 * \param id program id/number
266 * \param target program target/type
267 * \return pointer to new program object
268 */
Brian Paul122629f2006-07-20 16:49:57 +0000269struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000270_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
271{
272 switch (target) {
273 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000274 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
275 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000276 case GL_FRAGMENT_PROGRAM_NV:
277 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000278 return _mesa_init_fragment_program(ctx,
279 CALLOC_STRUCT(gl_fragment_program),
280 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000281 default:
282 _mesa_problem(ctx, "bad target in _mesa_new_program");
283 return NULL;
284 }
285}
286
287
288/**
289 * Delete a program and remove it from the hash table, ignoring the
290 * reference count.
291 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
292 * by a device driver function.
293 */
294void
Brian Paul122629f2006-07-20 16:49:57 +0000295_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000296{
Brian Paula6c423d2004-08-25 15:59:48 +0000297 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600298 ASSERT(prog);
299 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000300
Brian Paul308b85f2006-11-23 15:58:30 +0000301 if (prog == &_mesa_DummyProgram)
302 return;
303
Michal Krol2861e732004-03-29 11:09:34 +0000304 if (prog->String)
305 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000306
307 if (prog->Instructions) {
308 GLuint i;
309 for (i = 0; i < prog->NumInstructions; i++) {
310 if (prog->Instructions[i].Data)
311 _mesa_free(prog->Instructions[i].Data);
Brian4cc26742007-04-20 08:12:17 -0600312 if (prog->Instructions[i].Comment)
313 _mesa_free((char *) prog->Instructions[i].Comment);
Brian Paul575700f2004-12-16 03:07:18 +0000314 }
Brian Paulde997602005-11-12 17:53:14 +0000315 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000316 }
Brian Paulde997602005-11-12 17:53:14 +0000317
Brian Paul65a51c02006-05-24 03:30:31 +0000318 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000319 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000320 }
Brianfe1d01c2006-12-13 14:54:47 -0700321 if (prog->Varying) {
322 _mesa_free_parameter_list(prog->Varying);
323 }
Brian3493e862007-03-24 16:18:13 -0600324 if (prog->Attributes) {
325 _mesa_free_parameter_list(prog->Attributes);
326 }
Brianfe1d01c2006-12-13 14:54:47 -0700327
Brian Paul58d080b2006-08-25 19:46:31 +0000328 /* XXX this is a little ugly */
329 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
330 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
331 if (vprog->TnlData)
332 _mesa_free(vprog->TnlData);
333 }
334
Michal Krol2861e732004-03-29 11:09:34 +0000335 _mesa_free(prog);
336}
337
338
Brian Paul4d12a052006-08-23 23:10:14 +0000339/**
340 * Return the gl_program object for a given ID.
341 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
342 * casts elsewhere.
343 */
344struct gl_program *
345_mesa_lookup_program(GLcontext *ctx, GLuint id)
346{
347 if (id)
348 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
349 else
350 return NULL;
351}
352
Michal Krol2861e732004-03-29 11:09:34 +0000353
Brian Paul3b9b8de2006-08-24 21:57:36 +0000354/**
Briandf43fb62008-05-06 23:08:51 -0600355 * Reference counting for vertex/fragment programs
356 */
357void
358_mesa_reference_program(GLcontext *ctx,
359 struct gl_program **ptr,
360 struct gl_program *prog)
361{
362 assert(ptr);
363 if (*ptr && prog) {
364 /* sanity check */
365 ASSERT((*ptr)->Target == prog->Target);
366 }
367 if (*ptr == prog) {
368 return; /* no change */
369 }
370 if (*ptr) {
371 GLboolean deleteFlag;
372
373 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600374#if 0
Briandf43fb62008-05-06 23:08:51 -0600375 printf("Program %p %u 0x%x Refcount-- to %d\n",
376 *ptr, (*ptr)->Id, (*ptr)->Target, (*ptr)->RefCount - 1);
377#endif
378 ASSERT((*ptr)->RefCount > 0);
379 (*ptr)->RefCount--;
380
381 deleteFlag = ((*ptr)->RefCount == 0);
382 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
383
384 if (deleteFlag) {
385 ASSERT(ctx);
386 ctx->Driver.DeleteProgram(ctx, *ptr);
387 }
388
389 *ptr = NULL;
390 }
391
392 assert(!*ptr);
393 if (prog) {
394 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
395 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600396#if 0
Briandf43fb62008-05-06 23:08:51 -0600397 printf("Program %p %u 0x%x Refcount++ to %d\n",
398 prog, prog->Id, prog->Target, prog->RefCount);
399#endif
400 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
401 }
402
403 *ptr = prog;
404}
405
406
407/**
Brianb2a3a852006-12-14 13:56:58 -0700408 * Return a copy of a program.
409 * XXX Problem here if the program object is actually OO-derivation
410 * made by a device driver.
411 */
412struct gl_program *
413_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
414{
415 struct gl_program *clone;
416
Brian5b6858c2007-07-24 09:56:44 -0600417 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700418 if (!clone)
419 return NULL;
420
421 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600422 assert(clone->RefCount == 1);
423
Brianb2a3a852006-12-14 13:56:58 -0700424 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700425 clone->Format = prog->Format;
426 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
427 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600428 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700429 return NULL;
430 }
Brian12229f12007-03-22 09:11:26 -0600431 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
432 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700433 clone->InputsRead = prog->InputsRead;
434 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700435 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
436
Brian2e76f0a2006-12-19 09:52:07 -0700437 if (prog->Parameters)
438 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700439 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700440 if (prog->Varying)
441 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700442 if (prog->Attributes)
443 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700444 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
445 clone->NumInstructions = prog->NumInstructions;
446 clone->NumTemporaries = prog->NumTemporaries;
447 clone->NumParameters = prog->NumParameters;
448 clone->NumAttributes = prog->NumAttributes;
449 clone->NumAddressRegs = prog->NumAddressRegs;
450 clone->NumNativeInstructions = prog->NumNativeInstructions;
451 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
452 clone->NumNativeParameters = prog->NumNativeParameters;
453 clone->NumNativeAttributes = prog->NumNativeAttributes;
454 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700455 clone->NumAluInstructions = prog->NumAluInstructions;
456 clone->NumTexInstructions = prog->NumTexInstructions;
457 clone->NumTexIndirections = prog->NumTexIndirections;
458 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
459 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
460 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700461
462 switch (prog->Target) {
463 case GL_VERTEX_PROGRAM_ARB:
464 {
465 const struct gl_vertex_program *vp
466 = (const struct gl_vertex_program *) prog;
467 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
468 vpc->IsPositionInvariant = vp->IsPositionInvariant;
469 }
470 break;
471 case GL_FRAGMENT_PROGRAM_ARB:
472 {
473 const struct gl_fragment_program *fp
474 = (const struct gl_fragment_program *) prog;
475 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700476 fpc->FogOption = fp->FogOption;
477 fpc->UsesKill = fp->UsesKill;
478 }
479 break;
480 default:
481 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
482 }
483
484 return clone;
485}
486
487
488
489/**
Brian Paul77427a12006-08-24 23:11:39 +0000490 * Mixing ARB and NV vertex/fragment programs can be tricky.
491 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
492 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
493 * The two different fragment program targets are supposed to be compatible
494 * to some extent (see GL_ARB_fragment_program spec).
495 * This function does the compatibility check.
496 */
497static GLboolean
498compatible_program_targets(GLenum t1, GLenum t2)
499{
500 if (t1 == t2)
501 return GL_TRUE;
502 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
503 return GL_TRUE;
504 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
505 return GL_TRUE;
506 return GL_FALSE;
507}
508
509
Brian Paul30d6a4b2005-11-05 20:18:18 +0000510
Michal Krol2861e732004-03-29 11:09:34 +0000511/**********************************************************************/
512/* API functions */
513/**********************************************************************/
514
515
516/**
517 * Bind a program (make it current)
518 * \note Called from the GL API dispatcher by both glBindProgramNV
519 * and glBindProgramARB.
520 */
521void GLAPIENTRY
522_mesa_BindProgram(GLenum target, GLuint id)
523{
Brian Paula360bc32006-08-25 17:18:56 +0000524 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000525 GET_CURRENT_CONTEXT(ctx);
526 ASSERT_OUTSIDE_BEGIN_END(ctx);
527
528 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
529
Brian Paula360bc32006-08-25 17:18:56 +0000530 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000531 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
532 (ctx->Extensions.NV_vertex_program ||
533 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000534 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000535 }
536 else if ((target == GL_FRAGMENT_PROGRAM_NV
537 && ctx->Extensions.NV_fragment_program) ||
538 (target == GL_FRAGMENT_PROGRAM_ARB
539 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000540 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000541 }
542 else {
543 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
544 return;
545 }
546
Brian Paula360bc32006-08-25 17:18:56 +0000547 /*
548 * Get pointer to new program to bind.
549 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000550 * That's supposed to be caught in glBegin.
551 */
552 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000553 /* Bind a default program */
554 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000555 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600556 newProg = &ctx->Shared->DefaultVertexProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000557 else
Briandf43fb62008-05-06 23:08:51 -0600558 newProg = &ctx->Shared->DefaultFragmentProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000559 }
560 else {
Brian Paula360bc32006-08-25 17:18:56 +0000561 /* Bind a user program */
562 newProg = _mesa_lookup_program(ctx, id);
563 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000564 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000565 newProg = ctx->Driver.NewProgram(ctx, target, id);
566 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000567 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
568 return;
569 }
Brian Paula360bc32006-08-25 17:18:56 +0000570 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000571 }
Brian Paula360bc32006-08-25 17:18:56 +0000572 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000573 _mesa_error(ctx, GL_INVALID_OPERATION,
574 "glBindProgramNV/ARB(target mismatch)");
575 return;
576 }
Michal Krol2861e732004-03-29 11:09:34 +0000577 }
578
Brian Paula360bc32006-08-25 17:18:56 +0000579 /** All error checking is complete now **/
580
581 if (curProg->Id == id) {
582 /* binding same program - no change */
583 return;
584 }
585
Brian Paula360bc32006-08-25 17:18:56 +0000586 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000587 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600588 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
589 (struct gl_vertex_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000590 }
Brian Paula360bc32006-08-25 17:18:56 +0000591 else if (target == GL_FRAGMENT_PROGRAM_NV ||
592 target == GL_FRAGMENT_PROGRAM_ARB) {
Briandf43fb62008-05-06 23:08:51 -0600593 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
594 (struct gl_fragment_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000595 }
596
Brian Paul765f1a12004-09-14 22:28:27 +0000597 /* Never null pointers */
598 ASSERT(ctx->VertexProgram.Current);
599 ASSERT(ctx->FragmentProgram.Current);
600
Michal Krol2861e732004-03-29 11:09:34 +0000601 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000602 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000603}
604
605
606/**
607 * Delete a list of programs.
608 * \note Not compiled into display lists.
609 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
610 */
611void GLAPIENTRY
612_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
613{
614 GLint i;
615 GET_CURRENT_CONTEXT(ctx);
616 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
617
618 if (n < 0) {
619 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
620 return;
621 }
622
623 for (i = 0; i < n; i++) {
624 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000625 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000626 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000627 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
628 }
629 else if (prog) {
630 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000631 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000632 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
633 if (ctx->VertexProgram.Current &&
634 ctx->VertexProgram.Current->Base.Id == ids[i]) {
635 /* unbind this currently bound program */
636 _mesa_BindProgram(prog->Target, 0);
637 }
638 }
639 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
640 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
641 if (ctx->FragmentProgram.Current &&
642 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
643 /* unbind this currently bound program */
644 _mesa_BindProgram(prog->Target, 0);
645 }
646 }
647 else {
648 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
649 return;
650 }
Brian Paulea2943e2005-01-20 04:02:02 +0000651 /* The ID is immediately available for re-use now */
652 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
Briandf43fb62008-05-06 23:08:51 -0600653 _mesa_reference_program(ctx, &prog, NULL);
Michal Krol2861e732004-03-29 11:09:34 +0000654 }
Michal Krol2861e732004-03-29 11:09:34 +0000655 }
656 }
657}
658
659
660/**
661 * Generate a list of new program identifiers.
662 * \note Not compiled into display lists.
663 * \note Called by both glGenProgramsNV and glGenProgramsARB.
664 */
665void GLAPIENTRY
666_mesa_GenPrograms(GLsizei n, GLuint *ids)
667{
668 GLuint first;
669 GLuint i;
670 GET_CURRENT_CONTEXT(ctx);
671 ASSERT_OUTSIDE_BEGIN_END(ctx);
672
673 if (n < 0) {
674 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
675 return;
676 }
677
678 if (!ids)
679 return;
680
681 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
682
Brian Paul765f1a12004-09-14 22:28:27 +0000683 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000684 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000685 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000686 }
687
688 /* Return the program names */
689 for (i = 0; i < (GLuint) n; i++) {
690 ids[i] = first + i;
691 }
692}