blob: 63ab4dd67def0dd0e1ba47774d6ba8ebbd2423ae [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{
271 switch (target) {
272 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000273 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
274 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000275 case GL_FRAGMENT_PROGRAM_NV:
276 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000277 return _mesa_init_fragment_program(ctx,
278 CALLOC_STRUCT(gl_fragment_program),
279 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000280 default:
281 _mesa_problem(ctx, "bad target in _mesa_new_program");
282 return NULL;
283 }
284}
285
286
287/**
288 * Delete a program and remove it from the hash table, ignoring the
289 * reference count.
290 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
291 * by a device driver function.
292 */
293void
Brian Paul122629f2006-07-20 16:49:57 +0000294_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000295{
Brian Paula6c423d2004-08-25 15:59:48 +0000296 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600297 ASSERT(prog);
298 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000299
Brian Paul308b85f2006-11-23 15:58:30 +0000300 if (prog == &_mesa_DummyProgram)
301 return;
302
Michal Krol2861e732004-03-29 11:09:34 +0000303 if (prog->String)
304 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000305
Brian Paul19ad9cf2008-05-14 12:39:41 -0600306 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000307
Brian Paul65a51c02006-05-24 03:30:31 +0000308 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000309 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000310 }
Brianfe1d01c2006-12-13 14:54:47 -0700311 if (prog->Varying) {
312 _mesa_free_parameter_list(prog->Varying);
313 }
Brian3493e862007-03-24 16:18:13 -0600314 if (prog->Attributes) {
315 _mesa_free_parameter_list(prog->Attributes);
316 }
Brianfe1d01c2006-12-13 14:54:47 -0700317
Brian Paul58d080b2006-08-25 19:46:31 +0000318 /* XXX this is a little ugly */
319 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
320 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
321 if (vprog->TnlData)
322 _mesa_free(vprog->TnlData);
323 }
324
Michal Krol2861e732004-03-29 11:09:34 +0000325 _mesa_free(prog);
326}
327
328
Brian Paul4d12a052006-08-23 23:10:14 +0000329/**
330 * Return the gl_program object for a given ID.
331 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
332 * casts elsewhere.
333 */
334struct gl_program *
335_mesa_lookup_program(GLcontext *ctx, GLuint id)
336{
337 if (id)
338 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
339 else
340 return NULL;
341}
342
Michal Krol2861e732004-03-29 11:09:34 +0000343
Brian Paul3b9b8de2006-08-24 21:57:36 +0000344/**
Briandf43fb62008-05-06 23:08:51 -0600345 * Reference counting for vertex/fragment programs
346 */
347void
348_mesa_reference_program(GLcontext *ctx,
349 struct gl_program **ptr,
350 struct gl_program *prog)
351{
352 assert(ptr);
353 if (*ptr && prog) {
354 /* sanity check */
355 ASSERT((*ptr)->Target == prog->Target);
356 }
357 if (*ptr == prog) {
358 return; /* no change */
359 }
360 if (*ptr) {
361 GLboolean deleteFlag;
362
363 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600364#if 0
Briandf43fb62008-05-06 23:08:51 -0600365 printf("Program %p %u 0x%x Refcount-- to %d\n",
366 *ptr, (*ptr)->Id, (*ptr)->Target, (*ptr)->RefCount - 1);
367#endif
368 ASSERT((*ptr)->RefCount > 0);
369 (*ptr)->RefCount--;
370
371 deleteFlag = ((*ptr)->RefCount == 0);
372 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
373
374 if (deleteFlag) {
375 ASSERT(ctx);
376 ctx->Driver.DeleteProgram(ctx, *ptr);
377 }
378
379 *ptr = NULL;
380 }
381
382 assert(!*ptr);
383 if (prog) {
384 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
385 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600386#if 0
Briandf43fb62008-05-06 23:08:51 -0600387 printf("Program %p %u 0x%x Refcount++ to %d\n",
388 prog, prog->Id, prog->Target, prog->RefCount);
389#endif
390 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
391 }
392
393 *ptr = prog;
394}
395
396
397/**
Brianb2a3a852006-12-14 13:56:58 -0700398 * Return a copy of a program.
399 * XXX Problem here if the program object is actually OO-derivation
400 * made by a device driver.
401 */
402struct gl_program *
403_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
404{
405 struct gl_program *clone;
406
Brian5b6858c2007-07-24 09:56:44 -0600407 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700408 if (!clone)
409 return NULL;
410
411 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600412 assert(clone->RefCount == 1);
413
Brianb2a3a852006-12-14 13:56:58 -0700414 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700415 clone->Format = prog->Format;
416 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
417 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600418 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700419 return NULL;
420 }
Brian12229f12007-03-22 09:11:26 -0600421 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
422 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700423 clone->InputsRead = prog->InputsRead;
424 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700425 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
426
Brian2e76f0a2006-12-19 09:52:07 -0700427 if (prog->Parameters)
428 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700429 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700430 if (prog->Varying)
431 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700432 if (prog->Attributes)
433 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700434 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
435 clone->NumInstructions = prog->NumInstructions;
436 clone->NumTemporaries = prog->NumTemporaries;
437 clone->NumParameters = prog->NumParameters;
438 clone->NumAttributes = prog->NumAttributes;
439 clone->NumAddressRegs = prog->NumAddressRegs;
440 clone->NumNativeInstructions = prog->NumNativeInstructions;
441 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
442 clone->NumNativeParameters = prog->NumNativeParameters;
443 clone->NumNativeAttributes = prog->NumNativeAttributes;
444 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700445 clone->NumAluInstructions = prog->NumAluInstructions;
446 clone->NumTexInstructions = prog->NumTexInstructions;
447 clone->NumTexIndirections = prog->NumTexIndirections;
448 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
449 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
450 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700451
452 switch (prog->Target) {
453 case GL_VERTEX_PROGRAM_ARB:
454 {
455 const struct gl_vertex_program *vp
456 = (const struct gl_vertex_program *) prog;
457 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
458 vpc->IsPositionInvariant = vp->IsPositionInvariant;
459 }
460 break;
461 case GL_FRAGMENT_PROGRAM_ARB:
462 {
463 const struct gl_fragment_program *fp
464 = (const struct gl_fragment_program *) prog;
465 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700466 fpc->FogOption = fp->FogOption;
467 fpc->UsesKill = fp->UsesKill;
468 }
469 break;
470 default:
471 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
472 }
473
474 return clone;
475}
476
477
Brian Paul19ad9cf2008-05-14 12:39:41 -0600478/**
479 * Insert 'count' NOP instructions at 'start' in the given program.
480 * Adjust branch targets accordingly.
481 */
482GLboolean
483_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
484{
485 const GLuint origLen = prog->NumInstructions;
486 const GLuint newLen = origLen + count;
487 struct prog_instruction *newInst;
488 GLuint i;
489
490 /* adjust branches */
491 for (i = 0; i < prog->NumInstructions; i++) {
492 struct prog_instruction *inst = prog->Instructions + i;
493 if (inst->BranchTarget > 0) {
494 if (inst->BranchTarget >= start) {
495 inst->BranchTarget += count;
496 }
497 }
498 }
499
500 /* Alloc storage for new instructions */
501 newInst = _mesa_alloc_instructions(newLen);
502 if (!newInst) {
503 return GL_FALSE;
504 }
505
506 /* Copy 'start' instructions into new instruction buffer */
507 _mesa_copy_instructions(newInst, prog->Instructions, start);
508
509 /* init the new instructions */
510 _mesa_init_instructions(newInst + start, count);
511
512 /* Copy the remaining/tail instructions to new inst buffer */
513 _mesa_copy_instructions(newInst + start + count,
514 prog->Instructions + start,
515 origLen - start);
516
517 /* free old instructions */
518 _mesa_free_instructions(prog->Instructions, origLen);
519
520 /* install new instructions */
521 prog->Instructions = newInst;
522 prog->NumInstructions = newLen;
523
524 return GL_TRUE;
525}
526
Brianb2a3a852006-12-14 13:56:58 -0700527
528/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600529 * Search instructions for registers that match (oldFile, oldIndex),
530 * replacing them with (newFile, newIndex).
531 */
532static void
533replace_registers(struct prog_instruction *inst, GLuint numInst,
534 GLuint oldFile, GLuint oldIndex,
535 GLuint newFile, GLuint newIndex)
536{
537 GLuint i, j;
538 for (i = 0; i < numInst; i++) {
539 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
540 if (inst[i].SrcReg[j].File == oldFile &&
541 inst[i].SrcReg[j].Index == oldIndex) {
542 inst[i].SrcReg[j].File = newFile;
543 inst[i].SrcReg[j].Index = newIndex;
544 }
545 }
546 }
547}
548
549
550/**
551 * Search instructions for references to program parameters. When found,
552 * increment the parameter index by 'offset'.
553 * Used when combining programs.
554 */
555static void
556adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
557 GLuint offset)
558{
559 GLuint i, j;
560 for (i = 0; i < numInst; i++) {
561 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
562 GLuint f = inst[i].SrcReg[j].File;
563 if (f == PROGRAM_CONSTANT ||
564 f == PROGRAM_UNIFORM ||
565 f == PROGRAM_STATE_VAR) {
566 inst[i].SrcReg[j].Index += offset;
567 }
568 }
569 }
570}
571
572
573/**
574 * Combine two programs into one. Fix instructions so the outputs of
575 * the first program go to the inputs of the second program.
576 */
577struct gl_program *
578_mesa_combine_programs(GLcontext *ctx,
579 struct gl_program *progA, struct gl_program *progB)
580{
581 struct prog_instruction *newInst;
582 struct gl_program *newProg;
583 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
584 const GLuint lenB = progB->NumInstructions;
585 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
586 const GLuint newLength = lenA + lenB;
587 GLuint i;
588
589 ASSERT(progA->Target == progB->Target);
590
591 newInst = _mesa_alloc_instructions(newLength);
592 if (!newInst)
593 return GL_FALSE;
594
595 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
596 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
597
598 /* adjust branch / instruction addresses for B's instructions */
599 for (i = 0; i < lenB; i++) {
600 newInst[lenA + i].BranchTarget += lenA;
601 }
602
603 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
604 newProg->Instructions = newInst;
605 newProg->NumInstructions = newLength;
606
607 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
608 /* connect color outputs/inputs */
609 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
610 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
611 replace_registers(newInst + lenA, lenB,
612 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
613 PROGRAM_OUTPUT, FRAG_RESULT_COLR);
614 }
615
616 newProg->InputsRead = progA->InputsRead;
617 newProg->InputsRead |= (progB->InputsRead & ~(1 << FRAG_ATTRIB_COL0));
618 newProg->OutputsWritten = progB->OutputsWritten;
619 }
620 else {
621 /* vertex program */
622 assert(0); /* XXX todo */
623 }
624
625 /*
626 * Merge parameters (uniforms, constants, etc)
627 */
628 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
629 progB->Parameters);
630
631 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
632
633
634 return newProg;
635}
636
637
638
639
640/**
641 * Scan the given program to find a free register of the given type.
642 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
643 */
644GLint
645_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
646{
647 GLboolean used[MAX_PROGRAM_TEMPS];
648 GLuint i, k;
649
650 assert(regFile == PROGRAM_INPUT ||
651 regFile == PROGRAM_OUTPUT ||
652 regFile == PROGRAM_TEMPORARY);
653
654 _mesa_memset(used, 0, sizeof(used));
655
656 for (i = 0; i < prog->NumInstructions; i++) {
657 const struct prog_instruction *inst = prog->Instructions + i;
658 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
659
660 for (k = 0; k < n; k++) {
661 if (inst->SrcReg[k].File == regFile) {
662 used[inst->SrcReg[k].Index] = GL_TRUE;
663 }
664 }
665 }
666
667 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
668 if (!used[i])
669 return i;
670 }
671
672 return -1;
673}
674
675
676
677/**
Brian Paul77427a12006-08-24 23:11:39 +0000678 * Mixing ARB and NV vertex/fragment programs can be tricky.
679 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
680 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
681 * The two different fragment program targets are supposed to be compatible
682 * to some extent (see GL_ARB_fragment_program spec).
683 * This function does the compatibility check.
684 */
685static GLboolean
686compatible_program_targets(GLenum t1, GLenum t2)
687{
688 if (t1 == t2)
689 return GL_TRUE;
690 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
691 return GL_TRUE;
692 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
693 return GL_TRUE;
694 return GL_FALSE;
695}
696
697
Brian Paul30d6a4b2005-11-05 20:18:18 +0000698
Michal Krol2861e732004-03-29 11:09:34 +0000699/**********************************************************************/
700/* API functions */
701/**********************************************************************/
702
703
704/**
705 * Bind a program (make it current)
706 * \note Called from the GL API dispatcher by both glBindProgramNV
707 * and glBindProgramARB.
708 */
709void GLAPIENTRY
710_mesa_BindProgram(GLenum target, GLuint id)
711{
Brian Paula360bc32006-08-25 17:18:56 +0000712 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000713 GET_CURRENT_CONTEXT(ctx);
714 ASSERT_OUTSIDE_BEGIN_END(ctx);
715
716 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
717
Brian Paula360bc32006-08-25 17:18:56 +0000718 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000719 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
720 (ctx->Extensions.NV_vertex_program ||
721 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000722 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000723 }
724 else if ((target == GL_FRAGMENT_PROGRAM_NV
725 && ctx->Extensions.NV_fragment_program) ||
726 (target == GL_FRAGMENT_PROGRAM_ARB
727 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000728 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000729 }
730 else {
731 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
732 return;
733 }
734
Brian Paula360bc32006-08-25 17:18:56 +0000735 /*
736 * Get pointer to new program to bind.
737 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000738 * That's supposed to be caught in glBegin.
739 */
740 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000741 /* Bind a default program */
742 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000743 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600744 newProg = &ctx->Shared->DefaultVertexProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000745 else
Briandf43fb62008-05-06 23:08:51 -0600746 newProg = &ctx->Shared->DefaultFragmentProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000747 }
748 else {
Brian Paula360bc32006-08-25 17:18:56 +0000749 /* Bind a user program */
750 newProg = _mesa_lookup_program(ctx, id);
751 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000752 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000753 newProg = ctx->Driver.NewProgram(ctx, target, id);
754 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000755 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
756 return;
757 }
Brian Paula360bc32006-08-25 17:18:56 +0000758 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000759 }
Brian Paula360bc32006-08-25 17:18:56 +0000760 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000761 _mesa_error(ctx, GL_INVALID_OPERATION,
762 "glBindProgramNV/ARB(target mismatch)");
763 return;
764 }
Michal Krol2861e732004-03-29 11:09:34 +0000765 }
766
Brian Paula360bc32006-08-25 17:18:56 +0000767 /** All error checking is complete now **/
768
769 if (curProg->Id == id) {
770 /* binding same program - no change */
771 return;
772 }
773
Brian Paula360bc32006-08-25 17:18:56 +0000774 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000775 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600776 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
777 (struct gl_vertex_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000778 }
Brian Paula360bc32006-08-25 17:18:56 +0000779 else if (target == GL_FRAGMENT_PROGRAM_NV ||
780 target == GL_FRAGMENT_PROGRAM_ARB) {
Briandf43fb62008-05-06 23:08:51 -0600781 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
782 (struct gl_fragment_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000783 }
784
Brian Paul765f1a12004-09-14 22:28:27 +0000785 /* Never null pointers */
786 ASSERT(ctx->VertexProgram.Current);
787 ASSERT(ctx->FragmentProgram.Current);
788
Michal Krol2861e732004-03-29 11:09:34 +0000789 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000790 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000791}
792
793
794/**
795 * Delete a list of programs.
796 * \note Not compiled into display lists.
797 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
798 */
799void GLAPIENTRY
800_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
801{
802 GLint i;
803 GET_CURRENT_CONTEXT(ctx);
804 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
805
806 if (n < 0) {
807 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
808 return;
809 }
810
811 for (i = 0; i < n; i++) {
812 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000813 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000814 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000815 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
816 }
817 else if (prog) {
818 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000819 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000820 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
821 if (ctx->VertexProgram.Current &&
822 ctx->VertexProgram.Current->Base.Id == ids[i]) {
823 /* unbind this currently bound program */
824 _mesa_BindProgram(prog->Target, 0);
825 }
826 }
827 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
828 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
829 if (ctx->FragmentProgram.Current &&
830 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
831 /* unbind this currently bound program */
832 _mesa_BindProgram(prog->Target, 0);
833 }
834 }
835 else {
836 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
837 return;
838 }
Brian Paulea2943e2005-01-20 04:02:02 +0000839 /* The ID is immediately available for re-use now */
840 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
Briandf43fb62008-05-06 23:08:51 -0600841 _mesa_reference_program(ctx, &prog, NULL);
Michal Krol2861e732004-03-29 11:09:34 +0000842 }
Michal Krol2861e732004-03-29 11:09:34 +0000843 }
844 }
845}
846
847
848/**
849 * Generate a list of new program identifiers.
850 * \note Not compiled into display lists.
851 * \note Called by both glGenProgramsNV and glGenProgramsARB.
852 */
853void GLAPIENTRY
854_mesa_GenPrograms(GLsizei n, GLuint *ids)
855{
856 GLuint first;
857 GLuint i;
858 GET_CURRENT_CONTEXT(ctx);
859 ASSERT_OUTSIDE_BEGIN_END(ctx);
860
861 if (n < 0) {
862 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
863 return;
864 }
865
866 if (!ids)
867 return;
868
869 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
870
Brian Paul765f1a12004-09-14 22:28:27 +0000871 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000872 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000873 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000874 }
875
876 /* Return the program names */
877 for (i = 0; i < (GLuint) n; i++) {
878 ids[i] = first + i;
879 }
880}