blob: 14e401e57167cc1e30c6d43da89540042bbee4e4 [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
Brian Paul19ad9cf2008-05-14 12:39:41 -0600307 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000308
Brian Paul65a51c02006-05-24 03:30:31 +0000309 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000310 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000311 }
Brianfe1d01c2006-12-13 14:54:47 -0700312 if (prog->Varying) {
313 _mesa_free_parameter_list(prog->Varying);
314 }
Brian3493e862007-03-24 16:18:13 -0600315 if (prog->Attributes) {
316 _mesa_free_parameter_list(prog->Attributes);
317 }
Brianfe1d01c2006-12-13 14:54:47 -0700318
Brian Paul58d080b2006-08-25 19:46:31 +0000319 /* XXX this is a little ugly */
320 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
321 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
322 if (vprog->TnlData)
323 _mesa_free(vprog->TnlData);
324 }
325
Michal Krol2861e732004-03-29 11:09:34 +0000326 _mesa_free(prog);
327}
328
329
Brian Paul4d12a052006-08-23 23:10:14 +0000330/**
331 * Return the gl_program object for a given ID.
332 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
333 * casts elsewhere.
334 */
335struct gl_program *
336_mesa_lookup_program(GLcontext *ctx, GLuint id)
337{
338 if (id)
339 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
340 else
341 return NULL;
342}
343
Michal Krol2861e732004-03-29 11:09:34 +0000344
Brian Paul3b9b8de2006-08-24 21:57:36 +0000345/**
Briandf43fb62008-05-06 23:08:51 -0600346 * Reference counting for vertex/fragment programs
347 */
348void
349_mesa_reference_program(GLcontext *ctx,
350 struct gl_program **ptr,
351 struct gl_program *prog)
352{
353 assert(ptr);
354 if (*ptr && prog) {
355 /* sanity check */
356 ASSERT((*ptr)->Target == prog->Target);
357 }
358 if (*ptr == prog) {
359 return; /* no change */
360 }
361 if (*ptr) {
362 GLboolean deleteFlag;
363
364 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600365#if 0
Briandf43fb62008-05-06 23:08:51 -0600366 printf("Program %p %u 0x%x Refcount-- to %d\n",
367 *ptr, (*ptr)->Id, (*ptr)->Target, (*ptr)->RefCount - 1);
368#endif
369 ASSERT((*ptr)->RefCount > 0);
370 (*ptr)->RefCount--;
371
372 deleteFlag = ((*ptr)->RefCount == 0);
373 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
374
375 if (deleteFlag) {
376 ASSERT(ctx);
377 ctx->Driver.DeleteProgram(ctx, *ptr);
378 }
379
380 *ptr = NULL;
381 }
382
383 assert(!*ptr);
384 if (prog) {
385 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
386 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600387#if 0
Briandf43fb62008-05-06 23:08:51 -0600388 printf("Program %p %u 0x%x Refcount++ to %d\n",
389 prog, prog->Id, prog->Target, prog->RefCount);
390#endif
391 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
392 }
393
394 *ptr = prog;
395}
396
397
398/**
Brianb2a3a852006-12-14 13:56:58 -0700399 * Return a copy of a program.
400 * XXX Problem here if the program object is actually OO-derivation
401 * made by a device driver.
402 */
403struct gl_program *
404_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
405{
406 struct gl_program *clone;
407
Brian5b6858c2007-07-24 09:56:44 -0600408 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700409 if (!clone)
410 return NULL;
411
412 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600413 assert(clone->RefCount == 1);
414
Brianb2a3a852006-12-14 13:56:58 -0700415 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700416 clone->Format = prog->Format;
417 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
418 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600419 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700420 return NULL;
421 }
Brian12229f12007-03-22 09:11:26 -0600422 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
423 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700424 clone->InputsRead = prog->InputsRead;
425 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700426 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
427
Brian2e76f0a2006-12-19 09:52:07 -0700428 if (prog->Parameters)
429 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700430 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700431 if (prog->Varying)
432 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700433 if (prog->Attributes)
434 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700435 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
436 clone->NumInstructions = prog->NumInstructions;
437 clone->NumTemporaries = prog->NumTemporaries;
438 clone->NumParameters = prog->NumParameters;
439 clone->NumAttributes = prog->NumAttributes;
440 clone->NumAddressRegs = prog->NumAddressRegs;
441 clone->NumNativeInstructions = prog->NumNativeInstructions;
442 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
443 clone->NumNativeParameters = prog->NumNativeParameters;
444 clone->NumNativeAttributes = prog->NumNativeAttributes;
445 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700446 clone->NumAluInstructions = prog->NumAluInstructions;
447 clone->NumTexInstructions = prog->NumTexInstructions;
448 clone->NumTexIndirections = prog->NumTexIndirections;
449 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
450 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
451 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700452
453 switch (prog->Target) {
454 case GL_VERTEX_PROGRAM_ARB:
455 {
456 const struct gl_vertex_program *vp
457 = (const struct gl_vertex_program *) prog;
458 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
459 vpc->IsPositionInvariant = vp->IsPositionInvariant;
460 }
461 break;
462 case GL_FRAGMENT_PROGRAM_ARB:
463 {
464 const struct gl_fragment_program *fp
465 = (const struct gl_fragment_program *) prog;
466 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700467 fpc->FogOption = fp->FogOption;
468 fpc->UsesKill = fp->UsesKill;
469 }
470 break;
471 default:
472 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
473 }
474
475 return clone;
476}
477
478
Brian Paul19ad9cf2008-05-14 12:39:41 -0600479/**
480 * Insert 'count' NOP instructions at 'start' in the given program.
481 * Adjust branch targets accordingly.
482 */
483GLboolean
484_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
485{
486 const GLuint origLen = prog->NumInstructions;
487 const GLuint newLen = origLen + count;
488 struct prog_instruction *newInst;
489 GLuint i;
490
491 /* adjust branches */
492 for (i = 0; i < prog->NumInstructions; i++) {
493 struct prog_instruction *inst = prog->Instructions + i;
494 if (inst->BranchTarget > 0) {
495 if (inst->BranchTarget >= start) {
496 inst->BranchTarget += count;
497 }
498 }
499 }
500
501 /* Alloc storage for new instructions */
502 newInst = _mesa_alloc_instructions(newLen);
503 if (!newInst) {
504 return GL_FALSE;
505 }
506
507 /* Copy 'start' instructions into new instruction buffer */
508 _mesa_copy_instructions(newInst, prog->Instructions, start);
509
510 /* init the new instructions */
511 _mesa_init_instructions(newInst + start, count);
512
513 /* Copy the remaining/tail instructions to new inst buffer */
514 _mesa_copy_instructions(newInst + start + count,
515 prog->Instructions + start,
516 origLen - start);
517
518 /* free old instructions */
519 _mesa_free_instructions(prog->Instructions, origLen);
520
521 /* install new instructions */
522 prog->Instructions = newInst;
523 prog->NumInstructions = newLen;
524
525 return GL_TRUE;
526}
527
Brianb2a3a852006-12-14 13:56:58 -0700528
529/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600530 * Search instructions for registers that match (oldFile, oldIndex),
531 * replacing them with (newFile, newIndex).
532 */
533static void
534replace_registers(struct prog_instruction *inst, GLuint numInst,
535 GLuint oldFile, GLuint oldIndex,
536 GLuint newFile, GLuint newIndex)
537{
538 GLuint i, j;
539 for (i = 0; i < numInst; i++) {
540 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
541 if (inst[i].SrcReg[j].File == oldFile &&
542 inst[i].SrcReg[j].Index == oldIndex) {
543 inst[i].SrcReg[j].File = newFile;
544 inst[i].SrcReg[j].Index = newIndex;
545 }
546 }
547 }
548}
549
550
551/**
552 * Search instructions for references to program parameters. When found,
553 * increment the parameter index by 'offset'.
554 * Used when combining programs.
555 */
556static void
557adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
558 GLuint offset)
559{
560 GLuint i, j;
561 for (i = 0; i < numInst; i++) {
562 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
563 GLuint f = inst[i].SrcReg[j].File;
564 if (f == PROGRAM_CONSTANT ||
565 f == PROGRAM_UNIFORM ||
566 f == PROGRAM_STATE_VAR) {
567 inst[i].SrcReg[j].Index += offset;
568 }
569 }
570 }
571}
572
573
574/**
575 * Combine two programs into one. Fix instructions so the outputs of
576 * the first program go to the inputs of the second program.
577 */
578struct gl_program *
579_mesa_combine_programs(GLcontext *ctx,
580 struct gl_program *progA, struct gl_program *progB)
581{
582 struct prog_instruction *newInst;
583 struct gl_program *newProg;
584 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
585 const GLuint lenB = progB->NumInstructions;
586 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
587 const GLuint newLength = lenA + lenB;
588 GLuint i;
589
590 ASSERT(progA->Target == progB->Target);
591
592 newInst = _mesa_alloc_instructions(newLength);
593 if (!newInst)
594 return GL_FALSE;
595
596 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
597 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
598
599 /* adjust branch / instruction addresses for B's instructions */
600 for (i = 0; i < lenB; i++) {
601 newInst[lenA + i].BranchTarget += lenA;
602 }
603
604 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
605 newProg->Instructions = newInst;
606 newProg->NumInstructions = newLength;
607
608 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
609 /* connect color outputs/inputs */
610 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
611 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
612 replace_registers(newInst + lenA, lenB,
613 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
614 PROGRAM_OUTPUT, FRAG_RESULT_COLR);
615 }
616
617 newProg->InputsRead = progA->InputsRead;
618 newProg->InputsRead |= (progB->InputsRead & ~(1 << FRAG_ATTRIB_COL0));
619 newProg->OutputsWritten = progB->OutputsWritten;
620 }
621 else {
622 /* vertex program */
623 assert(0); /* XXX todo */
624 }
625
626 /*
627 * Merge parameters (uniforms, constants, etc)
628 */
629 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
630 progB->Parameters);
631
632 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
633
634
635 return newProg;
636}
637
638
639
640
641/**
642 * Scan the given program to find a free register of the given type.
643 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
644 */
645GLint
646_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
647{
648 GLboolean used[MAX_PROGRAM_TEMPS];
649 GLuint i, k;
650
651 assert(regFile == PROGRAM_INPUT ||
652 regFile == PROGRAM_OUTPUT ||
653 regFile == PROGRAM_TEMPORARY);
654
655 _mesa_memset(used, 0, sizeof(used));
656
657 for (i = 0; i < prog->NumInstructions; i++) {
658 const struct prog_instruction *inst = prog->Instructions + i;
659 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
660
661 for (k = 0; k < n; k++) {
662 if (inst->SrcReg[k].File == regFile) {
663 used[inst->SrcReg[k].Index] = GL_TRUE;
664 }
665 }
666 }
667
668 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
669 if (!used[i])
670 return i;
671 }
672
673 return -1;
674}
675
676
677
678/**
Brian Paul77427a12006-08-24 23:11:39 +0000679 * Mixing ARB and NV vertex/fragment programs can be tricky.
680 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
681 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
682 * The two different fragment program targets are supposed to be compatible
683 * to some extent (see GL_ARB_fragment_program spec).
684 * This function does the compatibility check.
685 */
686static GLboolean
687compatible_program_targets(GLenum t1, GLenum t2)
688{
689 if (t1 == t2)
690 return GL_TRUE;
691 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
692 return GL_TRUE;
693 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
694 return GL_TRUE;
695 return GL_FALSE;
696}
697
698
Brian Paul30d6a4b2005-11-05 20:18:18 +0000699
Michal Krol2861e732004-03-29 11:09:34 +0000700/**********************************************************************/
701/* API functions */
702/**********************************************************************/
703
704
705/**
706 * Bind a program (make it current)
707 * \note Called from the GL API dispatcher by both glBindProgramNV
708 * and glBindProgramARB.
709 */
710void GLAPIENTRY
711_mesa_BindProgram(GLenum target, GLuint id)
712{
Brian Paula360bc32006-08-25 17:18:56 +0000713 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000714 GET_CURRENT_CONTEXT(ctx);
715 ASSERT_OUTSIDE_BEGIN_END(ctx);
716
717 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
718
Brian Paula360bc32006-08-25 17:18:56 +0000719 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000720 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
721 (ctx->Extensions.NV_vertex_program ||
722 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000723 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000724 }
725 else if ((target == GL_FRAGMENT_PROGRAM_NV
726 && ctx->Extensions.NV_fragment_program) ||
727 (target == GL_FRAGMENT_PROGRAM_ARB
728 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000729 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000730 }
731 else {
732 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
733 return;
734 }
735
Brian Paula360bc32006-08-25 17:18:56 +0000736 /*
737 * Get pointer to new program to bind.
738 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000739 * That's supposed to be caught in glBegin.
740 */
741 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000742 /* Bind a default program */
743 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000744 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600745 newProg = &ctx->Shared->DefaultVertexProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000746 else
Briandf43fb62008-05-06 23:08:51 -0600747 newProg = &ctx->Shared->DefaultFragmentProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000748 }
749 else {
Brian Paula360bc32006-08-25 17:18:56 +0000750 /* Bind a user program */
751 newProg = _mesa_lookup_program(ctx, id);
752 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000753 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000754 newProg = ctx->Driver.NewProgram(ctx, target, id);
755 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000756 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
757 return;
758 }
Brian Paula360bc32006-08-25 17:18:56 +0000759 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000760 }
Brian Paula360bc32006-08-25 17:18:56 +0000761 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000762 _mesa_error(ctx, GL_INVALID_OPERATION,
763 "glBindProgramNV/ARB(target mismatch)");
764 return;
765 }
Michal Krol2861e732004-03-29 11:09:34 +0000766 }
767
Brian Paula360bc32006-08-25 17:18:56 +0000768 /** All error checking is complete now **/
769
770 if (curProg->Id == id) {
771 /* binding same program - no change */
772 return;
773 }
774
Brian Paula360bc32006-08-25 17:18:56 +0000775 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000776 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Briandf43fb62008-05-06 23:08:51 -0600777 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
778 (struct gl_vertex_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000779 }
Brian Paula360bc32006-08-25 17:18:56 +0000780 else if (target == GL_FRAGMENT_PROGRAM_NV ||
781 target == GL_FRAGMENT_PROGRAM_ARB) {
Briandf43fb62008-05-06 23:08:51 -0600782 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
783 (struct gl_fragment_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000784 }
785
Brian Paul765f1a12004-09-14 22:28:27 +0000786 /* Never null pointers */
787 ASSERT(ctx->VertexProgram.Current);
788 ASSERT(ctx->FragmentProgram.Current);
789
Michal Krol2861e732004-03-29 11:09:34 +0000790 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000791 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000792}
793
794
795/**
796 * Delete a list of programs.
797 * \note Not compiled into display lists.
798 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
799 */
800void GLAPIENTRY
801_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
802{
803 GLint i;
804 GET_CURRENT_CONTEXT(ctx);
805 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
806
807 if (n < 0) {
808 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
809 return;
810 }
811
812 for (i = 0; i < n; i++) {
813 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000814 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000815 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000816 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
817 }
818 else if (prog) {
819 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000820 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000821 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
822 if (ctx->VertexProgram.Current &&
823 ctx->VertexProgram.Current->Base.Id == ids[i]) {
824 /* unbind this currently bound program */
825 _mesa_BindProgram(prog->Target, 0);
826 }
827 }
828 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
829 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
830 if (ctx->FragmentProgram.Current &&
831 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
832 /* unbind this currently bound program */
833 _mesa_BindProgram(prog->Target, 0);
834 }
835 }
836 else {
837 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
838 return;
839 }
Brian Paulea2943e2005-01-20 04:02:02 +0000840 /* The ID is immediately available for re-use now */
841 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
Briandf43fb62008-05-06 23:08:51 -0600842 _mesa_reference_program(ctx, &prog, NULL);
Michal Krol2861e732004-03-29 11:09:34 +0000843 }
Michal Krol2861e732004-03-29 11:09:34 +0000844 }
845 }
846}
847
848
849/**
850 * Generate a list of new program identifiers.
851 * \note Not compiled into display lists.
852 * \note Called by both glGenProgramsNV and glGenProgramsARB.
853 */
854void GLAPIENTRY
855_mesa_GenPrograms(GLsizei n, GLuint *ids)
856{
857 GLuint first;
858 GLuint i;
859 GET_CURRENT_CONTEXT(ctx);
860 ASSERT_OUTSIDE_BEGIN_END(ctx);
861
862 if (n < 0) {
863 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
864 return;
865 }
866
867 if (!ids)
868 return;
869
870 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
871
Brian Paul765f1a12004-09-14 22:28:27 +0000872 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000873 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000874 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000875 }
876
877 /* Return the program names */
878 for (i = 0; i < (GLuint) n; i++) {
879 ids[i] = first + i;
880 }
881}