blob: adc7000995543e309cf7629dea9851456baf3e18 [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);
Michal Krol2861e732004-03-29 11:09:34 +000076#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
Briandf43fb62008-05-06 23:08:51 -060095 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian Paul21841f02004-08-14 14:28:11 +000096#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +000097#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -060098 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Brian Paul21841f02004-08-14 14:28:11 +000099#endif
Brian Paul63d68302005-11-19 16:43:04 +0000100 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000101#if FEATURE_ATI_fragment_shader
102 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000103 ctx->ATIFragmentShader.Current->RefCount--;
104 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
105 _mesa_free(ctx->ATIFragmentShader.Current);
106 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000107 }
108#endif
Brian Paul21841f02004-08-14 14:28:11 +0000109 _mesa_free((void *) ctx->Program.ErrorString);
110}
111
112
Brian4b654d42007-08-23 08:53:43 +0100113/**
114 * Update the default program objects in the given context to reference those
115 * specified in the shared state and release those referencing the old
116 * shared state.
117 */
118void
119_mesa_update_default_objects_program(GLcontext *ctx)
120{
121#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600122 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
123 (struct gl_vertex_program *)
124 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100125 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100126#endif
127
128#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600129 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
130 (struct gl_fragment_program *)
131 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100132 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100133#endif
134
135 /* XXX probably move this stuff */
136#if FEATURE_ATI_fragment_shader
137 if (ctx->ATIFragmentShader.Current) {
138 ctx->ATIFragmentShader.Current->RefCount--;
139 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
140 _mesa_free(ctx->ATIFragmentShader.Current);
141 }
142 }
143 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
144 assert(ctx->ATIFragmentShader.Current);
145 ctx->ATIFragmentShader.Current->RefCount++;
146#endif
147}
Brian Paul21841f02004-08-14 14:28:11 +0000148
149
150/**
Michal Krol2861e732004-03-29 11:09:34 +0000151 * Set the vertex/fragment program error state (position and error string).
152 * This is generally called from within the parsers.
153 */
154void
155_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
156{
157 ctx->Program.ErrorPos = pos;
158 _mesa_free((void *) ctx->Program.ErrorString);
159 if (!string)
160 string = "";
161 ctx->Program.ErrorString = _mesa_strdup(string);
162}
163
164
165/**
166 * Find the line number and column for 'pos' within 'string'.
167 * Return a copy of the line which contains 'pos'. Free the line with
168 * _mesa_free().
169 * \param string the program string
170 * \param pos the position within the string
171 * \param line returns the line number corresponding to 'pos'.
172 * \param col returns the column number corresponding to 'pos'.
173 * \return copy of the line containing 'pos'.
174 */
175const GLubyte *
176_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
177 GLint *line, GLint *col)
178{
179 const GLubyte *lineStart = string;
180 const GLubyte *p = string;
181 GLubyte *s;
182 int len;
183
184 *line = 1;
185
186 while (p != pos) {
187 if (*p == (GLubyte) '\n') {
188 (*line)++;
189 lineStart = p + 1;
190 }
191 p++;
192 }
193
194 *col = (pos - lineStart) + 1;
195
196 /* return copy of this line */
197 while (*p != 0 && *p != '\n')
198 p++;
199 len = p - lineStart;
200 s = (GLubyte *) _mesa_malloc(len + 1);
201 _mesa_memcpy(s, lineStart, len);
202 s[len] = 0;
203
204 return s;
205}
206
207
Brian Paul765f1a12004-09-14 22:28:27 +0000208/**
209 * Initialize a new vertex/fragment program object.
210 */
Brian Paul122629f2006-07-20 16:49:57 +0000211static struct gl_program *
212_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000213 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000214{
Brian Paula6c423d2004-08-25 15:59:48 +0000215 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000216 if (prog) {
217 prog->Id = id;
218 prog->Target = target;
219 prog->Resident = GL_TRUE;
220 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000221 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Michal Krol2861e732004-03-29 11:09:34 +0000222 }
223
224 return prog;
225}
226
Brian Paul765f1a12004-09-14 22:28:27 +0000227
228/**
229 * Initialize a new fragment program object.
230 */
Brian Paul122629f2006-07-20 16:49:57 +0000231struct gl_program *
232_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000233 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000234{
235 if (prog)
236 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
237 else
238 return NULL;
239}
240
Brian Paul765f1a12004-09-14 22:28:27 +0000241
242/**
243 * Initialize a new vertex program object.
244 */
Brian Paul122629f2006-07-20 16:49:57 +0000245struct gl_program *
246_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000247 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000248{
249 if (prog)
250 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
251 else
252 return NULL;
253}
254
255
256/**
257 * Allocate and initialize a new fragment/vertex program object but
258 * don't put it into the program hash table. Called via
259 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
260 * device driver function to implement OO deriviation with additional
261 * types not understood by this function.
262 *
263 * \param ctx context
264 * \param id program id/number
265 * \param target program target/type
266 * \return pointer to new program object
267 */
Brian Paul122629f2006-07-20 16:49:57 +0000268struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000269_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
270{
Brian Paula3e86d42008-05-16 09:56:11 -0600271 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000272 switch (target) {
273 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paula3e86d42008-05-16 09:56:11 -0600274 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000275 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600276 break;
Michal Krol2861e732004-03-29 11:09:34 +0000277 case GL_FRAGMENT_PROGRAM_NV:
278 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600279 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000280 CALLOC_STRUCT(gl_fragment_program),
281 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600282 break;
Michal Krol2861e732004-03-29 11:09:34 +0000283 default:
284 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600285 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000286 }
Brian Paula3e86d42008-05-16 09:56:11 -0600287 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000288}
289
290
291/**
292 * Delete a program and remove it from the hash table, ignoring the
293 * reference count.
294 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
295 * by a device driver function.
296 */
297void
Brian Paul122629f2006-07-20 16:49:57 +0000298_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000299{
Brian Paula6c423d2004-08-25 15:59:48 +0000300 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600301 ASSERT(prog);
302 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000303
Brian Paul308b85f2006-11-23 15:58:30 +0000304 if (prog == &_mesa_DummyProgram)
305 return;
306
Michal Krol2861e732004-03-29 11:09:34 +0000307 if (prog->String)
308 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000309
Brian Paul19ad9cf2008-05-14 12:39:41 -0600310 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000311
Brian Paul65a51c02006-05-24 03:30:31 +0000312 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000313 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000314 }
Brianfe1d01c2006-12-13 14:54:47 -0700315 if (prog->Varying) {
316 _mesa_free_parameter_list(prog->Varying);
317 }
Brian3493e862007-03-24 16:18:13 -0600318 if (prog->Attributes) {
319 _mesa_free_parameter_list(prog->Attributes);
320 }
Brianfe1d01c2006-12-13 14:54:47 -0700321
Brian Paul58d080b2006-08-25 19:46:31 +0000322 /* XXX this is a little ugly */
323 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
324 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
325 if (vprog->TnlData)
326 _mesa_free(vprog->TnlData);
327 }
328
Michal Krol2861e732004-03-29 11:09:34 +0000329 _mesa_free(prog);
330}
331
332
Brian Paul4d12a052006-08-23 23:10:14 +0000333/**
334 * Return the gl_program object for a given ID.
335 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
336 * casts elsewhere.
337 */
338struct gl_program *
339_mesa_lookup_program(GLcontext *ctx, GLuint id)
340{
341 if (id)
342 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
343 else
344 return NULL;
345}
346
Michal Krol2861e732004-03-29 11:09:34 +0000347
Brian Paul3b9b8de2006-08-24 21:57:36 +0000348/**
Briandf43fb62008-05-06 23:08:51 -0600349 * Reference counting for vertex/fragment programs
350 */
351void
352_mesa_reference_program(GLcontext *ctx,
353 struct gl_program **ptr,
354 struct gl_program *prog)
355{
356 assert(ptr);
357 if (*ptr && prog) {
358 /* sanity check */
359 ASSERT((*ptr)->Target == prog->Target);
360 }
361 if (*ptr == prog) {
362 return; /* no change */
363 }
364 if (*ptr) {
365 GLboolean deleteFlag;
366
367 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600368#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600369 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
370 *ptr, (*ptr)->Id,
371 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
372 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600373#endif
374 ASSERT((*ptr)->RefCount > 0);
375 (*ptr)->RefCount--;
376
377 deleteFlag = ((*ptr)->RefCount == 0);
378 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
379
380 if (deleteFlag) {
381 ASSERT(ctx);
382 ctx->Driver.DeleteProgram(ctx, *ptr);
383 }
384
385 *ptr = NULL;
386 }
387
388 assert(!*ptr);
389 if (prog) {
390 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
391 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600392#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600393 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
394 prog, prog->Id,
395 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
396 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600397#endif
398 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
399 }
400
401 *ptr = prog;
402}
403
404
405/**
Brianb2a3a852006-12-14 13:56:58 -0700406 * Return a copy of a program.
407 * XXX Problem here if the program object is actually OO-derivation
408 * made by a device driver.
409 */
410struct gl_program *
411_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
412{
413 struct gl_program *clone;
414
Brian5b6858c2007-07-24 09:56:44 -0600415 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700416 if (!clone)
417 return NULL;
418
419 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600420 assert(clone->RefCount == 1);
421
Brianb2a3a852006-12-14 13:56:58 -0700422 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700423 clone->Format = prog->Format;
424 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
425 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600426 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700427 return NULL;
428 }
Brian12229f12007-03-22 09:11:26 -0600429 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
430 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700431 clone->InputsRead = prog->InputsRead;
432 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700433 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
434
Brian2e76f0a2006-12-19 09:52:07 -0700435 if (prog->Parameters)
436 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700437 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700438 if (prog->Varying)
439 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700440 if (prog->Attributes)
441 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700442 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
443 clone->NumInstructions = prog->NumInstructions;
444 clone->NumTemporaries = prog->NumTemporaries;
445 clone->NumParameters = prog->NumParameters;
446 clone->NumAttributes = prog->NumAttributes;
447 clone->NumAddressRegs = prog->NumAddressRegs;
448 clone->NumNativeInstructions = prog->NumNativeInstructions;
449 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
450 clone->NumNativeParameters = prog->NumNativeParameters;
451 clone->NumNativeAttributes = prog->NumNativeAttributes;
452 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700453 clone->NumAluInstructions = prog->NumAluInstructions;
454 clone->NumTexInstructions = prog->NumTexInstructions;
455 clone->NumTexIndirections = prog->NumTexIndirections;
456 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
457 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
458 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700459
460 switch (prog->Target) {
461 case GL_VERTEX_PROGRAM_ARB:
462 {
463 const struct gl_vertex_program *vp
464 = (const struct gl_vertex_program *) prog;
465 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
466 vpc->IsPositionInvariant = vp->IsPositionInvariant;
467 }
468 break;
469 case GL_FRAGMENT_PROGRAM_ARB:
470 {
471 const struct gl_fragment_program *fp
472 = (const struct gl_fragment_program *) prog;
473 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700474 fpc->FogOption = fp->FogOption;
475 fpc->UsesKill = fp->UsesKill;
476 }
477 break;
478 default:
479 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
480 }
481
482 return clone;
483}
484
485
Brian Paul19ad9cf2008-05-14 12:39:41 -0600486/**
487 * Insert 'count' NOP instructions at 'start' in the given program.
488 * Adjust branch targets accordingly.
489 */
490GLboolean
491_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
492{
493 const GLuint origLen = prog->NumInstructions;
494 const GLuint newLen = origLen + count;
495 struct prog_instruction *newInst;
496 GLuint i;
497
498 /* adjust branches */
499 for (i = 0; i < prog->NumInstructions; i++) {
500 struct prog_instruction *inst = prog->Instructions + i;
501 if (inst->BranchTarget > 0) {
502 if (inst->BranchTarget >= start) {
503 inst->BranchTarget += count;
504 }
505 }
506 }
507
508 /* Alloc storage for new instructions */
509 newInst = _mesa_alloc_instructions(newLen);
510 if (!newInst) {
511 return GL_FALSE;
512 }
513
514 /* Copy 'start' instructions into new instruction buffer */
515 _mesa_copy_instructions(newInst, prog->Instructions, start);
516
517 /* init the new instructions */
518 _mesa_init_instructions(newInst + start, count);
519
520 /* Copy the remaining/tail instructions to new inst buffer */
521 _mesa_copy_instructions(newInst + start + count,
522 prog->Instructions + start,
523 origLen - start);
524
525 /* free old instructions */
526 _mesa_free_instructions(prog->Instructions, origLen);
527
528 /* install new instructions */
529 prog->Instructions = newInst;
530 prog->NumInstructions = newLen;
531
532 return GL_TRUE;
533}
534
Brianb2a3a852006-12-14 13:56:58 -0700535
536/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600537 * Search instructions for registers that match (oldFile, oldIndex),
538 * replacing them with (newFile, newIndex).
539 */
540static void
541replace_registers(struct prog_instruction *inst, GLuint numInst,
542 GLuint oldFile, GLuint oldIndex,
543 GLuint newFile, GLuint newIndex)
544{
545 GLuint i, j;
546 for (i = 0; i < numInst; i++) {
547 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
548 if (inst[i].SrcReg[j].File == oldFile &&
549 inst[i].SrcReg[j].Index == oldIndex) {
550 inst[i].SrcReg[j].File = newFile;
551 inst[i].SrcReg[j].Index = newIndex;
552 }
553 }
554 }
555}
556
557
558/**
559 * Search instructions for references to program parameters. When found,
560 * increment the parameter index by 'offset'.
561 * Used when combining programs.
562 */
563static void
564adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
565 GLuint offset)
566{
567 GLuint i, j;
568 for (i = 0; i < numInst; i++) {
569 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
570 GLuint f = inst[i].SrcReg[j].File;
571 if (f == PROGRAM_CONSTANT ||
572 f == PROGRAM_UNIFORM ||
573 f == PROGRAM_STATE_VAR) {
574 inst[i].SrcReg[j].Index += offset;
575 }
576 }
577 }
578}
579
580
581/**
582 * Combine two programs into one. Fix instructions so the outputs of
583 * the first program go to the inputs of the second program.
584 */
585struct gl_program *
586_mesa_combine_programs(GLcontext *ctx,
587 struct gl_program *progA, struct gl_program *progB)
588{
589 struct prog_instruction *newInst;
590 struct gl_program *newProg;
591 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
592 const GLuint lenB = progB->NumInstructions;
593 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
594 const GLuint newLength = lenA + lenB;
595 GLuint i;
596
597 ASSERT(progA->Target == progB->Target);
598
599 newInst = _mesa_alloc_instructions(newLength);
600 if (!newInst)
601 return GL_FALSE;
602
603 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
604 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
605
606 /* adjust branch / instruction addresses for B's instructions */
607 for (i = 0; i < lenB; i++) {
608 newInst[lenA + i].BranchTarget += lenA;
609 }
610
611 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
612 newProg->Instructions = newInst;
613 newProg->NumInstructions = newLength;
614
615 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
616 /* connect color outputs/inputs */
617 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
618 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
619 replace_registers(newInst + lenA, lenB,
620 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
621 PROGRAM_OUTPUT, FRAG_RESULT_COLR);
622 }
623
624 newProg->InputsRead = progA->InputsRead;
625 newProg->InputsRead |= (progB->InputsRead & ~(1 << FRAG_ATTRIB_COL0));
626 newProg->OutputsWritten = progB->OutputsWritten;
627 }
628 else {
629 /* vertex program */
630 assert(0); /* XXX todo */
631 }
632
633 /*
634 * Merge parameters (uniforms, constants, etc)
635 */
636 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
637 progB->Parameters);
638
639 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
640
641
642 return newProg;
643}
644
645
646
647
648/**
649 * Scan the given program to find a free register of the given type.
650 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
651 */
652GLint
653_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
654{
655 GLboolean used[MAX_PROGRAM_TEMPS];
656 GLuint i, k;
657
658 assert(regFile == PROGRAM_INPUT ||
659 regFile == PROGRAM_OUTPUT ||
660 regFile == PROGRAM_TEMPORARY);
661
662 _mesa_memset(used, 0, sizeof(used));
663
664 for (i = 0; i < prog->NumInstructions; i++) {
665 const struct prog_instruction *inst = prog->Instructions + i;
666 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
667
668 for (k = 0; k < n; k++) {
669 if (inst->SrcReg[k].File == regFile) {
670 used[inst->SrcReg[k].Index] = GL_TRUE;
671 }
672 }
673 }
674
675 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
676 if (!used[i])
677 return i;
678 }
679
680 return -1;
681}
682
683
684
685/**
Brian Paul77427a12006-08-24 23:11:39 +0000686 * Mixing ARB and NV vertex/fragment programs can be tricky.
687 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
688 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
689 * The two different fragment program targets are supposed to be compatible
690 * to some extent (see GL_ARB_fragment_program spec).
691 * This function does the compatibility check.
692 */
693static GLboolean
694compatible_program_targets(GLenum t1, GLenum t2)
695{
696 if (t1 == t2)
697 return GL_TRUE;
698 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
699 return GL_TRUE;
700 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
701 return GL_TRUE;
702 return GL_FALSE;
703}
704
705
Brian Paul30d6a4b2005-11-05 20:18:18 +0000706
Michal Krol2861e732004-03-29 11:09:34 +0000707/**********************************************************************/
708/* API functions */
709/**********************************************************************/
710
711
712/**
713 * Bind a program (make it current)
714 * \note Called from the GL API dispatcher by both glBindProgramNV
715 * and glBindProgramARB.
716 */
717void GLAPIENTRY
718_mesa_BindProgram(GLenum target, GLuint id)
719{
Brian Paula360bc32006-08-25 17:18:56 +0000720 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000721 GET_CURRENT_CONTEXT(ctx);
722 ASSERT_OUTSIDE_BEGIN_END(ctx);
723
724 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
725
Brian Paula360bc32006-08-25 17:18:56 +0000726 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000727 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
728 (ctx->Extensions.NV_vertex_program ||
729 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000730 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000731 }
732 else if ((target == GL_FRAGMENT_PROGRAM_NV
733 && ctx->Extensions.NV_fragment_program) ||
734 (target == GL_FRAGMENT_PROGRAM_ARB
735 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000736 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000737 }
738 else {
739 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
740 return;
741 }
742
Brian Paula360bc32006-08-25 17:18:56 +0000743 /*
744 * Get pointer to new program to bind.
745 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000746 * That's supposed to be caught in glBegin.
747 */
748 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000749 /* Bind a default program */
750 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000751 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600752 newProg = &ctx->Shared->DefaultVertexProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000753 else
Briandf43fb62008-05-06 23:08:51 -0600754 newProg = &ctx->Shared->DefaultFragmentProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000755 }
756 else {
Brian Paula360bc32006-08-25 17:18:56 +0000757 /* Bind a user program */
758 newProg = _mesa_lookup_program(ctx, id);
759 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000760 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000761 newProg = ctx->Driver.NewProgram(ctx, target, id);
762 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000763 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
764 return;
765 }
Brian Paula360bc32006-08-25 17:18:56 +0000766 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000767 }
Brian Paula360bc32006-08-25 17:18:56 +0000768 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000769 _mesa_error(ctx, GL_INVALID_OPERATION,
770 "glBindProgramNV/ARB(target mismatch)");
771 return;
772 }
Michal Krol2861e732004-03-29 11:09:34 +0000773 }
774
Brian Paula360bc32006-08-25 17:18:56 +0000775 /** All error checking is complete now **/
776
777 if (curProg->Id == id) {
778 /* binding same program - no change */
779 return;
780 }
781
Brian Paula360bc32006-08-25 17:18:56 +0000782 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000783 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600784 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
785 (struct gl_vertex_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000786 }
Brian Paula360bc32006-08-25 17:18:56 +0000787 else if (target == GL_FRAGMENT_PROGRAM_NV ||
788 target == GL_FRAGMENT_PROGRAM_ARB) {
Briandf43fb62008-05-06 23:08:51 -0600789 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
790 (struct gl_fragment_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000791 }
792
Brian Paul765f1a12004-09-14 22:28:27 +0000793 /* Never null pointers */
794 ASSERT(ctx->VertexProgram.Current);
795 ASSERT(ctx->FragmentProgram.Current);
796
Michal Krol2861e732004-03-29 11:09:34 +0000797 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000798 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000799}
800
801
802/**
803 * Delete a list of programs.
804 * \note Not compiled into display lists.
805 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
806 */
807void GLAPIENTRY
808_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
809{
810 GLint i;
811 GET_CURRENT_CONTEXT(ctx);
812 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
813
814 if (n < 0) {
815 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
816 return;
817 }
818
819 for (i = 0; i < n; i++) {
820 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000821 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000822 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000823 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
824 }
825 else if (prog) {
826 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000827 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000828 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
829 if (ctx->VertexProgram.Current &&
830 ctx->VertexProgram.Current->Base.Id == ids[i]) {
831 /* unbind this currently bound program */
832 _mesa_BindProgram(prog->Target, 0);
833 }
834 }
835 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
836 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
837 if (ctx->FragmentProgram.Current &&
838 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
839 /* unbind this currently bound program */
840 _mesa_BindProgram(prog->Target, 0);
841 }
842 }
843 else {
844 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
845 return;
846 }
Brian Paulea2943e2005-01-20 04:02:02 +0000847 /* The ID is immediately available for re-use now */
848 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
Briandf43fb62008-05-06 23:08:51 -0600849 _mesa_reference_program(ctx, &prog, NULL);
Michal Krol2861e732004-03-29 11:09:34 +0000850 }
Michal Krol2861e732004-03-29 11:09:34 +0000851 }
852 }
853}
854
855
856/**
857 * Generate a list of new program identifiers.
858 * \note Not compiled into display lists.
859 * \note Called by both glGenProgramsNV and glGenProgramsARB.
860 */
861void GLAPIENTRY
862_mesa_GenPrograms(GLsizei n, GLuint *ids)
863{
864 GLuint first;
865 GLuint i;
866 GET_CURRENT_CONTEXT(ctx);
867 ASSERT_OUTSIDE_BEGIN_END(ctx);
868
869 if (n < 0) {
870 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
871 return;
872 }
873
874 if (!ids)
875 return;
876
877 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
878
Brian Paul765f1a12004-09-14 22:28:27 +0000879 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000880 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000881 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000882 }
883
884 /* Return the program names */
885 for (i = 0; i < (GLuint) n; i++) {
886 ids[i] = first + i;
887 }
888}